]> sigrok.org Git - libsigrokdecode.git/blame - tests/check_session.c
avr_isp: Fix 'Device' annotation sample numbers + row.
[libsigrokdecode.git] / tests / check_session.c
CommitLineData
269b442d
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 "../libsigrokdecode-internal.h"
23#include <stdlib.h>
24#include <check.h>
b9223384 25#include "lib.h"
269b442d
UH
26
27static void setup(void)
28{
29 /* Silence libsigrokdecode while the unit tests run. */
30 srd_log_loglevel_set(SRD_LOG_NONE);
31}
32
33static void teardown(void)
34{
35}
36
37/*
38 * Check whether srd_session_new() works.
39 * If it returns != SRD_OK (or segfaults) this test will fail.
40 */
41START_TEST(test_session_new)
42{
43 int ret;
44 struct srd_session *sess;
45
46 srd_init(NULL);
47 ret = srd_session_new(&sess);
48 fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
49 srd_exit();
50}
51END_TEST
52
53/*
54 * Check whether srd_session_new() fails for bogus parameters.
55 * If it returns SRD_OK (or segfaults) this test will fail.
56 */
57START_TEST(test_session_new_bogus)
58{
59 int ret;
60
61 srd_init(NULL);
62 ret = srd_session_new(NULL);
63 fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
64 srd_exit();
65}
66END_TEST
67
68/*
69 * Check whether multiple srd_session_new() calls work.
70 * If any call returns != SRD_OK (or segfaults) this test will fail.
71 */
72START_TEST(test_session_new_multiple)
73{
74 int ret;
75 struct srd_session *sess1, *sess2, *sess3;
76
77 sess1 = sess2 = sess3 = NULL;
78
79 srd_init(NULL);
80
81 /* Multiple srd_session_new() calls must work. */
82 ret = srd_session_new(&sess1);
83 fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
84 ret = srd_session_new(&sess2);
85 fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
86 ret = srd_session_new(&sess3);
87 fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
88
89 /* The returned session pointers must all be non-NULL. */
90 fail_unless(sess1 != NULL);
91 fail_unless(sess2 != NULL);
92 fail_unless(sess3 != NULL);
93
94 /* The returned session pointers must not be the same. */
95 fail_unless(sess1 != sess2);
96 fail_unless(sess1 != sess3);
97 fail_unless(sess2 != sess3);
98
99 /* Each session must have another ID than any other session. */
100 fail_unless(sess1->session_id != sess2->session_id);
101 fail_unless(sess1->session_id != sess3->session_id);
102 fail_unless(sess2->session_id != sess3->session_id);
103
104 /* Destroying any of the sessions must work. */
105 ret = srd_session_destroy(sess1);
106 fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
107 ret = srd_session_destroy(sess2);
108 fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
109 ret = srd_session_destroy(sess3);
110 fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
111
112 srd_exit();
113}
114END_TEST
115
116/*
117 * Check whether srd_session_destroy() works.
118 * If it returns != SRD_OK (or segfaults) this test will fail.
119 */
120START_TEST(test_session_destroy)
121{
122 int ret;
123 struct srd_session *sess;
124
125 srd_init(NULL);
126 srd_session_new(&sess);
127 ret = srd_session_destroy(sess);
128 fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
129 srd_exit();
130}
131END_TEST
132
133/*
134 * Check whether srd_session_destroy() fails for bogus sessions.
135 * If it returns SRD_OK (or segfaults) this test will fail.
136 */
137START_TEST(test_session_destroy_bogus)
138{
139 int ret;
140
141 srd_init(NULL);
142 ret = srd_session_destroy(NULL);
143 fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
144 srd_exit();
145}
146END_TEST
147
148static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
149{
150 int ret;
151
98e0b79b
UH
152 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
153 fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
269b442d
UH
154 PRIu64 ") failed: %d.", sess, key, x, ret);
155}
156
157static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
158{
159 int ret;
160
98e0b79b
UH
161 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
162 fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
269b442d
UH
163 PRIu64 ") worked.", sess, key, x);
164}
165
166static void conf_check_fail_null(struct srd_session *sess, int key)
167{
168 int ret;
169
98e0b79b 170 ret = srd_session_metadata_set(sess, key, NULL);
269b442d 171 fail_unless(ret != SRD_OK,
98e0b79b 172 "srd_session_metadata_set(NULL) for key %d worked.", key);
269b442d
UH
173}
174
175static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
176{
177 int ret;
178
98e0b79b
UH
179 ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
180 fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
269b442d
UH
181 "failed: %d.", key, ret);
182}
183
184/*
98e0b79b 185 * Check whether srd_session_metadata_set() works.
269b442d
UH
186 * If it returns != SRD_OK (or segfaults) this test will fail.
187 */
98e0b79b 188START_TEST(test_session_metadata_set)
269b442d
UH
189{
190 uint64_t i;
191 struct srd_session *sess;
192
193 srd_init(NULL);
194 srd_session_new(&sess);
195 /* Try a bunch of values. */
196 for (i = 0; i < 1000; i++) {
269b442d
UH
197 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
198 }
199 /* Try the max. possible value. */
269b442d
UH
200 conf_check_ok(sess, SRD_CONF_SAMPLERATE, 18446744073709551615ULL);
201 srd_session_destroy(sess);
202 srd_exit();
203}
204END_TEST
205
206/*
98e0b79b 207 * Check whether srd_session_metadata_set() fails with invalid input.
269b442d
UH
208 * If it returns SRD_OK (or segfaults) this test will fail.
209 */
98e0b79b 210START_TEST(test_session_metadata_set_bogus)
269b442d
UH
211{
212 struct srd_session *sess;
213
214 srd_init(NULL);
215 srd_session_new(&sess);
216
217 /* Incorrect gvariant type (currently only uint64 is used). */
269b442d 218 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
269b442d
UH
219 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
220
221 /* NULL data pointer. */
269b442d
UH
222 conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
223
224 /* NULL session. */
269b442d
UH
225 conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
226
227 /* Invalid keys. */
228 conf_check_fail(sess, -1, 0);
229 conf_check_fail(sess, 9, 0);
230 conf_check_fail(sess, 123, 0);
231
232 srd_session_destroy(sess);
233 srd_exit();
234}
235END_TEST
236
237Suite *suite_session(void)
238{
239 Suite *s;
240 TCase *tc;
241
242 s = suite_create("session");
243
244 tc = tcase_create("new_destroy");
245 tcase_add_checked_fixture(tc, setup, teardown);
246 tcase_add_test(tc, test_session_new);
247 tcase_add_test(tc, test_session_new_bogus);
248 tcase_add_test(tc, test_session_new_multiple);
249 tcase_add_test(tc, test_session_destroy);
250 tcase_add_test(tc, test_session_destroy_bogus);
251 suite_add_tcase(s, tc);
252
253 tc = tcase_create("config");
254 tcase_add_checked_fixture(tc, setup, teardown);
98e0b79b
UH
255 tcase_add_test(tc, test_session_metadata_set);
256 tcase_add_test(tc, test_session_metadata_set_bogus);
269b442d
UH
257 suite_add_tcase(s, tc);
258
259 return s;
260}