1272343Sngie/* $NetBSD: t_bitstring.c,v 1.4 2012/03/25 06:54:04 joerg Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 1993, 2008, 2010 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26272343Sngie * POSSIBILITY OF SUCH DAMAGE. 27272343Sngie */ 28272343Sngie 29272343Sngie#include <assert.h> 30272343Sngie#include <bitstring.h> 31272343Sngie#include <stdio.h> 32272343Sngie#include <stdlib.h> 33272343Sngie 34272343Sngie#include <atf-c.h> 35272343Sngie 36272343Sngiestatic void 37272343Sngieclearbits(bitstr_t *b, int n) 38272343Sngie{ 39272343Sngie int i = bitstr_size(n); 40272343Sngie 41272343Sngie while(i--) 42272343Sngie *(b + i) = 0; 43272343Sngie} 44272343Sngie 45272343Sngiestatic void 46272343Sngieprintbits(FILE *file, bitstr_t *b, int n) 47272343Sngie{ 48272343Sngie int i; 49272343Sngie int jc, js; 50272343Sngie 51272343Sngie bit_ffc(b, n, &jc); 52272343Sngie bit_ffs(b, n, &js); 53272343Sngie 54272343Sngie (void) fprintf(file, "%3d %3d ", jc, js); 55272343Sngie 56272343Sngie for (i=0; i < n; i++) { 57272343Sngie (void) fprintf(file, "%c", (bit_test(b, i) ? '1' : '0')); 58272343Sngie } 59272343Sngie 60272343Sngie (void) fprintf(file, "%c", '\n'); 61272343Sngie} 62272343Sngie 63272343Sngiestatic void 64272343Sngiecalculate_data(FILE *file, const int test_length) 65272343Sngie{ 66272343Sngie int i; 67272343Sngie bitstr_t *bs; 68272343Sngie 69272343Sngie assert(test_length >= 4); 70272343Sngie 71272343Sngie (void) fprintf(file, "Testing with TEST_LENGTH = %d\n\n", test_length); 72272343Sngie 73272343Sngie (void) fprintf(file, "test _bit_byte, _bit_mask, and bitstr_size\n"); 74272343Sngie (void) fprintf(file, " i _bit_byte(i) _bit_mask(i) bitstr_size(i)\n"); 75272343Sngie 76272343Sngie for (i=0; i < test_length; i++) { 77272343Sngie (void) fprintf(file, "%3d%15u%15u%15zu\n", 78272343Sngie i, _bit_byte(i), _bit_mask(i), bitstr_size(i)); 79272343Sngie } 80272343Sngie 81272343Sngie bs = bit_alloc(test_length); 82272343Sngie clearbits(bs, test_length); 83272343Sngie (void) fprintf(file, "\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n"); 84272343Sngie (void) fprintf(file, "be: 0 -1 "); 85272343Sngie for (i=0; i < test_length; i++) 86272343Sngie (void) fprintf(file, "%c", '0'); 87272343Sngie (void) fprintf(file, "\nis: "); 88272343Sngie printbits(file, bs, test_length); 89272343Sngie 90272343Sngie (void) fprintf(file, "\ntest bit_set\n"); 91272343Sngie for (i=0; i < test_length; i+=3) 92272343Sngie bit_set(bs, i); 93272343Sngie (void) fprintf(file, "be: 1 0 "); 94272343Sngie for (i=0; i < test_length; i++) 95272343Sngie (void) fprintf(file, "%c", "100"[i % 3]); 96272343Sngie (void) fprintf(file, "\nis: "); 97272343Sngie printbits(file, bs, test_length); 98272343Sngie 99272343Sngie (void) fprintf(file, "\ntest bit_clear\n"); 100272343Sngie for (i=0; i < test_length; i+=6) 101272343Sngie bit_clear(bs, i); 102272343Sngie (void) fprintf(file, "be: 0 3 "); 103272343Sngie for (i=0; i < test_length; i++) 104272343Sngie (void) fprintf(file, "%c", "000100"[i % 6]); 105272343Sngie (void) fprintf(file, "\nis: "); 106272343Sngie printbits(file, bs, test_length); 107272343Sngie 108272343Sngie (void) fprintf(file, "\ntest bit_test using previous bitstring\n"); 109272343Sngie (void) fprintf(file, " i bit_test(i)\n"); 110272343Sngie for (i=0; i < test_length; i++) 111272343Sngie (void) fprintf(file, "%3d%15d\n", i, bit_test(bs, i)); 112272343Sngie 113272343Sngie clearbits(bs, test_length); 114272343Sngie (void) fprintf(file, "\ntest clearbits\n"); 115272343Sngie (void) fprintf(file, "be: 0 -1 "); 116272343Sngie for (i=0; i < test_length; i++) 117272343Sngie (void) fprintf(file, "%c", '0'); 118272343Sngie (void) fprintf(file, "\nis: "); 119272343Sngie printbits(file, bs, test_length); 120272343Sngie 121272343Sngie (void) fprintf(file, "\ntest bit_nset and bit_nclear\n"); 122272343Sngie bit_nset(bs, 1, test_length - 2); 123272343Sngie (void) fprintf(file, "be: 0 1 0"); 124272343Sngie for (i=0; i < test_length - 2; i++) 125272343Sngie (void) fprintf(file, "%c", '1'); 126272343Sngie (void) fprintf(file, "0\nis: "); 127272343Sngie printbits(file, bs, test_length); 128272343Sngie 129272343Sngie bit_nclear(bs, 2, test_length - 3); 130272343Sngie (void) fprintf(file, "be: 0 1 01"); 131272343Sngie for (i=0; i < test_length - 4; i++) 132272343Sngie (void) fprintf(file, "%c", '0'); 133272343Sngie (void) fprintf(file, "10\nis: "); 134272343Sngie printbits(file, bs, test_length); 135272343Sngie 136272343Sngie bit_nclear(bs, 0, test_length - 1); 137272343Sngie (void) fprintf(file, "be: 0 -1 "); 138272343Sngie for (i=0; i < test_length; i++) 139272343Sngie (void) fprintf(file, "%c", '0'); 140272343Sngie (void) fprintf(file, "\nis: "); 141272343Sngie printbits(file, bs, test_length); 142272343Sngie bit_nset(bs, 0, test_length - 2); 143272343Sngie (void) fprintf(file, "be: %3d 0 ",test_length - 1); 144272343Sngie for (i=0; i < test_length - 1; i++) 145272343Sngie (void) fprintf(file, "%c", '1'); 146272343Sngie fprintf(file, "%c", '0'); 147272343Sngie (void) fprintf(file, "\nis: "); 148272343Sngie printbits(file, bs, test_length); 149272343Sngie bit_nclear(bs, 0, test_length - 1); 150272343Sngie (void) fprintf(file, "be: 0 -1 "); 151272343Sngie for (i=0; i < test_length; i++) 152272343Sngie (void) fprintf(file, "%c", '0'); 153272343Sngie (void) fprintf(file, "\nis: "); 154272343Sngie printbits(file, bs, test_length); 155272343Sngie 156272343Sngie (void) fprintf(file, "\n"); 157272343Sngie (void) fprintf(file, "first 1 bit should move right 1 position each line\n"); 158272343Sngie for (i=0; i < test_length; i++) { 159272343Sngie bit_nclear(bs, 0, test_length - 1); 160272343Sngie bit_nset(bs, i, test_length - 1); 161272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 162272343Sngie } 163272343Sngie 164272343Sngie (void) fprintf(file, "\n"); 165272343Sngie (void) fprintf(file, "first 0 bit should move right 1 position each line\n"); 166272343Sngie for (i=0; i < test_length; i++) { 167272343Sngie bit_nset(bs, 0, test_length - 1); 168272343Sngie bit_nclear(bs, i, test_length - 1); 169272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 170272343Sngie } 171272343Sngie 172272343Sngie (void) fprintf(file, "\n"); 173272343Sngie (void) fprintf(file, "first 0 bit should move left 1 position each line\n"); 174272343Sngie for (i=0; i < test_length; i++) { 175272343Sngie bit_nclear(bs, 0, test_length - 1); 176272343Sngie bit_nset(bs, 0, test_length - 1 - i); 177272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 178272343Sngie } 179272343Sngie 180272343Sngie (void) fprintf(file, "\n"); 181272343Sngie (void) fprintf(file, "first 1 bit should move left 1 position each line\n"); 182272343Sngie for (i=0; i < test_length; i++) { 183272343Sngie bit_nset(bs, 0, test_length - 1); 184272343Sngie bit_nclear(bs, 0, test_length - 1 - i); 185272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 186272343Sngie } 187272343Sngie 188272343Sngie (void) fprintf(file, "\n"); 189272343Sngie (void) fprintf(file, "0 bit should move right 1 position each line\n"); 190272343Sngie for (i=0; i < test_length; i++) { 191272343Sngie bit_nset(bs, 0, test_length - 1); 192272343Sngie bit_nclear(bs, i, i); 193272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 194272343Sngie } 195272343Sngie 196272343Sngie (void) fprintf(file, "\n"); 197272343Sngie (void) fprintf(file, "1 bit should move right 1 position each line\n"); 198272343Sngie for (i=0; i < test_length; i++) { 199272343Sngie bit_nclear(bs, 0, test_length - 1); 200272343Sngie bit_nset(bs, i, i); 201272343Sngie (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length); 202272343Sngie } 203272343Sngie 204272343Sngie (void) free(bs); 205272343Sngie} 206272343Sngie 207272343Sngiestatic void 208272343Sngieone_check(const atf_tc_t *tc, const int test_length) 209272343Sngie{ 210272343Sngie FILE *out; 211272343Sngie char command[1024]; 212272343Sngie 213272343Sngie ATF_REQUIRE((out = fopen("out", "w")) != NULL); 214272343Sngie calculate_data(out, test_length); 215272343Sngie fclose(out); 216272343Sngie 217272343Sngie /* XXX The following is a huge hack that was added to simplify the 218272343Sngie * conversion of these tests from src/regress/ to src/tests/. The 219272343Sngie * tests in this file should be checking their own results, without 220272343Sngie * having to resort to external data files. */ 221272343Sngie snprintf(command, sizeof(command), "diff -u %s/d_bitstring_%d.out out", 222272343Sngie atf_tc_get_config_var(tc, "srcdir"), test_length); 223272343Sngie if (system(command) != EXIT_SUCCESS) 224272343Sngie atf_tc_fail("Test failed; see output for details"); 225272343Sngie} 226272343Sngie 227272343SngieATF_TC(bits_8); 228272343SngieATF_TC_HEAD(bits_8, tc) 229272343Sngie{ 230272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 8-bit long bitstrings"); 231272343Sngie} 232272343SngieATF_TC_BODY(bits_8, tc) 233272343Sngie{ 234272343Sngie one_check(tc, 8); 235272343Sngie} 236272343Sngie 237272343SngieATF_TC(bits_27); 238272343SngieATF_TC_HEAD(bits_27, tc) 239272343Sngie{ 240272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 27-bit long bitstrings"); 241272343Sngie} 242272343SngieATF_TC_BODY(bits_27, tc) 243272343Sngie{ 244272343Sngie one_check(tc, 27); 245272343Sngie} 246272343Sngie 247272343SngieATF_TC(bits_32); 248272343SngieATF_TC_HEAD(bits_32, tc) 249272343Sngie{ 250272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 32-bit long bitstrings"); 251272343Sngie} 252272343SngieATF_TC_BODY(bits_32, tc) 253272343Sngie{ 254272343Sngie one_check(tc, 32); 255272343Sngie} 256272343Sngie 257272343SngieATF_TC(bits_49); 258272343SngieATF_TC_HEAD(bits_49, tc) 259272343Sngie{ 260272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 49-bit long bitstrings"); 261272343Sngie} 262272343SngieATF_TC_BODY(bits_49, tc) 263272343Sngie{ 264272343Sngie one_check(tc, 49); 265272343Sngie} 266272343Sngie 267272343SngieATF_TC(bits_64); 268272343SngieATF_TC_HEAD(bits_64, tc) 269272343Sngie{ 270272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 64-bit long bitstrings"); 271272343Sngie} 272272343SngieATF_TC_BODY(bits_64, tc) 273272343Sngie{ 274272343Sngie one_check(tc, 64); 275272343Sngie} 276272343Sngie 277272343SngieATF_TC(bits_67); 278272343SngieATF_TC_HEAD(bits_67, tc) 279272343Sngie{ 280272343Sngie atf_tc_set_md_var(tc, "descr", "Checks 67-bit long bitstrings"); 281272343Sngie} 282272343SngieATF_TC_BODY(bits_67, tc) 283272343Sngie{ 284272343Sngie one_check(tc, 67); 285272343Sngie} 286272343Sngie 287272343SngieATF_TP_ADD_TCS(tp) 288272343Sngie{ 289272343Sngie 290272343Sngie ATF_TP_ADD_TC(tp, bits_8); 291272343Sngie ATF_TP_ADD_TC(tp, bits_27); 292272343Sngie ATF_TP_ADD_TC(tp, bits_32); 293272343Sngie ATF_TP_ADD_TC(tp, bits_49); 294272343Sngie ATF_TP_ADD_TC(tp, bits_64); 295272343Sngie ATF_TP_ADD_TC(tp, bits_67); 296272343Sngie 297272343Sngie return atf_no_error(); 298272343Sngie} 299