Ken Chen | d27d6c9 | 2021-10-21 22:18:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include "netdutils/Log.h" |
| 20 | |
| 21 | android::netdutils::LogEntry globalFunctionName() { |
| 22 | return android::netdutils::LogEntry().function(__FUNCTION__); |
| 23 | } |
| 24 | |
| 25 | android::netdutils::LogEntry globalPrettyFunctionName() { |
| 26 | return android::netdutils::LogEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 27 | } |
| 28 | |
| 29 | namespace android { |
| 30 | namespace netdutils { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | LogEntry functionName() { |
| 35 | return LogEntry().function(__FUNCTION__); |
| 36 | } |
| 37 | |
| 38 | LogEntry prettyFunctionName() { |
| 39 | return LogEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | class AAA { |
| 45 | public: |
| 46 | AAA() = default; |
| 47 | |
| 48 | LogEntry functionName() { |
| 49 | return LogEntry().function(__FUNCTION__); |
| 50 | } |
| 51 | |
| 52 | LogEntry prettyFunctionName() { |
| 53 | return LogEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 54 | } |
| 55 | |
| 56 | class BBB { |
| 57 | public: |
| 58 | BBB() = default; |
| 59 | |
| 60 | LogEntry functionName() { |
| 61 | return LogEntry().function(__FUNCTION__); |
| 62 | } |
| 63 | |
| 64 | LogEntry prettyFunctionName() { |
| 65 | return LogEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 66 | } |
| 67 | }; |
| 68 | }; |
| 69 | |
| 70 | TEST(LogEntryTest, Empty) { |
| 71 | LogEntry empty; |
| 72 | EXPECT_EQ("", empty.toString()); |
| 73 | } |
| 74 | |
| 75 | TEST(LogEntryTest, GlobalFunction) { |
| 76 | EXPECT_EQ("globalFunctionName()", ::globalFunctionName().toString()); |
| 77 | } |
| 78 | |
| 79 | TEST(LogEntryTest, GlobalPrettyFunction) { |
| 80 | EXPECT_EQ("globalPrettyFunctionName()", ::globalPrettyFunctionName().toString()); |
| 81 | } |
| 82 | |
| 83 | TEST(LogEntryTest, UnnamedNamespaceFunction) { |
| 84 | const LogEntry entry = functionName(); |
| 85 | EXPECT_EQ("functionName()", entry.toString()); |
| 86 | } |
| 87 | |
| 88 | TEST(LogEntryTest, UnnamedNamespacePrettyFunction) { |
| 89 | const LogEntry entry = prettyFunctionName(); |
| 90 | EXPECT_EQ("prettyFunctionName()", entry.toString()); |
| 91 | } |
| 92 | |
| 93 | TEST(LogEntryTest, ClassFunction) { |
| 94 | const LogEntry entry = AAA().functionName(); |
| 95 | EXPECT_EQ("functionName()", entry.toString()); |
| 96 | } |
| 97 | |
| 98 | TEST(LogEntryTest, ClassPrettyFunction) { |
| 99 | const LogEntry entry = AAA().prettyFunctionName(); |
| 100 | EXPECT_EQ("AAA::prettyFunctionName()", entry.toString()); |
| 101 | } |
| 102 | |
| 103 | TEST(LogEntryTest, InnerClassFunction) { |
| 104 | const LogEntry entry = AAA::BBB().functionName(); |
| 105 | EXPECT_EQ("functionName()", entry.toString()); |
| 106 | } |
| 107 | |
| 108 | TEST(LogEntryTest, InnerClassPrettyFunction) { |
| 109 | const LogEntry entry = AAA::BBB().prettyFunctionName(); |
| 110 | EXPECT_EQ("BBB::prettyFunctionName()", entry.toString()); |
| 111 | } |
| 112 | |
| 113 | TEST(LogEntryTest, PrintChainedArguments) { |
| 114 | const LogEntry entry = LogEntry() |
| 115 | .function("testFunc") |
| 116 | .arg("hello") |
| 117 | .arg(42) |
| 118 | .arg(true); |
| 119 | EXPECT_EQ("testFunc(hello, 42, true)", entry.toString()); |
| 120 | } |
| 121 | |
| 122 | TEST(LogEntryTest, PrintIntegralTypes) { |
| 123 | const LogEntry entry = LogEntry() |
| 124 | .function("testFunc") |
| 125 | .arg('A') |
| 126 | .arg(100U) |
| 127 | .arg(-1000LL); |
| 128 | EXPECT_EQ("testFunc(65, 100, -1000)", entry.toString()); |
| 129 | } |
| 130 | |
| 131 | TEST(LogEntryTest, PrintHex) { |
| 132 | const std::vector<uint8_t> buf{0xDE, 0xAD, 0xBE, 0xEF}; |
| 133 | const LogEntry entry = LogEntry().function("testFunc").arg(buf); |
| 134 | EXPECT_EQ("testFunc({deadbeef})", entry.toString()); |
| 135 | } |
| 136 | |
| 137 | TEST(LogEntryTest, PrintArgumentPack) { |
| 138 | const LogEntry entry = LogEntry().function("testFunc").args("hello", 42, false); |
| 139 | EXPECT_EQ("testFunc(hello, 42, false)", entry.toString()); |
| 140 | } |
| 141 | |
| 142 | } // namespace netdutils |
| 143 | } // namespace android |