powerlog_tests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_powerlog_tests"
  18. #include <audio_utils/PowerLog.h>
  19. #include <gtest/gtest.h>
  20. #include <iostream>
  21. #include <log/log.h>
  22. using namespace android;
  23. static size_t countNewLines(const std::string &s) {
  24. return std::count(s.begin(), s.end(), '\n');
  25. }
  26. TEST(audio_utils_powerlog, basic) {
  27. auto plog = std::make_unique<PowerLog>(
  28. 48000 /* sampleRate */,
  29. 1 /* channelCount */,
  30. AUDIO_FORMAT_PCM_16_BIT,
  31. 100 /* entries */,
  32. 1 /* framesPerEntry */);
  33. // header
  34. EXPECT_EQ((size_t)1, countNewLines(plog->dumpToString()));
  35. const int16_t zero = 0;
  36. const int16_t half = 0x4000;
  37. plog->log(&half, 1 /* frame */, 0 /* nowNs */);
  38. plog->log(&half, 1 /* frame */, 1 /* nowNs */);
  39. plog->log(&half, 1 /* frame */, 2 /* nowNs */);
  40. // one line / signal
  41. EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString()));
  42. plog->log(&zero, 1 /* frame */, 3 /* nowNs */);
  43. // zero termination doesn't change this.
  44. EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString()));
  45. // but adding next line does.
  46. plog->log(&half, 1 /* frame */, 4 /* nowNs */);
  47. EXPECT_EQ((size_t)3, countNewLines(plog->dumpToString()));
  48. // truncating on lines
  49. EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
  50. "" /* prefix */, 2 /* lines */)));
  51. // truncating on time
  52. EXPECT_EQ((size_t)3, countNewLines(plog->dumpToString(
  53. "" /* prefix */, 0 /* lines */, 2 /* limitNs */)));
  54. EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
  55. "" /* prefix */, 0 /* lines */, 3 /* limitNs */)));
  56. plog->dump(0 /* fd (stdout) */);
  57. // The output below depends on the local time zone.
  58. // The indentation below is exact, check alignment.
  59. /*
  60. Signal power history:
  61. 12-31 16:00:00.000: [ -6.0 -6.0 -6.0 ] sum(-1.2)
  62. 12-31 16:00:00.000: [ -6.0
  63. */
  64. }
  65. TEST(audio_utils_powerlog, c) {
  66. power_log_t *power_log = power_log_create(
  67. 48000 /* sample_rate */,
  68. 1 /* channel_count */,
  69. AUDIO_FORMAT_PCM_16_BIT,
  70. 100 /* entries */,
  71. 1 /* frames_per_entry */);
  72. // sanity test
  73. const int16_t zero = 0;
  74. const int16_t quarter = 0x2000;
  75. power_log_log(power_log, &quarter, 1 /* frame */, 0 /* now_ns */);
  76. power_log_log(power_log, &zero, 1 /* frame */, 1 /* now_ns */);
  77. power_log_dump(power_log, 0 /* fd */, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
  78. power_log_destroy(power_log);
  79. // This has a 2 character prefix offset from the previous test when dumping.
  80. // The indentation below is exact, check alignment.
  81. /*
  82. Signal power history:
  83. 12-31 16:00:00.000: [ -12.0 ] sum(-12.0)
  84. */
  85. }