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