OSDN Git Service

Merge "Make inlines static"
[android-x86/system-media.git] / audio_utils / tests / simplelog_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/SimpleLog.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_simplelog, basic) {
32     auto slog = std::make_unique<SimpleLog>();
33     const int64_t oneSecond = 1000000000;
34
35     EXPECT_EQ((size_t)0, countNewLines(slog->dumpToString()));
36
37     const int nine = 9;
38     slog->log("Hello %d", nine);
39     slog->log("World");
40
41     // two lines (no header)
42     EXPECT_EQ((size_t)2, countNewLines(slog->dumpToString()));
43
44     // another two lines (this is out of time order, but the log doesn't care)
45     slog->log(oneSecond /* nowNs */, "Hello World %d", 10);
46     slog->log(oneSecond * 2 /* nowNs */, "%s", "Goodbye");
47
48     EXPECT_EQ((size_t)4, countNewLines(slog->dumpToString()));
49
50     // truncate on lines
51     EXPECT_EQ((size_t)1, countNewLines(slog->dumpToString("" /* prefix */, 1 /* lines */)));
52
53     // truncate on time
54     EXPECT_EQ((size_t)4, countNewLines(
55             slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond /* limitNs */)));
56
57     // truncate on time (more)
58     EXPECT_EQ((size_t)3, countNewLines(
59             slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 /* limitNs */)));
60
61     // truncate on time (more)
62     EXPECT_EQ((size_t)2, countNewLines(
63             slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 + 1 /* limitNs */)));
64
65     std::cout << slog->dumpToString() << std::flush;
66
67     slog->dump(0 /* fd (stdout) */, "  "); // add a prefix
68
69     // The output below depends on the local time zone and current time.
70     // The indentation below is exact, check alignment.
71     /*
72 03-27 14:47:43.567 Hello 9
73 03-27 14:47:43.567 World
74 12-31 16:00:01.000 Hello World 10
75 12-31 16:00:02.000 Goodbye
76   03-27 14:47:43.567 Hello 9
77   03-27 14:47:43.567 World
78   12-31 16:00:01.000 Hello World 10
79   12-31 16:00:02.000 Goodbye
80      */
81 }