4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #pragma once /* __TEST_BENCH_H__ */
28 #define FAIL(left, comp, right) \
31 std::ostringstream os; \
32 os << __FUNCTION__ << "(" << __LINE__ << ") : " \
33 << #left << "(" << left << ") " \
34 << #comp << " " << #right << "(" << right << ")"; \
35 std::cout << os.str() << std::endl; \
36 test_bench::push_failure(__FUNCTION__, __LINE__, os.str()); \
39 #define PASS(left, comp, right) \
41 if (test_option::verbose) { \
43 std::ostringstream os; \
44 os << __FUNCTION__ << "(" << __LINE__ << ") : " \
45 << #left << "(" << left << ") " \
46 << #comp << " " << #right << "(" << right << ")"; \
47 std::cout << os.str() << std::endl; \
51 #define ASSERT(left, comp, right) \
53 if (!((left) comp (right))) { \
54 FAIL(left, comp, right); \
57 PASS(left, comp, right); \
61 #define EXPECT(left, comp, right) \
63 if (!((left) comp (right))) { \
64 FAIL(left, comp, right); \
66 PASS(left, comp, right); \
70 #define ASSERT_TRUE(condition) ASSERT(condition, ==, true)
71 #define ASSERT_FALSE(condition) ASSERT(condition, ==, false)
72 #define ASSERT_EQ(left, right) ASSERT(left, ==, right)
73 #define ASSERT_NE(left, right) ASSERT(left, !=, right)
74 #define ASSERT_LT(left, right) ASSERT(left, <, right)
75 #define ASSERT_LE(left, right) ASSERT(left, <=, right)
76 #define ASSERT_GT(left, right) ASSERT(left, >, right)
77 #define ASSERT_GE(left, right) ASSERT(left, >=, right)
78 #define ASSERT_NEAR(left, right, err) \
80 ASSERT(left, >=, (right - (err))); \
81 ASSERT(left, <=, (right + (err))); \
84 #define EXPECT_TRUE(condition) EXPECT(condition, ==, true)
85 #define EXPECT_FALSE(condition) EXPECT(condition, ==, false)
86 #define EXPECT_EQ(left, right) EXPECT(left, ==, right)
87 #define EXPECT_NE(left, right) EXPECT(left, !=, right)
88 #define EXPECT_LT(left, right) EXPECT(left, <, right)
89 #define EXPECT_LE(left, right) EXPECT(left, <=, right)
90 #define EXPECT_GT(left, right) EXPECT(left, >, right)
91 #define EXPECT_GE(left, right) EXPECT(left, >=, right)
92 #define EXPECT_NEAR(left, right, err) \
94 EXPECT(left, >=, (right - (err))); \
95 EXPECT(left, <=, (right + (err))); \
98 #define TESTCASE(group, name) \
99 class test_case_##group##_##name : public test_case { \
101 test_case_##group##_##name() \
102 : test_case(#group, #name) \
104 register_func(static_cast<test_case::test_func>(&test_case_##group##_##name::test)); \
107 } test_case_##group##_##name##_instance; \
108 bool test_case_##group##_##name::test(void)
111 * Declaration of test_option
116 static bool shuffle; /* TODO */
117 static bool show_list;
118 static int repeat; /* TODO */
119 static std::string filter;
120 static std::string output; /* TODO */
123 static int powersave;
125 static bool set_options(int argc, char *argv[]);
129 * Declaration of test_result
133 test_result(const std::string &_function, long _line, const std::string &_msg)
134 : function(_function)
138 std::string function;
144 * Declaration of test_case
148 test_case(const std::string &group, const std::string &name);
150 void run_testcase(void);
152 const std::string& group() const { return m_group; }
153 const std::string& name() const { return m_name; }
154 const std::string& fullname() const { return m_fullname; }
157 typedef bool (test_case::*test_func)();
160 void stopped(bool result);
161 void register_func(test_func func);
164 const std::string m_group;
165 const std::string m_name;
166 const std::string m_fullname;
171 * Declaration of test_bench
180 static void show_testcases(void);
182 static void register_testcase(const std::string &group, test_case *testcase);
184 static void run_all_testcases(void);
185 static void stop_all_testcases(void);
187 static void push_failure(const std::string &function, long line, const std::string &msg);
190 static test_bench& instance();
192 void add_failure(const std::string &function, long line, const std::string &msg);
196 void show_failures(void);
198 void add_testcase(const std::string &group, test_case *testcase);
200 bool filter(const std::string &name);
206 unsigned int count(void);
208 std::multimap<const std::string, test_case *> testcases;
209 std::vector<test_result> results;