1 /* $NetBSD: t_bitstring.c,v 1.4 2012/03/25 06:54:04 joerg Exp $ */
4 * Copyright (c) 1993, 2008, 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 #include <bitstring.h>
37 clearbits(bitstr_t
*b
, int n
)
39 int i
= bitstr_size(n
);
46 printbits(FILE *file
, bitstr_t
*b
, int n
)
54 (void) fprintf(file
, "%3d %3d ", jc
, js
);
56 for (i
=0; i
< n
; i
++) {
57 (void) fprintf(file
, "%c", (bit_test(b
, i
) ? '1' : '0'));
60 (void) fprintf(file
, "%c", '\n');
64 calculate_data(FILE *file
, const int test_length
)
69 assert(test_length
>= 4);
71 (void) fprintf(file
, "Testing with TEST_LENGTH = %d\n\n", test_length
);
73 (void) fprintf(file
, "test _bit_byte, _bit_mask, and bitstr_size\n");
74 (void) fprintf(file
, " i _bit_byte(i) _bit_mask(i) bitstr_size(i)\n");
76 for (i
=0; i
< test_length
; i
++) {
77 (void) fprintf(file
, "%3d%15u%15u%15zu\n",
78 i
, _bit_byte(i
), _bit_mask(i
), bitstr_size(i
));
81 bs
= bit_alloc(test_length
);
82 clearbits(bs
, test_length
);
83 (void) fprintf(file
, "\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n");
84 (void) fprintf(file
, "be: 0 -1 ");
85 for (i
=0; i
< test_length
; i
++)
86 (void) fprintf(file
, "%c", '0');
87 (void) fprintf(file
, "\nis: ");
88 printbits(file
, bs
, test_length
);
90 (void) fprintf(file
, "\ntest bit_set\n");
91 for (i
=0; i
< test_length
; i
+=3)
93 (void) fprintf(file
, "be: 1 0 ");
94 for (i
=0; i
< test_length
; i
++)
95 (void) fprintf(file
, "%c", "100"[i
% 3]);
96 (void) fprintf(file
, "\nis: ");
97 printbits(file
, bs
, test_length
);
99 (void) fprintf(file
, "\ntest bit_clear\n");
100 for (i
=0; i
< test_length
; i
+=6)
102 (void) fprintf(file
, "be: 0 3 ");
103 for (i
=0; i
< test_length
; i
++)
104 (void) fprintf(file
, "%c", "000100"[i
% 6]);
105 (void) fprintf(file
, "\nis: ");
106 printbits(file
, bs
, test_length
);
108 (void) fprintf(file
, "\ntest bit_test using previous bitstring\n");
109 (void) fprintf(file
, " i bit_test(i)\n");
110 for (i
=0; i
< test_length
; i
++)
111 (void) fprintf(file
, "%3d%15d\n", i
, bit_test(bs
, i
));
113 clearbits(bs
, test_length
);
114 (void) fprintf(file
, "\ntest clearbits\n");
115 (void) fprintf(file
, "be: 0 -1 ");
116 for (i
=0; i
< test_length
; i
++)
117 (void) fprintf(file
, "%c", '0');
118 (void) fprintf(file
, "\nis: ");
119 printbits(file
, bs
, test_length
);
121 (void) fprintf(file
, "\ntest bit_nset and bit_nclear\n");
122 bit_nset(bs
, 1, test_length
- 2);
123 (void) fprintf(file
, "be: 0 1 0");
124 for (i
=0; i
< test_length
- 2; i
++)
125 (void) fprintf(file
, "%c", '1');
126 (void) fprintf(file
, "0\nis: ");
127 printbits(file
, bs
, test_length
);
129 bit_nclear(bs
, 2, test_length
- 3);
130 (void) fprintf(file
, "be: 0 1 01");
131 for (i
=0; i
< test_length
- 4; i
++)
132 (void) fprintf(file
, "%c", '0');
133 (void) fprintf(file
, "10\nis: ");
134 printbits(file
, bs
, test_length
);
136 bit_nclear(bs
, 0, test_length
- 1);
137 (void) fprintf(file
, "be: 0 -1 ");
138 for (i
=0; i
< test_length
; i
++)
139 (void) fprintf(file
, "%c", '0');
140 (void) fprintf(file
, "\nis: ");
141 printbits(file
, bs
, test_length
);
142 bit_nset(bs
, 0, test_length
- 2);
143 (void) fprintf(file
, "be: %3d 0 ",test_length
- 1);
144 for (i
=0; i
< test_length
- 1; i
++)
145 (void) fprintf(file
, "%c", '1');
146 fprintf(file
, "%c", '0');
147 (void) fprintf(file
, "\nis: ");
148 printbits(file
, bs
, test_length
);
149 bit_nclear(bs
, 0, test_length
- 1);
150 (void) fprintf(file
, "be: 0 -1 ");
151 for (i
=0; i
< test_length
; i
++)
152 (void) fprintf(file
, "%c", '0');
153 (void) fprintf(file
, "\nis: ");
154 printbits(file
, bs
, test_length
);
156 (void) fprintf(file
, "\n");
157 (void) fprintf(file
, "first 1 bit should move right 1 position each line\n");
158 for (i
=0; i
< test_length
; i
++) {
159 bit_nclear(bs
, 0, test_length
- 1);
160 bit_nset(bs
, i
, test_length
- 1);
161 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
164 (void) fprintf(file
, "\n");
165 (void) fprintf(file
, "first 0 bit should move right 1 position each line\n");
166 for (i
=0; i
< test_length
; i
++) {
167 bit_nset(bs
, 0, test_length
- 1);
168 bit_nclear(bs
, i
, test_length
- 1);
169 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
172 (void) fprintf(file
, "\n");
173 (void) fprintf(file
, "first 0 bit should move left 1 position each line\n");
174 for (i
=0; i
< test_length
; i
++) {
175 bit_nclear(bs
, 0, test_length
- 1);
176 bit_nset(bs
, 0, test_length
- 1 - i
);
177 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
180 (void) fprintf(file
, "\n");
181 (void) fprintf(file
, "first 1 bit should move left 1 position each line\n");
182 for (i
=0; i
< test_length
; i
++) {
183 bit_nset(bs
, 0, test_length
- 1);
184 bit_nclear(bs
, 0, test_length
- 1 - i
);
185 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
188 (void) fprintf(file
, "\n");
189 (void) fprintf(file
, "0 bit should move right 1 position each line\n");
190 for (i
=0; i
< test_length
; i
++) {
191 bit_nset(bs
, 0, test_length
- 1);
192 bit_nclear(bs
, i
, i
);
193 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
196 (void) fprintf(file
, "\n");
197 (void) fprintf(file
, "1 bit should move right 1 position each line\n");
198 for (i
=0; i
< test_length
; i
++) {
199 bit_nclear(bs
, 0, test_length
- 1);
201 (void) fprintf(file
, "%3d ", i
); printbits(file
, bs
, test_length
);
208 one_check(const atf_tc_t
*tc
, const int test_length
)
213 ATF_REQUIRE((out
= fopen("out", "w")) != NULL
);
214 calculate_data(out
, test_length
);
217 /* XXX The following is a huge hack that was added to simplify the
218 * conversion of these tests from src/regress/ to src/tests/. The
219 * tests in this file should be checking their own results, without
220 * having to resort to external data files. */
221 snprintf(command
, sizeof(command
), "diff -u %s/d_bitstring_%d.out out",
222 atf_tc_get_config_var(tc
, "srcdir"), test_length
);
223 if (system(command
) != EXIT_SUCCESS
)
224 atf_tc_fail("Test failed; see output for details");
228 ATF_TC_HEAD(bits_8
, tc
)
230 atf_tc_set_md_var(tc
, "descr", "Checks 8-bit long bitstrings");
232 ATF_TC_BODY(bits_8
, tc
)
238 ATF_TC_HEAD(bits_27
, tc
)
240 atf_tc_set_md_var(tc
, "descr", "Checks 27-bit long bitstrings");
242 ATF_TC_BODY(bits_27
, tc
)
248 ATF_TC_HEAD(bits_32
, tc
)
250 atf_tc_set_md_var(tc
, "descr", "Checks 32-bit long bitstrings");
252 ATF_TC_BODY(bits_32
, tc
)
258 ATF_TC_HEAD(bits_49
, tc
)
260 atf_tc_set_md_var(tc
, "descr", "Checks 49-bit long bitstrings");
262 ATF_TC_BODY(bits_49
, tc
)
268 ATF_TC_HEAD(bits_64
, tc
)
270 atf_tc_set_md_var(tc
, "descr", "Checks 64-bit long bitstrings");
272 ATF_TC_BODY(bits_64
, tc
)
278 ATF_TC_HEAD(bits_67
, tc
)
280 atf_tc_set_md_var(tc
, "descr", "Checks 67-bit long bitstrings");
282 ATF_TC_BODY(bits_67
, tc
)
290 ATF_TP_ADD_TC(tp
, bits_8
);
291 ATF_TP_ADD_TC(tp
, bits_27
);
292 ATF_TP_ADD_TC(tp
, bits_32
);
293 ATF_TP_ADD_TC(tp
, bits_49
);
294 ATF_TP_ADD_TC(tp
, bits_64
);
295 ATF_TP_ADD_TC(tp
, bits_67
);
297 return atf_no_error();