1 /* GSequencer - Advanced GTK Sequencer
2 * Copyright (C) 2005-2020 Joël Krähemann
3 *
4 * This file is part of GSequencer.
5 *
6 * GSequencer is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GSequencer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GSequencer. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <glib.h>
21 #include <glib-object.h>
22
23 #include <CUnit/CUnit.h>
24 #include <CUnit/Automated.h>
25 #include <CUnit/Basic.h>
26
27 #include <ags/libags.h>
28
29 int ags_concurrency_provider_test_init_suite();
30 int ags_concurrency_provider_test_clean_suite();
31
32 void ags_concurrency_provider_test_get_main_loop();
33 void ags_concurrency_provider_test_set_main_loop();
34 void ags_concurrency_provider_test_get_task_launcher();
35 void ags_concurrency_provider_test_set_task_launcher();
36 void ags_concurrency_provider_test_get_thread_pool();
37 void ags_concurrency_provider_test_set_thread_pool();
38 void ags_concurrency_provider_test_get_worker();
39 void ags_concurrency_provider_test_set_worker();
40
41 /* The suite initialization function.
42 * Opens the temporary file used by the tests.
43 * Returns zero on success, non-zero otherwise.
44 */
45 int
ags_concurrency_provider_test_init_suite()46 ags_concurrency_provider_test_init_suite()
47 {
48 return(0);
49 }
50
51 /* The suite cleanup function.
52 * Closes the temporary file used by the tests.
53 * Returns zero on success, non-zero otherwise.
54 */
55 int
ags_concurrency_provider_test_clean_suite()56 ags_concurrency_provider_test_clean_suite()
57 {
58 return(0);
59 }
60
61 void
ags_concurrency_provider_test_get_main_loop()62 ags_concurrency_provider_test_get_main_loop()
63 {
64 AgsThreadApplicationContext *thread_application_context;
65
66 thread_application_context = ags_thread_application_context_new();
67
68 CU_ASSERT(ags_concurrency_provider_get_main_loop(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == NULL);
69
70 AGS_APPLICATION_CONTEXT(thread_application_context)->main_loop = ags_generic_main_loop_new();
71
72 CU_ASSERT(ags_concurrency_provider_get_main_loop(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == AGS_APPLICATION_CONTEXT(thread_application_context)->main_loop);
73 }
74
75 void
ags_concurrency_provider_test_set_main_loop()76 ags_concurrency_provider_test_set_main_loop()
77 {
78 AgsThreadApplicationContext *thread_application_context;
79 AgsThread *main_loop;
80
81 thread_application_context = ags_thread_application_context_new();
82
83 main_loop = ags_generic_main_loop_new();
84
85 ags_concurrency_provider_set_main_loop(AGS_CONCURRENCY_PROVIDER(thread_application_context), main_loop);
86
87 CU_ASSERT(AGS_APPLICATION_CONTEXT(thread_application_context)->main_loop == main_loop);
88 }
89
90 void
ags_concurrency_provider_test_get_task_launcher()91 ags_concurrency_provider_test_get_task_launcher()
92 {
93 AgsThreadApplicationContext *thread_application_context;
94
95 thread_application_context = ags_thread_application_context_new();
96
97 CU_ASSERT(ags_concurrency_provider_get_task_launcher(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == NULL);
98
99 AGS_APPLICATION_CONTEXT(thread_application_context)->task_launcher = ags_task_launcher_new();
100
101 CU_ASSERT(ags_concurrency_provider_get_task_launcher(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == AGS_APPLICATION_CONTEXT(thread_application_context)->task_launcher);
102 }
103
104 void
ags_concurrency_provider_test_set_task_launcher()105 ags_concurrency_provider_test_set_task_launcher()
106 {
107 AgsThreadApplicationContext *thread_application_context;
108 AgsTaskLauncher *task_launcher;
109
110 thread_application_context = ags_thread_application_context_new();
111
112 task_launcher = ags_task_launcher_new();
113
114 ags_concurrency_provider_set_task_launcher(AGS_CONCURRENCY_PROVIDER(thread_application_context), task_launcher);
115
116 CU_ASSERT(AGS_APPLICATION_CONTEXT(thread_application_context)->task_launcher == task_launcher);
117 }
118
119 void
ags_concurrency_provider_test_get_thread_pool()120 ags_concurrency_provider_test_get_thread_pool()
121 {
122 AgsThreadApplicationContext *thread_application_context;
123
124 thread_application_context = ags_thread_application_context_new();
125
126 CU_ASSERT(ags_concurrency_provider_get_thread_pool(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == NULL);
127
128 thread_application_context->thread_pool = ags_thread_pool_new(NULL);
129
130 CU_ASSERT(ags_concurrency_provider_get_thread_pool(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == thread_application_context->thread_pool);
131 }
132
133 void
ags_concurrency_provider_test_set_thread_pool()134 ags_concurrency_provider_test_set_thread_pool()
135 {
136 AgsThreadApplicationContext *thread_application_context;
137 AgsThreadPool *thread_pool;
138
139 thread_application_context = ags_thread_application_context_new();
140
141 thread_pool = ags_thread_pool_new(NULL);
142
143 ags_concurrency_provider_set_thread_pool(AGS_CONCURRENCY_PROVIDER(thread_application_context), thread_pool);
144
145 CU_ASSERT(thread_application_context->thread_pool == thread_pool);
146 }
147
148 void
ags_concurrency_provider_test_get_worker()149 ags_concurrency_provider_test_get_worker()
150 {
151 AgsThreadApplicationContext *thread_application_context;
152
153 GList *start_worker, *worker;
154 GList *copy_start_worker, *copy_worker;
155
156 guint i;
157
158 thread_application_context = ags_thread_application_context_new();
159
160 start_worker = NULL;
161
162 start_worker = g_list_prepend(start_worker,
163 ags_worker_thread_new());
164 start_worker = g_list_prepend(start_worker,
165 ags_worker_thread_new());
166 start_worker = g_list_prepend(start_worker,
167 ags_worker_thread_new());
168
169 CU_ASSERT(ags_concurrency_provider_get_worker(AGS_CONCURRENCY_PROVIDER(thread_application_context)) == NULL);
170
171 thread_application_context->worker = start_worker;
172
173 copy_start_worker = ags_concurrency_provider_get_worker(AGS_CONCURRENCY_PROVIDER(thread_application_context));
174
175 worker = start_worker;
176 copy_worker = copy_start_worker;
177
178 for(i = 0; worker != NULL && copy_worker != NULL && worker->data == copy_worker->data; i++){
179 worker = worker->next;
180 copy_worker = copy_worker->next;
181 }
182
183 CU_ASSERT(i == 3);
184 }
185
186 void
ags_concurrency_provider_test_set_worker()187 ags_concurrency_provider_test_set_worker()
188 {
189 AgsThreadApplicationContext *thread_application_context;
190 GList *start_worker;
191
192 thread_application_context = ags_thread_application_context_new();
193
194 start_worker = NULL;
195
196 start_worker = g_list_prepend(start_worker,
197 ags_worker_thread_new());
198 start_worker = g_list_prepend(start_worker,
199 ags_worker_thread_new());
200 start_worker = g_list_prepend(start_worker,
201 ags_worker_thread_new());
202
203 ags_concurrency_provider_set_worker(AGS_CONCURRENCY_PROVIDER(thread_application_context), start_worker);
204
205 CU_ASSERT(thread_application_context->worker == start_worker);
206 }
207
208 int
main(int argc,char ** argv)209 main(int argc, char **argv)
210 {
211 CU_pSuite pSuite = NULL;
212
213 /* initialize the CUnit test registry */
214 if(CUE_SUCCESS != CU_initialize_registry()){
215 return CU_get_error();
216 }
217
218 /* add a suite to the registry */
219 pSuite = CU_add_suite("AgsConcurrencyProviderTest", ags_concurrency_provider_test_init_suite, ags_concurrency_provider_test_clean_suite);
220
221 if(pSuite == NULL){
222 CU_cleanup_registry();
223
224 return CU_get_error();
225 }
226
227 /* add the tests to the suite */
228 if((CU_add_test(pSuite, "test of AgsConcurrencyProvider get main loop", ags_concurrency_provider_test_get_main_loop) == NULL) ||
229 (CU_add_test(pSuite, "test of AgsConcurrencyProvider set main loop", ags_concurrency_provider_test_set_main_loop) == NULL) ||
230 (CU_add_test(pSuite, "test of AgsConcurrencyProvider get task launcher", ags_concurrency_provider_test_get_task_launcher) == NULL) ||
231 (CU_add_test(pSuite, "test of AgsConcurrencyProvider set task launcher", ags_concurrency_provider_test_set_task_launcher) == NULL) ||
232 (CU_add_test(pSuite, "test of AgsConcurrencyProvider get thread pool", ags_concurrency_provider_test_get_thread_pool) == NULL) ||
233 (CU_add_test(pSuite, "test of AgsConcurrencyProvider set thread pool", ags_concurrency_provider_test_set_thread_pool) == NULL) ||
234 (CU_add_test(pSuite, "test of AgsConcurrencyProvider get worker", ags_concurrency_provider_test_get_worker) == NULL) ||
235 (CU_add_test(pSuite, "test of AgsConcurrencyProvider set worker", ags_concurrency_provider_test_set_worker) == NULL)){
236 CU_cleanup_registry();
237
238 return CU_get_error();
239 }
240
241 /* Run all tests using the CUnit Basic interface */
242 CU_basic_set_mode(CU_BRM_VERBOSE);
243 CU_basic_run_tests();
244
245 CU_cleanup_registry();
246
247 return(CU_get_error());
248 }
249
250