Line data Source code
1 : /***************************************************************************//**
2 :
3 : @file bitfieldtest.c
4 :
5 : @author Stephen Brennan
6 :
7 : @date Created Wednesday, 5 February 2014
8 :
9 : @brief Test for the libstephen bitfield.
10 :
11 : @copyright Copyright (c) 2013-2015, Stephen Brennan. Released under the
12 : Revised BSD License. See the LICENSE.txt file for details.
13 :
14 : *******************************************************************************/
15 :
16 : #include "libstephen/bf.h"
17 : #include "libstephen/ut.h"
18 : #include "tests.h"
19 :
20 : #define test_bools 80
21 :
22 : ////////////////////////////////////////////////////////////////////////////////
23 : // TESTS
24 :
25 1 : int bf_test_init()
26 : {
27 : unsigned char field[SMB_BITFIELD_SIZE(test_bools)];
28 : int i;
29 1 : bf_init(field, test_bools);
30 :
31 11 : for (i = 0; i < SMB_BITFIELD_SIZE(test_bools); i++) {
32 10 : TEST_ASSERT(field[i] == 0);
33 : }
34 :
35 1 : return 0;
36 : }
37 :
38 1 : int bf_test_memory()
39 : {
40 : unsigned char *field;
41 1 : field = bf_create(test_bools);
42 1 : bf_delete(field, test_bools);
43 1 : return 0; // looking for memory leaks here
44 : }
45 :
46 1 : int bf_test_check()
47 : {
48 1 : unsigned char field[2] = {0x00, 0xFF};
49 : int i;
50 :
51 : // don't want to init...that ruins our test variables
52 : //bf_init(field, 16);
53 :
54 9 : for (i = 0; i < 8; i++) {
55 8 : TEST_ASSERT(!bf_check(field, i));
56 : }
57 :
58 9 : for (i = 8; i < 16; i++) {
59 8 : TEST_ASSERT(bf_check(field, i));
60 : }
61 :
62 1 : return 0;
63 : }
64 :
65 1 : int bf_test_set()
66 : {
67 : unsigned char field[SMB_BITFIELD_SIZE(test_bools)];
68 : int i;
69 :
70 1 : bf_init(field, test_bools);
71 41 : for (i = 0; i < test_bools; i += 2) {
72 40 : TEST_ASSERT(!bf_check(field, i));
73 40 : bf_set(field, i);
74 40 : TEST_ASSERT(bf_check(field, i));
75 : }
76 :
77 81 : for (i = 0; i < test_bools; i++) {
78 80 : if (i % 2 == 0) {// even
79 40 : TEST_ASSERT(bf_check(field, i));
80 : } else {
81 40 : TEST_ASSERT(!bf_check(field, i));
82 : }
83 : }
84 :
85 1 : return 0;
86 : }
87 :
88 1 : int bf_test_clear()
89 : {
90 : unsigned char field[SMB_BITFIELD_SIZE(test_bools)];
91 : int i;
92 :
93 1 : bf_init(field, test_bools);
94 81 : for (i = 0; i < test_bools; i++) {
95 80 : bf_set(field, i); // this is already tested
96 : }
97 :
98 41 : for (i = 0; i < test_bools; i += 2) {
99 40 : TEST_ASSERT(bf_check(field, i));
100 40 : bf_clear(field, i);
101 40 : TEST_ASSERT(!bf_check(field, i));
102 : }
103 :
104 81 : for (i = 0; i < test_bools; i++) {
105 80 : if (i % 2 == 0) {// even
106 40 : TEST_ASSERT(!bf_check(field, i));
107 : } else {
108 40 : TEST_ASSERT(bf_check(field, i));
109 : }
110 : }
111 :
112 1 : return 0;
113 : }
114 :
115 1 : int bf_test_flip()
116 : {
117 : unsigned char field[SMB_BITFIELD_SIZE(test_bools)];
118 : int i;
119 :
120 1 : bf_init(field, test_bools);
121 : // Set all the even numbered fields
122 41 : for (i = 0; i < test_bools; i += 2) {
123 40 : bf_set(field, i); //tested
124 : }
125 :
126 : // Flip them all
127 81 : for (i = 0; i < test_bools; i++) {
128 80 : bf_flip(field, i);
129 : }
130 :
131 81 : for (i = 0; i < test_bools; i++) {
132 80 : if (i % 2 == 0) {// even
133 40 : TEST_ASSERT(!bf_check(field, i));
134 : } else {
135 40 : TEST_ASSERT(bf_check(field, i));
136 : }
137 : }
138 :
139 1 : return 0;
140 : }
141 :
142 : ////////////////////////////////////////////////////////////////////////////////
143 : // TEST LOADER AND RUNNER
144 :
145 1 : void bit_field_test() {
146 1 : smb_ut_group *group = su_create_test_group("bit field");
147 :
148 1 : smb_ut_test *init = su_create_test("init", bf_test_init);
149 1 : su_add_test(group, init);
150 :
151 1 : smb_ut_test *memory = su_create_test("memory", bf_test_memory);
152 1 : su_add_test(group, memory);
153 :
154 1 : smb_ut_test *check = su_create_test("check", bf_test_check);
155 1 : su_add_test(group, check);
156 :
157 1 : smb_ut_test *set = su_create_test("set", bf_test_set);
158 1 : su_add_test(group, set);
159 :
160 1 : smb_ut_test *clear = su_create_test("clear", bf_test_clear);
161 1 : su_add_test(group, clear);
162 :
163 1 : smb_ut_test *flip = su_create_test("flip", bf_test_flip);
164 1 : su_add_test(group, flip);
165 :
166 1 : su_run_group(group);
167 1 : su_delete_group(group);
168 1 : }
|