1 /* GSequencer - Advanced GTK Sequencer
2 * Copyright (C) 2017 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 <ags/libags.h>
24 #include <ags/libags-audio.h>
25
26 #include <CUnit/CUnit.h>
27 #include <CUnit/Automated.h>
28 #include <CUnit/Basic.h>
29
30 int ags_preset_test_init_suite();
31 int ags_preset_test_clean_suite();
32
33 void ags_preset_test_find_scope();
34 void ags_preset_test_find_name();
35 void ags_preset_test_add_parameter();
36 void ags_preset_test_remove_parameter();
37 void ags_preset_test_get_parameter();
38
39 #define AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X "attack-x"
40 #define AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X_VALUE (128)
41
42 #define AGS_PRESET_TEST_REMOVE_PARAMETER_ATTACK_X "attack-x"
43 #define AGS_PRESET_TEST_REMOVE_PARAMETER_ATTACK_X_VALUE (128)
44 #define AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X "decay-x"
45 #define AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X_VALUE (256)
46
47 #define AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X "attack-x"
48 #define AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X_VALUE (128)
49
50 /* The suite initialization function.
51 * Opens the temporary file used by the tests.
52 * Returns zero on success, non-zero otherwise.
53 */
54 int
ags_preset_test_init_suite()55 ags_preset_test_init_suite()
56 {
57 return(0);
58 }
59
60 /* The suite cleanup function.
61 * Closes the temporary file used by the tests.
62 * Returns zero on success, non-zero otherwise.
63 */
64 int
ags_preset_test_clean_suite()65 ags_preset_test_clean_suite()
66 {
67 return(0);
68 }
69
70 void
ags_preset_test_find_scope()71 ags_preset_test_find_scope()
72 {
73 AgsPreset *preset[3];
74
75 GList *list, *current;
76
77 list = NULL;
78
79 /* preset #0 */
80 preset[0] = g_object_new(AGS_TYPE_PRESET,
81 "scope", "ags-preset-test-scope-0",
82 NULL);
83 list = g_list_prepend(list,
84 preset[0]);
85
86 /* preset #1 */
87 preset[1] = g_object_new(AGS_TYPE_PRESET,
88 "scope", "ags-preset-test-scope-1",
89 NULL);
90 list = g_list_prepend(list,
91 preset[1]);
92
93 /* preset #2 */
94 preset[2] = g_object_new(AGS_TYPE_PRESET,
95 "scope", "ags-preset-test-scope-2",
96 NULL);
97 list = g_list_prepend(list,
98 preset[2]);
99
100 /* assert */
101 CU_ASSERT((current = ags_preset_find_scope(list,
102 "ags-preset-test-scope-0")) != NULL &&
103 current->data == preset[0]);
104
105 CU_ASSERT((current = ags_preset_find_scope(list,
106 "ags-preset-test-scope-1")) != NULL &&
107 current->data == preset[1]);
108
109 CU_ASSERT((current = ags_preset_find_scope(list,
110 "ags-preset-test-scope-2")) != NULL &&
111 current->data == preset[2]);
112 }
113
114 void
ags_preset_test_find_name()115 ags_preset_test_find_name()
116 {
117 AgsPreset *preset[3];
118
119 GList *list, *current;
120
121 list = NULL;
122
123 /* preset #0 */
124 preset[0] = g_object_new(AGS_TYPE_PRESET,
125 "preset-name", "ags-preset-test-name-0",
126 NULL);
127 list = g_list_prepend(list,
128 preset[0]);
129
130 /* preset #1 */
131 preset[1] = g_object_new(AGS_TYPE_PRESET,
132 "preset-name", "ags-preset-test-name-1",
133 NULL);
134 list = g_list_prepend(list,
135 preset[1]);
136
137 /* preset #2 */
138 preset[2] = g_object_new(AGS_TYPE_PRESET,
139 "preset-name", "ags-preset-test-name-2",
140 NULL);
141 list = g_list_prepend(list,
142 preset[2]);
143
144 /* assert */
145 CU_ASSERT((current = ags_preset_find_name(list,
146 "ags-preset-test-name-0")) != NULL &&
147 current->data == preset[0]);
148
149 CU_ASSERT((current = ags_preset_find_name(list,
150 "ags-preset-test-name-1")) != NULL &&
151 current->data == preset[1]);
152
153 CU_ASSERT((current = ags_preset_find_name(list,
154 "ags-preset-test-name-2")) != NULL &&
155 current->data == preset[2]);
156 }
157
158 void
ags_preset_test_add_parameter()159 ags_preset_test_add_parameter()
160 {
161 AgsPreset *preset;
162
163 GValue value = {0,};
164
165 preset = g_object_new(AGS_TYPE_PRESET,
166 NULL);
167
168 g_value_init(&value,
169 G_TYPE_UINT);
170 g_value_set_uint(&value,
171 AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X_VALUE);
172
173 ags_preset_add_parameter(preset,
174 AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X, &value);
175
176 CU_ASSERT(preset->n_params == 1 &&
177 !g_strcmp0(preset->parameter_name[0],
178 AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X) &&
179 g_value_get_uint(&(preset->value[0])) == AGS_PRESET_TEST_ADD_PARAMETER_ATTACK_X_VALUE);
180 }
181
182 void
ags_preset_test_remove_parameter()183 ags_preset_test_remove_parameter()
184 {
185 AgsPreset *preset;
186
187 GValue value = {0,};
188
189 preset = g_object_new(AGS_TYPE_PRESET,
190 NULL);
191
192 g_value_init(&value,
193 G_TYPE_UINT);
194 g_value_set_uint(&value,
195 AGS_PRESET_TEST_REMOVE_PARAMETER_ATTACK_X_VALUE);
196 ags_preset_add_parameter(preset,
197 AGS_PRESET_TEST_REMOVE_PARAMETER_ATTACK_X, &value);
198
199 g_value_reset(&value);
200 g_value_set_uint(&value,
201 AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X_VALUE);
202 ags_preset_add_parameter(preset,
203 AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X, &value);
204
205 /* remove and assert */
206 ags_preset_remove_parameter(preset,
207 0);
208
209 CU_ASSERT(preset->n_params == 1 &&
210 !g_strcmp0(preset->parameter_name[0],
211 AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X) &&
212 g_value_get_uint(&(preset->value[0])) == AGS_PRESET_TEST_REMOVE_PARAMETER_DECAY_X_VALUE);
213
214 ags_preset_remove_parameter(preset,
215 0);
216
217 CU_ASSERT(preset->n_params == 0);
218 }
219
220 void
ags_preset_test_get_parameter()221 ags_preset_test_get_parameter()
222 {
223 AgsPreset *preset;
224
225 GValue value = {0,};
226 GValue ret_value = {0,};
227
228 GError *error;
229
230 preset = g_object_new(AGS_TYPE_PRESET,
231 NULL);
232
233 g_value_init(&value,
234 G_TYPE_UINT);
235 g_value_set_uint(&value,
236 AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X_VALUE);
237
238 ags_preset_add_parameter(preset,
239 AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X, &value);
240
241 /* get and assert */
242 g_value_init(&ret_value,
243 G_TYPE_UINT);
244
245 error = NULL;
246 ags_preset_get_parameter(preset,
247 AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X, &ret_value,
248 &error);
249
250 CU_ASSERT(error == NULL &&
251 g_value_get_uint(&ret_value) == AGS_PRESET_TEST_GET_PARAMETER_ATTACK_X_VALUE);
252 }
253
254 int
main(int argc,char ** argv)255 main(int argc, char **argv)
256 {
257 CU_pSuite pSuite = NULL;
258
259 /* initialize the CUnit test registry */
260 if(CUE_SUCCESS != CU_initialize_registry()){
261 return CU_get_error();
262 }
263
264 /* add a suite to the registry */
265 pSuite = CU_add_suite("AgsPresetTest", ags_preset_test_init_suite, ags_preset_test_clean_suite);
266
267 if(pSuite == NULL){
268 CU_cleanup_registry();
269
270 return CU_get_error();
271 }
272
273 /* add the tests to the suite */
274 if((CU_add_test(pSuite, "test of AgsPreset find scope", ags_preset_test_find_scope) == NULL) ||
275 (CU_add_test(pSuite, "test of AgsPreset find name", ags_preset_test_find_name) == NULL) ||
276 (CU_add_test(pSuite, "test of AgsPreset add parameter", ags_preset_test_add_parameter) == NULL) ||
277 (CU_add_test(pSuite, "test of AgsPreset remove parameter", ags_preset_test_remove_parameter) == NULL) ||
278 (CU_add_test(pSuite, "test of AgsPreset get parameter", ags_preset_test_get_parameter) == NULL)){
279 CU_cleanup_registry();
280
281 return CU_get_error();
282 }
283
284 /* Run all tests using the CUnit Basic interface */
285 CU_basic_set_mode(CU_BRM_VERBOSE);
286 CU_basic_run_tests();
287
288 CU_cleanup_registry();
289
290 return(CU_get_error());
291 }
292
293