]> sigrok.org Git - libsigrok.git/blob - tests/check_session.c
e48e1d468af08f186a5966c9bebd9652c868e96b
[libsigrok.git] / tests / check_session.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 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 "../include/libsigrok/libsigrok.h"
24 #include "lib.h"
25
26 static struct sr_context *sr_ctx;
27
28 static void setup(void)
29 {
30         int ret;
31
32         ret = sr_init(&sr_ctx);
33         fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
34 }
35
36 static void teardown(void)
37 {
38         int ret;
39
40         ret = sr_exit(sr_ctx);
41         fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
42 }
43
44 /*
45  * Check whether sr_session_new() works.
46  * If it returns != SR_OK (or segfaults) this test will fail.
47  */
48 START_TEST(test_session_new)
49 {
50         int ret;
51         struct sr_session *sess;
52
53         ret = sr_session_new(&sess);
54         fail_unless(ret == SR_OK, "sr_session_new() failed: %d.", ret);
55         sr_session_destroy(sess);
56 }
57 END_TEST
58
59 /*
60  * Check whether sr_session_new() fails for bogus parameters.
61  * If it returns SR_OK (or segfaults) this test will fail.
62  */
63 START_TEST(test_session_new_bogus)
64 {
65         int ret;
66
67         ret = sr_session_new(NULL);
68         fail_unless(ret != SR_OK, "sr_session_new(NULL) worked.");
69 }
70 END_TEST
71
72 /*
73  * Check whether multiple sr_session_new() calls work.
74  * If any call returns != SR_OK (or segfaults) this test will fail.
75  */
76 START_TEST(test_session_new_multiple)
77 {
78         int ret;
79         struct sr_session *sess1, *sess2, *sess3;
80
81         sess1 = sess2 = sess3 = NULL;
82
83         /* Multiple sr_session_new() calls must work. */
84         ret = sr_session_new(&sess1);
85         fail_unless(ret == SR_OK, "sr_session_new() 1 failed: %d.", ret);
86         ret = sr_session_new(&sess2);
87         fail_unless(ret == SR_OK, "sr_session_new() 2 failed: %d.", ret);
88         ret = sr_session_new(&sess3);
89         fail_unless(ret == SR_OK, "sr_session_new() 3 failed: %d.", ret);
90
91         /* The returned session pointers must all be non-NULL. */
92         fail_unless(sess1 != NULL);
93         fail_unless(sess2 != NULL);
94         fail_unless(sess3 != NULL);
95
96         /* The returned session pointers must not be the same. */
97         fail_unless(sess1 != sess2);
98         fail_unless(sess1 != sess3);
99         fail_unless(sess2 != sess3);
100
101         /* Destroying any of the sessions must work. */
102         ret = sr_session_destroy(sess1);
103         fail_unless(ret == SR_OK, "sr_session_destroy() 1 failed: %d.", ret);
104         ret = sr_session_destroy(sess2);
105         fail_unless(ret == SR_OK, "sr_session_destroy() 2 failed: %d.", ret);
106         ret = sr_session_destroy(sess3);
107         fail_unless(ret == SR_OK, "sr_session_destroy() 3 failed: %d.", ret);
108 }
109 END_TEST
110
111 /*
112  * Check whether sr_session_destroy() works.
113  * If it returns != SR_OK (or segfaults) this test will fail.
114  */
115 START_TEST(test_session_destroy)
116 {
117         int ret;
118         struct sr_session *sess;
119
120         sr_session_new(&sess);
121         ret = sr_session_destroy(sess);
122         fail_unless(ret == SR_OK, "sr_session_destroy() failed: %d.", ret);
123 }
124 END_TEST
125
126 /*
127  * Check whether sr_session_destroy() fails for bogus sessions.
128  * If it returns SR_OK (or segfaults) this test will fail.
129  */
130 START_TEST(test_session_destroy_bogus)
131 {
132         int ret;
133
134         ret = sr_session_destroy(NULL);
135         fail_unless(ret != SR_OK, "sr_session_destroy() worked.");
136 }
137 END_TEST
138
139 Suite *suite_session(void)
140 {
141         Suite *s;
142         TCase *tc;
143
144         s = suite_create("session");
145
146         tc = tcase_create("new_destroy");
147         tcase_add_checked_fixture(tc, setup, teardown);
148         tcase_add_test(tc, test_session_new);
149         tcase_add_test(tc, test_session_new_bogus);
150         tcase_add_test(tc, test_session_new_multiple);
151         tcase_add_test(tc, test_session_destroy);
152         tcase_add_test(tc, test_session_destroy_bogus);
153         suite_add_tcase(s, tc);
154
155         return s;
156 }