forked from sigrokproject/libsigrok
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.c
183 lines (162 loc) · 5.26 KB
/
core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <check.h>
#include <libsigrok/libsigrok.h>
#include "lib.h"
/*
* Check various basic init related things.
*
* - Check whether an sr_init() call with a proper sr_ctx works.
* If it returns != SR_OK (or segfaults) this test will fail.
* The sr_init() call (among other things) also runs sanity checks on
* all libsigrok hardware drivers and errors out upon issues.
*
* - Check whether a subsequent sr_exit() with that sr_ctx works.
* If it returns != SR_OK (or segfaults) this test will fail.
*/
START_TEST(test_init_exit)
{
int ret;
struct sr_context *sr_ctx;
ret = sr_init(&sr_ctx);
fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
ret = sr_exit(sr_ctx);
fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
}
END_TEST
/*
* Check whether two nested sr_init() and sr_exit() calls work.
* The two functions have two different contexts.
* If any function returns != SR_OK (or segfaults) this test will fail.
*/
START_TEST(test_init_exit_2)
{
int ret;
struct sr_context *sr_ctx1, *sr_ctx2;
ret = sr_init(&sr_ctx1);
fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
ret = sr_init(&sr_ctx2);
fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
ret = sr_exit(sr_ctx2);
fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
ret = sr_exit(sr_ctx1);
fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
}
END_TEST
/*
* Same as above, but sr_exit() in the "wrong" order.
* This should work fine, it's not a bug to do this.
*/
START_TEST(test_init_exit_2_reverse)
{
int ret;
struct sr_context *sr_ctx1, *sr_ctx2;
ret = sr_init(&sr_ctx1);
fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
ret = sr_init(&sr_ctx2);
fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
ret = sr_exit(sr_ctx1);
fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
ret = sr_exit(sr_ctx2);
fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
}
END_TEST
/*
* Check whether three nested sr_init() and sr_exit() calls work.
* The three functions have three different contexts.
* If any function returns != SR_OK (or segfaults) this test will fail.
*/
START_TEST(test_init_exit_3)
{
int ret;
struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
ret = sr_init(&sr_ctx1);
fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
ret = sr_init(&sr_ctx2);
fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
ret = sr_init(&sr_ctx3);
fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
ret = sr_exit(sr_ctx3);
fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
ret = sr_exit(sr_ctx2);
fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
ret = sr_exit(sr_ctx1);
fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
}
END_TEST
/*
* Same as above, but sr_exit() in the "wrong" order.
* This should work fine, it's not a bug to do this.
*/
START_TEST(test_init_exit_3_reverse)
{
int ret;
struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
ret = sr_init(&sr_ctx1);
fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
ret = sr_init(&sr_ctx2);
fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
ret = sr_init(&sr_ctx3);
fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
ret = sr_exit(sr_ctx1);
fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
ret = sr_exit(sr_ctx2);
fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
ret = sr_exit(sr_ctx3);
fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
}
END_TEST
/* Check whether sr_init(NULL) fails as it should. */
START_TEST(test_init_null)
{
int ret;
ret = sr_log_loglevel_set(SR_LOG_NONE);
fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
ret = sr_init(NULL);
fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
}
END_TEST
/* Check whether sr_exit(NULL) fails as it should. */
START_TEST(test_exit_null)
{
int ret;
ret = sr_log_loglevel_set(SR_LOG_NONE);
fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
ret = sr_exit(NULL);
fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
}
END_TEST
Suite *suite_core(void)
{
Suite *s;
TCase *tc;
s = suite_create("core");
tc = tcase_create("init_exit");
tcase_add_test(tc, test_init_exit);
tcase_add_test(tc, test_init_exit_2);
tcase_add_test(tc, test_init_exit_2_reverse);
tcase_add_test(tc, test_init_exit_3);
tcase_add_test(tc, test_init_exit_3_reverse);
tcase_add_test(tc, test_init_null);
tcase_add_test(tc, test_exit_null);
suite_add_tcase(s, tc);
return s;
}