fix invalid typecasting on 64bit
[platform/core/api/system-settings.git] / tests / sstt_main.c
1 /*
2  * Copyright (c) 2017-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <unistd.h>
17
18 #include "sst.h"
19 #include "sstt.h"
20
21 typedef struct __thread_test_table {
22         char * test_name;
23         GTestFunc t_func;
24 } thread_test_node;
25
26 static GThread *my_thread = NULL;
27 static GMainLoop *main_loop = NULL;
28 static thread_test_node thread_test_table[1000] = { 0 };
29 static int thread_test_idx = 0;
30 static int loop = 100;
31 static int thread_sw = 1;
32
33 static void *thread_function(void *data)
34 {
35         int cnt = 0;
36         int test_idx = GPOINTER_TO_INT(data);
37
38         while (thread_sw) {
39                 if (cnt > loop)
40                         break;
41                 cnt++;
42                 thread_test_table[test_idx].t_func();
43                 sleep(0);
44         }
45
46         return 0;
47
48 }
49
50 static GThread **unit_test_thread_start(int test_num)
51 {
52         int i;
53         GThread **thread_list = (GThread **)calloc(sizeof(GThread*), N_THREADS);
54         for (i = 0; i < N_THREADS; i++)
55                 thread_list[i] = g_thread_new(NULL, thread_function, GINT_TO_POINTER(test_num));
56
57         return thread_list;
58 }
59
60 static void unit_test_thread_stop(GThread **thread_list)
61 {
62         int i;
63         for (i = 0; i < N_THREADS; i++)
64                 g_thread_join(thread_list[i]);
65
66         g_free(thread_list);
67 }
68
69 static void unit_thread_test()
70 {
71         int i;
72         GThread **thread_test[MAX_TEST_CASE];
73
74         printf("START!!!! thread test for system-settings %d tests\n", thread_test_idx);
75
76         thread_sw = 1;
77         for (i = 0; i < thread_test_idx; i++) {
78                 thread_test[i] = unit_test_thread_start(i);
79                 printf("%3d %s:start!\n", i, thread_test_table[i].test_name);
80
81                 unit_test_thread_stop(thread_test[i]);
82                 printf("%3d %s:OK\n", i, thread_test_table[i].test_name);
83         }
84
85         printf("END!!!! thread test for system-settings\n");
86 }
87
88 static gpointer loop_func(gpointer data)
89 {
90         g_main_loop_run(main_loop);
91         return NULL;
92 }
93
94 static void start_main_loop_thread()
95 {
96         my_thread = g_thread_new(NULL, loop_func, NULL);
97 }
98
99 static void stop_main_loop_thread()
100 {
101         g_main_loop_quit(main_loop);
102         g_thread_join(my_thread);
103         g_main_loop_unref(main_loop);
104 }
105
106 void add_test_func(char *test_name, GTestFunc func_pointer)
107 {
108         g_test_add_func(test_name, func_pointer);
109
110         thread_test_table[thread_test_idx].test_name = test_name;
111         thread_test_table[thread_test_idx].t_func = func_pointer;
112
113         thread_test_idx++;
114 }
115
116 int main(int argc, char **argv)
117 {
118         main_loop = g_main_loop_new(NULL, FALSE);
119
120         g_test_init(&argc, &argv, NULL);
121         g_test_set_nonfatal_assertions();
122
123         start_main_loop_thread();
124
125         unittest_api();
126
127         g_test_run();
128         unit_thread_test();
129
130         stop_main_loop_thread();
131         return 0;
132 }