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_FREE(expr, X) \
71 do { if (expr) { free (X); X = NULL; } } while (0)
73 #define ASSERT_TRUE(condition) ASSERT(condition, ==, true)
74 #define ASSERT_FALSE(condition) ASSERT(condition, ==, false)
75 #define ASSERT_EQ(left, right) ASSERT(left, ==, right)
76 #define ASSERT_NE(left, right) ASSERT(left, !=, right)
77 #define ASSERT_LT(left, right) ASSERT(left, <, right)
78 #define ASSERT_LE(left, right) ASSERT(left, <=, right)
79 #define ASSERT_GT(left, right) ASSERT(left, >, right)
80 #define ASSERT_GE(left, right) ASSERT(left, >=, right)
81 #define ASSERT_NEAR(left, right, err) \
83 ASSERT(left, >=, (right - (err))); \
84 ASSERT(left, <=, (right + (err))); \
87 #define EXPECT_TRUE(condition) EXPECT(condition, ==, true)
88 #define EXPECT_FALSE(condition) EXPECT(condition, ==, false)
89 #define EXPECT_EQ(left, right) EXPECT(left, ==, right)
90 #define EXPECT_NE(left, right) EXPECT(left, !=, right)
91 #define EXPECT_LT(left, right) EXPECT(left, <, right)
92 #define EXPECT_LE(left, right) EXPECT(left, <=, right)
93 #define EXPECT_GT(left, right) EXPECT(left, >, right)
94 #define EXPECT_GE(left, right) EXPECT(left, >=, right)
95 #define EXPECT_NEAR(left, right, err) \
97 EXPECT(left, >=, (right - (err))); \
98 EXPECT(left, <=, (right + (err))); \
101 #define TESTCASE(group, name) \
102 class test_case_##group##_##name : public test_case { \
104 test_case_##group##_##name() \
105 : test_case(#group, #name) \
107 register_func(static_cast<test_case::test_func>(&test_case_##group##_##name::test)); \
110 } test_case_##group##_##name##_instance; \
111 bool test_case_##group##_##name::test(void)
114 * Declaration of test_option
119 static bool shuffle; /* TODO */
120 static bool show_list;
121 static int repeat; /* TODO */
122 static std::string filter;
123 static std::string output; /* TODO */
126 static int powersave;
128 static bool set_options(int argc, char *argv[]);
132 * Declaration of test_result
136 test_result(const std::string &_function, long _line, const std::string &_msg)
137 : function(_function)
141 std::string function;
147 * Declaration of test_case
151 test_case(const std::string &group, const std::string &name);
153 void run_testcase(void);
155 const std::string& group() const { return m_group; }
156 const std::string& name() const { return m_name; }
157 const std::string& fullname() const { return m_fullname; }
160 typedef bool (test_case::*test_func)();
163 void stopped(bool result);
164 void register_func(test_func func);
167 const std::string m_group;
168 const std::string m_name;
169 const std::string m_fullname;
174 * Declaration of test_bench
183 static void show_testcases(void);
185 static void register_testcase(const std::string &group, test_case *testcase);
187 static void run_all_testcases(void);
188 static void stop_all_testcases(void);
190 static void push_failure(const std::string &function, long line, const std::string &msg);
193 static test_bench& instance();
195 void add_failure(const std::string &function, long line, const std::string &msg);
199 void show_failures(void);
201 void add_testcase(const std::string &group, test_case *testcase);
203 bool filter(const std::string &name, const std::string &filter);
204 bool filter_name(const std::string &name);
205 bool filter_group(const std::string &group);
212 unsigned int count(void);
214 std::multimap<const std::string, test_case *> testcases;
215 std::vector<test_result> results;