1 /* $OpenBSD: bitstring_test.c,v 1.5 2003/07/31 21:48:02 deraadt Exp $	 */
2 /* $NetBSD: bitstring_test.c,v 1.4 1995/04/29 05:44:35 cgd Exp $	 */
3 
4 /*
5  * this is a simple program to test bitstring.h
6  * inspect the output, you should notice problems
7  * choose the ATT or BSD flavor
8  */
9 /* #define ATT /*- */
10 #define BSD			/*-*/
11 
12 /*
13  * include the following define if you want the program to link. this
14  * corrects a misspeling in bitstring.h
15  */
16 #define _bitstr_size bitstr_size
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 /* #ifdef NOTSOGOOD */
22 #include "bitstring.h"
23 /* #else */
24 /* #include "gbitstring.h" */
25 /* #endif */
26 
27 int             TEST_LENGTH;
28 #define DECL_TEST_LENGTH	37	/* a mostly random number */
29 
30 static void
31 clearbits(bitstr_t *b, int n)
32 {
33 	register int    i = bitstr_size(n);
34 
35 	while (i--)
36 		*(b + i) = 0;
37 }
38 
39 static void
40 printbits(bitstr_t *b, int n)
41 {
42 	register int    i;
43 	int             jc, js;
44 
45 	bit_ffc(b, n, &jc);
46 	bit_ffs(b, n, &js);
47 	(void) printf("%3d %3d ", jc, js);
48 	for (i = 0; i < n; i++) {
49 		(void) putchar((bit_test(b, i) ? '1' : '0'));
50 	}
51 	(void) putchar('\n');
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	int             i;
58 	bitstr_t       *bs;
59 	bitstr_t        bit_decl(bss, DECL_TEST_LENGTH);
60 
61 	if (argc > 1)
62 		TEST_LENGTH = atoi(argv[1]);
63 	else
64 		TEST_LENGTH = DECL_TEST_LENGTH;
65 
66 	if (TEST_LENGTH < 4) {
67 		fprintf(stderr, "TEST_LENGTH must be at least 4, but it is %d\n",
68 			TEST_LENGTH);
69 		exit(1);
70 	}
71 	(void) printf("Testing with TEST_LENGTH = %d\n\n", TEST_LENGTH);
72 
73 	(void) printf("test _bit_byte, _bit_mask, and bitstr_size\n");
74 	(void) printf("  i   _bit_byte(i)   _bit_mask(i) bitstr_size(i)\n");
75 	for (i = 0; i < TEST_LENGTH; i++) {
76 		(void) printf("%3d%15d%15d%15d\n",
77 			      i, _bit_byte(i), _bit_mask(i), bitstr_size(i));
78 	}
79 
80 	bs = bit_alloc(TEST_LENGTH);
81 	clearbits(bs, TEST_LENGTH);
82 	(void) printf("\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n");
83 	(void) printf("be:   0  -1 ");
84 	for (i = 0; i < TEST_LENGTH; i++)
85 		(void) putchar('0');
86 	(void) printf("\nis: ");
87 	printbits(bs, TEST_LENGTH);
88 
89 	(void) printf("\ntest bit_set\n");
90 	for (i = 0; i < TEST_LENGTH; i += 3) {
91 		bit_set(bs, i);
92 	}
93 	(void) printf("be:   1   0 ");
94 	for (i = 0; i < TEST_LENGTH; i++)
95 		(void) putchar(*("100" + (i % 3)));
96 	(void) printf("\nis: ");
97 	printbits(bs, TEST_LENGTH);
98 
99 	(void) printf("\ntest bit_clear\n");
100 	for (i = 0; i < TEST_LENGTH; i += 6) {
101 		bit_clear(bs, i);
102 	}
103 	(void) printf("be:   0   3 ");
104 	for (i = 0; i < TEST_LENGTH; i++)
105 		(void) putchar(*("000100" + (i % 6)));
106 	(void) printf("\nis: ");
107 	printbits(bs, TEST_LENGTH);
108 
109 	(void) printf("\ntest bit_test using previous bitstring\n");
110 	(void) printf("  i    bit_test(i)\n");
111 	for (i = 0; i < TEST_LENGTH; i++) {
112 		(void) printf("%3d%15d\n",
113 			      i, bit_test(bs, i));
114 	}
115 
116 	clearbits(bs, TEST_LENGTH);
117 	(void) printf("\ntest clearbits\n");
118 	(void) printf("be:   0  -1 ");
119 	for (i = 0; i < TEST_LENGTH; i++)
120 		(void) putchar('0');
121 	(void) printf("\nis: ");
122 	printbits(bs, TEST_LENGTH);
123 
124 	(void) printf("\ntest bit_nset and bit_nclear\n");
125 	bit_nset(bs, 1, TEST_LENGTH - 2);
126 	(void) printf("be:   0   1 0");
127 	for (i = 0; i < TEST_LENGTH - 2; i++)
128 		(void) putchar('1');
129 	(void) printf("0\nis: ");
130 	printbits(bs, TEST_LENGTH);
131 
132 	bit_nclear(bs, 2, TEST_LENGTH - 3);
133 	(void) printf("be:   0   1 01");
134 	for (i = 0; i < TEST_LENGTH - 4; i++)
135 		(void) putchar('0');
136 	(void) printf("10\nis: ");
137 	printbits(bs, TEST_LENGTH);
138 
139 	bit_nclear(bs, 0, TEST_LENGTH - 1);
140 	(void) printf("be:   0  -1 ");
141 	for (i = 0; i < TEST_LENGTH; i++)
142 		(void) putchar('0');
143 	(void) printf("\nis: ");
144 	printbits(bs, TEST_LENGTH);
145 	bit_nset(bs, 0, TEST_LENGTH - 2);
146 	(void) printf("be: %3d   0 ", TEST_LENGTH - 1);
147 	for (i = 0; i < TEST_LENGTH - 1; i++)
148 		(void) putchar('1');
149 	putchar('0');
150 	(void) printf("\nis: ");
151 	printbits(bs, TEST_LENGTH);
152 	bit_nclear(bs, 0, TEST_LENGTH - 1);
153 	(void) printf("be:   0  -1 ");
154 	for (i = 0; i < TEST_LENGTH; i++)
155 		(void) putchar('0');
156 	(void) printf("\nis: ");
157 	printbits(bs, TEST_LENGTH);
158 
159 	(void) printf("\n");
160 	(void) printf("first 1 bit should move right 1 position each line\n");
161 	for (i = 0; i < TEST_LENGTH; i++) {
162 		bit_nclear(bs, 0, TEST_LENGTH - 1);
163 		bit_nset(bs, i, TEST_LENGTH - 1);
164 		(void) printf("%3d ", i);
165 		printbits(bs, TEST_LENGTH);
166 	}
167 
168 	(void) printf("\n");
169 	(void) printf("first 0 bit should move right 1 position each line\n");
170 	for (i = 0; i < TEST_LENGTH; i++) {
171 		bit_nset(bs, 0, TEST_LENGTH - 1);
172 		bit_nclear(bs, i, TEST_LENGTH - 1);
173 		(void) printf("%3d ", i);
174 		printbits(bs, TEST_LENGTH);
175 	}
176 
177 	(void) printf("\n");
178 	(void) printf("first 0 bit should move left 1 position each line\n");
179 	for (i = 0; i < TEST_LENGTH; i++) {
180 		bit_nclear(bs, 0, TEST_LENGTH - 1);
181 		bit_nset(bs, 0, TEST_LENGTH - 1 - i);
182 		(void) printf("%3d ", i);
183 		printbits(bs, TEST_LENGTH);
184 	}
185 
186 	(void) printf("\n");
187 	(void) printf("first 1 bit should move left 1 position each line\n");
188 	for (i = 0; i < TEST_LENGTH; i++) {
189 		bit_nset(bs, 0, TEST_LENGTH - 1);
190 		bit_nclear(bs, 0, TEST_LENGTH - 1 - i);
191 		(void) printf("%3d ", i);
192 		printbits(bs, TEST_LENGTH);
193 	}
194 
195 	(void) printf("\n");
196 	(void) printf("0 bit should move right 1 position each line\n");
197 	for (i = 0; i < TEST_LENGTH; i++) {
198 		bit_nset(bs, 0, TEST_LENGTH - 1);
199 		bit_nclear(bs, i, i);
200 		(void) printf("%3d ", i);
201 		printbits(bs, TEST_LENGTH);
202 	}
203 
204 	(void) printf("\n");
205 	(void) printf("1 bit should move right 1 position each line\n");
206 	for (i = 0; i < TEST_LENGTH; i++) {
207 		bit_nclear(bs, 0, TEST_LENGTH - 1);
208 		bit_nset(bs, i, i);
209 		(void) printf("%3d ", i);
210 		printbits(bs, TEST_LENGTH);
211 	}
212 
213 	(void) free(bs);
214 	(void) exit(0);
215 }
216