blob: 1d47e62a68d63091be837179e96ee04a064ca7b3 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/queue_testutils.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08002
3#include <string.h>
4
Brian Silvermanb3616972013-03-05 19:58:10 -08005#include "gtest/gtest.h"
6
brians343bc112013-02-10 01:53:46 +00007#include "aos/common/queue.h"
Brian Silvermanb3616972013-03-05 19:58:10 -08008#include "aos/common/logging/logging_impl.h"
9#include "aos/common/once.h"
10
11using ::aos::logging::LogMessage;
brians343bc112013-02-10 01:53:46 +000012
13namespace aos {
14namespace common {
15namespace testing {
Brian Silvermanb3616972013-03-05 19:58:10 -080016namespace {
17
18class TestLogImplementation : public logging::LogImplementation {
19 public:
20 const ::std::vector<LogMessage> &messages() { return messages_; }
21
22 // This class has to be a singleton so that everybody can get access to the
23 // same instance to read out the messages etc.
24 static TestLogImplementation *GetInstance() {
25 static Once<TestLogImplementation> once(CreateInstance);
26 return once.Get();
27 }
28
29 // Clears out all of the messages already recorded.
30 void ClearMessages() {
31 messages_.clear();
32 }
33
34 // Prints out all of the messages (like when a test fails).
35 void PrintAllMessages() {
36 for (auto it = messages_.begin(); it != messages_.end(); ++it) {
37 logging::internal::PrintMessage(stdout, *it);
38 }
39 }
40
41 private:
42 TestLogImplementation() {}
43
44 static TestLogImplementation *CreateInstance() {
45 return new TestLogImplementation();
46 }
47
48 virtual void DoLog(log_level level, const char *format, va_list ap) {
49 LogMessage message;
50
51 logging::internal::FillInMessage(level, format, ap, &message);
52
53 if (!logging::log_gt_important(WARNING, level)) {
54 logging::internal::PrintMessage(stdout, message);
55 }
56
57 messages_.push_back(message);
58 }
59
60 ::std::vector<LogMessage> messages_;
61};
62
63class MyTestEventListener : public ::testing::EmptyTestEventListener {
64 virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
65 TestLogImplementation::GetInstance()->ClearMessages();
66 }
67 virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
68 if (test_info.result()->Failed()) {
69 printf("Test %s failed. Printing out all log messages.\n",
70 test_info.name());
71 fputs("\tThis will include already printed WARNING and up messages.\n",
72 stdout);
Brian Silvermanb91a37d2013-03-09 17:54:30 -080073 fputs("\tIt will also include duplicates of all gtest failures.\n",
74 stdout);
Brian Silvermanb3616972013-03-05 19:58:10 -080075 TestLogImplementation::GetInstance()->PrintAllMessages();
76 }
77 }
Brian Silvermanb91a37d2013-03-09 17:54:30 -080078
79 virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
80 if (result.failed()) {
81 const char *failure_type = "unknown";
82 switch (result.type()) {
83 case ::testing::TestPartResult::Type::kNonFatalFailure:
84 failure_type = "EXPECT";
85 break;
86 case ::testing::TestPartResult::Type::kFatalFailure:
87 failure_type = "ASSERT";
88 break;
89 case ::testing::TestPartResult::Type::kSuccess:
90 break;
91 }
92 log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
93 result.file_name(),
94 result.line_number(),
95 failure_type,
96 result.message());
97 }
98 }
Brian Silvermanb3616972013-03-05 19:58:10 -080099};
100
101void *DoEnableTestLogging() {
102 logging::Init();
103 logging::AddImplementation(TestLogImplementation::GetInstance());
104
105 ::testing::UnitTest::GetInstance()->listeners().Append(
106 new MyTestEventListener());
107
108 return NULL;
109}
110
111Once<void> enable_test_logging_once(DoEnableTestLogging);
112
113} // namespace
brians343bc112013-02-10 01:53:46 +0000114
115GlobalCoreInstance::GlobalCoreInstance() {
116 const size_t kCoreSize = 0x100000;
117 global_core = &global_core_data_;
118 global_core->owner = 1;
119 void *memory = malloc(kCoreSize);
120 assert(memory != NULL);
121 memset(memory, 0, kCoreSize);
122
123 assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0);
Brian Silvermanb3616972013-03-05 19:58:10 -0800124
125 EnableTestLogging();
brians343bc112013-02-10 01:53:46 +0000126}
127
128GlobalCoreInstance::~GlobalCoreInstance() {
129 free(global_core->mem_struct);
130 global_core = NULL;
131}
132
Brian Silvermanb3616972013-03-05 19:58:10 -0800133void EnableTestLogging() {
134 enable_test_logging_once.Get();
135}
136
brians343bc112013-02-10 01:53:46 +0000137} // namespace testing
138} // namespace common
139} // namespace aos