]> sigrok.org Git - libsigrokdecode.git/blame - tests/inst.c
Random whitespace/cosmetic/consistency fixes.
[libsigrokdecode.git] / tests / inst.c
CommitLineData
368b406e
UH
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
4539e9ca 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
368b406e
UH
18 */
19
36784362 20#include <config.h>
b480383d 21#include <libsigrokdecode.h> /* First, to avoid compiler warning. */
368b406e
UH
22#include <stdlib.h>
23#include <check.h>
b9223384 24#include "lib.h"
368b406e 25
368b406e
UH
26/*
27 * Check whether srd_inst_new() works.
28 * If it returns NULL (or segfaults) this test will fail.
29 */
30START_TEST(test_inst_new)
31{
32 struct srd_session *sess;
33 struct srd_decoder_inst *inst;
34
41bab682 35 srd_init(DECODERS_TESTDIR);
368b406e
UH
36 srd_decoder_load("uart");
37 srd_session_new(&sess);
38 inst = srd_inst_new(sess, "uart", NULL);
39 fail_unless(inst != NULL, "srd_inst_new() failed.");
40 srd_exit();
41}
42END_TEST
43
44/*
45 * Check whether multiple srd_inst_new() calls work.
46 * If any of them returns NULL (or segfaults) this test will fail.
47 */
48START_TEST(test_inst_new_multiple)
49{
50 struct srd_session *sess;
51 struct srd_decoder_inst *inst1, *inst2, *inst3;
52
53 inst1 = inst2 = inst3 = NULL;
54
41bab682 55 srd_init(DECODERS_TESTDIR);
368b406e
UH
56 srd_decoder_load_all();
57 srd_session_new(&sess);
58
59 /* Multiple srd_inst_new() calls must work. */
60 inst1 = srd_inst_new(sess, "uart", NULL);
61 fail_unless(inst1 != NULL, "srd_inst_new() 1 failed.");
62 inst2 = srd_inst_new(sess, "spi", NULL);
63 fail_unless(inst2 != NULL, "srd_inst_new() 2 failed.");
64 inst3 = srd_inst_new(sess, "can", NULL);
65 fail_unless(inst3 != NULL, "srd_inst_new() 3 failed.");
66
67 /* The returned instance pointers must not be the same. */
68 fail_unless(inst1 != inst2);
69 fail_unless(inst1 != inst3);
70 fail_unless(inst2 != inst3);
71
72 /* Each instance must have another py_inst than any of the others. */
73 fail_unless(inst1->py_inst != inst2->py_inst);
74 fail_unless(inst1->py_inst != inst3->py_inst);
75 fail_unless(inst2->py_inst != inst3->py_inst);
76
77 srd_exit();
78}
79END_TEST
80
81/*
82 * Check whether srd_inst_option_set() works for an empty options hash.
83 * If it returns != SRD_OK (or segfaults) this test will fail.
84 */
85START_TEST(test_inst_option_set_empty)
86{
87 int ret;
88 struct srd_session *sess;
89 struct srd_decoder_inst *inst;
90 GHashTable *options;
91
41bab682 92 srd_init(DECODERS_TESTDIR);
368b406e
UH
93 srd_decoder_load_all();
94 srd_session_new(&sess);
95 inst = srd_inst_new(sess, "uart", NULL);
96 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
97 (GDestroyNotify)g_variant_unref);
98 ret = srd_inst_option_set(inst, options);
99 fail_unless(ret == SRD_OK, "srd_inst_option_set() with empty options "
100 "hash failed: %d.", ret);
101 srd_exit();
102}
103END_TEST
104
105/*
106 * Check whether srd_inst_option_set() works for bogus options.
107 * If it returns != SRD_OK (or segfaults) this test will fail.
108 */
109START_TEST(test_inst_option_set_bogus)
110{
111 int ret;
112 struct srd_session *sess;
113 struct srd_decoder_inst *inst;
114 GHashTable *options;
115
41bab682 116 srd_init(DECODERS_TESTDIR);
368b406e
UH
117 srd_decoder_load_all();
118 srd_session_new(&sess);
119 inst = srd_inst_new(sess, "uart", NULL);
120
121 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
122 (GDestroyNotify)g_variant_unref);
123
124 /* NULL instance. */
125 ret = srd_inst_option_set(NULL, options);
126 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
127 "instance failed: %d.", ret);
128
129 /* NULL 'options' GHashTable. */
130 ret = srd_inst_option_set(inst, NULL);
131 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
132 "options hash failed: %d.", ret);
133
134 /* NULL instance and NULL 'options' GHashTable. */
135 ret = srd_inst_option_set(NULL, NULL);
136 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
137 "instance and NULL options hash failed: %d.", ret);
138
139 srd_exit();
140}
141END_TEST
142
143Suite *suite_inst(void)
144{
145 Suite *s;
146 TCase *tc;
147
148 s = suite_create("inst");
149
150 tc = tcase_create("new");
28c3c217 151 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
368b406e
UH
152 tcase_add_test(tc, test_inst_new);
153 tcase_add_test(tc, test_inst_new_multiple);
154 suite_add_tcase(s, tc);
155
156 tc = tcase_create("option");
28c3c217 157 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
368b406e
UH
158 tcase_add_test(tc, test_inst_option_set_empty);
159 tcase_add_test(tc, test_inst_option_set_bogus);
160 suite_add_tcase(s, tc);
161
162 return s;
163}