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_task_launcher_test_init_suite();
30 int ags_task_launcher_test_clean_suite();
31 
32 void ags_task_launcher_test_test_flags();
33 void ags_task_launcher_test_set_flags();
34 void ags_task_launcher_test_unset_flags();
35 void ags_task_launcher_test_add_task();
36 void ags_task_launcher_test_add_task_all();
37 void ags_task_launcher_test_add_cyclic_task();
38 void ags_task_launcher_test_remove_cyclic_task();
39 
40 /* The suite initialization function.
41  * Opens the temporary file used by the tests.
42  * Returns zero on success, non-zero otherwise.
43  */
44 int
ags_task_launcher_test_init_suite()45 ags_task_launcher_test_init_suite()
46 {
47   return(0);
48 }
49 
50 /* The suite cleanup function.
51  * Closes the temporary file used by the tests.
52  * Returns zero on success, non-zero otherwise.
53  */
54 int
ags_task_launcher_test_clean_suite()55 ags_task_launcher_test_clean_suite()
56 {
57   return(0);
58 }
59 
60 void
ags_task_launcher_test_test_flags()61 ags_task_launcher_test_test_flags()
62 {
63   AgsTaskLauncher *task_launcher;
64 
65   task_launcher = ags_task_launcher_new();
66 
67   task_launcher->flags = 0;
68 
69   CU_ASSERT(ags_task_launcher_test_flags(task_launcher, AGS_TASK_LAUNCHER_CONNECTED) == FALSE);
70 
71   task_launcher->flags = AGS_TASK_LAUNCHER_CONNECTED;
72 
73   CU_ASSERT(ags_task_launcher_test_flags(task_launcher, AGS_TASK_LAUNCHER_CONNECTED) == TRUE);
74 }
75 
76 void
ags_task_launcher_test_set_flags()77 ags_task_launcher_test_set_flags()
78 {
79   AgsTaskLauncher *task_launcher;
80 
81   task_launcher = ags_task_launcher_new();
82 
83   task_launcher->flags = 0;
84 
85   ags_task_launcher_set_flags(task_launcher, AGS_TASK_LAUNCHER_CONNECTED);
86 
87   CU_ASSERT((AGS_TASK_LAUNCHER_CONNECTED & (task_launcher->flags)) != 0);
88 }
89 
90 void
ags_task_launcher_test_unset_flags()91 ags_task_launcher_test_unset_flags()
92 {
93   AgsTaskLauncher *task_launcher;
94 
95   task_launcher = ags_task_launcher_new();
96 
97   task_launcher->flags = AGS_TASK_LAUNCHER_CONNECTED;
98 
99   ags_task_launcher_unset_flags(task_launcher, AGS_TASK_LAUNCHER_CONNECTED);
100 
101   CU_ASSERT((AGS_TASK_LAUNCHER_CONNECTED & (task_launcher->flags)) == 0);
102 }
103 
104 void
ags_task_launcher_test_add_task()105 ags_task_launcher_test_add_task()
106 {
107   AgsTaskLauncher *task_launcher;
108   AgsTask *task_0, *task_1;
109 
110   task_launcher = ags_task_launcher_new();
111 
112   CU_ASSERT(task_launcher->task == NULL);
113 
114   task_0 = ags_task_new();
115   ags_task_launcher_add_task(task_launcher, task_0);
116 
117   CU_ASSERT(g_list_find(task_launcher->task, task_0) != NULL);
118 
119   task_1 = ags_task_new();
120   ags_task_launcher_add_task(task_launcher, task_1);
121 
122   CU_ASSERT(g_list_find(task_launcher->task, task_1) != NULL);
123 }
124 
125 void
ags_task_launcher_test_add_task_all()126 ags_task_launcher_test_add_task_all()
127 {
128   AgsTaskLauncher *task_launcher;
129   AgsTask *task_0, *task_1;
130 
131   GList *start_task;
132 
133   task_launcher = ags_task_launcher_new();
134 
135   start_task = NULL;
136 
137   task_0 = ags_task_new();
138   start_task = g_list_prepend(start_task,
139 			      task_0);
140 
141   task_1 = ags_task_new();
142   start_task = g_list_prepend(start_task,
143 			      task_1);
144 
145   ags_task_launcher_add_task_all(task_launcher, start_task);
146 
147   CU_ASSERT(g_list_find(task_launcher->task, task_0) != NULL);
148   CU_ASSERT(g_list_find(task_launcher->task, task_1) != NULL);
149 }
150 
151 void
ags_task_launcher_test_add_cyclic_task()152 ags_task_launcher_test_add_cyclic_task()
153 {
154   AgsTaskLauncher *task_launcher;
155   AgsTask *task_0, *task_1;
156 
157   task_launcher = ags_task_launcher_new();
158 
159   CU_ASSERT(task_launcher->cyclic_task == NULL);
160 
161   task_0 = ags_task_new();
162   ags_task_launcher_add_cyclic_task(task_launcher, task_0);
163 
164   CU_ASSERT(g_list_find(task_launcher->cyclic_task, task_0) != NULL);
165 
166   task_1 = ags_task_new();
167   ags_task_launcher_add_cyclic_task(task_launcher, task_1);
168 
169   CU_ASSERT(g_list_find(task_launcher->cyclic_task, task_1) != NULL);
170 }
171 
172 void
ags_task_launcher_test_remove_cyclic_task()173 ags_task_launcher_test_remove_cyclic_task()
174 {
175   AgsTaskLauncher *task_launcher;
176   AgsTask *task_0, *task_1;
177 
178   GList *start_task;
179 
180   task_launcher = ags_task_launcher_new();
181 
182   start_task = NULL;
183 
184   task_0 = ags_task_new();
185   start_task = g_list_prepend(start_task,
186 			      task_0);
187 
188   task_1 = ags_task_new();
189   start_task = g_list_prepend(start_task,
190 			      task_1);
191 
192   task_launcher->cyclic_task = start_task;
193 
194   ags_task_launcher_remove_cyclic_task(task_launcher, task_0);
195 
196   CU_ASSERT(g_list_find(task_launcher->cyclic_task, task_0) == NULL);
197 
198   ags_task_launcher_remove_cyclic_task(task_launcher, task_1);
199 
200   CU_ASSERT(g_list_find(task_launcher->cyclic_task, task_1) == NULL);
201 }
202 
203 int
main(int argc,char ** argv)204 main(int argc, char **argv)
205 {
206   CU_pSuite pSuite = NULL;
207 
208   /* initialize the CUnit test registry */
209   if(CUE_SUCCESS != CU_initialize_registry()){
210     return CU_get_error();
211   }
212 
213   /* add a suite to the registry */
214   pSuite = CU_add_suite("AgsTaskLauncherTest", ags_task_launcher_test_init_suite, ags_task_launcher_test_clean_suite);
215 
216   if(pSuite == NULL){
217     CU_cleanup_registry();
218 
219     return CU_get_error();
220   }
221 
222   /* add the tests to the suite */
223   if((CU_add_test(pSuite, "test of AgsTaskLauncher test flags", ags_task_launcher_test_test_flags) == NULL) ||
224      (CU_add_test(pSuite, "test of AgsTaskLauncher set flags", ags_task_launcher_test_set_flags) == NULL) ||
225      (CU_add_test(pSuite, "test of AgsTaskLauncher unset flags", ags_task_launcher_test_unset_flags) == NULL) ||
226      (CU_add_test(pSuite, "test of AgsTaskLauncher add task", ags_task_launcher_test_add_task) == NULL) ||
227      (CU_add_test(pSuite, "test of AgsTaskLauncher add task all", ags_task_launcher_test_add_task_all) == NULL) ||
228      (CU_add_test(pSuite, "test of AgsTaskLauncher add cyclic task", ags_task_launcher_test_add_cyclic_task) == NULL) ||
229      (CU_add_test(pSuite, "test of AgsTaskLauncher remove cyclic task", ags_task_launcher_test_remove_cyclic_task) == NULL)){
230     CU_cleanup_registry();
231 
232     return CU_get_error();
233   }
234 
235   /* Run all tests using the CUnit Basic interface */
236   CU_basic_set_mode(CU_BRM_VERBOSE);
237   CU_basic_run_tests();
238 
239   CU_cleanup_registry();
240 
241   return(CU_get_error());
242 }
243 
244