rgb_led_ws281x: refactor bit/bits handling, use more common code
[libsigrokdecode/gsi.git] / tests / decoder.c
blob3acabaeca30e2c7c30c910eec8ff0c61323596e1
1 /*
2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <libsigrokdecode.h> /* First, to avoid compiler warning. */
22 #include <stdlib.h>
23 #include <check.h>
24 #include "lib.h"
27 * Check whether srd_decoder_load_all() works.
28 * If it returns != SRD_OK (or segfaults) this test will fail.
30 START_TEST(test_load_all)
32 int ret;
34 srd_init(DECODERS_TESTDIR);
35 ret = srd_decoder_load_all();
36 fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
37 srd_exit();
39 END_TEST
42 * Check whether srd_decoder_load_all() fails without prior srd_init().
43 * If it returns != SRD_OK (or segfaults) this test will fail.
44 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
46 START_TEST(test_load_all_no_init)
48 int ret;
50 ret = srd_decoder_load_all();
51 fail_unless(ret != SRD_OK, "srd_decoder_load_all() didn't fail properly.");
53 END_TEST
56 * Check whether srd_decoder_load() works.
57 * If it returns != SRD_OK (or segfaults) this test will fail.
59 START_TEST(test_load)
61 int ret;
63 srd_init(DECODERS_TESTDIR);
64 ret = srd_decoder_load("uart");
65 fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
66 ret = srd_decoder_load("spi");
67 fail_unless(ret == SRD_OK, "srd_decoder_load(spi) failed: %d.", ret);
68 ret = srd_decoder_load("usb_signalling");
69 fail_unless(ret == SRD_OK, "srd_decoder_load(usb_signalling) failed: %d.", ret);
70 srd_exit();
72 END_TEST
75 * Check whether srd_decoder_load() fails for non-existing or bogus PDs.
76 * If it returns SRD_OK (or segfaults) this test will fail.
78 START_TEST(test_load_bogus)
80 srd_init(DECODERS_TESTDIR);
81 /* http://sigrok.org/bugzilla/show_bug.cgi?id=176 */
82 fail_unless(srd_decoder_load(NULL) != SRD_OK);
83 fail_unless(srd_decoder_load("") != SRD_OK);
84 fail_unless(srd_decoder_load(" ") != SRD_OK);
85 fail_unless(srd_decoder_load("nonexisting") != SRD_OK);
86 fail_unless(srd_decoder_load("UART") != SRD_OK);
87 fail_unless(srd_decoder_load("UaRt") != SRD_OK);
88 fail_unless(srd_decoder_load("u a r t") != SRD_OK);
89 fail_unless(srd_decoder_load("uart ") != SRD_OK);
90 fail_unless(srd_decoder_load(" uart") != SRD_OK);
91 fail_unless(srd_decoder_load(" uart ") != SRD_OK);
92 fail_unless(srd_decoder_load("uart spi") != SRD_OK);
93 srd_exit();
95 END_TEST
98 * Check whether srd_decoder_load() works/fails for valid/bogus PDs.
99 * If it returns incorrect values (or segfaults) this test will fail.
101 START_TEST(test_load_valid_and_bogus)
103 srd_init(DECODERS_TESTDIR);
104 fail_unless(srd_decoder_load("") != SRD_OK);
105 fail_unless(srd_decoder_load("uart") == SRD_OK);
106 fail_unless(srd_decoder_load("") != SRD_OK);
107 fail_unless(srd_decoder_load("spi") == SRD_OK);
108 fail_unless(srd_decoder_load("") != SRD_OK);
109 fail_unless(srd_decoder_load("can") == SRD_OK);
110 fail_unless(srd_decoder_load("") != SRD_OK);
111 srd_exit();
113 END_TEST
116 * Check whether srd_decoder_load() fails when run multiple times.
117 * If it returns a value != SRD_OK (or segfaults) this test will fail.
118 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=177
120 START_TEST(test_load_multiple)
122 int ret;
124 srd_init(DECODERS_TESTDIR);
125 ret = srd_decoder_load("uart");
126 fail_unless(ret == SRD_OK, "Loading uart PD 1x failed: %d", ret);
127 ret = srd_decoder_load("uart");
128 fail_unless(ret == SRD_OK, "Loading uart PD 2x failed: %d", ret);
129 ret = srd_decoder_load("uart");
130 fail_unless(ret == SRD_OK, "Loading uart PD 3x failed: %d", ret);
131 srd_exit();
133 END_TEST
136 * Check whether srd_decoder_load() fails if a non-existing PD dir is used.
137 * If it returns SRD_OK (or segfaults) this test will fail.
139 START_TEST(test_load_nonexisting_pd_dir)
141 #if 0
142 /* TODO: Build libsigrokdecode with no default PD dir. */
143 srd_init("/nonexisting_dir");
144 fail_unless(srd_decoder_load("spi") != SRD_OK);
145 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
146 srd_exit();
147 #endif
149 END_TEST
152 * Check whether srd_decoder_unload_all() works.
153 * If it returns != SRD_OK (or segfaults) this test will fail.
155 START_TEST(test_unload_all)
157 int ret;
159 srd_init(DECODERS_TESTDIR);
160 ret = srd_decoder_load_all();
161 fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
162 ret = srd_decoder_unload_all();
163 fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
164 srd_exit();
166 END_TEST
169 * Check whether srd_decoder_unload_all() works without prior srd_init().
170 * If it returns != SRD_OK (or segfaults) this test will fail.
172 START_TEST(test_unload_all_no_init)
174 int ret;
176 ret = srd_decoder_unload_all();
177 fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
179 END_TEST
182 * Check whether srd_decoder_unload_all() works multiple times.
183 * If it returns != SRD_OK (or segfaults) this test will fail.
185 START_TEST(test_unload_all_multiple)
187 int ret, i;
189 srd_init(DECODERS_TESTDIR);
190 for (i = 0; i < 10; i++) {
191 ret = srd_decoder_load_all();
192 fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
193 ret = srd_decoder_unload_all();
194 fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
196 srd_exit();
198 END_TEST
201 * Check whether srd_decoder_unload_all() works multiple times (no load).
202 * If it returns != SRD_OK (or segfaults) this test will fail.
204 START_TEST(test_unload_all_multiple_noload)
206 int ret, i;
208 srd_init(DECODERS_TESTDIR);
209 for (i = 0; i < 10; i++) {
210 ret = srd_decoder_unload_all();
211 fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
213 srd_exit();
215 END_TEST
218 * Check whether srd_decoder_unload() works.
219 * If it returns != SRD_OK (or segfaults) this test will fail.
221 START_TEST(test_unload)
223 int ret;
224 struct srd_decoder *dec;
226 srd_init(DECODERS_TESTDIR);
227 ret = srd_decoder_load("uart");
228 fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
229 dec = srd_decoder_get_by_id("uart");
230 fail_unless(dec != NULL);
231 ret = srd_decoder_unload(dec);
232 fail_unless(ret == SRD_OK, "srd_decoder_unload() failed: %d.", ret);
233 srd_exit();
235 END_TEST
238 * Check whether srd_decoder_unload(NULL) fails.
239 * If it returns SRD_OK (or segfaults) this test will fail.
241 START_TEST(test_unload_null)
243 srd_init(DECODERS_TESTDIR);
244 fail_unless(srd_decoder_unload(NULL) != SRD_OK);
245 srd_exit();
247 END_TEST
250 * Check whether srd_decoder_unload(NULL) fails without prior srd_init().
251 * If it returns SRD_OK (or segfaults) this test will fail.
253 START_TEST(test_unload_null_no_init)
255 fail_unless(srd_decoder_unload(NULL) != SRD_OK);
257 END_TEST
260 * Check whether srd_decoder_list() returns a non-empty list.
261 * If it returns an empty list (or segfaults) this test will fail.
263 START_TEST(test_decoder_list)
265 srd_init(DECODERS_TESTDIR);
266 srd_decoder_load_all();
267 fail_unless(srd_decoder_list() != NULL);
268 srd_exit();
270 END_TEST
273 * Check whether srd_decoder_list() without prior srd_decoder_load_all()
274 * returns an empty list (return value != NULL).
275 * If it returns a non-empty list (or segfaults) this test will fail.
277 START_TEST(test_decoder_list_no_load)
279 srd_init(DECODERS_TESTDIR);
280 fail_unless(srd_decoder_list() == NULL);
281 srd_exit();
283 END_TEST
286 * Check whether srd_decoder_list() without prior srd_init()
287 * returns an empty list.
288 * If it returns a non-empty list (or segfaults) this test will fail.
289 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
291 START_TEST(test_decoder_list_no_init)
293 srd_decoder_load_all();
294 fail_unless(srd_decoder_list() == NULL);
296 END_TEST
299 * Check whether srd_decoder_list() without prior srd_init() and without
300 * prior srd_decoder_load_all() returns an empty list.
301 * If it returns a non-empty list (or segfaults) this test will fail.
303 START_TEST(test_decoder_list_no_init_no_load)
305 fail_unless(srd_decoder_list() == NULL);
307 END_TEST
310 * Check whether srd_decoder_list() returns the correct number of PDs.
311 * If it returns a wrong number (or segfaults) this test will fail.
313 START_TEST(test_decoder_list_correct_numbers)
315 srd_init(DECODERS_TESTDIR);
316 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
317 srd_decoder_load("spi");
318 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 1);
319 srd_decoder_load("uart");
320 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 2);
321 srd_decoder_load("can");
322 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
323 srd_decoder_load("can"); /* Load same PD twice. */
324 fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
325 srd_exit();
327 END_TEST
330 * Check whether srd_decoder_get_by_id() works.
331 * If it returns NULL for valid PDs (or segfaults) this test will fail.
333 START_TEST(test_get_by_id)
335 srd_init(DECODERS_TESTDIR);
336 srd_decoder_load("uart");
337 fail_unless(srd_decoder_get_by_id("uart") != NULL);
338 fail_unless(srd_decoder_get_by_id("can") == NULL);
339 srd_decoder_load("can");
340 fail_unless(srd_decoder_get_by_id("uart") != NULL);
341 fail_unless(srd_decoder_get_by_id("can") != NULL);
342 srd_exit();
344 END_TEST
347 * Check whether srd_decoder_get_by_id() works multiple times in a row.
348 * If it returns NULL for valid PDs (or segfaults) this test will fail.
350 START_TEST(test_get_by_id_multiple)
352 srd_init(DECODERS_TESTDIR);
353 srd_decoder_load("uart");
354 fail_unless(srd_decoder_get_by_id("uart") != NULL);
355 fail_unless(srd_decoder_get_by_id("uart") != NULL);
356 fail_unless(srd_decoder_get_by_id("uart") != NULL);
357 fail_unless(srd_decoder_get_by_id("uart") != NULL);
358 srd_exit();
360 END_TEST
363 * Check whether srd_decoder_get_by_id() fails for bogus PDs.
364 * If it returns a value != NULL (or segfaults) this test will fail.
366 START_TEST(test_get_by_id_bogus)
368 srd_init(DECODERS_TESTDIR);
369 fail_unless(srd_decoder_get_by_id(NULL) == NULL);
370 fail_unless(srd_decoder_get_by_id("") == NULL);
371 fail_unless(srd_decoder_get_by_id(" ") == NULL);
372 fail_unless(srd_decoder_get_by_id("nonexisting") == NULL);
373 fail_unless(srd_decoder_get_by_id("sPi") == NULL);
374 fail_unless(srd_decoder_get_by_id("SPI") == NULL);
375 fail_unless(srd_decoder_get_by_id("s p i") == NULL);
376 fail_unless(srd_decoder_get_by_id(" spi") == NULL);
377 fail_unless(srd_decoder_get_by_id("spi ") == NULL);
378 fail_unless(srd_decoder_get_by_id(" spi ") == NULL);
379 fail_unless(srd_decoder_get_by_id("spi uart") == NULL);
380 srd_exit();
382 END_TEST
385 * Check whether srd_decoder_doc_get() works.
386 * If it returns NULL for valid PDs (or segfaults) this test will fail.
388 START_TEST(test_doc_get)
390 struct srd_decoder *dec;
391 char *doc;
393 srd_init(DECODERS_TESTDIR);
394 srd_decoder_load("uart");
395 dec = srd_decoder_get_by_id("uart");
396 doc = srd_decoder_doc_get(dec);
397 fail_unless(doc != NULL);
398 g_free(doc);
399 srd_exit();
401 END_TEST
404 * Check whether srd_decoder_doc_get() fails with NULL as argument.
405 * If it returns a value != NULL (or segfaults) this test will fail.
406 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=179
407 * Check whether srd_decoder_doc_get() fails with dec->py_mod == NULL.
408 * If it returns a value != NULL (or segfaults) this test will fail.
409 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=180
411 START_TEST(test_doc_get_null)
413 struct srd_decoder dec;
415 dec.py_mod = NULL;
417 srd_init(DECODERS_TESTDIR);
418 fail_unless(srd_decoder_doc_get(NULL) == NULL);
419 fail_unless(srd_decoder_doc_get(&dec) == NULL);
420 srd_exit();
422 END_TEST
424 Suite *suite_decoder(void)
426 Suite *s;
427 TCase *tc;
429 s = suite_create("decoder");
431 tc = tcase_create("load");
432 tcase_set_timeout(tc, 0);
433 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
434 tcase_add_test(tc, test_load_all);
435 tcase_add_test(tc, test_load_all_no_init);
436 tcase_add_test(tc, test_load);
437 tcase_add_test(tc, test_load_bogus);
438 tcase_add_test(tc, test_load_valid_and_bogus);
439 tcase_add_test(tc, test_load_multiple);
440 tcase_add_test(tc, test_load_nonexisting_pd_dir);
441 suite_add_tcase(s, tc);
443 tc = tcase_create("unload");
444 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
445 tcase_add_test(tc, test_unload_all);
446 tcase_add_test(tc, test_unload_all_no_init);
447 tcase_add_test(tc, test_unload_all_multiple);
448 tcase_add_test(tc, test_unload_all_multiple_noload);
449 tcase_add_test(tc, test_unload);
450 tcase_add_test(tc, test_unload_null);
451 tcase_add_test(tc, test_unload_null_no_init);
452 suite_add_tcase(s, tc);
454 tc = tcase_create("list");
455 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
456 tcase_add_test(tc, test_decoder_list);
457 tcase_add_test(tc, test_decoder_list_no_load);
458 tcase_add_test(tc, test_decoder_list_no_init);
459 tcase_add_test(tc, test_decoder_list_no_init_no_load);
460 tcase_add_test(tc, test_decoder_list_correct_numbers);
461 suite_add_tcase(s, tc);
463 tc = tcase_create("get_by_id");
464 tcase_add_test(tc, test_get_by_id);
465 tcase_add_test(tc, test_get_by_id_multiple);
466 tcase_add_test(tc, test_get_by_id_bogus);
467 suite_add_tcase(s, tc);
469 tc = tcase_create("doc_get");
470 tcase_add_test(tc, test_doc_get);
471 tcase_add_test(tc, test_doc_get_null);
472 suite_add_tcase(s, tc);
474 return s;