OSDN Git Service

Merge "Moved libalsautils to vendor. Marked libaudioutils and libaudioroute as vendor...
[android-x86/system-media.git] / audio_utils / tests / errorlog_tests.cpp
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_errorlog_tests"
19
20 #include <audio_utils/ErrorLog.h>
21 #include <gtest/gtest.h>
22 #include <iostream>
23 #include <log/log.h>
24
25 using namespace android;
26
27 static size_t countNewLines(const std::string &s) {
28     return std::count(s.begin(), s.end(), '\n');
29 }
30
31 TEST(audio_utils_errorlog, basic) {
32     auto elog = std::make_unique<ErrorLog<int32_t>>(100 /* lines */);
33     const int64_t oneSecond = 1000000000;
34
35     EXPECT_EQ((size_t)1, countNewLines(elog->dumpToString()));
36
37     elog->log(1 /* code */, 0 /* nowNs */);
38     elog->log(2 /* code */, 1 /* nowNs */);
39
40     // two separate errors (4 lines including 2 header lines)
41     EXPECT_EQ((size_t)4, countNewLines(elog->dumpToString()));
42
43     // aggregation at (oneSecond - 1)
44     elog->log(2 /* code */, oneSecond /* nowNs */);
45     EXPECT_EQ((size_t)4, countNewLines(elog->dumpToString()));
46
47     // no aggregation if spaced exactly one second apart
48     elog->log(2 /* code */, oneSecond * 2 /* nowNs */);
49     EXPECT_EQ((size_t)5, countNewLines(elog->dumpToString()));
50
51     // Check log:
52     // truncate on lines
53     EXPECT_EQ((size_t)3, countNewLines(elog->dumpToString("" /* prefix */, 3 /* lines */)));
54
55     // truncate on time
56     EXPECT_EQ((size_t)4, countNewLines(
57             elog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond - 1 /* limitNs */)));
58
59     // truncate on time
60     EXPECT_EQ((size_t)4, countNewLines(
61             elog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond /* limitNs */)));
62
63     // truncate on time (more)
64     EXPECT_EQ((size_t)3, countNewLines(
65             elog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond + 1 /* limitNs */)));
66
67     // truncate on time
68     EXPECT_EQ((size_t)3, countNewLines(
69             elog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 /* limitNs */)));
70
71     // truncate on time (to first header line)
72     EXPECT_EQ((size_t)1, countNewLines(
73             elog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 + 1/* limitNs */)));
74
75     elog->dump(0 /* fd (stdout) */);
76
77     // The output below depends on the local time zone.
78     // The indentation below is exact, check alignment.
79     /*
80 Errors: 4
81  Code  Freq          First time           Last time
82     1     1  12-31 16:00:00.000  12-31 16:00:00.000
83     2     2  12-31 16:00:00.000  12-31 16:00:01.000
84     2     1  12-31 16:00:02.000  12-31 16:00:02.000
85      */
86 }
87
88 TEST(audio_utils_errorlog, c) {
89     error_log_t *error_log =
90             error_log_create(100 /* lines */, 1000000000 /* one second aggregation */);
91
92     // just a sanity test
93     error_log_log(error_log, 2 /* code */, 1 /* now_ns */);
94     error_log_dump(error_log, 0 /* fd */, "  " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
95     error_log_destroy(error_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   Errors: 1
101    Code  Freq          First time           Last time
102       2     1  12-31 16:00:00.000  12-31 16:00:00.000
103      */
104 }