#include "log.h"
#include "macro.h"
#include "mainloop.h"
-#include "test_bench.h"
-#include "sensor_adapter.h"
-#define TESTER_ARGC 3 /* e.g. {sensorctl, test, accelerometer} */
+std::vector<tester *> tester_manager::testers;
-#define MAX_COUNT 999999
-#define TEST_DEFAULT_INTERVAL 100
-#define TEST_DEFAULT_LATENCY 0
-#define TEST_DEFAULT_POWERSAVE_OPTION SENSOR_OPTION_ALWAYS_ON
-
-static sensor_type_t type;
-static int interval;
-static int latency;
-static int powersave;
-static int repeat;
-
-static int event_count = 0;
+tester::tester(const char *name)
+: m_name(name)
+{
+ tester_manager::register_tester(this);
+}
tester_manager::tester_manager()
{
{
}
+void tester_manager::register_tester(tester *test)
+{
+ testers.push_back(test);
+}
+
bool tester_manager::run(int argc, char *argv[])
{
- if (argc < TESTER_ARGC) {
- usage();
- return false;
- }
+ bool ret;
+ sensor_type_t type = ACCELEROMETER_SENSOR;
- if (!setup(argc, argv)) {
+ if (argc < TESTER_ARGC) {
usage();
return false;
}
- test_bench::run_all_testcase();
+ if (strncmp(argv[2], "auto", 4) != 0)
+ type = get_sensor_type(argv[2]);
- return true;
-}
+ tester *_tester = get_tester(argv[2]);
+ RETVM_IF(!_tester, false, "Cannot find matched tester\n");
-bool tester_manager::setup(int argc, char *argv[])
-{
- if (strncmp(argv[2], "auto", 4) == 0)
- return setup_auto(argc, argv);
+ ret = _tester->setup(type, argc, argv);
+ RETVM_IF(!ret, false, "Failed to setup injector\n");
- return setup_manual(argc, argv);
-}
+ ret = _tester->run(argc, argv);
+ RETVM_IF(!ret, false, "Failed to run tester\n");
-bool tester_manager::setup_auto(int argc, char *argv[])
-{
- if (argc > 5)
- repeat = atoi(argv[5]);
-
- test_option::show_full_log(true);
- test_option::set_options(argc, argv);
+ ret = _tester->teardown();
+ RETVM_IF(!ret, false, "Failed to tear down tester\n");
return true;
}
-bool tester_manager::setup_manual(int argc, char *argv[])
+tester *tester_manager::get_tester(const char *name)
{
- type = get_sensor_type(argv[2]);
- RETVM_IF(type == UNKNOWN_SENSOR, false, "Invalid argument\n");
-
- interval = TEST_DEFAULT_INTERVAL;
- latency = TEST_DEFAULT_LATENCY;
- powersave = TEST_DEFAULT_POWERSAVE_OPTION;
- event_count = 0;
-
- if (argc >= TESTER_ARGC + 1)
- interval = atoi(argv[TESTER_ARGC]);
- if (argc >= TESTER_ARGC + 2)
- latency = atoi(argv[TESTER_ARGC + 1]);
- if (argc >= TESTER_ARGC + 3)
- powersave = atoi(argv[TESTER_ARGC + 2]);
-
- test_option::show_full_log(true);
- test_option::set_group("skip_manual");
- /* test_option::set_options(argc, argv); */
-
- return true;
+ int count = testers.size();
+
+ for (int i = 0; i < count; ++i) {
+ if (strncmp(name, "auto", 4) == 0) {
+ if (testers[i]->name() == "auto")
+ return testers[i];
+ } else {
+ if (testers[i]->name() == "manual")
+ return testers[i];
+ }
+ }
+ return NULL;
}
void tester_manager::stop(void)
void tester_manager::usage(void)
{
- _N("usage: sensorctl test auto [group] [log]\n");
- _N("usage: sensorctl test <sensor_type> [interval] [batch_latency] [event_count] [test_count]\n");
+ _N("usage: sensorctl test auto [options] [--help]\n");
+ _N("usage: sensorctl test <sensor_type> [--help]\n");
usage_sensors();
_N("auto:\n");
- _N(" test sensors automatically.\n");
- _N("group:\n");
- _N(" a group name(or a specific word contained in the group name).\n");
- _N("log:\n");
- _N(" enable(1) or disable(0). default value is 1.\n");
- _N("sensor_type: specific sensor\n");
- _N(" test specific sensor manually.\n");
- _N("interval_ms:\n");
- _N(" interval. default value is 100ms.\n");
- _N("batch_latency_ms:\n");
- _N(" batch_latency. default value is 1000ms.\n");
- _N("event count(n):\n");
- _N(" test sensor until it gets n event. default is 999999(infinitly).\n");
- _N("test count(n):\n");
- _N(" test sensor in n times repetitively, default is 1.\n\n");
-}
-
-/* manual test case */
-static void test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
-{
- if (event_count >= MAX_COUNT) {
- mainloop::stop();
- return;
- }
-
- _N("%llu ", data->timestamp);
- for (int i = 0; i < data->value_count; ++i)
- _N(" %10f", data->values[i]);
- _N("\n");
-}
-
-TESTCASE(skip_manual, sensor_test)
-{
- int handle;
- bool ret;
- int index = 0;
- sensor_data_t data;
-
- if (sensor_adapter::get_count(type) > 1) {
- _N("There are more than 2 sensors. please enter the index : ");
- if (scanf("%d", &index) != 1)
- return false;
- }
-
- sensor_info info(type, index, interval, latency, powersave, test_cb, NULL);
-
- ret = sensor_adapter::start(info, handle);
- ASSERT_TRUE(ret);
-
- ret = sensor_adapter::get_data(handle, type, data);
- EXPECT_TRUE(ret);
-
- mainloop::run();
-
- ret = sensor_adapter::stop(info, handle);
- ASSERT_TRUE(ret);
-
- return true;
+ _N(" Run all testcases automatically.\n");
+ _N("sensor_type:\n");
+ _N(" Run the specific sensor manually.\n");
+ _N("help: \n");
+ _N(" Prints the synopsis and a list of options.\n");
}
#include "sensor_manager.h"
+#define TESTER_ARGC 3 /* e.g. {sensorctl, test, accelerometer} */
+
+#define REGISTER_TESTER(name, tester_type) \
+static tester_type tester(name);
+
+class tester {
+public:
+ tester(const char *name);
+ virtual ~tester() {}
+
+ virtual bool setup(sensor_type_t type, int argc, char *argv[]) { return true; }
+ virtual bool teardown(void) { return true; }
+ virtual bool run(int argc, char *argv[]) = 0;
+
+ const std::string& name() const { return m_name; }
+
+private:
+ std::string m_name;
+};
+
class tester_manager : public sensor_manager {
public:
+ static void register_tester(tester *test);
+
tester_manager();
virtual ~tester_manager();
void stop(void);
private:
- bool setup(int argc, char *argv[]);
- bool setup_auto(int argc, char *argv[]);
- bool setup_manual(int argc, char *argv[]);
+ static std::vector<tester *> testers;
+ tester *get_tester(const char *type);
void usage(void);
};
-
--- /dev/null
+/*
+ * sensorctl
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "log.h"
+#include "tester.h"
+#include "test_bench.h"
+
+#define TEST_AUTO "auto"
+
+class tester_auto : public tester {
+public:
+ tester_auto(const char *name);
+ virtual ~tester_auto();
+
+ bool setup(sensor_type_t type, int argc, char *argv[]);
+ bool teardown(void);
+
+ bool run(int argc, char *argv[]);
+ void usage(void);
+};
+
+tester_auto::tester_auto(const char *name)
+: tester(name)
+{
+}
+
+tester_auto::~tester_auto()
+{
+}
+
+bool tester_auto::setup(sensor_type_t type, int argc, char *argv[])
+{
+ test_option::filter = "(?!manual|skip)[\\w\\.]+";
+
+ if (!test_option::set_options(argc, argv)) {
+ usage();
+ return false;
+ }
+
+ return true;
+}
+
+bool tester_auto::teardown(void)
+{
+ return true;
+}
+
+bool tester_auto::run(int argc, char *argv[])
+{
+ test_bench::run_all_testcases();
+ return true;
+}
+
+void tester_auto::usage(void)
+{
+ _N("Usage : sensorctl test auto [--help] [--list] [--filter=<regex>]\n");
+ _N(" [--verbose] [--shuffle] [--repeat]\n");
+ _N(" [--output]\n");
+
+ _N("Examples:\n");
+ _N(" sensorctl test auto --list\n");
+ _N(" sensorctl test auto --filter=accelerometer.interval*\n");
+ _N(" sensorctl test auto --filter=accelerometer.start\n");
+ _N(" sensorctl test auto --filter=accelerometer.verify\n");
+ _N(" sensorctl test auto --filter=ipc.socket*\n");
+ _N(" sensorctl test auto --shuffle\n");
+ _N(" sensorctl test auto --verbose\n");
+ _N(" sensorctl test auto --output=results.log\n");
+}
+
+REGISTER_TESTER(TEST_AUTO, tester_auto);
--- /dev/null
+/*
+ * sensorctl
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <getopt.h>
+
+#include "tester.h"
+#include "mainloop.h"
+#include "log.h"
+#include "test_bench.h"
+#include "sensor_adapter.h"
+
+#define MAX_COUNT 999999
+#define TEST_DEFAULT_INTERVAL 100
+#define TEST_DEFAULT_LATENCY 0
+#define TEST_DEFAULT_POWERSAVE_OPTION SENSOR_OPTION_ALWAYS_ON
+
+#define TEST_MANUAL "manual"
+
+static sensor_type_t type;
+static int interval;
+static int latency;
+static int powersave;
+//static int repeat;
+
+class tester_manual : public tester {
+public:
+ tester_manual(const char *name);
+ virtual ~tester_manual();
+
+ bool setup(sensor_type_t type, int argc, char *argv[]);
+ bool teardown(void);
+
+ bool run(int argc, char *argv[]);
+ void usage(void);
+};
+
+tester_manual::tester_manual(const char *name)
+: tester(name)
+{
+}
+
+tester_manual::~tester_manual()
+{
+}
+
+bool tester_manual::setup(sensor_type_t type, int argc, char *argv[])
+{
+ interval = TEST_DEFAULT_INTERVAL;
+ latency = TEST_DEFAULT_LATENCY;
+ powersave = TEST_DEFAULT_POWERSAVE_OPTION;
+
+ if (!test_option::set_options(argc, argv)) {
+ usage();
+ return false;
+ }
+
+ interval = test_option::interval;
+ latency = test_option::latency;
+ powersave = test_option::powersave;
+
+ if (interval == -1)
+ interval = TEST_DEFAULT_INTERVAL;
+ if (latency == -1)
+ latency = TEST_DEFAULT_LATENCY;
+ if (powersave == -1)
+ powersave = TEST_DEFAULT_POWERSAVE_OPTION;
+
+ return true;
+}
+
+bool tester_manual::teardown(void)
+{
+ return true;
+}
+
+bool tester_manual::run(int argc, char *argv[])
+{
+ test_option::filter = "^manual[\\w\\.]+";
+ test_bench::run_all_testcases();
+
+ return true;
+}
+
+void tester_manual::usage(void)
+{
+ _N("Usage : sensorctl test <sensor_type>\n");
+ _N(" [--interval=NUMBER] [--batch_latency=NUMBER] [--powersave=TYPE]\n");
+ _N(" [--repeat=NUMBER] [--output=FILE_PATH] [--help] [--verbose]\n");
+
+ _N("Examples:\n");
+ _N(" sensorctl test accelerometer\n");
+ _N(" sensorctl test accelerometer --interval=100 --batch_latency=1000\n");
+ _N(" sensorctl test accelerometer --i 100 --b 1000 --p 0\n");
+ _N(" sensorctl test accelerometer --i 100 --shuffle\n");
+ _N(" sensorctl test accelerometer --i 100 --verbose\n");
+ _N(" sensorctl test accelerometer --i 100 --output=results.log\n");
+}
+
+static void test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
+{
+ _N("%llu ", data->timestamp);
+ for (int i = 0; i < data->value_count; ++i)
+ _N(" %10f", data->values[i]);
+ _N("\n");
+}
+
+TESTCASE(manual_test, sensor)
+{
+ int handle;
+ bool ret;
+ int index = 0;
+ sensor_data_t data;
+
+ if (sensor_adapter::get_count(type) > 1) {
+ _N("There are more than 2 sensors. please enter the index : ");
+ if (scanf("%d", &index) != 1)
+ return false;
+ }
+
+ sensor_info info(type, index, interval, latency, powersave, test_cb, NULL);
+
+ ret = sensor_adapter::start(info, handle);
+ ASSERT_TRUE(ret);
+
+ ret = sensor_adapter::get_data(handle, type, data);
+ EXPECT_TRUE(ret);
+
+ mainloop::run();
+
+ ret = sensor_adapter::stop(info, handle);
+ ASSERT_TRUE(ret);
+
+ return true;
+}
+
+REGISTER_TESTER(TEST_MANUAL, tester_manual);