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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_powerlog_tests"
19
20 #include <audio_utils/PowerLog.h>
21 #include <gtest/gtest.h>
22 #include <iostream>
23 #include <log/log.h>
24
25 using namespace android;
26
countNewLines(const std::string & s)27 static size_t countNewLines(const std::string &s) {
28 return std::count(s.begin(), s.end(), '\n');
29 }
30
TEST(audio_utils_powerlog,basic)31 TEST(audio_utils_powerlog, basic) {
32 auto plog = std::make_unique<PowerLog>(
33 48000 /* sampleRate */,
34 1 /* channelCount */,
35 AUDIO_FORMAT_PCM_16_BIT,
36 100 /* entries */,
37 1 /* framesPerEntry */);
38
39 // header
40 EXPECT_EQ((size_t)1, countNewLines(plog->dumpToString()));
41
42 const int16_t zero = 0;
43 const int16_t half = 0x4000;
44
45 plog->log(&half, 1 /* frame */, 0 /* nowNs */);
46 plog->log(&half, 1 /* frame */, 1 /* nowNs */);
47 plog->log(&half, 1 /* frame */, 2 /* nowNs */);
48
49 // one line / signal
50 EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
51 "" /* prefix */, 0 /* lines */, 0 /* limitNs */, false /* logPlot */)));
52
53 // one line / signal + logplot
54 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString()));
55
56 plog->log(&zero, 1 /* frame */, 3 /* nowNs */);
57 // zero termination doesn't change this.
58 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString()));
59
60 // but adding next line does.
61 plog->log(&half, 1 /* frame */, 4 /* nowNs */);
62 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString()));
63
64 // truncating on lines (this does not include the logplot).
65 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString(
66 "" /* prefix */, 2 /* lines */)));
67
68 // truncating on time as well.
69 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString(
70 "" /* prefix */, 0 /* lines */, 2 /* limitNs */)));
71 // truncating on different time limit.
72 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString(
73 "" /* prefix */, 0 /* lines */, 3 /* limitNs */)));
74
75 // truncating on a larger line count (this doesn't include the logplot).
76 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString(
77 "" /* prefix */, 3 /* lines */, 2 /* limitNs */)));
78
79 plog->dump(0 /* fd (stdout) */);
80
81 // The output below depends on the local time zone.
82 // The indentation below is exact, check alignment.
83 /*
84 Signal power history:
85 01-01 00:00:00.000: [ -6.0 -6.0 -6.0 ] sum(-1.2)
86 01-01 00:00:00.000: [ -6.0
87
88 -0.0 -| |
89 -1.0 -| |
90 -2.0 -| |
91 -3.0 -| |
92 -4.0 -| |
93 -5.0 -| |
94 -6.0 -|***|
95 -7.0 -| |
96 -8.0 -| |
97 -9.0 -| |
98 -10.0 -| |
99 -11.0 -| |
100 -12.0 -| |
101 -13.0 -| |
102 |____
103
104 */
105 }
106
TEST(audio_utils_powerlog,c)107 TEST(audio_utils_powerlog, c) {
108 power_log_t *power_log = power_log_create(
109 48000 /* sample_rate */,
110 1 /* channel_count */,
111 AUDIO_FORMAT_PCM_16_BIT,
112 100 /* entries */,
113 1 /* frames_per_entry */);
114
115 // soundness test
116 const int16_t zero = 0;
117 const int16_t quarter = 0x2000;
118
119 power_log_log(power_log, &quarter, 1 /* frame */, 0 /* now_ns */);
120 power_log_log(power_log, &zero, 1 /* frame */, 1 /* now_ns */);
121 power_log_dump(power_log, 0 /* fd */, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
122 power_log_destroy(power_log);
123
124 // This has a 2 character prefix offset from the previous test when dumping.
125 // The indentation below is exact, check alignment.
126 /*
127 Signal power history:
128 12-31 16:00:00.000: [ -12.0 ] sum(-12.0)
129 */
130 }
131