1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF. The full HDF copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
FILE
tbv.c
Test HDF bit-vector (bv) routines.
*/
#include "tproto.h"
#include "bitvect_priv.h"
static void test_1(void);
static void test_2(void);
static void test_3(void);
static void test_4(void);
/* Basic creation & deletion tests */
static void
test_1(void)
{
bv_ptr b;
int32 size;
intn ret;
MESSAGE(6, printf("Testing basic bit-vector creation & deletion\n"););
/* Test basic default creation */
MESSAGE(7, printf("Create a bit-vector with all defaults\n"););
b = bv_new(-1);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, BV_DEFAULT_BITS, "bv_size");
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
/* Test creating a bit-vector w/ size >> defaults */
MESSAGE(7, printf("Create an extendable bit-vector with large # of bits\n"););
b = bv_new(80000);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, 80000, "bv_size");
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
} /* end test_1 */
/* Basic set & get tests */
static void
test_2(void)
{
bv_ptr b;
int32 size;
intn ret;
MESSAGE(6, printf("Testing basic bit-vector set & get\n"););
/* test basic default creation */
MESSAGE(7, printf("Create a bit-vector with all defaults\n"););
b = bv_new(-1);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, BV_DEFAULT_BITS, "bv_size");
/* Check setting bits */
ret = bv_set(b, 13, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 3, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 150, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* Check getting bits */
ret = bv_get(b, 2);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 3);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, 0);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 13);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, -1);
VERIFY_VOID(ret, FAIL, "bv_get");
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
/* Test another bit vector with a large number of bits */
MESSAGE(7, printf("Create an extendable bit-vector with large # of bits\n"););
b = bv_new(1000);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, 1000, "bv_size");
/* Check setting bits */
ret = bv_set(b, 13, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 3, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 1050, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* Check getting bits */
ret = bv_get(b, 2);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 3);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, 0);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 13);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, 1040);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 1050);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
} /* end test_2 */
/* Advanced set & get tests */
static void
test_3(void)
{
bv_ptr b;
int32 size;
int32 bit_num;
intn ret;
MESSAGE(6, printf("Testing basic bit-vector set & get\n"););
/* Create an extendable bit-vector */
MESSAGE(7, printf("Create an extendable bit-vector\n"););
b = bv_new(-1);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, BV_DEFAULT_BITS, "bv_size");
/* Check setting bits */
ret = bv_set(b, 13, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 3, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 150, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 152, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* Check getting bits */
ret = bv_get(b, 2);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 3);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, 0);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 13);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
ret = bv_get(b, 140);
VERIFY_VOID(ret, BV_FALSE, "bv_get");
ret = bv_get(b, 150);
VERIFY_VOID(ret, BV_TRUE, "bv_get");
/* Check finding 0s in the bit-vector, remembering the last one. This
* should skip bit 3, which we set to 1 in the above code.
*/
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 0, "bv_find_next_zero");
MESSAGE(8, printf("First 0 found at: %" PRId32 "\n", bit_num););
ret = bv_set(b, bit_num, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 1, "bv_find_next_zero");
MESSAGE(8, printf("Second 0 found at: %" PRId32 "\n", bit_num););
ret = bv_set(b, bit_num, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 2, "bv_find_next_zero");
MESSAGE(8, printf("Third 0 found at: %" PRId32 "\n", bit_num););
ret = bv_set(b, bit_num, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 4, "bv_find_next_zero");
MESSAGE(8, printf("Fourth 0 found at: %" PRId32 "\n", bit_num););
ret = bv_set(b, bit_num, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
} /* end test_3 */
/* Pathologic set/find test */
static void
test_4(void)
{
bv_ptr b;
int32 size;
int32 bit_num;
int i;
intn ret;
MESSAGE(6, printf("Testing pathologic bit-vector set & get\n"););
/* Create an extendable bit-vector */
MESSAGE(7, printf("Create an extendable bit-vector\n"););
b = bv_new(16);
CHECK_VOID(b, NULL, "bv_new");
size = bv_size(b);
MESSAGE(8, printf("Bit-vector size=%" PRId32 "\n", size););
VERIFY_VOID(size, 16, "bv_size");
/* Set all the bits to 1 */
for (i = 0; i < 16; i++) {
ret = bv_set(b, i, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
}
/* Set two bits to 0 (order is important!), one in each byte. At this
* point the cached first zero should be 5.
*/
ret = bv_set(b, 13, BV_FALSE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 5, BV_FALSE);
CHECK_VOID(ret, FAIL, "bv_set");
/* Now set the lower bit to 1. This should invalidate the cached value. */
ret = bv_set(b, 5, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* See if we can find the remaining 0 */
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 13, "bv_find_next_zero");
MESSAGE(8, printf("Remaining 0 found at: %" PRId32 "\n", bit_num););
/* Reset the zero bits */
ret = bv_set(b, 13, BV_FALSE);
CHECK_VOID(ret, FAIL, "bv_set");
ret = bv_set(b, 5, BV_FALSE);
CHECK_VOID(ret, FAIL, "bv_set");
/* Now set the higher bit to 1, just to make sure this order works */
ret = bv_set(b, 13, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* See if we can find the remaining 0 */
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 5, "bv_find_next_zero");
MESSAGE(8, printf("Remaining 0 found at: %" PRId32 "\n", bit_num););
/* Now set remaining bit to 1 */
ret = bv_set(b, 5, BV_TRUE);
CHECK_VOID(ret, FAIL, "bv_set");
/* The next bit should extend the array */
bit_num = bv_find_next_zero(b);
CHECK_VOID(bit_num, FAIL, "bv_find_next_zero");
VERIFY_VOID(bit_num, 16, "bv_find_next_zero");
MESSAGE(8, printf("Remaining 0 found at: %" PRId32 "\n", bit_num););
ret = bv_delete(b);
CHECK_VOID(ret, FAIL, "bv_delete");
} /* end test_4 */
void
test_bitvect(void)
{
test_1(); /* basic creation & deletion tests */
test_2(); /* basic set & get testing */
test_3(); /* advanced set & get testing */
test_4(); /* pathological set & find testing */
}
|