summaryrefslogtreecommitdiff
path: root/tests/decoder.c
blob: 2298d9334698af70e0cd4406cb16f825c1ce06f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * This file is part of the libsigrokdecode project.
 *
 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <libsigrokdecode.h> /* First, to avoid compiler warning. */
#include <stdlib.h>
#include <check.h>
#include "lib.h"

/*
 * Check whether srd_decoder_load_all() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_load_all)
{
	int ret;

	srd_init(DECODERS_TESTDIR);
	ret = srd_decoder_load_all();
	fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_load_all() fails without prior srd_init().
 * If it returns != SRD_OK (or segfaults) this test will fail.
 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
 */
START_TEST(test_load_all_no_init)
{
	int ret;

	ret = srd_decoder_load_all();
	fail_unless(ret != SRD_OK, "srd_decoder_load_all() didn't fail properly.");
}
END_TEST

/*
 * Check whether srd_decoder_load() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_load)
{
	int ret;

	srd_init(DECODERS_TESTDIR);
	ret = srd_decoder_load("uart");
	fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
	ret = srd_decoder_load("spi");
	fail_unless(ret == SRD_OK, "srd_decoder_load(spi) failed: %d.", ret);
	ret = srd_decoder_load("usb_signalling");
	fail_unless(ret == SRD_OK, "srd_decoder_load(usb_signalling) failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_load() fails for non-existing or bogus PDs.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_load_bogus)
{
	srd_init(DECODERS_TESTDIR);
	/* http://sigrok.org/bugzilla/show_bug.cgi?id=176 */
	fail_unless(srd_decoder_load(NULL) != SRD_OK);
	fail_unless(srd_decoder_load("") != SRD_OK);
	fail_unless(srd_decoder_load(" ") != SRD_OK);
	fail_unless(srd_decoder_load("nonexisting") != SRD_OK);
	fail_unless(srd_decoder_load("UART") != SRD_OK);
	fail_unless(srd_decoder_load("UaRt") != SRD_OK);
	fail_unless(srd_decoder_load("u a r t") != SRD_OK);
	fail_unless(srd_decoder_load("uart ") != SRD_OK);
	fail_unless(srd_decoder_load(" uart") != SRD_OK);
	fail_unless(srd_decoder_load(" uart ") != SRD_OK);
	fail_unless(srd_decoder_load("uart spi") != SRD_OK);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_load() works/fails for valid/bogus PDs.
 * If it returns incorrect values (or segfaults) this test will fail.
 */
START_TEST(test_load_valid_and_bogus)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(srd_decoder_load("") != SRD_OK);
	fail_unless(srd_decoder_load("uart") == SRD_OK);
	fail_unless(srd_decoder_load("") != SRD_OK);
	fail_unless(srd_decoder_load("spi") == SRD_OK);
	fail_unless(srd_decoder_load("") != SRD_OK);
	fail_unless(srd_decoder_load("can") == SRD_OK);
	fail_unless(srd_decoder_load("") != SRD_OK);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_load() fails when run multiple times.
 * If it returns a value != SRD_OK (or segfaults) this test will fail.
 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=177
 */
START_TEST(test_load_multiple)
{
	int ret;

	srd_init(DECODERS_TESTDIR);
	ret = srd_decoder_load("uart");
	fail_unless(ret == SRD_OK, "Loading uart PD 1x failed: %d", ret);
	ret = srd_decoder_load("uart");
	fail_unless(ret == SRD_OK, "Loading uart PD 2x failed: %d", ret);
	ret = srd_decoder_load("uart");
	fail_unless(ret == SRD_OK, "Loading uart PD 3x failed: %d", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_load() fails if a non-existing PD dir is used.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_load_nonexisting_pd_dir)
{
#if 0
	/* TODO: Build libsigrokdecode with no default PD dir. */
	srd_init("/nonexisting_dir");
	fail_unless(srd_decoder_load("spi") != SRD_OK);
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
	srd_exit();
#endif
}
END_TEST

/*
 * Check whether srd_decoder_unload_all() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_all)
{
	int ret;

	srd_init(DECODERS_TESTDIR);
	ret = srd_decoder_load_all();
	fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
	ret = srd_decoder_unload_all();
	fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_unload_all() works without prior srd_init().
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_all_no_init)
{
	int ret;

	ret = srd_decoder_unload_all();
	fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
}
END_TEST

/*
 * Check whether srd_decoder_unload_all() works multiple times.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_all_multiple)
{
	int ret, i;

	srd_init(DECODERS_TESTDIR);
	for (i = 0; i < 10; i++) {
		ret = srd_decoder_load_all();
		fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
		ret = srd_decoder_unload_all();
		fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
	}
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_unload_all() works multiple times (no load).
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_all_multiple_noload)
{
	int ret, i;

	srd_init(DECODERS_TESTDIR);
	for (i = 0; i < 10; i++) {
		ret = srd_decoder_unload_all();
		fail_unless(ret == SRD_OK, "srd_decoder_unload_all() failed: %d.", ret);
	}
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_unload() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload)
{
	int ret;
	struct srd_decoder *dec;

	srd_init(DECODERS_TESTDIR);
	ret = srd_decoder_load("uart");
	fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
	dec = srd_decoder_get_by_id("uart");
	fail_unless(dec != NULL);
	ret = srd_decoder_unload(dec);
	fail_unless(ret == SRD_OK, "srd_decoder_unload() failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_unload(NULL) fails.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_null)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(srd_decoder_unload(NULL) != SRD_OK);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_unload(NULL) fails without prior srd_init().
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_unload_null_no_init)
{
	fail_unless(srd_decoder_unload(NULL) != SRD_OK);
}
END_TEST

/*
 * Check whether srd_decoder_list() returns a non-empty list.
 * If it returns an empty list (or segfaults) this test will fail.
 */
START_TEST(test_decoder_list)
{
	srd_init(DECODERS_TESTDIR);
	srd_decoder_load_all();
	fail_unless(srd_decoder_list() != NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_list() without prior srd_decoder_load_all()
 * returns an empty list (return value != NULL).
 * If it returns a non-empty list (or segfaults) this test will fail.
 */
START_TEST(test_decoder_list_no_load)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(srd_decoder_list() == NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_list() without prior srd_init()
 * returns an empty list.
 * If it returns a non-empty list (or segfaults) this test will fail.
 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
 */
START_TEST(test_decoder_list_no_init)
{
	srd_decoder_load_all();
	fail_unless(srd_decoder_list() == NULL);
}
END_TEST

/*
 * Check whether srd_decoder_list() without prior srd_init() and without
 * prior srd_decoder_load_all() returns an empty list.
 * If it returns a non-empty list (or segfaults) this test will fail.
 */
START_TEST(test_decoder_list_no_init_no_load)
{
	fail_unless(srd_decoder_list() == NULL);
}
END_TEST

/*
 * Check whether srd_decoder_list() returns the correct number of PDs.
 * If it returns a wrong number (or segfaults) this test will fail.
 */
START_TEST(test_decoder_list_correct_numbers)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
	srd_decoder_load("spi");
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 1);
	srd_decoder_load("uart");
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 2);
	srd_decoder_load("can");
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
	srd_decoder_load("can"); /* Load same PD twice. */
	fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_get_by_id() works.
 * If it returns NULL for valid PDs (or segfaults) this test will fail.
 */
START_TEST(test_get_by_id)
{
	srd_init(DECODERS_TESTDIR);
	srd_decoder_load("uart");
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	fail_unless(srd_decoder_get_by_id("can") == NULL);
	srd_decoder_load("can");
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	fail_unless(srd_decoder_get_by_id("can") != NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_get_by_id() works multiple times in a row.
 * If it returns NULL for valid PDs (or segfaults) this test will fail.
 */
START_TEST(test_get_by_id_multiple)
{
	srd_init(DECODERS_TESTDIR);
	srd_decoder_load("uart");
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	fail_unless(srd_decoder_get_by_id("uart") != NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_get_by_id() fails for bogus PDs.
 * If it returns a value != NULL (or segfaults) this test will fail.
 */
START_TEST(test_get_by_id_bogus)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(srd_decoder_get_by_id(NULL) == NULL);
	fail_unless(srd_decoder_get_by_id("") == NULL);
	fail_unless(srd_decoder_get_by_id(" ") == NULL);
	fail_unless(srd_decoder_get_by_id("nonexisting") == NULL);
	fail_unless(srd_decoder_get_by_id("sPi") == NULL);
	fail_unless(srd_decoder_get_by_id("SPI") == NULL);
	fail_unless(srd_decoder_get_by_id("s p i") == NULL);
	fail_unless(srd_decoder_get_by_id(" spi") == NULL);
	fail_unless(srd_decoder_get_by_id("spi ") == NULL);
	fail_unless(srd_decoder_get_by_id(" spi ") == NULL);
	fail_unless(srd_decoder_get_by_id("spi uart") == NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_doc_get() works.
 * If it returns NULL for valid PDs (or segfaults) this test will fail.
 */
START_TEST(test_doc_get)
{
	struct srd_decoder *dec;

	srd_init(DECODERS_TESTDIR);
	srd_decoder_load("uart");
	dec = srd_decoder_get_by_id("uart");
	fail_unless(srd_decoder_doc_get(dec) != NULL);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_decoder_doc_get() fails with NULL as argument.
 * If it returns a value != NULL (or segfaults) this test will fail.
 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=179
 */
START_TEST(test_doc_get_null)
{
	srd_init(DECODERS_TESTDIR);
	fail_unless(srd_decoder_doc_get(NULL) == NULL);
	srd_exit();
}
END_TEST

Suite *suite_decoder(void)
{
	Suite *s;
	TCase *tc;

	s = suite_create("decoder");

	tc = tcase_create("load");
	tcase_set_timeout(tc, 0);
	tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
	tcase_add_test(tc, test_load_all);
	tcase_add_test(tc, test_load_all_no_init);
	tcase_add_test(tc, test_load);
	tcase_add_test(tc, test_load_bogus);
	tcase_add_test(tc, test_load_valid_and_bogus);
	tcase_add_test(tc, test_load_multiple);
	tcase_add_test(tc, test_load_nonexisting_pd_dir);
	suite_add_tcase(s, tc);

	tc = tcase_create("unload");
	tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
	tcase_add_test(tc, test_unload_all);
	tcase_add_test(tc, test_unload_all_no_init);
	tcase_add_test(tc, test_unload_all_multiple);
	tcase_add_test(tc, test_unload_all_multiple_noload);
	tcase_add_test(tc, test_unload);
	tcase_add_test(tc, test_unload_null);
	tcase_add_test(tc, test_unload_null_no_init);
	suite_add_tcase(s, tc);

	tc = tcase_create("list");
	tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
	tcase_add_test(tc, test_decoder_list);
	tcase_add_test(tc, test_decoder_list_no_load);
	tcase_add_test(tc, test_decoder_list_no_init);
	tcase_add_test(tc, test_decoder_list_no_init_no_load);
	tcase_add_test(tc, test_decoder_list_correct_numbers);
	suite_add_tcase(s, tc);

	tc = tcase_create("get_by_id");
	tcase_add_test(tc, test_get_by_id);
	tcase_add_test(tc, test_get_by_id_multiple);
	tcase_add_test(tc, test_get_by_id_bogus);
	suite_add_tcase(s, tc);

	tc = tcase_create("doc_get");
	tcase_add_test(tc, test_doc_get);
	tcase_add_test(tc, test_doc_get_null);
	suite_add_tcase(s, tc);

	return s;
}