1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "debug.h"
6 
7 #include <sstream>
8 #include "gtest/gtest.h"
9 
10 namespace relocation_packer {
11 
TEST(Debug,Log)12 TEST(Debug, Log) {
13   Logger::Reset();
14   std::ostringstream info;
15   std::ostringstream error;
16   Logger::SetStreams(&info, &error);
17 
18   LOG(INFO) << "INFO log message";
19   LOG(WARNING) << "WARNING log message";
20   LOG(ERROR) << "ERROR log message";
21 
22   EXPECT_EQ("INFO: INFO log message\n", info.str());
23   EXPECT_EQ("WARNING: WARNING log message\n"
24             "ERROR: ERROR log message\n", error.str());
25   Logger::Reset();
26 }
27 
TEST(Debug,LogIf)28 TEST(Debug, LogIf) {
29   Logger::Reset();
30   std::ostringstream info;
31   std::ostringstream error;
32   Logger::SetStreams(&info, &error);
33 
34   LOG_IF(INFO, true) << "INFO log message";
35   LOG_IF(INFO, false) << "INFO log message, SHOULD NOT PRINT";
36   LOG_IF(WARNING, true) << "WARNING log message";
37   LOG_IF(WARNING, false) << "WARNING log message, SHOULD NOT PRINT";
38   LOG_IF(ERROR, true) << "ERROR log message";
39   LOG_IF(ERROR, false) << "ERROR log message, SHOULD NOT PRINT";
40   LOG_IF(FATAL, false) << "FATAL log message, SHOULD NOT PRINT";
41 
42   EXPECT_EQ("INFO: INFO log message\n", info.str());
43   EXPECT_EQ("WARNING: WARNING log message\n"
44             "ERROR: ERROR log message\n", error.str());
45   Logger::Reset();
46 }
47 
TEST(Debug,Vlog)48 TEST(Debug, Vlog) {
49   Logger::Reset();
50   std::ostringstream info;
51   std::ostringstream error;
52   Logger::SetStreams(&info, &error);
53 
54   VLOG(0) << "VLOG 0 INFO log message, SHOULD NOT PRINT";
55   VLOG(1) << "VLOG 1 INFO log message, SHOULD NOT PRINT";
56   VLOG(2) << "VLOG 2 INFO log message, SHOULD NOT PRINT";
57 
58   EXPECT_EQ("", info.str());
59   EXPECT_EQ("", error.str());
60 
61   Logger::SetVerbose(1);
62 
63   VLOG(0) << "VLOG 0 INFO log message";
64   VLOG(1) << "VLOG 1 INFO log message";
65   VLOG(2) << "VLOG 2 INFO log message, SHOULD NOT PRINT";
66 
67   EXPECT_EQ("INFO: VLOG 0 INFO log message\n"
68             "INFO: VLOG 1 INFO log message\n", info.str());
69   EXPECT_EQ("", error.str());
70   Logger::Reset();
71 }
72 
TEST(Debug,VlogIf)73 TEST(Debug, VlogIf) {
74   Logger::Reset();
75   std::ostringstream info;
76   std::ostringstream error;
77   Logger::SetStreams(&info, &error);
78 
79   VLOG_IF(0, true) << "VLOG 0 INFO log message, SHOULD NOT PRINT";
80   VLOG_IF(1, true) << "VLOG 1 INFO log message, SHOULD NOT PRINT";
81   VLOG_IF(2, true) << "VLOG 2 INFO log message, SHOULD NOT PRINT";
82 
83   EXPECT_EQ("", info.str());
84   EXPECT_EQ("", error.str());
85 
86   Logger::SetVerbose(1);
87 
88   VLOG_IF(0, true) << "VLOG 0 INFO log message";
89   VLOG_IF(0, false) << "VLOG 0 INFO log message, SHOULD NOT PRINT";
90   VLOG_IF(1, true) << "VLOG 1 INFO log message";
91   VLOG_IF(1, false) << "VLOG 1 INFO log message, SHOULD NOT PRINT";
92   VLOG_IF(2, true) << "VLOG 2 INFO log message, SHOULD NOT PRINT";
93   VLOG_IF(2, false) << "VLOG 2 INFO log message, SHOULD NOT PRINT";
94 
95   EXPECT_EQ("INFO: VLOG 0 INFO log message\n"
96             "INFO: VLOG 1 INFO log message\n", info.str());
97   EXPECT_EQ("", error.str());
98   Logger::Reset();
99 }
100 
TEST(DebugDeathTest,Fatal)101 TEST(DebugDeathTest, Fatal) {
102   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
103   Logger::Reset();
104   EXPECT_DEATH(LOG(FATAL) << "FATAL log message", "FATAL: FATAL log message");
105   EXPECT_DEATH(
106       LOG_IF(FATAL, true) << "FATAL log message", "FATAL: FATAL log message");
107 }
108 
TEST(DebugDeathTest,Check)109 TEST(DebugDeathTest, Check) {
110   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
111   Logger::Reset();
112   CHECK(0 == 0);
113   EXPECT_DEATH(CHECK(0 == 1), "FATAL: .*:.*: .*: CHECK '0 == 1' failed");
114 }
115 
TEST(DebugDeathTest,NotReached)116 TEST(DebugDeathTest, NotReached) {
117   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
118   Logger::Reset();
119   EXPECT_DEATH(NOTREACHED(), "FATAL: .*:.*: .*: NOTREACHED\\(\\) hit");
120 }
121 
122 }  // namespace relocation_packer
123