[Coverity Issue Fixes]
[platform/core/system/sensord.git] / src / sensorctl / test_bench.h
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #pragma once /* __TEST_BENCH_H__ */
21
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 #include <map>
27
28 #define FAIL(left, comp, right) \
29 do { \
30         _E("[   FAIL   ] "); \
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()); \
37 } while (0)
38
39 #define PASS(left, comp, right) \
40 do { \
41         if (test_option::verbose) { \
42                 _I("[   PASS   ] "); \
43                 std::ostringstream os; \
44                 os << __FUNCTION__ << "(" << __LINE__ << ") : " \
45                         << #left << "(" << left << ") " \
46                         << #comp << " " << #right << "(" << right << ")"; \
47                 std::cout << os.str() << std::endl; \
48         } \
49 } while (0)
50
51 #define ASSERT(left, comp, right) \
52 do { \
53         if (!((left) comp (right))) { \
54                 FAIL(left, comp, right); \
55                 return false; \
56         } \
57         PASS(left, comp, right); \
58 } while (0)
59
60
61 #define EXPECT(left, comp, right) \
62 do { \
63         if (!((left) comp (right))) { \
64                 FAIL(left, comp, right); \
65         } else { \
66                 PASS(left, comp, right); \
67         } \
68 } while (0)
69
70 #define ASSERT_FREE(expr, X) \
71         do { if (expr) { free (X); X = NULL; } } while (0)
72
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) \
82 do { \
83         ASSERT(left, >=, (right - (err))); \
84         ASSERT(left, <=, (right + (err))); \
85 } while (0)
86
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) \
96 do { \
97         EXPECT(left, >=, (right - (err))); \
98         EXPECT(left, <=, (right + (err))); \
99 } while (0)
100
101 #define TESTCASE(group, name) \
102 class test_case_##group##_##name : public test_case { \
103 public: \
104         test_case_##group##_##name() \
105         : test_case(#group, #name) \
106         { \
107                 register_func(static_cast<test_case::test_func>(&test_case_##group##_##name::test)); \
108         } \
109         bool test(void); \
110 } test_case_##group##_##name##_instance; \
111 bool test_case_##group##_##name::test(void)
112
113 /*
114  * Declaration of test_option
115  */
116 class test_option {
117 public:
118         static bool verbose;
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 */
124         static int interval;
125         static int latency;
126         static int powersave;
127
128         static bool set_options(int argc, char *argv[]);
129 };
130
131 /*
132  * Declaration of test_result
133  */
134 class test_result {
135 public:
136         test_result(const std::string &_function, long _line, const std::string &_msg)
137         : function(_function)
138         , line(_line)
139         , msg(_msg) { }
140
141         std::string function;
142         long line;
143         std::string msg;
144 };
145
146 /*
147  * Declaration of test_case
148  */
149 class test_case {
150 public:
151         test_case(const std::string &group, const std::string &name);
152
153         void run_testcase(void);
154
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; }
158
159 protected:
160         typedef bool (test_case::*test_func)();
161
162         void started(void);
163         void stopped(bool result);
164         void register_func(test_func func);
165
166 private:
167         const std::string m_group;
168         const std::string m_name;
169         const std::string m_fullname;
170         test_func m_func;
171 };
172
173 /*
174  * Declaration of test_bench
175  */
176 class test_bench {
177 public:
178         test_bench()
179         : m_failure_count(0)
180         , m_stop(false)
181         {}
182
183         static void show_testcases(void);
184
185         static void register_testcase(const std::string &group, test_case *testcase);
186
187         static void run_all_testcases(void);
188         static void stop_all_testcases(void);
189
190         static void push_failure(const std::string &function, long line, const std::string &msg);
191
192 private:
193         static test_bench& instance();
194
195         void add_failure(const std::string &function, long line, const std::string &msg);
196
197         void started(void);
198         void stopped(void);
199         void show_failures(void);
200
201         void add_testcase(const std::string &group, test_case *testcase);
202
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);
206
207         void run(void);
208         void stop(void);
209
210         void show(void);
211
212         unsigned int count(void);
213
214         std::multimap<const std::string, test_case *> testcases;
215         std::vector<test_result> results;
216         int m_failure_count;
217         bool m_stop;
218 };