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