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