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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
26 static void setup(void)
28 /* Silence libsigrokdecode while the unit tests run. */
29 srd_log_loglevel_set(SRD_LOG_NONE);
32 static void teardown(void)
37 * Check whether srd_inst_new() works.
38 * If it returns NULL (or segfaults) this test will fail.
40 START_TEST(test_inst_new)
42 struct srd_session *sess;
43 struct srd_decoder_inst *inst;
45 srd_init(DECODERS_DIR);
46 srd_decoder_load("uart");
47 srd_session_new(&sess);
48 inst = srd_inst_new(sess, "uart", NULL);
49 fail_unless(inst != NULL, "srd_inst_new() failed.");
55 * Check whether multiple srd_inst_new() calls work.
56 * If any of them returns NULL (or segfaults) this test will fail.
58 START_TEST(test_inst_new_multiple)
60 struct srd_session *sess;
61 struct srd_decoder_inst *inst1, *inst2, *inst3;
63 inst1 = inst2 = inst3 = NULL;
65 srd_init(DECODERS_DIR);
66 srd_decoder_load_all();
67 srd_session_new(&sess);
69 /* Multiple srd_inst_new() calls must work. */
70 inst1 = srd_inst_new(sess, "uart", NULL);
71 fail_unless(inst1 != NULL, "srd_inst_new() 1 failed.");
72 inst2 = srd_inst_new(sess, "spi", NULL);
73 fail_unless(inst2 != NULL, "srd_inst_new() 2 failed.");
74 inst3 = srd_inst_new(sess, "can", NULL);
75 fail_unless(inst3 != NULL, "srd_inst_new() 3 failed.");
77 /* The returned instance pointers must not be the same. */
78 fail_unless(inst1 != inst2);
79 fail_unless(inst1 != inst3);
80 fail_unless(inst2 != inst3);
82 /* Each instance must have another py_inst than any of the others. */
83 fail_unless(inst1->py_inst != inst2->py_inst);
84 fail_unless(inst1->py_inst != inst3->py_inst);
85 fail_unless(inst2->py_inst != inst3->py_inst);
92 * Check whether srd_inst_option_set() works for an empty options hash.
93 * If it returns != SRD_OK (or segfaults) this test will fail.
95 START_TEST(test_inst_option_set_empty)
98 struct srd_session *sess;
99 struct srd_decoder_inst *inst;
102 srd_init(DECODERS_DIR);
103 srd_decoder_load_all();
104 srd_session_new(&sess);
105 inst = srd_inst_new(sess, "uart", NULL);
106 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
107 (GDestroyNotify)g_variant_unref);
108 ret = srd_inst_option_set(inst, options);
109 fail_unless(ret == SRD_OK, "srd_inst_option_set() with empty options "
110 "hash failed: %d.", ret);
116 * Check whether srd_inst_option_set() works for bogus options.
117 * If it returns != SRD_OK (or segfaults) this test will fail.
119 START_TEST(test_inst_option_set_bogus)
122 struct srd_session *sess;
123 struct srd_decoder_inst *inst;
126 srd_init(DECODERS_DIR);
127 srd_decoder_load_all();
128 srd_session_new(&sess);
129 inst = srd_inst_new(sess, "uart", NULL);
131 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
132 (GDestroyNotify)g_variant_unref);
135 ret = srd_inst_option_set(NULL, options);
136 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
137 "instance failed: %d.", ret);
139 /* NULL 'options' GHashTable. */
140 ret = srd_inst_option_set(inst, NULL);
141 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
142 "options hash failed: %d.", ret);
144 /* NULL instance and NULL 'options' GHashTable. */
145 ret = srd_inst_option_set(NULL, NULL);
146 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
147 "instance and NULL options hash failed: %d.", ret);
153 Suite *suite_inst(void)
158 s = suite_create("inst");
160 tc = tcase_create("new");
161 tcase_add_checked_fixture(tc, setup, teardown);
162 tcase_add_test(tc, test_inst_new);
163 tcase_add_test(tc, test_inst_new_multiple);
164 suite_add_tcase(s, tc);
166 tc = tcase_create("option");
167 tcase_add_checked_fixture(tc, setup, teardown);
168 tcase_add_test(tc, test_inst_option_set_empty);
169 tcase_add_test(tc, test_inst_option_set_bogus);
170 suite_add_tcase(s, tc);