• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
52     plog->log(&zero, 1 /* frame */, 3 /* nowNs */);
53     // zero termination doesn't change this.
54     EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString()));
55 
56     // but adding next line does.
57     plog->log(&half, 1 /* frame */, 4 /* nowNs */);
58     EXPECT_EQ((size_t)3, countNewLines(plog->dumpToString()));
59 
60     // truncating on lines
61     EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
62             "" /* prefix */, 2 /* lines */)));
63 
64     // truncating on time
65     EXPECT_EQ((size_t)3, countNewLines(plog->dumpToString(
66             "" /* prefix */, 0 /* lines */, 2 /* limitNs */)));
67     EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
68             "" /* prefix */, 0 /* lines */, 3 /* limitNs */)));
69     plog->dump(0 /* fd (stdout) */);
70 
71     // The output below depends on the local time zone.
72     // The indentation below is exact, check alignment.
73     /*
74 Signal power history:
75  12-31 16:00:00.000: [   -6.0   -6.0   -6.0 ] sum(-1.2)
76  12-31 16:00:00.000: [   -6.0
77      */
78 }
79 
TEST(audio_utils_powerlog,c)80 TEST(audio_utils_powerlog, c) {
81     power_log_t *power_log = power_log_create(
82             48000 /* sample_rate */,
83             1 /* channel_count */,
84             AUDIO_FORMAT_PCM_16_BIT,
85             100 /* entries */,
86             1 /* frames_per_entry */);
87 
88     // sanity test
89     const int16_t zero = 0;
90     const int16_t quarter = 0x2000;
91 
92     power_log_log(power_log, &quarter, 1 /* frame */, 0 /* now_ns */);
93     power_log_log(power_log, &zero, 1 /* frame */, 1 /* now_ns */);
94     power_log_dump(power_log, 0 /* fd */, "  " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
95     power_log_destroy(power_log);
96 
97     // This has a 2 character prefix offset from the previous test when dumping.
98     // The indentation below is exact, check alignment.
99     /*
100   Signal power history:
101    12-31 16:00:00.000: [  -12.0 ] sum(-12.0)
102      */
103 }
104