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 #include "test_bench.h"
28 * Implementation of test_option
30 bool test_option::verbose = false;
31 bool test_option::shuffle = false;
32 bool test_option::show_list = false;
33 int test_option::repeat = 1;
34 std::string test_option::filter = "";
35 std::string test_option::output = "";
36 int test_option::interval = -1;
37 int test_option::latency = -1;
38 int test_option::powersave = -1;
40 bool test_option::set_options(int argc, char *argv[])
45 static struct option options[] = {
46 {"list", no_argument, 0, 'l'},
47 {"filter", required_argument, 0, 'f'},
48 {"verbose", no_argument, 0, 'v'},
49 {"shuffle", no_argument, 0, 's'},
50 {"repeat", required_argument, 0, 'r'},
51 {"output", required_argument, 0, 'o'},
54 {"interval", required_argument, 0, 'i'},
55 {"batch_latency", required_argument, 0, 'b'},
56 {"powersave", required_argument, 0, 'p'},
58 {"help", no_argument, 0, 'h'},
63 c = getopt_long(argc, argv, "lfvsroibph:", options, &option_index);
71 _I("== Testcase List ==\n");
72 test_bench::show_testcases();
73 test_option::show_list = true;
76 _I("Filter : %s\n", optarg);
78 test_option::filter = optarg;
81 _I("Verbose is on\n");
82 test_option::verbose = true;
85 _I("Shuffle is on(Default seed)\n");
86 test_option::shuffle = true;
89 _I("Repeat : %s\n", optarg);
91 test_option::repeat = atoi(optarg);
95 _W("File output is not supported yet, use $sensorctl > out : %s\n", optarg);
97 test_option::output = optarg;
100 _I("Interval : %s\n", optarg);
102 test_option::interval = atoi(optarg);
105 _I("Batch latency : %s\n", optarg);
107 test_option::latency = atoi(optarg);
110 _I("Power save : %s\n", optarg);
112 test_option::powersave = atoi(optarg);
125 * Implementation of test_case
127 test_case::test_case(const std::string &group, const std::string &name)
130 , m_fullname(group + "." + m_name)
133 test_bench::register_testcase(group, this);
136 void test_case::started(void)
139 _N("%s\n", m_fullname.c_str());
142 void test_case::stopped(bool result)
148 _N("%s\n", m_fullname.c_str());
151 void test_case::run_testcase(void)
156 result = (this->*m_func)();
160 void test_case::register_func(test_func func)
166 * Implementation of test_bench
168 test_bench& test_bench::instance()
170 static test_bench bench;
174 void test_bench::show_testcases(void)
179 void test_bench::register_testcase(const std::string &group, test_case *testcase)
181 instance().add_testcase(group, testcase);
184 void test_bench::push_failure(const std::string &function, long line, const std::string &msg)
186 instance().add_failure(function, line, msg);
189 void test_bench::run_all_testcases(void)
191 if (test_option::show_list)
197 void test_bench::stop_all_testcases(void)
202 void test_bench::add_testcase(const std::string &group, test_case *testcase)
207 testcases.insert(std::pair<const std::string, test_case *>(group, testcase));
210 void test_bench::add_failure(const std::string &function, long line, const std::string &msg)
212 results.emplace_back(function, line, msg);
216 void test_bench::started(void)
219 _N("Running %d testcase(s)\n", count());
222 void test_bench::stopped(void)
225 _N("%d testcase(s) ran\n", count());
228 void test_bench::show(void)
238 for (auto it = testcases.begin(); it != testcases.end();
239 it = testcases.upper_bound(it->first)) {
240 auto range = testcases.equal_range(it->first);
241 _I("[%s]\n", it->first.c_str());
243 for (auto testcase = range.first; testcase != range.second; ++testcase)
244 _N(" * %s\n", testcase->second->name().c_str());
247 _I("Testcase Count : %u\n", count());
250 void test_bench::show_failures(void)
252 _N("================================\n");
254 if (m_failure_count == 0) {
256 _N("%d tests\n", count() - m_failure_count);
260 _N("%d case(s) are failed, listed below:\n", m_failure_count);
262 for (unsigned int i = 0; i < results.size(); ++i) {
264 _N("%s\n", results[i].msg.c_str());
268 bool test_bench::filter(const std::string &name, const std::string &filter_str)
271 std::regex filter(filter_str.c_str(), std::regex::optimize);
272 if (!std::regex_match(name, filter))
274 } catch (std::regex_error &e) {
280 bool test_bench::filter_name(const std::string &name)
282 std::size_t gindex = test_option::filter.find(".");
283 if (gindex == std::string::npos)
286 std::size_t size = test_option::filter.size();
287 std::string filter_name = test_option::filter.substr(gindex + 1, size);
289 return filter(name, filter_name);
292 bool test_bench::filter_group(const std::string &group)
294 std::size_t gindex = test_option::filter.find(".");
295 if (gindex == std::string::npos)
296 gindex = test_option::filter.size();
298 std::string filter_group = test_option::filter.substr(0, gindex);
300 return filter(group, filter_group);
303 void test_bench::run(void)
310 for (auto it = testcases.begin(); it != testcases.end();
311 it = testcases.upper_bound(it->first)) {
313 if (!filter_group(it->second->group())) continue;
315 auto range = testcases.equal_range(it->first);
317 /* Time measurement for test group */
318 clock_t start = clock();
320 _I("[----------] %d tests from %s\n", testcases.count(it->first), it->first.c_str());
321 for (auto testcase = range.first; testcase != range.second; ++testcase) {
323 if (!filter_name(testcase->second->name())) continue;
325 testcase->second->run_testcase();
328 _I("[----------] %d tests from %s (%.4f sec)\n",
329 testcases.count(it->first), it->first.c_str(),
330 (double)(clock() - start)/ CLOCKS_PER_SEC);
337 void test_bench::stop(void)
342 unsigned int test_bench::count(void)
344 static int count = -1;
349 for (auto it = testcases.begin(); it != testcases.end(); ++it) {
350 if (!filter_group(it->second->group()))
353 if (!filter_name(it->second->name()))