4 * Copyright (c) 2022 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.
24 #include "spec_test.h"
25 #include "sensor-info-private.h"
27 #define TEST_COUNT_ONE 100
28 #define TEST_COUNT_ALL 1000
31 static GMainLoop *loop;
32 static bool turned_on[SENSOR_NUM];
33 static int min_range[SENSOR_NUM];
34 static int max_range[SENSOR_NUM];
35 static int called_num[SENSOR_NUM];
36 static int miss[SENSOR_NUM];
37 static int callback_count;
41 static void sensor_one_callback(sensor_h sensor, sensor_event_s events[], int events_count, void *user_data)
44 sensor_get_type(sensor, &type);
50 for (int i = 1; i < SENSOR_NUM; ++i){
51 if (sensor_info_lists[i].type == type){
57 for (int i = 0; i < events_count; ++i) {
62 if (events[i].value_count < sensor_info_lists[num].value_num)
63 value_count = events[i].value_count;
65 value_count = sensor_info_lists[num].value_num;
67 for (int j = 0; j < value_count; ++j) {
68 if (events[i].values[j] < min_range[num] || events[i].values[j] > max_range[num])
75 for (int j = 0; j < value_count; ++j)
76 g_print("%lf ", events[i].values[j]);
82 if (running && callback_count >= TEST_COUNT_ONE) {
85 g_main_loop_quit(loop);
88 else if (callback_count == TEST_COUNT_ONE / 10 * 2) {
92 else if (callback_count == TEST_COUNT_ONE / 10 * 4) {
96 else if (callback_count == TEST_COUNT_ONE / 10 * 6) {
100 else if (callback_count == TEST_COUNT_ONE / 10 * 8) {
101 g_print("80%%...\n");
105 static void sensor_all_callback(sensor_h sensor, sensor_event_s events[], int events_count, void *user_data)
108 sensor_get_type(sensor, &type);
114 for (int i = 1; i < SENSOR_NUM; ++i){
115 if (sensor_info_lists[i].type == type){
121 for (int i = 0; i < events_count; ++i) {
126 if (events[i].value_count < sensor_info_lists[num].value_num)
127 value_count = events[i].value_count;
129 value_count = sensor_info_lists[num].value_num;
131 for (int j = 0; j < value_count; ++j) {
132 if (events[i].values[j] < min_range[num] || events[i].values[j] > max_range[num]) {
141 if (running && callback_count >= TEST_COUNT_ALL) {
144 g_main_loop_quit(loop);
147 else if (callback_count == TEST_COUNT_ALL / 10 * 2) {
151 else if (callback_count == TEST_COUNT_ALL / 10 * 4) {
155 else if (callback_count == TEST_COUNT_ALL / 10 * 6) {
159 else if (callback_count == TEST_COUNT_ALL / 10 * 8) {
164 static int create_sensor_listener(int num, sensor_h *sensor, sensor_listener_h *listener, bool all)
169 const char *name = sensor_info_lists[num].name;
170 sensor_type_e type = sensor_info_lists[num].type;
172 ret = sensor_is_supported(type, &supported);
174 printf("fail \"sensor_is_supported\"\n");
178 else if (!supported) {
182 ret = sensor_get_default_sensor(type, sensor);
184 printf("fail \"sensor_get_default_sensor\"\n");
188 ret = sensor_create_listener(*sensor, listener);
190 printf("fail \"sensor_create_listener\"\n");
195 ret = sensor_listener_set_events_cb(*listener, sensor_all_callback, NULL);
197 ret = sensor_listener_set_events_cb(*listener, sensor_one_callback, NULL);
199 printf("fail \"sensor_listener_set_events_cb\"\n");
203 ret = sensor_listener_set_option(*listener, SENSOR_OPTION_ALWAYS_ON);
205 printf("fail \"sensor_listener_set_option\"\n");
209 ret = sensor_listener_set_interval(*listener, 10);
211 printf("fail \"sensor_listener_set_interval\"\n");
215 ret = sensor_get_min_range(*sensor, &min);
217 printf("fail \"sensor_get_min_range\"\n");
221 ret = sensor_get_max_range(*sensor, &max);
223 printf("fail \"sensor_get_max_range\"\n");
227 min_range[num] = min;
228 max_range[num] = max;
230 printf("%s : %f ~ %f\n", name, min, max);
232 ret = sensor_listener_start(*listener);
234 printf("%s error in listnener_start : %d\n", name, ret);
241 static void spec_test_one(int type)
244 sensor_listener_h listener;
245 int ret = create_sensor_listener(type, &sensor, &listener, false);
250 loop = g_main_loop_new(NULL, FALSE);
251 g_main_loop_run(loop);
253 printf("=====================================\n");
254 printf("test result\n");
255 printf("=====================================\n");
257 double rate = (double) (called_num[type] - miss[type]) / called_num[type] * 100;
258 printf("%s : %d / %d \n", sensor_info_lists[type].name, called_num[type] - miss[type], called_num[type]);
261 printf("PASS %.2lf%%\n", rate);
263 printf("FAIL %.2lf%%\n", rate);
266 static void spec_test_all(void)
268 sensor_h sensor[SENSOR_NUM];
269 sensor_listener_h listener[SENSOR_NUM];
272 for (int i = 1; i < SENSOR_NUM; ++i) {
275 if (create_sensor_listener(i, &sensor[i], &listener[i], true)) {
276 turned_on[i] = false;
285 printf("There is no supported sensor.\n");
289 printf("testing... ");
292 loop = g_main_loop_new(NULL, FALSE);
293 g_main_loop_run(loop);
295 printf("=====================================\n");
296 printf("test result\n");
297 printf("=====================================\n");
301 for (int i = 1; i < SENSOR_NUM; ++i) {
304 rate = (double) (called_num[i] - miss[i]) / called_num[i] * 100;
305 printf("%s : %d / %d \n", sensor_info_lists[i].name, called_num[i] - miss[i], called_num[i]);
308 printf("PASS %.2lf%%\n", rate);
310 printf("FAIL %.2lf%%\n", rate);
313 printf("%s value change was not occurred.\n", sensor_info_lists[i].name);
316 total_miss += miss[i];
319 printf("total : %d / %d \n", callback_count - total_miss, callback_count);
320 rate = (double) (callback_count - total_miss) / callback_count * 100;
322 printf("PASS %.2lf%%\n", rate);
324 printf("FAIL %.2lf%%\n", rate);
326 printf("=====================================\n");
327 printf("Not supported sensors list\n");
328 printf("=====================================\n");
330 for (int i = 1; i < SENSOR_NUM; ++i) {
331 if (turned_on[i] == false)
332 printf("%s\n", sensor_info_lists[i].name);
336 void spec_test(int type)
338 printf("=====================================\n");
339 printf("Sensor spec test (%s)\n", sensor_info_lists[type].name);
340 printf("=====================================\n");