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