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. */
25 static void setup(void)
27 /* Silence libsigrokdecode while the unit tests run. */
28 srd_log_loglevel_set(SRD_LOG_NONE);
31 static void teardown(void)
36 * Check whether srd_inst_new() works.
37 * If it returns NULL (or segfaults) this test will fail.
39 START_TEST(test_inst_new)
41 struct srd_session *sess;
42 struct srd_decoder_inst *inst;
45 srd_decoder_load("uart");
46 srd_session_new(&sess);
47 inst = srd_inst_new(sess, "uart", NULL);
48 fail_unless(inst != NULL, "srd_inst_new() failed.");
54 * Check whether multiple srd_inst_new() calls work.
55 * If any of them returns NULL (or segfaults) this test will fail.
57 START_TEST(test_inst_new_multiple)
59 struct srd_session *sess;
60 struct srd_decoder_inst *inst1, *inst2, *inst3;
62 inst1 = inst2 = inst3 = NULL;
65 srd_decoder_load_all();
66 srd_session_new(&sess);
68 /* Multiple srd_inst_new() calls must work. */
69 inst1 = srd_inst_new(sess, "uart", NULL);
70 fail_unless(inst1 != NULL, "srd_inst_new() 1 failed.");
71 inst2 = srd_inst_new(sess, "spi", NULL);
72 fail_unless(inst2 != NULL, "srd_inst_new() 2 failed.");
73 inst3 = srd_inst_new(sess, "can", NULL);
74 fail_unless(inst3 != NULL, "srd_inst_new() 3 failed.");
76 /* The returned instance pointers must not be the same. */
77 fail_unless(inst1 != inst2);
78 fail_unless(inst1 != inst3);
79 fail_unless(inst2 != inst3);
81 /* Each instance must have another py_inst than any of the others. */
82 fail_unless(inst1->py_inst != inst2->py_inst);
83 fail_unless(inst1->py_inst != inst3->py_inst);
84 fail_unless(inst2->py_inst != inst3->py_inst);
91 * Check whether srd_inst_option_set() works for an empty options hash.
92 * If it returns != SRD_OK (or segfaults) this test will fail.
94 START_TEST(test_inst_option_set_empty)
97 struct srd_session *sess;
98 struct srd_decoder_inst *inst;
102 srd_decoder_load_all();
103 srd_session_new(&sess);
104 inst = srd_inst_new(sess, "uart", NULL);
105 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
106 (GDestroyNotify)g_variant_unref);
107 ret = srd_inst_option_set(inst, options);
108 fail_unless(ret == SRD_OK, "srd_inst_option_set() with empty options "
109 "hash failed: %d.", ret);
115 * Check whether srd_inst_option_set() works for bogus options.
116 * If it returns != SRD_OK (or segfaults) this test will fail.
118 START_TEST(test_inst_option_set_bogus)
121 struct srd_session *sess;
122 struct srd_decoder_inst *inst;
126 srd_decoder_load_all();
127 srd_session_new(&sess);
128 inst = srd_inst_new(sess, "uart", NULL);
130 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
131 (GDestroyNotify)g_variant_unref);
134 ret = srd_inst_option_set(NULL, options);
135 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
136 "instance failed: %d.", ret);
138 /* NULL 'options' GHashTable. */
139 ret = srd_inst_option_set(inst, NULL);
140 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
141 "options hash failed: %d.", ret);
143 /* NULL instance and NULL 'options' GHashTable. */
144 ret = srd_inst_option_set(NULL, NULL);
145 fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
146 "instance and NULL options hash failed: %d.", ret);
152 Suite *suite_inst(void)
157 s = suite_create("inst");
159 tc = tcase_create("new");
160 tcase_add_checked_fixture(tc, setup, teardown);
161 tcase_add_test(tc, test_inst_new);
162 tcase_add_test(tc, test_inst_new_multiple);
163 suite_add_tcase(s, tc);
165 tc = tcase_create("option");
166 tcase_add_checked_fixture(tc, setup, teardown);
167 tcase_add_test(tc, test_inst_option_set_empty);
168 tcase_add_test(tc, test_inst_option_set_bogus);
169 suite_add_tcase(s, tc);