]> sigrok.org Git - libsigrok.git/blame - tests/core.c
Build: Include <config.h> first in all source files
[libsigrok.git] / tests / core.c
CommitLineData
79bb0e97
UH
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
6ec6c43b 21#include <config.h>
79bb0e97
UH
22#include <stdlib.h>
23#include <check.h>
4960aeb0 24#include <libsigrok/libsigrok.h>
17794067 25#include "lib.h"
79bb0e97
UH
26
27/*
28 * Check various basic init related things.
29 *
30 * - Check whether an sr_init() call with a proper sr_ctx works.
31 * If it returns != SR_OK (or segfaults) this test will fail.
32 * The sr_init() call (among other things) also runs sanity checks on
33 * all libsigrok hardware drivers and errors out upon issues.
34 *
35 * - Check whether a subsequent sr_exit() with that sr_ctx works.
36 * If it returns != SR_OK (or segfaults) this test will fail.
37 */
38START_TEST(test_init_exit)
39{
40 int ret;
41 struct sr_context *sr_ctx;
42
43 ret = sr_init(&sr_ctx);
44 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
45 ret = sr_exit(sr_ctx);
46 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
47}
48END_TEST
49
50/*
51 * Check whether two nested sr_init() and sr_exit() calls work.
52 * The two functions have two different contexts.
53 * If any function returns != SR_OK (or segfaults) this test will fail.
54 */
55START_TEST(test_init_exit_2)
56{
57 int ret;
58 struct sr_context *sr_ctx1, *sr_ctx2;
59
60 ret = sr_init(&sr_ctx1);
61 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
62 ret = sr_init(&sr_ctx2);
63 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
64 ret = sr_exit(sr_ctx2);
65 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
66 ret = sr_exit(sr_ctx1);
67 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
68}
69END_TEST
70
71/*
72 * Same as above, but sr_exit() in the "wrong" order.
73 * This should work fine, it's not a bug to do this.
74 */
75START_TEST(test_init_exit_2_reverse)
76{
77 int ret;
78 struct sr_context *sr_ctx1, *sr_ctx2;
79
80 ret = sr_init(&sr_ctx1);
81 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
82 ret = sr_init(&sr_ctx2);
83 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
84 ret = sr_exit(sr_ctx1);
85 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
86 ret = sr_exit(sr_ctx2);
87 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
88}
89END_TEST
90
91/*
92 * Check whether three nested sr_init() and sr_exit() calls work.
93 * The three functions have three different contexts.
94 * If any function returns != SR_OK (or segfaults) this test will fail.
95 */
96START_TEST(test_init_exit_3)
97{
98 int ret;
99 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
100
101 ret = sr_init(&sr_ctx1);
102 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
103 ret = sr_init(&sr_ctx2);
104 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
105 ret = sr_init(&sr_ctx3);
106 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
107 ret = sr_exit(sr_ctx3);
108 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
109 ret = sr_exit(sr_ctx2);
110 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
111 ret = sr_exit(sr_ctx1);
112 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
113}
114END_TEST
115
116/*
117 * Same as above, but sr_exit() in the "wrong" order.
118 * This should work fine, it's not a bug to do this.
119 */
120START_TEST(test_init_exit_3_reverse)
121{
122 int ret;
123 struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
124
125 ret = sr_init(&sr_ctx1);
126 fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
127 ret = sr_init(&sr_ctx2);
128 fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
129 ret = sr_init(&sr_ctx3);
130 fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
131 ret = sr_exit(sr_ctx1);
132 fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
133 ret = sr_exit(sr_ctx2);
134 fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
135 ret = sr_exit(sr_ctx3);
136 fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
137}
138END_TEST
139
140/* Check whether sr_init(NULL) fails as it should. */
141START_TEST(test_init_null)
142{
143 int ret;
144
145 ret = sr_log_loglevel_set(SR_LOG_NONE);
146 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
147
148 ret = sr_init(NULL);
149 fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
150}
151END_TEST
152
153/* Check whether sr_exit(NULL) fails as it should. */
154START_TEST(test_exit_null)
155{
156 int ret;
157
158 ret = sr_log_loglevel_set(SR_LOG_NONE);
159 fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
160
161 ret = sr_exit(NULL);
162 fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
163}
164END_TEST
165
166Suite *suite_core(void)
167{
168 Suite *s;
169 TCase *tc;
170
171 s = suite_create("core");
172
173 tc = tcase_create("init_exit");
174 tcase_add_test(tc, test_init_exit);
175 tcase_add_test(tc, test_init_exit_2);
176 tcase_add_test(tc, test_init_exit_2_reverse);
177 tcase_add_test(tc, test_init_exit_3);
178 tcase_add_test(tc, test_init_exit_3_reverse);
179 tcase_add_test(tc, test_init_null);
180 tcase_add_test(tc, test_exit_null);
181 suite_add_tcase(s, tc);
182
183 return s;
184}