brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/queue_testutils.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
| 4 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame^] | 5 | #include "gtest/gtest.h" |
| 6 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | #include "aos/common/queue.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame^] | 8 | #include "aos/common/logging/logging_impl.h" |
| 9 | #include "aos/common/once.h" |
| 10 | |
| 11 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace common { |
| 15 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame^] | 16 | namespace { |
| 17 | |
| 18 | class 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 | |
| 63 | class 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); |
| 73 | TestLogImplementation::GetInstance()->PrintAllMessages(); |
| 74 | } |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | void *DoEnableTestLogging() { |
| 79 | logging::Init(); |
| 80 | logging::AddImplementation(TestLogImplementation::GetInstance()); |
| 81 | |
| 82 | ::testing::UnitTest::GetInstance()->listeners().Append( |
| 83 | new MyTestEventListener()); |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 89 | |
| 90 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | |
| 92 | GlobalCoreInstance::GlobalCoreInstance() { |
| 93 | const size_t kCoreSize = 0x100000; |
| 94 | global_core = &global_core_data_; |
| 95 | global_core->owner = 1; |
| 96 | void *memory = malloc(kCoreSize); |
| 97 | assert(memory != NULL); |
| 98 | memset(memory, 0, kCoreSize); |
| 99 | |
| 100 | assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame^] | 101 | |
| 102 | EnableTestLogging(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | GlobalCoreInstance::~GlobalCoreInstance() { |
| 106 | free(global_core->mem_struct); |
| 107 | global_core = NULL; |
| 108 | } |
| 109 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame^] | 110 | void EnableTestLogging() { |
| 111 | enable_test_logging_once.Get(); |
| 112 | } |
| 113 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 114 | } // namespace testing |
| 115 | } // namespace common |
| 116 | } // namespace aos |