Change naming style
[platform/core/api/sensor.git] / tests / spec_test.c
1 /*
2  * sensor-test
3  *
4  * Copyright (c) 2022 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 #include <stdio.h>
21 #include <glib.h>
22 #include <sensor.h>
23
24 #include "spec_test.h"
25 #include "sensor-info-private.h"
26
27 #define TEST_COUNT_ONE 100
28 #define TEST_COUNT_ALL 1000
29 #define PASS 90
30
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;
38 static bool running;
39
40
41 static void sensor_one_callback(sensor_h sensor, sensor_event_s events[], int events_count, void *user_data)
42 {
43         sensor_type_e type;
44         sensor_get_type(sensor, &type);
45         int num = 0;
46         bool missed;
47
48         ++callback_count;
49
50         for (int i = 1; i < SENSOR_NUM; ++i){
51                 if (sensor_info_lists[i].type == type){
52                         num = i;
53                         break;
54                 }
55         }
56
57         for (int i = 0; i < events_count; ++i) {
58                 missed = false;
59                 ++called_num[num];
60
61                 int value_count;
62                 if (events[i].value_count < sensor_info_lists[num].value_num)
63                         value_count = events[i].value_count;
64                 else
65                         value_count = sensor_info_lists[num].value_num;
66
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])
69                                 missed = true;
70                 }
71
72                 if (missed) {
73                         ++miss[num];
74                         g_print("miss : ");
75                         for (int j = 0; j < value_count; ++j)
76                                 g_print("%lf ", events[i].values[j]);
77                         g_print("\n");
78                 }
79         }
80
81
82         if (running && callback_count >= TEST_COUNT_ONE) {
83                 g_print("done.\n");
84                 running = false;
85                 g_main_loop_quit(loop);
86         }
87
88         else if (callback_count == TEST_COUNT_ONE / 10 * 2) {
89                 g_print("20%%...\n");
90         }
91
92         else if (callback_count == TEST_COUNT_ONE / 10 * 4) {
93                 g_print("40%%...\n");
94         }
95
96         else if (callback_count == TEST_COUNT_ONE / 10 * 6) {
97                 g_print("60%%...\n");
98         }
99
100         else if (callback_count == TEST_COUNT_ONE / 10 * 8) {
101                 g_print("80%%...\n");
102         }
103 }
104
105 static void sensor_all_callback(sensor_h sensor, sensor_event_s events[], int events_count, void *user_data)
106 {
107         sensor_type_e type;
108         sensor_get_type(sensor, &type);
109         int num = 0;
110         bool missed;
111
112         ++callback_count;
113
114         for (int i = 1; i < SENSOR_NUM; ++i){
115                 if (sensor_info_lists[i].type == type){
116                         num = i;
117                         break;
118                 }
119         }
120
121         for (int i = 0; i < events_count; ++i) {
122                 missed = false;
123                 ++called_num[num];
124
125                 int value_count;
126                 if (events[i].value_count < sensor_info_lists[num].value_num)
127                         value_count = events[i].value_count;
128                 else
129                         value_count = sensor_info_lists[num].value_num;
130
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]) {
133                                 missed = true;
134                         }
135                 }
136
137                 if (missed)
138                         ++miss[num];
139         }
140
141         if (running && callback_count >= TEST_COUNT_ALL) {
142                 g_print("done.\n");
143                 running = false;
144                 g_main_loop_quit(loop);
145         }
146
147         else if (callback_count == TEST_COUNT_ALL / 10 * 2) {
148                 g_print("20%%...");
149         }
150
151         else if (callback_count == TEST_COUNT_ALL / 10 * 4) {
152                 g_print("40%%...");
153         }
154
155         else if (callback_count == TEST_COUNT_ALL / 10 * 6) {
156                 g_print("60%%...");
157         }
158
159         else if (callback_count == TEST_COUNT_ALL / 10 * 8) {
160                 g_print("80%%...");
161         }
162 }
163
164 static int create_sensor_listener(int num, sensor_h *sensor, sensor_listener_h *listener, bool all)
165 {
166         bool supported;
167         int ret = 0;
168         float min, max;
169         const char *name = sensor_info_lists[num].name;
170         sensor_type_e type = sensor_info_lists[num].type;
171
172         ret = sensor_is_supported(type, &supported);
173         if (ret < 0) {
174                 printf("fail \"sensor_is_supported\"\n");
175                 return ret;
176         }
177
178         else if (!supported) {
179                 return -1;
180         }
181
182         ret = sensor_get_default_sensor(type, sensor);
183         if (ret < 0) {
184                 printf("fail \"sensor_get_default_sensor\"\n");
185                 return ret;
186         }
187
188         ret = sensor_create_listener(*sensor, listener);
189         if (ret < 0) {
190                 printf("fail \"sensor_create_listener\"\n");
191                 return ret;
192         }
193
194         if (all)
195                 ret = sensor_listener_set_events_cb(*listener, sensor_all_callback, NULL);
196         else
197                 ret = sensor_listener_set_events_cb(*listener, sensor_one_callback, NULL);
198         if (ret < 0) {
199                 printf("fail \"sensor_listener_set_events_cb\"\n");
200                 return ret;
201         }
202
203         ret = sensor_listener_set_option(*listener, SENSOR_OPTION_ALWAYS_ON);
204         if (ret < 0) {
205                 printf("fail \"sensor_listener_set_option\"\n");
206                 return ret;
207         }
208
209         ret = sensor_listener_set_interval(*listener, 10);
210         if (ret < 0) {
211                 printf("fail \"sensor_listener_set_interval\"\n");
212                 return ret;
213         }
214
215         ret = sensor_get_min_range(*sensor, &min);
216         if (ret < 0) {
217                 printf("fail \"sensor_get_min_range\"\n");
218                 return ret;
219         }
220
221         ret = sensor_get_max_range(*sensor, &max);
222         if (ret < 0) {
223                 printf("fail \"sensor_get_max_range\"\n");
224                 return ret;
225         }
226
227         min_range[num] = min;
228         max_range[num] = max;
229
230         printf("%s : %f ~ %f\n", name, min, max);
231
232         ret = sensor_listener_start(*listener);
233         if (ret < 0) {
234                 printf("%s error in listnener_start : %d\n", name, ret);
235                 return ret;
236         }
237
238         return 0;
239 }
240
241 static void spec_test_one(int type)
242 {
243         sensor_h sensor;
244         sensor_listener_h listener;
245         int ret = create_sensor_listener(type, &sensor, &listener, false);
246         if (ret < 0)
247                 return;
248
249         running = true;
250         loop = g_main_loop_new(NULL, FALSE);
251         g_main_loop_run(loop);
252
253         printf("=====================================\n");
254         printf("test result\n");
255         printf("=====================================\n");
256
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]);
259
260         if (rate >= PASS)
261                 printf("PASS %.2lf%%\n", rate);
262         else
263                 printf("FAIL %.2lf%%\n", rate);
264 }
265
266 static void spec_test_all(void)
267 {
268         sensor_h sensor[SENSOR_NUM];
269         sensor_listener_h listener[SENSOR_NUM];
270         int usable = 0;
271
272         for (int i = 1; i < SENSOR_NUM; ++i) {
273                 called_num[i] = 0;
274                 miss[i] = 0;
275                 if (create_sensor_listener(i, &sensor[i], &listener[i], true)) {
276                         turned_on[i] = false;
277                 }
278                 else {
279                         turned_on[i] = true;
280                         ++usable;
281                 }
282         }
283
284         if (usable == 0) {
285                 printf("There is no supported sensor.\n");
286                 return;
287         }
288
289         printf("testing... ");
290
291         running = true;
292         loop = g_main_loop_new(NULL, FALSE);
293         g_main_loop_run(loop);
294
295         printf("=====================================\n");
296         printf("test result\n");
297         printf("=====================================\n");
298
299         int total_miss = 0;
300         double rate = 0;
301         for (int i = 1; i < SENSOR_NUM; ++i) {
302                 if (turned_on[i]) {
303                         if (called_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]);
306
307                                 if (rate >= PASS)
308                                         printf("PASS %.2lf%%\n", rate);
309                                 else
310                                         printf("FAIL %.2lf%%\n", rate);
311                         }
312                         else {
313                                 printf("%s value change was not occurred.\n", sensor_info_lists[i].name);
314                         }
315                 }
316                 total_miss += miss[i];
317         }
318
319         printf("total : %d / %d \n", callback_count - total_miss, callback_count);
320         rate = (double) (callback_count - total_miss) / callback_count * 100;
321         if (rate >= PASS)
322                 printf("PASS %.2lf%%\n", rate);
323         else
324                 printf("FAIL %.2lf%%\n", rate);
325
326         printf("=====================================\n");
327         printf("Not supported sensors list\n");
328         printf("=====================================\n");
329
330         for (int i = 1; i < SENSOR_NUM; ++i) {
331                 if (turned_on[i] == false)
332                         printf("%s\n", sensor_info_lists[i].name);
333         }
334 }
335
336 void spec_test(int type)
337 {
338         printf("=====================================\n");
339         printf("Sensor spec test (%s)\n", sensor_info_lists[type].name);
340         printf("=====================================\n");
341
342         if (type == 0)
343                 spec_test_all();
344         else
345                 spec_test_one(type);
346 }