1 /* $NetBSD: t_bitstring.c,v 1.1 2010/07/17 19:26:27 jmmv Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 2008, 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 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. 15 * 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. 27 */ 28 29 #include <assert.h> 30 #include <bitstring.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 34 #include <atf-c.h> 35 36 static void 37 clearbits(bitstr_t *b, int n) 38 { 39 int i = bitstr_size(n); 40 41 while(i--) 42 *(b + i) = 0; 43 } 44 45 static void 46 printbits(FILE *file, bitstr_t *b, int n) 47 { 48 int i; 49 int jc, js; 50 51 bit_ffc(b, n, &jc); 52 bit_ffs(b, n, &js); 53 54 (void) fprintf(file, "%3d %3d ", jc, js); 55 56 for (i=0; i < n; i++) { 57 (void) fprintf(file, "%c", (bit_test(b, i) ? '1' : '0')); 58 } 59 60 (void) fprintf(file, "%c", '\n'); 61 } 62 63 static void 64 calculate_data(FILE *file, const int test_length) 65 { 66 int b, i; 67 bitstr_t *bs; 68 bitstr_t bit_decl(bss, 37); /* Mostly a random number. */ 69 70 assert(test_length >= 4); 71 72 (void) fprintf(file, "Testing with TEST_LENGTH = %d\n\n", test_length); 73 74 (void) fprintf(file, "test _bit_byte, _bit_mask, and bitstr_size\n"); 75 (void) fprintf(file, " i _bit_byte(i) _bit_mask(i) bitstr_size(i)\n"); 76 77 for (i=0; i < test_length; i++) { 78 (void) fprintf(file, "%3d%15d%15d%15d\n", 79 i, _bit_byte(i), _bit_mask(i), bitstr_size(i)); 80 } 81 82 bs = bit_alloc(test_length); 83 clearbits(bs, test_length); 84 (void) fprintf(file, "\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n"); 85 (void) fprintf(file, "be: 0 -1 "); 86 for (i=0; i < test_length; i++) 87 (void) fprintf(file, "%c", '0'); 88 (void) fprintf(file, "\nis: "); 89 printbits(file, bs, test_length); 90 91 (void) fprintf(file, "\ntest bit_set\n"); 92 for (i=0; i < test_length; i+=3) 93 bit_set(bs, i); 94 (void) fprintf(file, "be: 1 0 "); 95 for (i=0; i < test_length; i++) 96 (void) fprintf(file, "%c", *("100" + (i % 3))); 97 (void) fprintf(file, "\nis: "); 98 printbits(file, bs, test_length); 99 100 (void) fprintf(file, "\ntest bit_clear\n"); 101 for (i=0; i < test_length; i+=6) 102 bit_clear(bs, i); 103 (void) fprintf(file, "be: 0 3 "); 104 for (i=0; i < test_length; i++) 105 (void) fprintf(file, "%c", *("000100" + (i % 6))); 106 (void) fprintf(file, "\nis: "); 107 printbits(file, bs, test_length); 108 109 (void) fprintf(file, "\ntest bit_test using previous bitstring\n"); 110 (void) fprintf(file, " i bit_test(i)\n"); 111 for (i=0; i < test_length; i++) 112 (void) fprintf(file, "%3d%15d\n", i, bit_test(bs, i)); 113 114 clearbits(bs, test_length); 115 (void) fprintf(file, "\ntest clearbits\n"); 116 (void) fprintf(file, "be: 0 -1 "); 117 for (i=0; i < test_length; i++) 118 (void) fprintf(file, "%c", '0'); 119 (void) fprintf(file, "\nis: "); 120 printbits(file, bs, test_length); 121 122 (void) fprintf(file, "\ntest bit_nset and bit_nclear\n"); 123 bit_nset(bs, 1, test_length - 2); 124 (void) fprintf(file, "be: 0 1 0"); 125 for (i=0; i < test_length - 2; i++) 126 (void) fprintf(file, "%c", '1'); 127 (void) fprintf(file, "0\nis: "); 128 printbits(file, bs, test_length); 129 130 bit_nclear(bs, 2, test_length - 3); 131 (void) fprintf(file, "be: 0 1 01"); 132 for (i=0; i < test_length - 4; i++) 133 (void) fprintf(file, "%c", '0'); 134 (void) fprintf(file, "10\nis: "); 135 printbits(file, bs, test_length); 136 137 bit_nclear(bs, 0, test_length - 1); 138 (void) fprintf(file, "be: 0 -1 "); 139 for (i=0; i < test_length; i++) 140 (void) fprintf(file, "%c", '0'); 141 (void) fprintf(file, "\nis: "); 142 printbits(file, bs, test_length); 143 bit_nset(bs, 0, test_length - 2); 144 (void) fprintf(file, "be: %3d 0 ",test_length - 1); 145 for (i=0; i < test_length - 1; i++) 146 (void) fprintf(file, "%c", '1'); 147 fprintf(file, "%c", '0'); 148 (void) fprintf(file, "\nis: "); 149 printbits(file, bs, test_length); 150 bit_nclear(bs, 0, test_length - 1); 151 (void) fprintf(file, "be: 0 -1 "); 152 for (i=0; i < test_length; i++) 153 (void) fprintf(file, "%c", '0'); 154 (void) fprintf(file, "\nis: "); 155 printbits(file, bs, test_length); 156 157 (void) fprintf(file, "\n"); 158 (void) fprintf(file, "first 1 bit should move right 1 position each line\n"); 159 for (i=0; i < test_length; i++) { 160 bit_nclear(bs, 0, test_length - 1); 161 bit_nset(bs, i, test_length - 1); 162 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 163 } 164 165 (void) fprintf(file, "\n"); 166 (void) fprintf(file, "first 0 bit should move right 1 position each line\n"); 167 for (i=0; i < test_length; i++) { 168 bit_nset(bs, 0, test_length - 1); 169 bit_nclear(bs, i, test_length - 1); 170 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 171 } 172 173 (void) fprintf(file, "\n"); 174 (void) fprintf(file, "first 0 bit should move left 1 position each line\n"); 175 for (i=0; i < test_length; i++) { 176 bit_nclear(bs, 0, test_length - 1); 177 bit_nset(bs, 0, test_length - 1 - i); 178 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 179 } 180 181 (void) fprintf(file, "\n"); 182 (void) fprintf(file, "first 1 bit should move left 1 position each line\n"); 183 for (i=0; i < test_length; i++) { 184 bit_nset(bs, 0, test_length - 1); 185 bit_nclear(bs, 0, test_length - 1 - i); 186 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 187 } 188 189 (void) fprintf(file, "\n"); 190 (void) fprintf(file, "0 bit should move right 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, i, i); 194 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 195 } 196 197 (void) fprintf(file, "\n"); 198 (void) fprintf(file, "1 bit should move right 1 position each line\n"); 199 for (i=0; i < test_length; i++) { 200 bit_nclear(bs, 0, test_length - 1); 201 bit_nset(bs, i, i); 202 (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 203 } 204 205 (void) free(bs); 206 } 207 208 void 209 one_check(const atf_tc_t *tc, const int test_length) 210 { 211 FILE *out; 212 char command[1024]; 213 214 ATF_REQUIRE((out = fopen("out", "w")) != NULL); 215 calculate_data(out, test_length); 216 fclose(out); 217 218 /* XXX The following is a huge hack that was added to simplify the 219 * conversion of these tests from src/regress/ to src/tests/. The 220 * tests in this file should be checking their own results, without 221 * having to resort to external data files. */ 222 snprintf(command, sizeof(command), "diff -u %s/d_bitstring_%d.out out", 223 atf_tc_get_config_var(tc, "srcdir"), test_length); 224 if (system(command) != EXIT_SUCCESS) 225 atf_tc_fail("Test failed; see output for details"); 226 } 227 228 ATF_TC(bits_8); 229 ATF_TC_HEAD(bits_8, tc) 230 { 231 atf_tc_set_md_var(tc, "descr", "Checks 8-bit long bitstrings"); 232 atf_tc_set_md_var(tc, "use.fs", "true"); 233 } 234 ATF_TC_BODY(bits_8, tc) 235 { 236 one_check(tc, 8); 237 } 238 239 ATF_TC(bits_27); 240 ATF_TC_HEAD(bits_27, tc) 241 { 242 atf_tc_set_md_var(tc, "descr", "Checks 27-bit long bitstrings"); 243 atf_tc_set_md_var(tc, "use.fs", "true"); 244 } 245 ATF_TC_BODY(bits_27, tc) 246 { 247 one_check(tc, 27); 248 } 249 250 ATF_TC(bits_32); 251 ATF_TC_HEAD(bits_32, tc) 252 { 253 atf_tc_set_md_var(tc, "descr", "Checks 32-bit long bitstrings"); 254 atf_tc_set_md_var(tc, "use.fs", "true"); 255 } 256 ATF_TC_BODY(bits_32, tc) 257 { 258 one_check(tc, 32); 259 } 260 261 ATF_TC(bits_49); 262 ATF_TC_HEAD(bits_49, tc) 263 { 264 atf_tc_set_md_var(tc, "descr", "Checks 49-bit long bitstrings"); 265 atf_tc_set_md_var(tc, "use.fs", "true"); 266 } 267 ATF_TC_BODY(bits_49, tc) 268 { 269 one_check(tc, 49); 270 } 271 272 ATF_TC(bits_64); 273 ATF_TC_HEAD(bits_64, tc) 274 { 275 atf_tc_set_md_var(tc, "descr", "Checks 64-bit long bitstrings"); 276 atf_tc_set_md_var(tc, "use.fs", "true"); 277 } 278 ATF_TC_BODY(bits_64, tc) 279 { 280 one_check(tc, 64); 281 } 282 283 ATF_TC(bits_67); 284 ATF_TC_HEAD(bits_67, tc) 285 { 286 atf_tc_set_md_var(tc, "descr", "Checks 67-bit long bitstrings"); 287 atf_tc_set_md_var(tc, "use.fs", "true"); 288 } 289 ATF_TC_BODY(bits_67, tc) 290 { 291 one_check(tc, 67); 292 } 293 294 ATF_TP_ADD_TCS(tp) 295 { 296 297 ATF_TP_ADD_TC(tp, bits_8); 298 ATF_TP_ADD_TC(tp, bits_27); 299 ATF_TP_ADD_TC(tp, bits_32); 300 ATF_TP_ADD_TC(tp, bits_49); 301 ATF_TP_ADD_TC(tp, bits_64); 302 ATF_TP_ADD_TC(tp, bits_67); 303 304 return atf_no_error(); 305 } 306