]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
19 | */ | |
20 | ||
21 | #include "../libsigrokdecode.h" /* First, to avoid compiler warning. */ | |
22 | #include <stdlib.h> | |
23 | #include <check.h> | |
24 | ||
25 | static void setup(void) | |
26 | { | |
27 | /* Silence libsigrokdecode while the unit tests run. */ | |
28 | srd_log_loglevel_set(SRD_LOG_NONE); | |
29 | } | |
30 | ||
31 | static void teardown(void) | |
32 | { | |
33 | } | |
34 | ||
35 | /* | |
36 | * Check whether srd_inst_new() works. | |
37 | * If it returns NULL (or segfaults) this test will fail. | |
38 | */ | |
39 | START_TEST(test_inst_new) | |
40 | { | |
41 | struct srd_session *sess; | |
42 | struct srd_decoder_inst *inst; | |
43 | ||
554a49f9 | 44 | srd_init(DECODERS_DIR); |
368b406e UH |
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."); | |
49 | srd_exit(); | |
50 | } | |
51 | END_TEST | |
52 | ||
53 | /* | |
54 | * Check whether multiple srd_inst_new() calls work. | |
55 | * If any of them returns NULL (or segfaults) this test will fail. | |
56 | */ | |
57 | START_TEST(test_inst_new_multiple) | |
58 | { | |
59 | struct srd_session *sess; | |
60 | struct srd_decoder_inst *inst1, *inst2, *inst3; | |
61 | ||
62 | inst1 = inst2 = inst3 = NULL; | |
63 | ||
554a49f9 | 64 | srd_init(DECODERS_DIR); |
368b406e UH |
65 | srd_decoder_load_all(); |
66 | srd_session_new(&sess); | |
67 | ||
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."); | |
75 | ||
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); | |
80 | ||
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); | |
85 | ||
86 | srd_exit(); | |
87 | } | |
88 | END_TEST | |
89 | ||
90 | /* | |
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. | |
93 | */ | |
94 | START_TEST(test_inst_option_set_empty) | |
95 | { | |
96 | int ret; | |
97 | struct srd_session *sess; | |
98 | struct srd_decoder_inst *inst; | |
99 | GHashTable *options; | |
100 | ||
554a49f9 | 101 | srd_init(DECODERS_DIR); |
368b406e UH |
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); | |
110 | srd_exit(); | |
111 | } | |
112 | END_TEST | |
113 | ||
114 | /* | |
115 | * Check whether srd_inst_option_set() works for bogus options. | |
116 | * If it returns != SRD_OK (or segfaults) this test will fail. | |
117 | */ | |
118 | START_TEST(test_inst_option_set_bogus) | |
119 | { | |
120 | int ret; | |
121 | struct srd_session *sess; | |
122 | struct srd_decoder_inst *inst; | |
123 | GHashTable *options; | |
124 | ||
554a49f9 | 125 | srd_init(DECODERS_DIR); |
368b406e UH |
126 | srd_decoder_load_all(); |
127 | srd_session_new(&sess); | |
128 | inst = srd_inst_new(sess, "uart", NULL); | |
129 | ||
130 | options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
131 | (GDestroyNotify)g_variant_unref); | |
132 | ||
133 | /* NULL instance. */ | |
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); | |
137 | ||
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); | |
142 | ||
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); | |
147 | ||
148 | srd_exit(); | |
149 | } | |
150 | END_TEST | |
151 | ||
152 | Suite *suite_inst(void) | |
153 | { | |
154 | Suite *s; | |
155 | TCase *tc; | |
156 | ||
157 | s = suite_create("inst"); | |
158 | ||
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); | |
164 | ||
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); | |
170 | ||
171 | return s; | |
172 | } |