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"
26 * Check various basic init related things.
28 * - Check whether an sr_init() call with a proper sr_ctx works.
29 * If it returns != SR_OK (or segfaults) this test will fail.
30 * The sr_init() call (among other things) also runs sanity checks on
31 * all libsigrok hardware drivers and errors out upon issues.
33 * - Check whether a subsequent sr_exit() with that sr_ctx works.
34 * If it returns != SR_OK (or segfaults) this test will fail.
36 START_TEST(test_init_exit)
39 struct sr_context *sr_ctx;
41 ret = sr_init(&sr_ctx);
42 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
43 ret = sr_exit(sr_ctx);
44 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
49 * Check whether two nested sr_init() and sr_exit() calls work.
50 * The two functions have two different contexts.
51 * If any function returns != SR_OK (or segfaults) this test will fail.
53 START_TEST(test_init_exit_2)
56 struct sr_context *sr_ctx1, *sr_ctx2;
58 ret = sr_init(&sr_ctx1);
59 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
60 ret = sr_init(&sr_ctx2);
61 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
62 ret = sr_exit(sr_ctx2);
63 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
64 ret = sr_exit(sr_ctx1);
65 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
70 * Same as above, but sr_exit() in the "wrong" order.
71 * This should work fine, it's not a bug to do this.
73 START_TEST(test_init_exit_2_reverse)
76 struct sr_context *sr_ctx1, *sr_ctx2;
78 ret = sr_init(&sr_ctx1);
79 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
80 ret = sr_init(&sr_ctx2);
81 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
82 ret = sr_exit(sr_ctx1);
83 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
84 ret = sr_exit(sr_ctx2);
85 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
90 * Check whether three nested sr_init() and sr_exit() calls work.
91 * The three functions have three different contexts.
92 * If any function returns != SR_OK (or segfaults) this test will fail.
94 START_TEST(test_init_exit_3)
97 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
99 ret = sr_init(&sr_ctx1);
100 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
101 ret = sr_init(&sr_ctx2);
102 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
103 ret = sr_init(&sr_ctx3);
104 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
105 ret = sr_exit(sr_ctx3);
106 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
107 ret = sr_exit(sr_ctx2);
108 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
109 ret = sr_exit(sr_ctx1);
110 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
115 * Same as above, but sr_exit() in the "wrong" order.
116 * This should work fine, it's not a bug to do this.
118 START_TEST(test_init_exit_3_reverse)
121 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
123 ret = sr_init(&sr_ctx1);
124 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
125 ret = sr_init(&sr_ctx2);
126 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
127 ret = sr_init(&sr_ctx3);
128 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
129 ret = sr_exit(sr_ctx1);
130 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
131 ret = sr_exit(sr_ctx2);
132 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
133 ret = sr_exit(sr_ctx3);
134 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
138 /* Check whether sr_init(NULL) fails as it should. */
139 START_TEST(test_init_null)
143 ret = sr_log_loglevel_set(SR_LOG_NONE);
144 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
147 fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
151 /* Check whether sr_exit(NULL) fails as it should. */
152 START_TEST(test_exit_null)
156 ret = sr_log_loglevel_set(SR_LOG_NONE);
157 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
160 fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
164 Suite *suite_core(void)
169 s = suite_create("core");
171 tc = tcase_create("init_exit");
172 tcase_add_test(tc, test_init_exit);
173 tcase_add_test(tc, test_init_exit_2);
174 tcase_add_test(tc, test_init_exit_2_reverse);
175 tcase_add_test(tc, test_init_exit_3);
176 tcase_add_test(tc, test_init_exit_3_reverse);
177 tcase_add_test(tc, test_init_null);
178 tcase_add_test(tc, test_exit_null);
179 suite_add_tcase(s, tc);