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
22 #include <libsigrokdecode-internal.h> /* First, to avoid compiler warning. */
23 #include <libsigrokdecode.h>
30 * Check whether srd_session_new() works.
31 * If it returns != SRD_OK (or segfaults) this test will fail.
33 START_TEST(test_session_new)
36 struct srd_session *sess;
39 ret = srd_session_new(&sess);
40 fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
46 * Check whether srd_session_new() fails for bogus parameters.
47 * If it returns SRD_OK (or segfaults) this test will fail.
49 START_TEST(test_session_new_bogus)
54 ret = srd_session_new(NULL);
55 fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
61 * Check whether multiple srd_session_new() calls work.
62 * If any call returns != SRD_OK (or segfaults) this test will fail.
64 START_TEST(test_session_new_multiple)
67 struct srd_session *sess1, *sess2, *sess3;
69 sess1 = sess2 = sess3 = NULL;
73 /* Multiple srd_session_new() calls must work. */
74 ret = srd_session_new(&sess1);
75 fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
76 ret = srd_session_new(&sess2);
77 fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
78 ret = srd_session_new(&sess3);
79 fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
81 /* The returned session pointers must all be non-NULL. */
82 fail_unless(sess1 != NULL);
83 fail_unless(sess2 != NULL);
84 fail_unless(sess3 != NULL);
86 /* The returned session pointers must not be the same. */
87 fail_unless(sess1 != sess2);
88 fail_unless(sess1 != sess3);
89 fail_unless(sess2 != sess3);
91 /* Each session must have another ID than any other session. */
92 fail_unless(sess1->session_id != sess2->session_id);
93 fail_unless(sess1->session_id != sess3->session_id);
94 fail_unless(sess2->session_id != sess3->session_id);
96 /* Destroying any of the sessions must work. */
97 ret = srd_session_destroy(sess1);
98 fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
99 ret = srd_session_destroy(sess2);
100 fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
101 ret = srd_session_destroy(sess3);
102 fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
109 * Check whether srd_session_destroy() works.
110 * If it returns != SRD_OK (or segfaults) this test will fail.
112 START_TEST(test_session_destroy)
115 struct srd_session *sess;
118 srd_session_new(&sess);
119 ret = srd_session_destroy(sess);
120 fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
126 * Check whether srd_session_destroy() fails for bogus sessions.
127 * If it returns SRD_OK (or segfaults) this test will fail.
129 START_TEST(test_session_destroy_bogus)
134 ret = srd_session_destroy(NULL);
135 fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
140 static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
144 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
145 fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
146 PRIu64 ") failed: %d.", sess, key, x, ret);
149 static void conf_check_fail(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 ") worked.", sess, key, x);
158 static void conf_check_fail_null(struct srd_session *sess, int key)
162 ret = srd_session_metadata_set(sess, key, NULL);
163 fail_unless(ret != SRD_OK,
164 "srd_session_metadata_set(NULL) for key %d worked.", key);
167 static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
171 ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
172 fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
173 "failed: %d.", key, ret);
177 * Check whether srd_session_metadata_set() works.
178 * If it returns != SRD_OK (or segfaults) this test will fail.
180 START_TEST(test_session_metadata_set)
183 struct srd_session *sess;
186 srd_session_new(&sess);
187 /* Try a bunch of values. */
188 for (i = 0; i < 1000; i++)
189 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
190 /* Try the max. possible value. */
191 conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
192 srd_session_destroy(sess);
198 * Check whether srd_session_metadata_set() fails with invalid input.
199 * If it returns SRD_OK (or segfaults) this test will fail.
201 START_TEST(test_session_metadata_set_bogus)
203 struct srd_session *sess;
206 srd_session_new(&sess);
208 /* Incorrect GVariant type (currently only uint64 is used). */
209 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
210 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
212 /* NULL data pointer. */
213 conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
216 conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
219 conf_check_fail(sess, -1, 0);
220 conf_check_fail(sess, 9, 0);
221 conf_check_fail(sess, 123, 0);
223 srd_session_destroy(sess);
228 Suite *suite_session(void)
233 s = suite_create("session");
235 tc = tcase_create("new_destroy");
236 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
237 tcase_add_test(tc, test_session_new);
238 tcase_add_test(tc, test_session_new_bogus);
239 tcase_add_test(tc, test_session_new_multiple);
240 tcase_add_test(tc, test_session_destroy);
241 tcase_add_test(tc, test_session_destroy_bogus);
242 suite_add_tcase(s, tc);
244 tc = tcase_create("config");
245 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
246 tcase_add_test(tc, test_session_metadata_set);
247 tcase_add_test(tc, test_session_metadata_set_bogus);
248 suite_add_tcase(s, tc);