Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / include / bitstring / bitstring_test.c
blob38e8295f755fa1a5aa4cd1134ebd12ffb470f488
1 /* $NetBSD: bitstring_test.c,v 1.8 2005/02/06 06:05:18 perry Exp $ */
3 /*-
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
34 #if 0
35 #define ATT
36 #else
37 #define BSD /*-*/
38 #endif
40 /* include the following define if you want the
41 * program to link. this corrects a misspeling
42 * in bitstring.h
44 #define _bitstr_size bitstr_size
46 #include <stdio.h>
47 #include <stdlib.h>
49 /* #ifdef NOTSOGOOD */
50 #include "bitstring.h"
51 /* #else */
52 /* #include "gbitstring.h" */
53 /* #endif */
55 int TEST_LENGTH;
56 #define DECL_TEST_LENGTH 37 /* a mostly random number */
58 main(int argc, char *argv[])
60 void clearbits();
61 void printbits();
62 int b, i;
63 bitstr_t *bs;
64 bitstr_t bit_decl(bss, DECL_TEST_LENGTH);
66 if (argc > 1)
67 TEST_LENGTH = atoi(argv[1]);
68 else
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",
73 TEST_LENGTH);
74 exit(1);
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++)
91 (void) putchar('0');
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) {
97 bit_set(bs, i);
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) {
107 bit_clear(bs, i);
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",
119 i, bit_test(bs, i));
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++)
126 (void) putchar('0');
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++)
134 (void) putchar('1');
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++)
141 (void) putchar('0');
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++)
148 (void) putchar('0');
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++)
154 (void) putchar('1');
155 putchar('0');
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++)
161 (void) putchar('0');
162 (void) printf("\nis: ");
163 printbits(bs, TEST_LENGTH);
165 (void) printf("\n");
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);
173 (void) printf("\n");
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);
181 (void) printf("\n");
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);
189 (void) printf("\n");
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);
197 (void) printf("\n");
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);
205 (void) printf("\n");
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);
209 bit_nset(bs, i, i);
210 (void) printf("%3d ", i); printbits(bs, TEST_LENGTH);
213 (void)free(bs);
214 (void)exit(0);
216 void
217 clearbits(b, n)
218 bitstr_t *b;
219 int n;
221 int i = bitstr_size(n);
222 while(i--)
223 *(b + i) = 0;
225 void
226 printbits(b, n)
227 bitstr_t *b;
228 int n;
230 int i;
231 int jc, js;
233 bit_ffc(b, n, &jc);
234 bit_ffs(b, n, &js);
235 (void) printf("%3d %3d ", jc, js);
236 for (i=0; i< n; i++) {
237 (void) putchar((bit_test(b, i) ? '1' : '0'));
239 (void)putchar('\n');