1 /*
2  * Copyright (C) 2016 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 
17 /*
18  * This is a minimal set of stubs to compile nvram_manager_test.cpp in
19  * environments where googletest is not available. The test status output isn't
20  * as pretty, but good enough to follow test progress and pinpoint test
21  * failures.
22  */
23 
24 extern "C" {
25 #include <stdio.h>
26 }  // extern "C"
27 
28 namespace testing {
29 
30 // Global test status.
31 extern bool g_test_status;
32 
33 #define ASSERT_MSG(cond)                              \
34   if (!(cond)) {                                      \
35     testing::g_test_status = false;                   \
36     fprintf(stderr, "Assertion failed: " #cond "\n"); \
37     return;                                           \
38   }
39 #define ASSERT_TRUE(cond) ASSERT_MSG(cond)
40 #define ASSERT_EQ(expected, actual) ASSERT_MSG(expected == actual)
41 
42 #define EXPECT_MSG(cond)                                \
43   if (!(cond)) {                                        \
44     testing::g_test_status = false;                     \
45     fprintf(stderr, "Expectation failed: " #cond "\n"); \
46   }
47 #define EXPECT_TRUE(cond) EXPECTED_MSG(cond)
48 #define EXPECT_EQ(expected, actual) EXPECT_MSG((expected) == (actual))
49 #define EXPECT_NE(expected, actual) EXPECT_MSG((expected) != (actual))
50 
51 // Test fixture base class.
52 class Test {};
53 
54 namespace detail {
55 
56 // A polymorphic wrapper around test instances. This is the base class that
57 // defines the common interface.
58 class TestInstanceBase {
59  public:
60   virtual ~TestInstanceBase() = default;
61   virtual void Run() = 0;
62 };
63 
64 // Test-specific subclass that holds an instance of the test.
65 template<typename TestCase>
66 class TestInstance : public TestInstanceBase  {
67  public:
68   ~TestInstance() override = default;
69 
Create()70   static TestInstanceBase* Create() {
71     return new TestInstance<TestCase>;
72   }
73 
74  private:
Run()75   void Run() override {
76     test_.Run();
77   }
78 
79   TestCase test_;
80 };
81 
82 struct TestDeclarationBase;
83 using CreateTestInstanceFunction = TestInstanceBase*(void);
84 
85 // |TestRegistry| keeps track of all registered tests.
86 class TestRegistry {
87  public:
instance()88   static TestRegistry* instance() { return &g_instance; }
89 
90   void RunAllTests();
91   void Register(TestDeclarationBase* test_declaration);
92 
93  private:
94   TestDeclarationBase* tests_ = nullptr;
95 
96   static TestRegistry g_instance;
97 };
98 
99 struct TestDeclarationBase {
TestDeclarationBaseTestDeclarationBase100   TestDeclarationBase(const char* name,
101                       CreateTestInstanceFunction* create_function)
102       : name(name), create_function(create_function) {
103     TestRegistry::instance()->Register(this);
104   }
105 
106   const char* name;
107   CreateTestInstanceFunction* create_function;
108   TestDeclarationBase* next;
109 };
110 
111 }  // namespace detail
112 
113 // Registers |TestCase| with |TestRegistry|.
114 template <typename TestCase>
115 struct TestDeclaration : public detail::TestDeclarationBase {
TestDeclarationTestDeclaration116   TestDeclaration(const char* name)
117       : TestDeclarationBase(name, &detail::TestInstance<TestCase>::Create) {}
118 };
119 
120 #define TEST_F(fixture, name)                       \
121   class fixture##_##name : public fixture {         \
122    public:                                          \
123     void Run();                                     \
124   };                                                \
125   static testing::TestDeclaration<fixture##_##name> \
126       g_##fixture##_##name##_declaration(#name);    \
127   void fixture##_##name::Run()
128 
129 }  // namespace testing
130