2 * This file is part of the libsigrok 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
23 #include "../libsigrok.h"
27 * Check various basic init related things.
29 * - Check whether an sr_init() call with a proper sr_ctx works.
30 * If it returns != SR_OK (or segfaults) this test will fail.
31 * The sr_init() call (among other things) also runs sanity checks on
32 * all libsigrok hardware drivers and errors out upon issues.
34 * - Check whether a subsequent sr_exit() with that sr_ctx works.
35 * If it returns != SR_OK (or segfaults) this test will fail.
37 START_TEST(test_init_exit)
40 struct sr_context *sr_ctx;
42 ret = sr_init(&sr_ctx);
43 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
44 ret = sr_exit(sr_ctx);
45 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
50 * Check whether two nested sr_init() and sr_exit() calls work.
51 * The two functions have two different contexts.
52 * If any function returns != SR_OK (or segfaults) this test will fail.
54 START_TEST(test_init_exit_2)
57 struct sr_context *sr_ctx1, *sr_ctx2;
59 ret = sr_init(&sr_ctx1);
60 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
61 ret = sr_init(&sr_ctx2);
62 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
63 ret = sr_exit(sr_ctx2);
64 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
65 ret = sr_exit(sr_ctx1);
66 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
71 * Same as above, but sr_exit() in the "wrong" order.
72 * This should work fine, it's not a bug to do this.
74 START_TEST(test_init_exit_2_reverse)
77 struct sr_context *sr_ctx1, *sr_ctx2;
79 ret = sr_init(&sr_ctx1);
80 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
81 ret = sr_init(&sr_ctx2);
82 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
83 ret = sr_exit(sr_ctx1);
84 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
85 ret = sr_exit(sr_ctx2);
86 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
91 * Check whether three nested sr_init() and sr_exit() calls work.
92 * The three functions have three different contexts.
93 * If any function returns != SR_OK (or segfaults) this test will fail.
95 START_TEST(test_init_exit_3)
98 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
100 ret = sr_init(&sr_ctx1);
101 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
102 ret = sr_init(&sr_ctx2);
103 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
104 ret = sr_init(&sr_ctx3);
105 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
106 ret = sr_exit(sr_ctx3);
107 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
108 ret = sr_exit(sr_ctx2);
109 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
110 ret = sr_exit(sr_ctx1);
111 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
116 * Same as above, but sr_exit() in the "wrong" order.
117 * This should work fine, it's not a bug to do this.
119 START_TEST(test_init_exit_3_reverse)
122 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
124 ret = sr_init(&sr_ctx1);
125 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
126 ret = sr_init(&sr_ctx2);
127 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
128 ret = sr_init(&sr_ctx3);
129 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
130 ret = sr_exit(sr_ctx1);
131 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
132 ret = sr_exit(sr_ctx2);
133 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
134 ret = sr_exit(sr_ctx3);
135 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
139 /* Check whether sr_init(NULL) fails as it should. */
140 START_TEST(test_init_null)
144 ret = sr_log_loglevel_set(SR_LOG_NONE);
145 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
148 fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
152 /* Check whether sr_exit(NULL) fails as it should. */
153 START_TEST(test_exit_null)
157 ret = sr_log_loglevel_set(SR_LOG_NONE);
158 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
161 fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
165 Suite *suite_core(void)
170 s = suite_create("core");
172 tc = tcase_create("init_exit");
173 tcase_add_test(tc, test_init_exit);
174 tcase_add_test(tc, test_init_exit_2);
175 tcase_add_test(tc, test_init_exit_2_reverse);
176 tcase_add_test(tc, test_init_exit_3);
177 tcase_add_test(tc, test_init_exit_3_reverse);
178 tcase_add_test(tc, test_init_null);
179 tcase_add_test(tc, test_exit_null);
180 suite_add_tcase(s, tc);