4f92c3200f5daa093992a7c352a10263262464b4
[platform/core/api/system-settings.git] / unit_test / src / unit_test_fw.c
1 #include <unit_test_fw.h>
2
3 typedef struct __thread_test_table {
4         char * test_name;
5         GTestFunc t_func;
6 } thread_test_node;
7
8
9 static GThread *my_thread = NULL;
10 static GMainLoop *main_loop = NULL;
11 static thread_test_node thread_test_table[1000] = {0};
12 static int thread_test_idx = 0;
13 static int loop = 100;
14 static int thread_sw = 1;
15
16
17 void *thread_function(void *data)
18 {
19         int cnt = 0;
20         int test_idx = (int)data;
21
22         while (thread_sw) {
23                 if (cnt > loop)
24                         break;
25                 cnt++;
26                 thread_test_table[test_idx].t_func();
27                 sleep(0);
28         }
29
30         return 0;
31
32 }
33
34 static GThread **unit_test_thread_start(int test_num)
35 {
36         int i;
37         GThread **thread_list = (GThread **)calloc(sizeof(GThread*), N_THREADS);
38         for (i = 0; i < N_THREADS; i++) {
39                 thread_list[i] = g_thread_new(NULL, thread_function, (void*)test_num);
40         }
41         return thread_list;
42 }
43
44 static void unit_test_thread_stop(GThread **thread_list)
45 {
46         int i;
47         for (i = 0; i < N_THREADS; i++) {
48                 g_thread_join(thread_list[i]);
49         }
50
51         g_free(thread_list);
52 }
53
54 static void unit_thread_test()
55 {
56         int i;
57         GThread **thread_test[MAX_TEST_CASE];
58
59         printf("START!!!! thread test for system-settings %d tests\n", thread_test_idx);
60
61         thread_sw = 1;
62         for (i = 0; i < thread_test_idx; i++) {
63                 thread_test[i] = unit_test_thread_start(i);
64                 printf("%3d %s:start!\n", i, thread_test_table[i].test_name);
65
66                 unit_test_thread_stop(thread_test[i]);
67                 printf("%3d %s:OK\n", i, thread_test_table[i].test_name);
68         }
69
70         printf("END!!!! thread test for system-settings\n");
71 }
72
73 static gpointer loop_func(gpointer data)
74 {
75         g_main_loop_run(main_loop);
76         return NULL;
77 }
78
79 static void start_main_loop_thread()
80 {
81         my_thread = g_thread_new(NULL, loop_func, NULL);
82 }
83
84 static void stop_main_loop_thread()
85 {
86         g_main_loop_quit(main_loop);
87         g_thread_join(my_thread);
88         g_main_loop_unref(main_loop);
89 }
90
91 int main(int argc, char* argv[])
92 {
93         ecore_evas_init();
94
95         main_loop = g_main_loop_new(NULL, FALSE);
96
97         g_test_init(&argc, &argv, NULL);
98         g_test_set_nonfatal_assertions();
99
100         start_main_loop_thread();
101
102         unittest_api();
103
104         g_test_run();
105         unit_thread_test();
106
107         stop_main_loop_thread();
108         return 0;
109 }
110
111 void add_test_func(char * test_name,  GTestFunc func_pointer)
112 {
113         g_test_add_func(test_name, func_pointer);
114
115         thread_test_table[thread_test_idx].test_name = test_name;
116         thread_test_table[thread_test_idx].t_func = func_pointer;
117
118         thread_test_idx++;
119 }
120