sensorctl: add options/features for usability
[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_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) \
79 do { \
80         ASSERT(left, >=, (right - (err))); \
81         ASSERT(left, <=, (right + (err))); \
82 } while (0)
83
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) \
93 do { \
94         EXPECT(left, >=, (right - (err))); \
95         EXPECT(left, <=, (right + (err))); \
96 } while (0)
97
98 #define TESTCASE(group, name) \
99 class test_case_##group##_##name : public test_case { \
100 public: \
101         test_case_##group##_##name() \
102         : test_case(#group, #name) \
103         { \
104                 register_func(static_cast<test_case::test_func>(&test_case_##group##_##name::test)); \
105         } \
106         bool test(void); \
107 } test_case_##group##_##name##_instance; \
108 bool test_case_##group##_##name::test(void)
109
110 /*
111  * Declaration of test_option
112  */
113 class test_option {
114 public:
115         static bool verbose;
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 */
121         static int interval;
122         static int latency;
123         static int powersave;
124
125         static bool set_options(int argc, char *argv[]);
126 };
127
128 /*
129  * Declaration of test_result
130  */
131 class test_result {
132 public:
133         test_result(const std::string &_function, long _line, const std::string &_msg)
134         : function(_function)
135         , line(_line)
136         , msg(_msg) { }
137
138         std::string function;
139         long line;
140         std::string msg;
141 };
142
143 /*
144  * Declaration of test_case
145  */
146 class test_case {
147 public:
148         test_case(const std::string &group, const std::string &name);
149
150         void run_testcase(void);
151
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; }
155
156 protected:
157         typedef bool (test_case::*test_func)();
158
159         void started(void);
160         void stopped(bool result);
161         void register_func(test_func func);
162
163 private:
164         const std::string m_group;
165         const std::string m_name;
166         const std::string m_fullname;
167         test_func m_func;
168 };
169
170 /*
171  * Declaration of test_bench
172  */
173 class test_bench {
174 public:
175         test_bench()
176         : m_failure_count(0)
177         , m_stop(false)
178         {}
179
180         static void show_testcases(void);
181
182         static void register_testcase(const std::string &group, test_case *testcase);
183
184         static void run_all_testcases(void);
185         static void stop_all_testcases(void);
186
187         static void push_failure(const std::string &function, long line, const std::string &msg);
188
189 private:
190         static test_bench& instance();
191
192         void add_failure(const std::string &function, long line, const std::string &msg);
193
194         void started(void);
195         void stopped(void);
196         void show_failures(void);
197
198         void add_testcase(const std::string &group, test_case *testcase);
199
200         bool filter(const std::string &name);
201         void run(void);
202         void stop(void);
203
204         void show(void);
205
206         unsigned int count(void);
207
208         std::multimap<const std::string, test_case *> testcases;
209         std::vector<test_result> results;
210         int m_failure_count;
211         bool m_stop;
212 };