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 
25 #include <CUnit/CUnit.h>
26 #include <CUnit/Automated.h>
27 #include <CUnit/Basic.h>
28 
29 int ags_file_test_init_suite();
30 int ags_file_test_clean_suite();
31 
32 void ags_file_test_str2md5();
33 void ags_file_test_add_id_ref();
34 void ags_file_test_find_id_ref_by_node();
35 void ags_file_test_find_id_ref_by_xpath();
36 void ags_file_test_find_id_ref_by_reference();
37 void ags_file_test_add_lookup();
38 void ags_file_test_add_launch();
39 void ags_file_test_open();
40 void ags_file_test_open_from_data();
41 void ags_file_test_rw_open();
42 void ags_file_test_open_filename();
43 void ags_file_test_close();
44 void ags_file_test_write();
45 void ags_file_test_write_concurrent();
46 void ags_file_test_write_resolve();
47 void ags_file_test_read();
48 void ags_file_test_read_resolve();
49 void ags_file_test_read_start();
50 void ags_file_test_read_config();
51 void ags_file_test_write_config();
52 void ags_file_test_read_application_context();
53 void ags_file_test_write_application_context();
54 
55 void ags_file_test_stub_open(AgsFile *file,
56 			     GError **error);
57 void ags_file_test_stub_open_from_data(AgsFile *file,
58 				       gchar *data, guint length,
59 				       GError **error);
60 void ags_file_test_stub_rw_open(AgsFile *file,
61 				gboolean create,
62 				GError **error);
63 void ags_file_test_stub_write(AgsFile *file);
64 void ags_file_test_stub_write_concurrent(AgsFile *file);
65 void ags_file_test_stub_write_resolve(AgsFile *file);
66 void ags_file_test_stub_read(AgsFile *file);
67 void ags_file_test_stub_read_resolve(AgsFile *file);
68 void ags_file_test_stub_read_start(AgsFile *file);
69 
70 #define AGS_FILE_TEST_STR2MD5_CONTENT "GNU's not linux"
71 
72 gboolean stub_open = FALSE;
73 gboolean stub_open_from_data = FALSE;
74 gboolean stub_rw_open = FALSE;
75 gboolean stub_write = FALSE;
76 gboolean stub_write_concurrent = FALSE;
77 gboolean stub_write_resolve = FALSE;
78 gboolean stub_read = FALSE;
79 gboolean stub_read_resolve = FALSE;
80 gboolean stub_read_start = FALSE;
81 
82 /* The suite initialization time.
83  * Opens the temporary file used by the tests.
84  * Returns zero on success, non-zero otherwise.
85  */
86 int
ags_file_test_init_suite()87 ags_file_test_init_suite()
88 {
89   return(0);
90 }
91 
92 /* The suite cleanup time.
93  * Closes the temporary file used by the tests.
94  * Returns zero on success, non-zero otherwise.
95  */
96 int
ags_file_test_clean_suite()97 ags_file_test_clean_suite()
98 {
99   return(0);
100 }
101 
102 void
ags_file_test_str2md5()103 ags_file_test_str2md5()
104 {
105   gchar *md5_checksum;
106 
107   md5_checksum = ags_file_str2md5(AGS_FILE_TEST_STR2MD5_CONTENT,
108 				  strlen(AGS_FILE_TEST_STR2MD5_CONTENT));
109 
110   CU_ASSERT(strlen(md5_checksum) == AGS_FILE_CHECKSUM_LENGTH);
111 }
112 
113 void
ags_file_test_add_id_ref()114 ags_file_test_add_id_ref()
115 {
116   AgsFile *file;
117   AgsFileIdRef *file_id_ref[3];
118 
119   file = g_object_new(AGS_TYPE_FILE,
120 		      NULL);
121 
122   /* add some id refs */
123   file_id_ref[0] = g_object_new(AGS_TYPE_FILE_ID_REF,
124 				NULL);
125   ags_file_add_id_ref(file,
126 		      file_id_ref[0]);
127 
128   file_id_ref[1] = g_object_new(AGS_TYPE_FILE_ID_REF,
129 				NULL);
130   ags_file_add_id_ref(file,
131 		      file_id_ref[1]);
132 
133   file_id_ref[2] = g_object_new(AGS_TYPE_FILE_ID_REF,
134 				NULL);
135   ags_file_add_id_ref(file,
136 		      file_id_ref[2]);
137 
138   /* assert to be present */
139   CU_ASSERT(g_list_find(file->id_refs,
140 			file_id_ref[0]) != NULL);
141 
142   CU_ASSERT(g_list_find(file->id_refs,
143 			file_id_ref[1]) != NULL);
144 
145   CU_ASSERT(g_list_find(file->id_refs,
146 			file_id_ref[2]) != NULL);
147 }
148 
149 void
ags_file_test_find_id_ref_by_node()150 ags_file_test_find_id_ref_by_node()
151 {
152   AgsFile *file;
153   AgsFileIdRef *file_id_ref[3], *current;
154 
155   xmlNode *node[3];
156 
157   file = g_object_new(AGS_TYPE_FILE,
158 		      NULL);
159 
160   /* add some id refs */
161   node[0] = xmlNewNode(NULL,
162 		       "ags-file-test");
163 
164   file_id_ref[0] = g_object_new(AGS_TYPE_FILE_ID_REF,
165 				"node", node[0],
166 				NULL);
167   ags_file_add_id_ref(file,
168 		      file_id_ref[0]);
169 
170   node[1] = xmlNewNode(NULL,
171 		       "ags-file-test");
172 
173   file_id_ref[1] = g_object_new(AGS_TYPE_FILE_ID_REF,
174 				"node", node[1],
175 				NULL);
176   ags_file_add_id_ref(file,
177 		      file_id_ref[1]);
178 
179   node[2] = xmlNewNode(NULL,
180 		       "ags-file-test");
181 
182   file_id_ref[2] = g_object_new(AGS_TYPE_FILE_ID_REF,
183 				"node", node[2],
184 				NULL);
185   ags_file_add_id_ref(file,
186 		      file_id_ref[2]);
187 
188  /* assert find by node */
189   CU_ASSERT((current = ags_file_find_id_ref_by_node(file,
190 						    node[0])) != NULL &&
191 	    current == file_id_ref[0]);
192 
193   CU_ASSERT((current = ags_file_find_id_ref_by_node(file,
194 						    node[1])) != NULL &&
195 	    current == file_id_ref[1]);
196 
197   CU_ASSERT((current = ags_file_find_id_ref_by_node(file,
198 						    node[2])) != NULL &&
199 	    current == file_id_ref[2]);
200 }
201 
202 void
ags_file_test_find_id_ref_by_xpath()203 ags_file_test_find_id_ref_by_xpath()
204 {
205   //TODO:JK: implement me
206 }
207 
208 void
ags_file_test_find_id_ref_by_reference()209 ags_file_test_find_id_ref_by_reference()
210 {
211   AgsFile *file;
212   AgsFileIdRef *file_id_ref[3], *current;
213 
214   AgsTurtle *turtle[3];
215 
216   file = g_object_new(AGS_TYPE_FILE,
217 		      NULL);
218 
219   /* add some id refs */
220   turtle[0] = g_object_new(AGS_TYPE_TURTLE,
221 			   NULL);
222 
223   file_id_ref[0] = g_object_new(AGS_TYPE_FILE_ID_REF,
224 				"reference", turtle[0],
225 				NULL);
226   ags_file_add_id_ref(file,
227 		      file_id_ref[0]);
228 
229   turtle[1] = g_object_new(AGS_TYPE_TURTLE,
230 			   NULL);
231 
232   file_id_ref[1] = g_object_new(AGS_TYPE_FILE_ID_REF,
233 				"reference", turtle[1],
234 				NULL);
235   ags_file_add_id_ref(file,
236 		      file_id_ref[1]);
237 
238   turtle[2] = g_object_new(AGS_TYPE_TURTLE,
239 			   NULL);
240 
241   file_id_ref[2] = g_object_new(AGS_TYPE_FILE_ID_REF,
242 				"reference", turtle[2],
243 				NULL);
244   ags_file_add_id_ref(file,
245 		      file_id_ref[2]);
246 
247  /* assert find by reference */
248   CU_ASSERT((current = ags_file_find_id_ref_by_reference(file,
249 							 turtle[0])) != NULL &&
250 	    current == file_id_ref[0]);
251 
252   CU_ASSERT((current = ags_file_find_id_ref_by_reference(file,
253 							 turtle[1])) != NULL &&
254 	    current == file_id_ref[1]);
255 
256   CU_ASSERT((current = ags_file_find_id_ref_by_reference(file,
257 							 turtle[2])) != NULL &&
258 	    current == file_id_ref[2]);
259 }
260 
261 void
ags_file_test_add_lookup()262 ags_file_test_add_lookup()
263 {
264   AgsFile *file;
265   AgsFileIdRef *file_lookup[3];
266 
267   file = g_object_new(AGS_TYPE_FILE,
268 		      NULL);
269 
270   /* add some id refs */
271   file_lookup[0] = g_object_new(AGS_TYPE_FILE_LOOKUP,
272 				NULL);
273   ags_file_add_lookup(file,
274 		      file_lookup[0]);
275 
276   file_lookup[1] = g_object_new(AGS_TYPE_FILE_LOOKUP,
277 				NULL);
278   ags_file_add_lookup(file,
279 		      file_lookup[1]);
280 
281   file_lookup[2] = g_object_new(AGS_TYPE_FILE_LOOKUP,
282 				NULL);
283   ags_file_add_lookup(file,
284 		      file_lookup[2]);
285 
286   /* assert to be present */
287   CU_ASSERT(g_list_find(file->lookup,
288 			file_lookup[0]) != NULL);
289 
290   CU_ASSERT(g_list_find(file->lookup,
291 			file_lookup[1]) != NULL);
292 
293   CU_ASSERT(g_list_find(file->lookup,
294 			file_lookup[2]) != NULL);
295 }
296 
297 void
ags_file_test_add_launch()298 ags_file_test_add_launch()
299 {
300   AgsFile *file;
301   AgsFileIdRef *file_launch[3];
302 
303   file = g_object_new(AGS_TYPE_FILE,
304 		      NULL);
305 
306   /* add some id refs */
307   file_launch[0] = g_object_new(AGS_TYPE_FILE_LAUNCH,
308 				NULL);
309   ags_file_add_launch(file,
310 		      file_launch[0]);
311 
312   file_launch[1] = g_object_new(AGS_TYPE_FILE_LAUNCH,
313 				NULL);
314   ags_file_add_launch(file,
315 		      file_launch[1]);
316 
317   file_launch[2] = g_object_new(AGS_TYPE_FILE_LAUNCH,
318 				NULL);
319   ags_file_add_launch(file,
320 		      file_launch[2]);
321 
322   /* assert to be present */
323   CU_ASSERT(g_list_find(file->launch,
324 			file_launch[0]) != NULL);
325 
326   CU_ASSERT(g_list_find(file->launch,
327 			file_launch[1]) != NULL);
328 
329   CU_ASSERT(g_list_find(file->launch,
330 			file_launch[2]) != NULL);
331 }
332 
333 void
ags_file_test_open()334 ags_file_test_open()
335 {
336   AgsFile *file;
337 
338   gpointer ptr;
339 
340   GError *error;
341 
342   file = g_object_new(AGS_TYPE_FILE,
343 		      NULL);
344 
345   /* stub open */
346   ptr = AGS_FILE_GET_CLASS(file)->open;
347 
348   AGS_FILE_GET_CLASS(file)->open = ags_file_test_stub_open;
349 
350   /* assert open */
351   error = NULL;
352   ags_file_open(file,
353 		&error);
354 
355   CU_ASSERT(stub_open == TRUE);
356 
357   /* reset */
358   AGS_FILE_GET_CLASS(file)->open = ptr;
359 }
360 
361 void
ags_file_test_open_from_data()362 ags_file_test_open_from_data()
363 {
364   AgsFile *file;
365 
366   gpointer ptr;
367 
368   GError *error;
369 
370   file = g_object_new(AGS_TYPE_FILE,
371 		      NULL);
372 
373   /* stub open from data */
374   ptr = AGS_FILE_GET_CLASS(file)->open_from_data;
375 
376   AGS_FILE_GET_CLASS(file)->open_from_data = ags_file_test_stub_open_from_data;
377 
378   /* assert open from data */
379   error = NULL;
380   ags_file_open_from_data(file,
381 			  NULL, 0,
382 			  &error);
383 
384   CU_ASSERT(stub_open_from_data == TRUE);
385 
386   /* reset */
387   AGS_FILE_GET_CLASS(file)->open_from_data = ptr;
388 }
389 
390 void
ags_file_test_rw_open()391 ags_file_test_rw_open()
392 {
393   AgsFile *file;
394 
395   gpointer ptr;
396 
397   GError *error;
398 
399   file = g_object_new(AGS_TYPE_FILE,
400 		      NULL);
401 
402   /* stub read/write open */
403   ptr = AGS_FILE_GET_CLASS(file)->rw_open;
404 
405   AGS_FILE_GET_CLASS(file)->rw_open = ags_file_test_stub_rw_open;
406 
407   /* assert read/write open */
408   error = NULL;
409   ags_file_rw_open(file,
410 		   FALSE,
411 		   &error);
412 
413   CU_ASSERT(stub_rw_open == TRUE);
414 
415   /* reset */
416   AGS_FILE_GET_CLASS(file)->rw_open = ptr;
417 }
418 
419 void
ags_file_test_open_filename()420 ags_file_test_open_filename()
421 {
422   //TODO:JK: implement me
423 }
424 
425 void
ags_file_test_close()426 ags_file_test_close()
427 {
428   //TODO:JK: implement me
429 }
430 
431 void
ags_file_test_write()432 ags_file_test_write()
433 {
434   AgsFile *file;
435 
436   gpointer ptr;
437 
438   file = g_object_new(AGS_TYPE_FILE,
439 		      NULL);
440 
441   /* stub write */
442   ptr = AGS_FILE_GET_CLASS(file)->write;
443 
444   AGS_FILE_GET_CLASS(file)->write = ags_file_test_stub_write;
445 
446   /* assert write */
447   ags_file_write(file);
448 
449   CU_ASSERT(stub_write == TRUE);
450 
451   /* reset */
452   AGS_FILE_GET_CLASS(file)->write = ptr;
453 }
454 
455 void
ags_file_test_write_concurrent()456 ags_file_test_write_concurrent()
457 {
458   AgsFile *file;
459 
460   gpointer ptr;
461 
462   file = g_object_new(AGS_TYPE_FILE,
463 		      NULL);
464 
465   /* stub write concurrent */
466   ptr = AGS_FILE_GET_CLASS(file)->write_concurrent;
467 
468   AGS_FILE_GET_CLASS(file)->write_concurrent = ags_file_test_stub_write_concurrent;
469 
470   /* assert write concurrent */
471   ags_file_write_concurrent(file);
472 
473   CU_ASSERT(stub_write_concurrent == TRUE);
474 
475   /* reset */
476   AGS_FILE_GET_CLASS(file)->write_concurrent = ptr;
477 }
478 
479 void
ags_file_test_write_resolve()480 ags_file_test_write_resolve()
481 {
482   AgsFile *file;
483 
484   gpointer ptr;
485 
486   file = g_object_new(AGS_TYPE_FILE,
487 		      NULL);
488 
489   /* stub write resolve */
490   ptr = AGS_FILE_GET_CLASS(file)->write_resolve;
491 
492   AGS_FILE_GET_CLASS(file)->write_resolve = ags_file_test_stub_write_resolve;
493 
494   /* assert write resolve */
495   ags_file_write_resolve(file);
496 
497   CU_ASSERT(stub_write_resolve == TRUE);
498 
499   /* reset */
500   AGS_FILE_GET_CLASS(file)->write_resolve = ptr;
501 }
502 
503 void
ags_file_test_read()504 ags_file_test_read()
505 {
506   AgsFile *file;
507 
508   gpointer ptr;
509 
510   file = g_object_new(AGS_TYPE_FILE,
511 		      NULL);
512 
513   /* stub read */
514   ptr = AGS_FILE_GET_CLASS(file)->read;
515 
516   AGS_FILE_GET_CLASS(file)->read = ags_file_test_stub_read;
517 
518   /* assert read */
519   ags_file_read(file);
520 
521   CU_ASSERT(stub_read == TRUE);
522 
523   /* reset */
524   AGS_FILE_GET_CLASS(file)->read = ptr;
525 }
526 
527 void
ags_file_test_read_resolve()528 ags_file_test_read_resolve()
529 {
530   AgsFile *file;
531 
532   gpointer ptr;
533 
534   file = g_object_new(AGS_TYPE_FILE,
535 		      NULL);
536 
537   /* stub read resolve */
538   ptr = AGS_FILE_GET_CLASS(file)->read_resolve;
539 
540   AGS_FILE_GET_CLASS(file)->read_resolve = ags_file_test_stub_read_resolve;
541 
542   /* assert read resolve */
543   ags_file_read_resolve(file);
544 
545   CU_ASSERT(stub_read_resolve == TRUE);
546 
547   /* reset */
548   AGS_FILE_GET_CLASS(file)->read_resolve = ptr;
549 }
550 
551 void
ags_file_test_read_start()552 ags_file_test_read_start()
553 {
554   AgsFile *file;
555 
556   gpointer ptr;
557 
558   file = g_object_new(AGS_TYPE_FILE,
559 		      NULL);
560 
561   /* stub read start */
562   ptr = AGS_FILE_GET_CLASS(file)->read_start;
563 
564   AGS_FILE_GET_CLASS(file)->read_start = ags_file_test_stub_read_start;
565 
566   /* assert read start */
567   ags_file_read_start(file);
568 
569   CU_ASSERT(stub_read_start == TRUE);
570 
571   /* reset */
572   AGS_FILE_GET_CLASS(file)->read_start = ptr;
573 }
574 
575 void
ags_file_test_read_config()576 ags_file_test_read_config()
577 {
578   //TODO:JK: implement me
579 }
580 
581 void
ags_file_test_write_config()582 ags_file_test_write_config()
583 {
584   //TODO:JK: implement me
585 }
586 
587 void
ags_file_test_read_application_context()588 ags_file_test_read_application_context()
589 {
590   //TODO:JK: implement me
591 }
592 
593 void
ags_file_test_write_application_context()594 ags_file_test_write_application_context()
595 {
596   //TODO:JK: implement me
597 }
598 
599 void
ags_file_test_stub_open(AgsFile * file,GError ** error)600 ags_file_test_stub_open(AgsFile *file,
601 			GError **error)
602 {
603   stub_open = TRUE;
604 }
605 
606 void
ags_file_test_stub_open_from_data(AgsFile * file,gchar * data,guint length,GError ** error)607 ags_file_test_stub_open_from_data(AgsFile *file,
608 				  gchar *data, guint length,
609 				  GError **error)
610 {
611   stub_open_from_data = TRUE;
612 }
613 
614 void
ags_file_test_stub_rw_open(AgsFile * file,gboolean create,GError ** error)615 ags_file_test_stub_rw_open(AgsFile *file,
616 			   gboolean create,
617 			   GError **error)
618 {
619   stub_rw_open = TRUE;
620 }
621 
622 void
ags_file_test_stub_write(AgsFile * file)623 ags_file_test_stub_write(AgsFile *file)
624 {
625   stub_write = TRUE;
626 }
627 
628 void
ags_file_test_stub_write_concurrent(AgsFile * file)629 ags_file_test_stub_write_concurrent(AgsFile *file)
630 {
631   stub_write_concurrent = TRUE;
632 }
633 
634 void
ags_file_test_stub_write_resolve(AgsFile * file)635 ags_file_test_stub_write_resolve(AgsFile *file)
636 {
637   stub_write_resolve = TRUE;
638 }
639 
640 void
ags_file_test_stub_read(AgsFile * file)641 ags_file_test_stub_read(AgsFile *file)
642 {
643   stub_read = TRUE;
644 }
645 
646 void
ags_file_test_stub_read_resolve(AgsFile * file)647 ags_file_test_stub_read_resolve(AgsFile *file)
648 {
649   stub_read_resolve = TRUE;
650 }
651 
652 void
ags_file_test_stub_read_start(AgsFile * file)653 ags_file_test_stub_read_start(AgsFile *file)
654 {
655   stub_read_start = TRUE;
656 }
657 
658 int
main(int argc,char ** argv)659 main(int argc, char **argv)
660 {
661   CU_pSuite pSuite = NULL;
662 
663   /* initialize the CUnit test registry */
664   if(CUE_SUCCESS != CU_initialize_registry()){
665     return CU_get_error();
666   }
667 
668   /* add a suite to the registry */
669   pSuite = CU_add_suite("AgsFileTest\0", ags_file_test_init_suite, ags_file_test_clean_suite);
670 
671   if(pSuite == NULL){
672     CU_cleanup_registry();
673 
674     return CU_get_error();
675   }
676 
677   /* add the tests to the suite */
678   if((CU_add_test(pSuite, "test of AgsFile string to md5 sum\0", ags_file_test_str2md5) == NULL) ||
679      (CU_add_test(pSuite, "test of AgsFile add id ref\0", ags_file_test_add_id_ref) == NULL) ||
680      (CU_add_test(pSuite, "test of AgsFile find id ref by node\0", ags_file_test_find_id_ref_by_node) == NULL) ||
681      (CU_add_test(pSuite, "test of AgsFile find id ref by xpath\0", ags_file_test_find_id_ref_by_xpath) == NULL) ||
682      (CU_add_test(pSuite, "test of AgsFile find id ref by reference\0", ags_file_test_find_id_ref_by_reference) == NULL) ||
683      (CU_add_test(pSuite, "test of AgsFile add lookup\0", ags_file_test_add_lookup) == NULL) ||
684      (CU_add_test(pSuite, "test of AgsFile add launch\0", ags_file_test_add_launch) == NULL) ||
685      (CU_add_test(pSuite, "test of AgsFile open\0", ags_file_test_open) == NULL) ||
686      (CU_add_test(pSuite, "test of AgsFile open from data\0", ags_file_test_open_from_data) == NULL) ||
687      (CU_add_test(pSuite, "test of AgsFile rw open\0", ags_file_test_rw_open) == NULL) ||
688      (CU_add_test(pSuite, "test of AgsFile open filename\0", ags_file_test_open_filename) == NULL) ||
689      (CU_add_test(pSuite, "test of AgsFile close\0", ags_file_test_close) == NULL) ||
690      (CU_add_test(pSuite, "test of AgsFile write\0", ags_file_test_write) == NULL) ||
691      (CU_add_test(pSuite, "test of AgsFile write concurrent\0", ags_file_test_write_concurrent) == NULL) ||
692      (CU_add_test(pSuite, "test of AgsFile write resolve\0", ags_file_test_write_resolve) == NULL) ||
693      (CU_add_test(pSuite, "test of AgsFile read\0", ags_file_test_read) == NULL) ||
694      (CU_add_test(pSuite, "test of AgsFile read resolve\0", ags_file_test_read_resolve) == NULL) ||
695      (CU_add_test(pSuite, "test of AgsFile read start\0", ags_file_test_read_start) == NULL) ||
696      (CU_add_test(pSuite, "test of AgsFile read config\0", ags_file_test_read_config) == NULL) ||
697      (CU_add_test(pSuite, "test of AgsFile write config\0", ags_file_test_write_config) == NULL) ||
698      (CU_add_test(pSuite, "test of AgsFile read application context\0", ags_file_test_read_application_context) == NULL) ||
699      (CU_add_test(pSuite, "test of AgsFile write application context\0", ags_file_test_write_application_context) == NULL)){
700     CU_cleanup_registry();
701 
702     return CU_get_error();
703   }
704 
705   /* Run all tests using the CUnit Basic interface */
706   CU_basic_set_mode(CU_BRM_VERBOSE);
707   CU_basic_run_tests();
708 
709   CU_cleanup_registry();
710 
711   return(CU_get_error());
712 }
713