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. */
22 #include "../libsigrokdecode-internal.h"
28 static void setup(void)
30 /* Silence libsigrokdecode while the unit tests run. */
31 srd_log_loglevel_set(SRD_LOG_NONE);
34 static void teardown(void)
39 * Check whether srd_session_new() works.
40 * If it returns != SRD_OK (or segfaults) this test will fail.
42 START_TEST(test_session_new)
45 struct srd_session *sess;
48 ret = srd_session_new(&sess);
49 fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
55 * Check whether srd_session_new() fails for bogus parameters.
56 * If it returns SRD_OK (or segfaults) this test will fail.
58 START_TEST(test_session_new_bogus)
63 ret = srd_session_new(NULL);
64 fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
70 * Check whether multiple srd_session_new() calls work.
71 * If any call returns != SRD_OK (or segfaults) this test will fail.
73 START_TEST(test_session_new_multiple)
76 struct srd_session *sess1, *sess2, *sess3;
78 sess1 = sess2 = sess3 = NULL;
82 /* Multiple srd_session_new() calls must work. */
83 ret = srd_session_new(&sess1);
84 fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
85 ret = srd_session_new(&sess2);
86 fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
87 ret = srd_session_new(&sess3);
88 fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
90 /* The returned session pointers must all be non-NULL. */
91 fail_unless(sess1 != NULL);
92 fail_unless(sess2 != NULL);
93 fail_unless(sess3 != NULL);
95 /* The returned session pointers must not be the same. */
96 fail_unless(sess1 != sess2);
97 fail_unless(sess1 != sess3);
98 fail_unless(sess2 != sess3);
100 /* Each session must have another ID than any other session. */
101 fail_unless(sess1->session_id != sess2->session_id);
102 fail_unless(sess1->session_id != sess3->session_id);
103 fail_unless(sess2->session_id != sess3->session_id);
105 /* Destroying any of the sessions must work. */
106 ret = srd_session_destroy(sess1);
107 fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
108 ret = srd_session_destroy(sess2);
109 fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
110 ret = srd_session_destroy(sess3);
111 fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
118 * Check whether srd_session_destroy() works.
119 * If it returns != SRD_OK (or segfaults) this test will fail.
121 START_TEST(test_session_destroy)
124 struct srd_session *sess;
127 srd_session_new(&sess);
128 ret = srd_session_destroy(sess);
129 fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
135 * Check whether srd_session_destroy() fails for bogus sessions.
136 * If it returns SRD_OK (or segfaults) this test will fail.
138 START_TEST(test_session_destroy_bogus)
143 ret = srd_session_destroy(NULL);
144 fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
149 static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
153 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
154 fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
155 PRIu64 ") failed: %d.", sess, key, x, ret);
158 static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
162 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
163 fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
164 PRIu64 ") worked.", sess, key, x);
167 static void conf_check_fail_null(struct srd_session *sess, int key)
171 ret = srd_session_metadata_set(sess, key, NULL);
172 fail_unless(ret != SRD_OK,
173 "srd_session_metadata_set(NULL) for key %d worked.", key);
176 static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
180 ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
181 fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
182 "failed: %d.", key, ret);
186 * Check whether srd_session_metadata_set() works.
187 * If it returns != SRD_OK (or segfaults) this test will fail.
189 START_TEST(test_session_metadata_set)
192 struct srd_session *sess;
195 srd_session_new(&sess);
196 /* Try a bunch of values. */
197 for (i = 0; i < 1000; i++)
198 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
199 /* Try the max. possible value. */
200 conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
201 srd_session_destroy(sess);
207 * Check whether srd_session_metadata_set() fails with invalid input.
208 * If it returns SRD_OK (or segfaults) this test will fail.
210 START_TEST(test_session_metadata_set_bogus)
212 struct srd_session *sess;
215 srd_session_new(&sess);
217 /* Incorrect gvariant type (currently only uint64 is used). */
218 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
219 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
221 /* NULL data pointer. */
222 conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
225 conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
228 conf_check_fail(sess, -1, 0);
229 conf_check_fail(sess, 9, 0);
230 conf_check_fail(sess, 123, 0);
232 srd_session_destroy(sess);
237 Suite *suite_session(void)
242 s = suite_create("session");
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);
253 tc = tcase_create("config");
254 tcase_add_checked_fixture(tc, setup, teardown);
255 tcase_add_test(tc, test_session_metadata_set);
256 tcase_add_test(tc, test_session_metadata_set_bogus);
257 suite_add_tcase(s, tc);