LogTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <gtest/gtest.h>
  17. #include "netdutils/Log.h"
  18. android::netdutils::LogEntry globalFunctionName() {
  19. return android::netdutils::LogEntry().function(__FUNCTION__);
  20. }
  21. android::netdutils::LogEntry globalPrettyFunctionName() {
  22. return android::netdutils::LogEntry().prettyFunction(__PRETTY_FUNCTION__);
  23. }
  24. namespace android {
  25. namespace netdutils {
  26. namespace {
  27. LogEntry functionName() {
  28. return LogEntry().function(__FUNCTION__);
  29. }
  30. LogEntry prettyFunctionName() {
  31. return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
  32. }
  33. } // namespace
  34. class AAA {
  35. public:
  36. AAA() = default;
  37. LogEntry functionName() {
  38. return LogEntry().function(__FUNCTION__);
  39. }
  40. LogEntry prettyFunctionName() {
  41. return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
  42. }
  43. class BBB {
  44. public:
  45. BBB() = default;
  46. LogEntry functionName() {
  47. return LogEntry().function(__FUNCTION__);
  48. }
  49. LogEntry prettyFunctionName() {
  50. return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
  51. }
  52. };
  53. };
  54. TEST(LogEntryTest, Empty) {
  55. LogEntry empty;
  56. EXPECT_EQ("", empty.toString());
  57. }
  58. TEST(LogEntryTest, GlobalFunction) {
  59. EXPECT_EQ("globalFunctionName()", ::globalFunctionName().toString());
  60. }
  61. TEST(LogEntryTest, GlobalPrettyFunction) {
  62. EXPECT_EQ("globalPrettyFunctionName()", ::globalPrettyFunctionName().toString());
  63. }
  64. TEST(LogEntryTest, UnnamedNamespaceFunction) {
  65. const LogEntry entry = functionName();
  66. EXPECT_EQ("functionName()", entry.toString());
  67. }
  68. TEST(LogEntryTest, UnnamedNamespacePrettyFunction) {
  69. const LogEntry entry = prettyFunctionName();
  70. EXPECT_EQ("prettyFunctionName()", entry.toString());
  71. }
  72. TEST(LogEntryTest, ClassFunction) {
  73. const LogEntry entry = AAA().functionName();
  74. EXPECT_EQ("functionName()", entry.toString());
  75. }
  76. TEST(LogEntryTest, ClassPrettyFunction) {
  77. const LogEntry entry = AAA().prettyFunctionName();
  78. EXPECT_EQ("AAA::prettyFunctionName()", entry.toString());
  79. }
  80. TEST(LogEntryTest, InnerClassFunction) {
  81. const LogEntry entry = AAA::BBB().functionName();
  82. EXPECT_EQ("functionName()", entry.toString());
  83. }
  84. TEST(LogEntryTest, InnerClassPrettyFunction) {
  85. const LogEntry entry = AAA::BBB().prettyFunctionName();
  86. EXPECT_EQ("BBB::prettyFunctionName()", entry.toString());
  87. }
  88. TEST(LogEntryTest, PrintChainedArguments) {
  89. const LogEntry entry = LogEntry()
  90. .function("testFunc")
  91. .arg("hello")
  92. .arg(42)
  93. .arg(true);
  94. EXPECT_EQ("testFunc(hello, 42, true)", entry.toString());
  95. }
  96. TEST(LogEntryTest, PrintIntegralTypes) {
  97. const LogEntry entry = LogEntry()
  98. .function("testFunc")
  99. .arg('A')
  100. .arg(100U)
  101. .arg(-1000LL);
  102. EXPECT_EQ("testFunc(65, 100, -1000)", entry.toString());
  103. }
  104. TEST(LogEntryTest, PrintHex) {
  105. const std::vector<uint8_t> buf{0xDE, 0xAD, 0xBE, 0xEF};
  106. const LogEntry entry = LogEntry().function("testFunc").arg(buf);
  107. EXPECT_EQ("testFunc({deadbeef})", entry.toString());
  108. }
  109. TEST(LogEntryTest, PrintArgumentPack) {
  110. const LogEntry entry = LogEntry().function("testFunc").args("hello", 42, false);
  111. EXPECT_EQ("testFunc(hello, 42, false)", entry.toString());
  112. }
  113. } // namespace netdutils
  114. } // namespace android