PageRenderTime 175ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/capi/check_api.c

http://github.com/sahib/glyr
C | 236 lines | 153 code | 47 blank | 36 comment | 18 complexity | ef11cb7441fc70bb592d8705c9a0165f MD5 | raw file
Possible License(s): LGPL-3.0
  1. /***********************************************************
  2. * This file is part of glyr
  3. * + a command-line tool and library to download various sort of musicrelated metadata.
  4. * + Copyright (C) [2011-2016] [Christopher Pahl]
  5. * + Hosted at: https://github.com/sahib/glyr
  6. *
  7. * glyr is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * glyr is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with glyr. If not, see <http://www.gnu.org/licenses/>.
  19. **************************************************************/
  20. #include "test_common.h"
  21. #include <check.h>
  22. #include <glib.h>
  23. //--------------------
  24. //--------------------
  25. START_TEST (test_glyr_init)
  26. {
  27. GlyrQuery q;
  28. fail_unless (glyr_get (NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
  29. fail_unless (glyr_get (&q,NULL,NULL) == NULL,"glyr_get() should still return NULL, even with uninitalized query.");
  30. glyr_init();
  31. fail_unless (glyr_get (NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
  32. fail_unless (glyr_get (&q,NULL,NULL) == NULL,"should not access bad memory");
  33. glyr_init();
  34. fail_unless (glyr_get (NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
  35. fail_unless (glyr_get (&q,NULL,NULL) == NULL,"should not access bad memory");
  36. glyr_cleanup();
  37. glyr_cleanup();
  38. fail_unless (glyr_get (NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
  39. fail_unless (glyr_get (&q,NULL,NULL) == NULL,"should not access bad memory");
  40. glyr_query_init (&q);
  41. fail_unless (q.is_initalized == 0xDEADBEEF,NULL);
  42. fail_unless (glyr_get (&q,NULL,NULL) == NULL,"should not access bad memory");
  43. glyr_query_destroy (&q);
  44. }
  45. END_TEST
  46. //--------------------
  47. START_TEST (test_glyr_destroy_before_init)
  48. {
  49. glyr_cleanup();
  50. glyr_init();
  51. }
  52. END_TEST
  53. //--------------------
  54. START_TEST (test_glyr_query_init)
  55. {
  56. GlyrQuery q;
  57. /* Crash? */
  58. glyr_query_init (NULL);
  59. glyr_query_init (&q);
  60. fail_unless (q.is_initalized == 0xDEADBEEF,NULL);
  61. GlyrQuery * alloc = malloc (sizeof (GlyrQuery) );
  62. glyr_query_init (alloc);
  63. fail_unless (alloc->is_initalized == 0xDEADBEEF,NULL);
  64. glyr_query_destroy (&q);
  65. }
  66. END_TEST
  67. //--------------------
  68. START_TEST (test_glyr_cache_free)
  69. {
  70. GlyrMemCache * test = glyr_cache_new();
  71. glyr_cache_free (NULL);
  72. glyr_cache_free (test);
  73. }
  74. END_TEST
  75. //--------------------
  76. START_TEST (test_glyr_cache_copy)
  77. {
  78. GlyrMemCache * test = glyr_cache_new();
  79. glyr_cache_set_data (test,g_strdup ("some data"),-1);
  80. test->next = (GlyrMemCache*) 0x1010;
  81. test->prev = (GlyrMemCache*) 0x0101;
  82. GlyrMemCache * copy = glyr_cache_copy (test);
  83. fail_unless (memcmp (copy->data,test->data,test->size) == 0,"Should have the same data");
  84. fail_unless (copy->next == NULL,NULL);
  85. fail_unless (copy->prev == NULL,NULL);
  86. glyr_cache_free (copy);
  87. glyr_cache_free (test);
  88. }
  89. END_TEST
  90. //--------------------
  91. START_TEST (test_glyr_query_destroy)
  92. {
  93. GlyrQuery q;
  94. glyr_query_destroy (NULL);
  95. glyr_query_destroy (&q);
  96. glyr_query_init (&q);
  97. glyr_query_destroy (&q);
  98. }
  99. END_TEST
  100. //--------------------
  101. START_TEST (test_glyr_free_list)
  102. {
  103. glyr_free_list (NULL);
  104. GlyrMemCache * new1 = glyr_cache_new();
  105. GlyrMemCache * new2 = glyr_cache_new();
  106. GlyrMemCache * new3 = glyr_cache_new();
  107. new2->next = new3;
  108. new2->prev = new1;
  109. glyr_free_list (new2);
  110. }
  111. END_TEST
  112. //--------------------
  113. START_TEST (test_glyr_cache_set_data)
  114. {
  115. glyr_cache_set_data (NULL,"lala",10);
  116. GlyrMemCache * c = glyr_cache_new();
  117. c->data = g_strdup ("Hello?");
  118. c->size = strlen ("Hello?");
  119. glyr_cache_update_md5sum (c);
  120. unsigned char old_sum[16] = {0};
  121. memcpy (old_sum,c->md5sum,16);
  122. size_t old_size = c->size;
  123. glyr_cache_set_data (c,g_strdup (" World!"),-1);
  124. fail_if (g_strcmp0 (c->data," World!"),NULL);
  125. fail_if (old_size == c->size,NULL);
  126. fail_if (!memcmp (old_sum,c->md5sum,16),NULL);
  127. glyr_cache_free (c);
  128. }
  129. END_TEST
  130. //--------------------
  131. START_TEST (test_glyr_cache_write)
  132. {
  133. glyr_init();
  134. glyr_cache_write (NULL,"/tmp");
  135. GlyrMemCache * tmp = glyr_cache_new();
  136. glyr_cache_write (tmp,NULL);
  137. glyr_cache_set_data (tmp,g_strdup ("Test data of the cache"),-1);
  138. glyr_cache_write (tmp,"stdout");
  139. glyr_cache_write (tmp,"stderr");
  140. glyr_cache_write (tmp,"null");
  141. glyr_cache_write (tmp,"/tmp/test.txt");
  142. glyr_cache_free (tmp);
  143. glyr_cleanup();
  144. }
  145. END_TEST
  146. //--------------------
  147. //--------------------
  148. //--------------------
  149. START_TEST (test_glyr_download)
  150. {
  151. glyr_init();
  152. atexit (glyr_cleanup);
  153. GlyrMemCache * c = glyr_download ("www.duckduckgo.com",NULL);
  154. fail_unless (c != NULL,"Could not load www.google.de");
  155. glyr_cache_free (c);
  156. }
  157. END_TEST
  158. //--------------------
  159. Suite * create_test_suite (void)
  160. {
  161. Suite *s = suite_create ("Libglyr API");
  162. /* Core test case */
  163. TCase * tc_core = tcase_create ("Init");
  164. tcase_add_test (tc_core, test_glyr_init);
  165. tcase_add_test (tc_core, test_glyr_destroy_before_init);
  166. tcase_add_test (tc_core, test_glyr_query_init);
  167. tcase_add_test (tc_core, test_glyr_query_destroy);
  168. tcase_add_test (tc_core, test_glyr_free_list);
  169. tcase_add_test (tc_core, test_glyr_cache_free);
  170. tcase_add_test (tc_core, test_glyr_cache_copy);
  171. tcase_add_test (tc_core, test_glyr_cache_set_data);
  172. tcase_add_test (tc_core, test_glyr_cache_write);
  173. tcase_add_test (tc_core, test_glyr_download);
  174. suite_add_tcase (s, tc_core);
  175. return s;
  176. }
  177. //--------------------
  178. int main (void)
  179. {
  180. init();
  181. int number_failed;
  182. Suite *s = create_test_suite();
  183. SRunner * sr = srunner_create (s);
  184. srunner_set_log (sr, "check_glyr_api.log");
  185. srunner_run_all (sr, CK_VERBOSE);
  186. number_failed = srunner_ntests_failed (sr);
  187. srunner_free (sr);
  188. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  189. };