Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_unit_test / framework.cc
1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_unit_test/framework.h"
16
17 #include <cstring>
18
19 namespace pw {
20 namespace unit_test {
21
22 void RegisterEventHandler(EventHandler* event_handler) {
23   internal::Framework::Get().RegisterEventHandler(event_handler);
24 }
25
26 namespace internal {
27
28 // Singleton instance of the unit test framework class.
29 Framework Framework::framework_;
30
31 // Linked list of all test cases in the test executable. This is static as it is
32 // populated using static initialization.
33 TestInfo* Framework::tests_ = nullptr;
34
35 void Framework::RegisterTest(TestInfo* new_test) {
36   // If the test list is empty, set new_test as the first test.
37   if (tests_ == nullptr) {
38     tests_ = new_test;
39     return;
40   }
41
42   // Append the test case to the end of the test list.
43   TestInfo* info = tests_;
44   for (; info->next() != nullptr; info = info->next()) {
45   }
46   info->set_next(new_test);
47 }
48
49 int Framework::RunAllTests() {
50   run_tests_summary_.passed_tests = 0;
51   run_tests_summary_.failed_tests = 0;
52
53   if (event_handler_ != nullptr) {
54     event_handler_->RunAllTestsStart();
55   }
56   for (const TestInfo* test = tests_; test != nullptr; test = test->next()) {
57     if (test->enabled()) {
58       test->run();
59     } else if (event_handler_ != nullptr) {
60       event_handler_->TestCaseDisabled(test->test_case());
61     }
62   }
63   if (event_handler_ != nullptr) {
64     event_handler_->RunAllTestsEnd(run_tests_summary_);
65   }
66   return exit_status_;
67 }
68
69 void Framework::StartTest(const TestInfo& test) {
70   current_test_ = &test;
71   current_result_ = TestResult::kSuccess;
72
73   if (event_handler_ != nullptr) {
74     event_handler_->TestCaseStart(test.test_case());
75   }
76 }
77
78 void Framework::EndCurrentTest() {
79   switch (current_result_) {
80     case TestResult::kSuccess:
81       run_tests_summary_.passed_tests++;
82       break;
83     case TestResult::kFailure:
84       run_tests_summary_.failed_tests++;
85       break;
86   }
87
88   if (event_handler_ != nullptr) {
89     event_handler_->TestCaseEnd(current_test_->test_case(), current_result_);
90   }
91
92   current_test_ = nullptr;
93 }
94
95 void Framework::ExpectationResult(const char* expression,
96                                   const char* evaluated_expression,
97                                   int line,
98                                   bool success) {
99   if (!success) {
100     current_result_ = TestResult::kFailure;
101     exit_status_ = 1;
102   }
103
104   if (event_handler_ == nullptr) {
105     return;
106   }
107
108   TestExpectation expectation = {
109       .expression = expression,
110       .evaluated_expression = evaluated_expression,
111       .line_number = line,
112       .success = success,
113   };
114
115   event_handler_->TestCaseExpect(current_test_->test_case(), expectation);
116 }
117
118 bool TestInfo::enabled() const {
119   constexpr size_t kStringSize = sizeof("DISABLED_") - 1;
120   return std::strncmp("DISABLED_", test_case().test_name, kStringSize) != 0 &&
121          std::strncmp("DISABLED_", test_case().suite_name, kStringSize) != 0;
122 }
123
124 }  // namespace internal
125 }  // namespace unit_test
126 }  // namespace pw