]> sigrok.org Git - libsigrok.git/blob - tests/check_core.c
8bc1d5aeb2c68abc94f6d49d6ecae282395dc0b7
[libsigrok.git] / tests / check_core.c
1 /*
2  * This file is part of the libsigrok 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 <stdlib.h>
22 #include <check.h>
23 #include "../libsigrok.h"
24
25 /*
26  * Check various basic init related things.
27  *
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.
32  *
33  *  - Check whether a subsequent sr_exit() with that sr_ctx works.
34  *    If it returns != SR_OK (or segfaults) this test will fail.
35  */
36 START_TEST(test_init_exit)
37 {
38         int ret;
39         struct sr_context *sr_ctx;
40
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);
45 }
46 END_TEST
47
48 /*
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.
52  */
53 START_TEST(test_init_exit_2)
54 {
55         int ret;
56         struct sr_context *sr_ctx1, *sr_ctx2;
57
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);
66 }
67 END_TEST
68
69 /*
70  * Same as above, but sr_exit() in the "wrong" order.
71  * This should work fine, it's not a bug to do this.
72  */
73 START_TEST(test_init_exit_2_reverse)
74 {
75         int ret;
76         struct sr_context *sr_ctx1, *sr_ctx2;
77
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);
86 }
87 END_TEST
88
89 /*
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.
93  */
94 START_TEST(test_init_exit_3)
95 {
96         int ret;
97         struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
98
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);
111 }
112 END_TEST
113
114 /*
115  * Same as above, but sr_exit() in the "wrong" order.
116  * This should work fine, it's not a bug to do this.
117  */
118 START_TEST(test_init_exit_3_reverse)
119 {
120         int ret;
121         struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
122
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);
135 }
136 END_TEST
137
138 /* Check whether sr_init(NULL) fails as it should. */
139 START_TEST(test_init_null)
140 {
141         int ret;
142
143         ret = sr_log_loglevel_set(SR_LOG_NONE);
144         fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
145
146         ret = sr_init(NULL);
147         fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
148 }
149 END_TEST
150
151 /* Check whether sr_exit(NULL) fails as it should. */
152 START_TEST(test_exit_null)
153 {
154         int ret;
155
156         ret = sr_log_loglevel_set(SR_LOG_NONE);
157         fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
158
159         ret = sr_exit(NULL);
160         fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
161 }
162 END_TEST
163
164 Suite *suite_core(void)
165 {
166         Suite *s;
167         TCase *tc;
168
169         s = suite_create("core");
170
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);
180
181         return s;
182 }