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