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