1 /* $NetBSD: bitstring_test.c,v 1.8 2005/02/06 06:05:18 perry Exp $ */
4 * Copyright (c) 1993 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 * this is a simple program to test bitstring.h
31 * inspect the output, you should notice problems
32 * choose the ATT or BSD flavor
40 /* include the following define if you want the
41 * program to link. this corrects a misspeling
44 #define _bitstr_size bitstr_size
49 /* #ifdef NOTSOGOOD */
50 #include "bitstring.h"
52 /* #include "gbitstring.h" */
56 #define DECL_TEST_LENGTH 37 /* a mostly random number */
58 main(int argc
, char *argv
[])
64 bitstr_t
bit_decl(bss
, DECL_TEST_LENGTH
);
67 TEST_LENGTH
= atoi(argv
[1]);
69 TEST_LENGTH
= DECL_TEST_LENGTH
;
71 if (TEST_LENGTH
< 4) {
72 fprintf(stderr
, "TEST_LENGTH must be at least 4, but it is %d\n",
77 (void) printf("Testing with TEST_LENGTH = %d\n\n", TEST_LENGTH
);
79 (void) printf("test _bit_byte, _bit_mask, and bitstr_size\n");
80 (void) printf(" i _bit_byte(i) _bit_mask(i) bitstr_size(i)\n");
81 for (i
=0; i
<TEST_LENGTH
; i
++) {
82 (void) printf("%3d%15d%15d%15d\n",
83 i
, _bit_byte(i
), _bit_mask(i
), bitstr_size(i
));
86 bs
= bit_alloc(TEST_LENGTH
);
87 clearbits(bs
, TEST_LENGTH
);
88 (void) printf("\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n");
89 (void) printf("be: 0 -1 ");
90 for (i
=0; i
< TEST_LENGTH
; i
++)
92 (void) printf("\nis: ");
93 printbits(bs
, TEST_LENGTH
);
95 (void) printf("\ntest bit_set\n");
96 for (i
=0; i
<TEST_LENGTH
; i
+=3) {
99 (void) printf("be: 1 0 ");
100 for (i
=0; i
< TEST_LENGTH
; i
++)
101 (void) putchar(*("100" + (i
% 3)));
102 (void) printf("\nis: ");
103 printbits(bs
, TEST_LENGTH
);
105 (void) printf("\ntest bit_clear\n");
106 for (i
=0; i
<TEST_LENGTH
; i
+=6) {
109 (void) printf("be: 0 3 ");
110 for (i
=0; i
< TEST_LENGTH
; i
++)
111 (void) putchar(*("000100" + (i
% 6)));
112 (void) printf("\nis: ");
113 printbits(bs
, TEST_LENGTH
);
115 (void) printf("\ntest bit_test using previous bitstring\n");
116 (void) printf(" i bit_test(i)\n");
117 for (i
=0; i
<TEST_LENGTH
; i
++) {
118 (void) printf("%3d%15d\n",
122 clearbits(bs
, TEST_LENGTH
);
123 (void) printf("\ntest clearbits\n");
124 (void) printf("be: 0 -1 ");
125 for (i
=0; i
< TEST_LENGTH
; i
++)
127 (void) printf("\nis: ");
128 printbits(bs
, TEST_LENGTH
);
130 (void) printf("\ntest bit_nset and bit_nclear\n");
131 bit_nset(bs
, 1, TEST_LENGTH
- 2);
132 (void) printf("be: 0 1 0");
133 for (i
=0; i
< TEST_LENGTH
- 2; i
++)
135 (void) printf("0\nis: ");
136 printbits(bs
, TEST_LENGTH
);
138 bit_nclear(bs
, 2, TEST_LENGTH
- 3);
139 (void) printf("be: 0 1 01");
140 for (i
=0; i
< TEST_LENGTH
- 4; i
++)
142 (void) printf("10\nis: ");
143 printbits(bs
, TEST_LENGTH
);
145 bit_nclear(bs
, 0, TEST_LENGTH
- 1);
146 (void) printf("be: 0 -1 ");
147 for (i
=0; i
< TEST_LENGTH
; i
++)
149 (void) printf("\nis: ");
150 printbits(bs
, TEST_LENGTH
);
151 bit_nset(bs
, 0, TEST_LENGTH
- 2);
152 (void) printf("be: %3d 0 ",TEST_LENGTH
- 1);
153 for (i
=0; i
< TEST_LENGTH
- 1; i
++)
156 (void) printf("\nis: ");
157 printbits(bs
, TEST_LENGTH
);
158 bit_nclear(bs
, 0, TEST_LENGTH
- 1);
159 (void) printf("be: 0 -1 ");
160 for (i
=0; i
< TEST_LENGTH
; i
++)
162 (void) printf("\nis: ");
163 printbits(bs
, TEST_LENGTH
);
166 (void) printf("first 1 bit should move right 1 position each line\n");
167 for (i
=0; i
<TEST_LENGTH
; i
++) {
168 bit_nclear(bs
, 0, TEST_LENGTH
- 1);
169 bit_nset(bs
, i
, TEST_LENGTH
- 1);
170 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
174 (void) printf("first 0 bit should move right 1 position each line\n");
175 for (i
=0; i
<TEST_LENGTH
; i
++) {
176 bit_nset(bs
, 0, TEST_LENGTH
- 1);
177 bit_nclear(bs
, i
, TEST_LENGTH
- 1);
178 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
182 (void) printf("first 0 bit should move left 1 position each line\n");
183 for (i
=0; i
<TEST_LENGTH
; i
++) {
184 bit_nclear(bs
, 0, TEST_LENGTH
- 1);
185 bit_nset(bs
, 0, TEST_LENGTH
- 1 - i
);
186 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
190 (void) printf("first 1 bit should move left 1 position each line\n");
191 for (i
=0; i
<TEST_LENGTH
; i
++) {
192 bit_nset(bs
, 0, TEST_LENGTH
- 1);
193 bit_nclear(bs
, 0, TEST_LENGTH
- 1 - i
);
194 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
198 (void) printf("0 bit should move right 1 position each line\n");
199 for (i
=0; i
<TEST_LENGTH
; i
++) {
200 bit_nset(bs
, 0, TEST_LENGTH
- 1);
201 bit_nclear(bs
, i
, i
);
202 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
206 (void) printf("1 bit should move right 1 position each line\n");
207 for (i
=0; i
<TEST_LENGTH
; i
++) {
208 bit_nclear(bs
, 0, TEST_LENGTH
- 1);
210 (void) printf("%3d ", i
); printbits(bs
, TEST_LENGTH
);
221 int i
= bitstr_size(n
);
235 (void) printf("%3d %3d ", jc
, js
);
236 for (i
=0; i
< n
; i
++) {
237 (void) putchar((bit_test(b
, i
) ? '1' : '0'));