PageRenderTime 63ms CodeModel.GetById 20ms app.highlight 17ms RepoModel.GetById 24ms app.codeStats 0ms

/spec/C/check_api.c

http://github.com/sahib/glyr
C | 236 lines | 153 code | 47 blank | 36 comment | 18 complexity | 9f1455d547947c6c49109a893403f368 MD5 | raw file
  1/***********************************************************
  2 * This file is part of glyr
  3 * + a commnandline tool and library to download various sort of musicrelated metadata.
  4 * + Copyright (C) [2011-2012]  [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 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 General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with glyr. If not, see <http://www.gnu.org/licenses/>.
 19 **************************************************************/
 20
 21#include "test_common.h"
 22#include <check.h>
 23#include <glib.h>
 24
 25//--------------------
 26//--------------------
 27
 28START_TEST(test_glyr_init)
 29{
 30  GlyrQuery q;
 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,"glyr_get() should still return NULL, even with uninitalized query.");
 33
 34  glyr_init();
 35  fail_unless(glyr_get(NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
 36  fail_unless(glyr_get(&q,NULL,NULL) == NULL,"should not access bad memory");
 37
 38  glyr_init();
 39  fail_unless(glyr_get(NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
 40  fail_unless(glyr_get(&q,NULL,NULL) == NULL,"should not access bad memory");
 41
 42  glyr_cleanup();
 43  glyr_cleanup();
 44  fail_unless(glyr_get(NULL,NULL,NULL) == NULL,"glyr_get() should return NULL without INIT");
 45  fail_unless(glyr_get(&q,NULL,NULL) == NULL,"should not access bad memory");
 46
 47  glyr_query_init(&q);
 48  fail_unless(q.is_initalized == 0xDEADBEEF,NULL);
 49  fail_unless(glyr_get(&q,NULL,NULL) == NULL,"should not access bad memory");
 50  glyr_query_destroy(&q);
 51}
 52END_TEST
 53
 54//--------------------
 55
 56START_TEST(test_glyr_destroy_before_init)
 57{
 58    glyr_cleanup();
 59    glyr_init();
 60}
 61END_TEST
 62
 63
 64//--------------------
 65
 66START_TEST(test_glyr_query_init)
 67{
 68    GlyrQuery q;
 69
 70    /* Crash? */
 71    glyr_query_init(NULL);
 72
 73    glyr_query_init(&q);
 74    fail_unless(q.is_initalized == 0xDEADBEEF,NULL);
 75
 76    GlyrQuery * alloc = malloc(sizeof(GlyrQuery));
 77    glyr_query_init(alloc);
 78    fail_unless(alloc->is_initalized == 0xDEADBEEF,NULL);
 79
 80    glyr_query_destroy(&q);
 81}
 82END_TEST
 83
 84//--------------------
 85
 86START_TEST(test_glyr_cache_free)
 87{
 88    GlyrMemCache * test = glyr_cache_new();
 89    glyr_cache_free(NULL);
 90    glyr_cache_free(test);
 91}
 92END_TEST
 93
 94//--------------------
 95
 96START_TEST(test_glyr_cache_copy)
 97{
 98    GlyrMemCache * test = glyr_cache_new();
 99    glyr_cache_set_data(test,g_strdup("some data"),-1);
100    test->next = (GlyrMemCache*)0x1010;
101    test->prev = (GlyrMemCache*)0x0101;
102
103    GlyrMemCache * copy = glyr_cache_copy(test);
104    fail_unless(memcmp(copy->data,test->data,test->size) == 0,"Should have the same data");
105    fail_unless(copy->next == NULL,NULL);
106    fail_unless(copy->prev == NULL,NULL);
107
108    glyr_cache_free(copy);
109    glyr_cache_free(test);
110}
111END_TEST
112
113//--------------------
114
115START_TEST(test_glyr_query_destroy)
116{
117    GlyrQuery q;
118    glyr_query_destroy(NULL);
119    glyr_query_destroy(&q);
120    glyr_query_init(&q);
121    glyr_query_destroy(&q);
122}
123END_TEST
124
125//--------------------
126
127START_TEST(test_glyr_free_list)
128{
129    glyr_free_list(NULL);
130    GlyrMemCache * new1 = glyr_cache_new();
131    GlyrMemCache * new2 = glyr_cache_new();
132    GlyrMemCache * new3 = glyr_cache_new();
133    new2->next = new3;
134    new2->prev = new1;
135    glyr_free_list(new2);
136}
137END_TEST
138
139//--------------------
140
141START_TEST(test_glyr_cache_set_data)
142{
143    glyr_cache_set_data(NULL,"lala",10);
144
145    GlyrMemCache * c = glyr_cache_new();
146    c->data = g_strdup("Hello?");
147    c->size = strlen("Hello?");
148    glyr_cache_update_md5sum(c);    
149    unsigned char old_sum[16] = {0};
150    memcpy(old_sum,c->md5sum,16);
151    size_t old_size = c->size;
152
153    glyr_cache_set_data(c,g_strdup(" World!"),-1);
154    
155    fail_if(g_strcmp0(c->data," World!"),NULL);
156    fail_if(old_size == c->size,NULL);
157    fail_if(!memcmp(old_sum,c->md5sum,16),NULL);
158
159    glyr_cache_free(c);
160}
161END_TEST
162
163//--------------------
164
165START_TEST(test_glyr_cache_write)
166{
167    glyr_init();
168
169    glyr_cache_write(NULL,"/tmp");
170    GlyrMemCache * tmp = glyr_cache_new();
171    glyr_cache_write(tmp,NULL);
172
173    glyr_cache_set_data(tmp,g_strdup("Test data of the cache"),-1);
174    glyr_cache_write(tmp,"stdout");
175    glyr_cache_write(tmp,"stderr");
176    glyr_cache_write(tmp,"null");
177    glyr_cache_write(tmp,"/tmp/test.txt");
178
179    glyr_cache_free(tmp);
180    glyr_cleanup();
181}
182END_TEST
183
184//--------------------
185//--------------------
186//--------------------
187
188START_TEST(test_glyr_download)
189{
190    glyr_init();
191    atexit(glyr_cleanup);
192    GlyrMemCache * c = glyr_download("www.duckduckgo.com",NULL);
193    fail_unless(c != NULL,"Could not load www.google.de");
194    glyr_cache_free(c);
195}
196END_TEST
197
198//--------------------
199
200Suite * create_test_suite(void)
201{
202  Suite *s = suite_create ("Libglyr API");
203
204  /* Core test case */
205  TCase * tc_core = tcase_create ("Init");
206  tcase_add_test(tc_core, test_glyr_init);
207  tcase_add_test(tc_core, test_glyr_destroy_before_init);
208  tcase_add_test(tc_core, test_glyr_query_init);
209  tcase_add_test(tc_core, test_glyr_query_destroy);
210  tcase_add_test(tc_core, test_glyr_free_list);
211  tcase_add_test(tc_core, test_glyr_cache_free);
212  tcase_add_test(tc_core, test_glyr_cache_copy);
213  tcase_add_test(tc_core, test_glyr_cache_set_data);
214  tcase_add_test(tc_core, test_glyr_cache_write);
215  tcase_add_test(tc_core, test_glyr_download);
216  suite_add_tcase(s, tc_core);
217  return s;
218}
219
220//--------------------
221
222int main(void)
223{
224    init();
225
226    int number_failed;
227    Suite *s = create_test_suite();
228
229    SRunner * sr = srunner_create(s);
230    srunner_set_log(sr, "check_glyr_api.log");
231    srunner_run_all(sr, CK_VERBOSE);
232
233    number_failed = srunner_ntests_failed(sr);
234    srunner_free (sr);
235    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
236};