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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
/* btss.c: BIT TABLE COVERAGE TEST
*
* $Id$
* Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license.
*
* .readership: MPS developers
*
* .coverage: Direct coverage of BTFind*ResRange*, BTRangesSame,
* BTISResRange, BTIsSetRange, BTCopyRange, BTCopyOffsetRange.
* Reasonable coverage of BTCopyInvertRange, BTResRange,
* BTSetRange, BTRes, BTSet, BTCreate, BTDestroy.
*/
#include "mpm.h"
#include "mpsavm.h"
#include "mps.h"
#include "testlib.h"
#include "mpslib.h"
#include <stdio.h> /* printf */
SRCID(btcv, "$Id$");
/* bt*Symmetric -- Symmetric operations on bit tables
*
* The operations take 2 bit tables, btlo & bthi.
* They perform the equivalent BT* operation on btlo, and
* a reflected operation on the bits of bthi from the opposite
* direction.
*/
#define btReflectIndex(btSize, i) (btSize - (i) - 1)
#define btReflectLimit(btSize, i) (btSize - (i))
static void btSetSymmetric(BT btlo, BT bthi, Count btSize, Index i)
{
BTSet(btlo, i);
BTSet(bthi, btReflectIndex(btSize, i));
}
static void btResSymmetric(BT btlo, BT bthi, Count btSize, Index i)
{
BTRes(btlo, i);
BTRes(bthi, btReflectIndex(btSize, i));
}
static void btSetRangeSymmetric(BT btlo, BT bthi, Count btSize,
Index base, Index limit)
{
BTSetRange(btlo, base, limit);
BTSetRange(bthi, btReflectLimit(btSize, limit), btReflectLimit(btSize, base));
}
static void btResRangeSymmetric(BT btlo, BT bthi, Count btSize,
Index base, Index limit)
{
BTResRange(btlo, base, limit);
BTResRange(bthi, btReflectLimit(btSize, limit), btReflectLimit(btSize, base));
}
typedef Bool (*BTFinderFn)(Index *foundBase_o, Index *foundLimit_o,
BT bt, Index base, Index limit, Count length);
/* btTestSingleRange -- Test expectations for calls to BTFind*ResRange*
*
*/
static void btTestSingleRange(BTFinderFn finder, BT bt,
Index base, Index limit,
Count length,
Bool expect,
Index expectBase, Index expectLimit)
{
Bool found;
Index foundBase, foundLimit;
found = finder(&foundBase, &foundLimit, bt, base, limit, length);
cdie(found == expect, "FindResRange result");
if (expect) {
cdie(foundBase == expectBase, "FindResRange base");
cdie(foundLimit == expectLimit, "FindResRange limit");
}
}
/* btTestResRange -- Test expectations for calls to BTFindShortResRange
*
* Symmetrically call BTFindShortResRange / BTFindShortResRangeHigh
* and test the expected results
*/
static void btTestResRange(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length,
Bool expect,
Index expectBase, Index expectLimit)
{
btTestSingleRange(BTFindShortResRange, btlo,
base, limit,
length, expect,
expectBase, expectLimit);
btTestSingleRange(BTFindShortResRangeHigh, bthi,
btReflectLimit(btSize, limit),
btReflectLimit(btSize, base),
length, expect,
btReflectLimit(btSize, expectLimit),
btReflectLimit(btSize, expectBase));
}
/* btTestLongResRange -- Test expectations for calls to BTFindLongResRange
*
* Symmetrically call BTFindLongResRange / BTFindLongResRangeHigh
* and test the expected results
*/
static void btTestLongResRange(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length,
Bool expect,
Index expectBase, Index expectLimit)
{
btTestSingleRange(BTFindLongResRange, btlo,
base, limit,
length, expect,
expectBase, expectLimit);
btTestSingleRange(BTFindLongResRangeHigh, bthi,
btReflectLimit(btSize, limit),
btReflectLimit(btSize, base),
length, expect,
btReflectLimit(btSize, expectLimit),
btReflectLimit(btSize, expectBase));
}
/* btAllResTest -- tests with only a reset range
*
* Test finding reset ranges in an all-reset table.
*/
static void btAllResTest(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length)
{
btResRangeSymmetric(btlo, bthi, btSize, 0, btSize);
btTestResRange(btlo, bthi, btSize, base, limit, length,
TRUE, base, base + length);
btTestLongResRange(btlo, bthi, btSize, base, limit, length,
TRUE, base, limit);
}
/* btNoResTest -- tests with no reset ranges
*
* Test finding reset ranges in an all-set search area of a table.
* Reset the area outside the search to ensure it doesn't get found
* by mistake.
*/
static void btNoResTest(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length)
{
btResRangeSymmetric(btlo, bthi, btSize, 0, btSize);
btSetRangeSymmetric(btlo, bthi, btSize, base, limit);
btTestResRange(btlo, bthi, btSize, base, limit, length,
FALSE, 0, 0);
btTestLongResRange(btlo, bthi, btSize, base, limit, length,
FALSE, 0, 0);
}
/* btResAndFindTest -- Test finding ranges of given size
*
* Resets the range between resBase & resLimit, and then attempts
* to find it by searching in the range between base & limit.
* Expect to find the range if it's long enough,
*/
static void btResAndFindTest(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Index resBase, Index resLimit,
Count length)
{
btResRangeSymmetric(btlo, bthi, btSize, resBase, resLimit);
if ((resLimit - resBase) < length) {
btTestResRange(btlo, bthi, btSize, base, limit, length,
FALSE, 0, 0);
btTestLongResRange(btlo, bthi, btSize, base, limit, length,
FALSE, 0, 0);
} else {
btTestResRange(btlo, bthi, btSize, base, limit, length,
TRUE, resBase, resBase + length);
btTestLongResRange(btlo, bthi, btSize, base, limit, length,
TRUE, resBase, resLimit);
}
}
/* btSingleResTest -- tests with a single reset range
*
* Test finding single ranges of various sizes
*/
static void btSingleResTest(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length)
{
Count resLen;
/* choose varying range lengths from too short to longer than needed */
for (resLen = length - 1; resLen <= length + 1; resLen++) {
if ((resLen > 0) && (resLen < (limit - base -2))) {
/* place the ranges both near the beginning & near the end */
/* of the search space */
Index resBase, resLimit;
for (resBase = base; resBase <= base +2; resBase++) {
btResRangeSymmetric(btlo, bthi, btSize, 0, btSize);
btSetRangeSymmetric(btlo, bthi, btSize, base, limit);
btResAndFindTest(btlo, bthi, btSize, base, limit,
resBase, resBase + resLen, length);
}
for (resLimit = limit; resLimit >= limit -2; resLimit--) {
btResRangeSymmetric(btlo, bthi, btSize, 0, btSize);
btSetRangeSymmetric(btlo, bthi, btSize, base, limit);
btResAndFindTest(btlo, bthi, btSize, base, limit,
resLimit - resLen, resLimit, length);
}
}
}
}
/* btDoubleResTest -- Test finding double ranges of various sizes
*
* Set up 2 ranges with various relative positions. The first
* range is always too small.
*/
/* Constants describing the type of arrangement of the 2 ranges */
enum {
ArrangeGAP1 = 0,
ArrangeGAP2 = 1,
ArrangeSPREAD = 2,
ArrangeMAX
};
typedef unsigned Arrangement;
/* Choose a limit for reset range 1 */
static Index btArrangeRes1(Arrangement arrange,
Index base, Index res2Base,
Count length)
{
switch (arrange) {
case ArrangeGAP1: {
/* Gap between ranges is of length 1 */
return res2Base - 1;
}
case ArrangeGAP2: {
/* Gap between ranges is of length 2 */
return res2Base - 2;
}
case ArrangeSPREAD: {
/* range 1 starts as far before range 2 as possible */
return base + length;
}
default:
NOTREACHED;
return 0; /* keep the compiler happy */
}
}
/* Constants describing the type of pattern for the first range */
enum {
PatternLEN1 = 0,
PatternSETMID = 1,
PatternJUSTSMALL = 2,
PatternMAX
};
typedef unsigned Pattern;
/* Choose a limit for reset range 1 */
static void btResetFirstRange(BT btlo, BT bthi, Count btSize,
Index res1Limit,
Count length,
Pattern pattern)
{
switch (pattern) {
case PatternLEN1: {
/* First range is a single reset bit */
btResSymmetric(btlo, bthi, btSize, res1Limit-1);
return;
}
case PatternSETMID: {
/* Actually make 2 ranges here by setting a bit in the middle */
Index mid = res1Limit - length + (length / 2);
btResRangeSymmetric(btlo, bthi, btSize, res1Limit-length, res1Limit);
btSetSymmetric(btlo, bthi, btSize, mid);
return;
}
case PatternJUSTSMALL: {
/* Range of (length - 1) */
btResRangeSymmetric(btlo, bthi, btSize,
1 + res1Limit - length, res1Limit);
return;
}
default:
NOTREACHED;
}
}
static void btDoubleResTest(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length)
{
Count res2Len;
if (length < 2)
return; /* no possibility of making the first range too small */
/* choose varying range lengths for second res range */
for (res2Len = length - 1; res2Len <= length + 1; res2Len++) {
if ((res2Len > 0) && (res2Len < (limit - base -2))) {
Index res2Limit;
/* place the second ranges near the end of the search space */
for (res2Limit = limit; res2Limit >= limit-8; res2Limit--) {
Index res2Base = res2Limit - res2Len;
Arrangement arrange;
/* Pick one of a number of possible arrangements of the ranges */
for (arrange = ArrangeGAP1; arrange < ArrangeMAX; arrange++) {
Index res1Limit = btArrangeRes1(arrange, base, res2Base, length);
Pattern pat;
/* Pick one of a number of pattern types for range 1 */
for (pat = PatternLEN1; pat < PatternMAX; pat++) {
btResRangeSymmetric(btlo, bthi, btSize, 0, btSize);
btSetRangeSymmetric(btlo, bthi, btSize, base, limit);
btResetFirstRange(btlo, bthi, btSize, res1Limit, length, pat);
/* Set up range 2 and expect to find it when searching */
btResAndFindTest(btlo, bthi, btSize, base, limit,
res2Base, res2Limit, length);
}
}
}
}
}
}
/* btFindRangeTests -- Test BTFind*ResRange*
*
* Run a variety of FindResRange tests with different table patterns.
*/
static void btFindRangeTests(BT btlo, BT bthi, Count btSize,
Index base, Index limit,
Count length)
{
btAllResTest(btlo, bthi, btSize, base, limit, length);
btNoResTest(btlo, bthi, btSize, base, limit, length);
btSingleResTest(btlo, bthi, btSize, base, limit, length);
btDoubleResTest(btlo, bthi, btSize, base, limit, length);
}
/* btIsRangeTests -- Test BTIsResRange & BTIsSetRange
*
* Test ranges which are all reset or set apart from single
* bits near to the base and limit (both inside and outside
* the range).
*
* Test BTRangesSame by using the same bit patterns and comparing
* with an appropriate all-set or all-reset table.
*
* These tests also test BTCopyInvertRange
*/
static void btIsRangeTests(BT bt1, BT bt2, Count btSize,
Index base, Index limit)
{
Index minBase, maxLimit, b, l;
if (base > 0) {
minBase = base - 1;
} else {
minBase = 0;
}
if (limit < btSize) {
maxLimit = limit + 1;
} else {
maxLimit = btSize;
}
for (b = minBase; b <= base+1; b++) {
for (l = maxLimit; l >= limit-1; l--) {
/* test a table which is all reset apart from a set bit */
/* near each of the base and limit of the range in question */
Bool outside; /* true if set bits are both outside test range */
outside = (b < base) && (l > limit);
BTResRange(bt1, 0, btSize);
BTSet(bt1, b);
BTSet(bt1, l - 1);
/* invert the table for the inverse test */
BTCopyInvertRange(bt1, bt2, 0, btSize);
/* Check it with BTIsResRange, and the inverse with BTIsSetRange */
cdie(BTIsResRange(bt1, base, limit) == outside, "BTISResRange");
cdie(BTIsSetRange(bt2, base, limit) == outside, "BTISSetRange");
/* Check the same range with BTRangesSame on an empty table */
BTResRange(bt2, 0, btSize);
cdie(BTRangesSame(bt1, bt2, base, limit) == outside, "BTRangeSame");
/* Check the inverse with BTRangesSame on a full table */
BTCopyInvertRange(bt1, bt2, 0, btSize);
BTSetRange(bt1, 0, btSize);
cdie(BTRangesSame(bt1, bt2, base, limit) == outside, "BTRangeSame");
}
}
}
/* btCopyTests -- Test BTCopyRange & BTCopyOffsetRange
*
* Test copying ranges which are all reset or set apart from
* single bits near to the base and limit (both inside and outside
* the range).
*
*/
static void btCopyTests(BT bt1, BT bt2, Count btSize,
Index base, Index limit)
{
Index minBase, maxLimit, b, l;
if (base > 0) {
minBase = base - 1;
} else {
minBase = 0;
}
if (limit < btSize) {
maxLimit = limit + 1;
} else {
maxLimit = btSize;
}
for (b = minBase; b <= base+1; b++) {
for (l = maxLimit; l >= limit-1; l--) {
/* initialize a table which is all reset apart from a set bit */
/* near each of the base and limit of the range in question */
Bool outside; /* true if set bits are both outside test range */
outside = (b < base) && (l > limit);
BTResRange(bt1, 0, btSize);
BTSet(bt1, b);
BTSet(bt1, l - 1);
/* check copying the region to the bottom of the other table */
BTCopyOffsetRange(bt1, bt2, base, limit, 0, limit - base);
cdie(BTIsResRange(bt2, 0, limit - base) == outside, "BTIsResRange");
/* check copying the region to the top of the other table */
BTCopyOffsetRange(bt1, bt2,
base, limit, btSize + base - limit, btSize);
cdie(BTIsResRange(bt2, btSize + base - limit, btSize) == outside,
"BTIsResRange");
/* check copying the region to the same place in the other table */
BTCopyOffsetRange(bt1, bt2, base, limit, base, limit);
cdie(BTIsResRange(bt2, base, limit) == outside, "BTIsResRange");
/* copy the range and check its the same */
BTCopyRange(bt1, bt2, base, limit);
cdie(BTRangesSame(bt1, bt2, base, limit), "BTRangeSame");
/* invert the table, then copy it and check it again */
BTCopyInvertRange(bt2, bt1, 0, btSize);
BTCopyRange(bt1, bt2, base, limit);
cdie(BTRangesSame(bt1, bt2, base, limit), "BTRangeSame");
}
}
}
/* btTests -- Do all the tests
*/
static void btTests(BT btlo, BT bthi, Count btSize)
{
Index base, limit;
/* Perform lots of tests over different subranges */
for (base = 0; base < MPS_WORD_WIDTH; base++) {
for (limit = btSize; limit > (btSize-MPS_WORD_WIDTH); limit--) {
/* Perform Is*Range tests over those subranges */
btIsRangeTests(btlo, bthi, btSize, base, limit);
/* Perform Copy*Range tests over those subranges */
btCopyTests(btlo, bthi, btSize, base, limit);
/* Perform FindResRange tests with different lengths */
btFindRangeTests(btlo, bthi, btSize, base, limit, 1);
btFindRangeTests(btlo, bthi, btSize, base, limit, 2);
btFindRangeTests(btlo, bthi, btSize, base, limit, MPS_WORD_WIDTH - 1);
btFindRangeTests(btlo, bthi, btSize, base, limit, MPS_WORD_WIDTH);
btFindRangeTests(btlo, bthi, btSize, base, limit, MPS_WORD_WIDTH + 1);
btFindRangeTests(btlo, bthi, btSize, base, limit, limit - base -1);
btFindRangeTests(btlo, bthi, btSize, base, limit, limit - base);
}
}
}
/* Start the world */
#define testArenaSIZE (((size_t)64)<<20)
int main(int argc, char *argv[])
{
mps_arena_t mpsArena;
Arena arena; /* the ANSI arena which we use to allocate the BT */
BT btlo, bthi;
Count btSize;
/* tests need 4 whole words plus a few extra bits */
btSize = MPS_WORD_WIDTH * 4 + 10;
testlib_init(argc, argv);
die(mps_arena_create(&mpsArena, mps_arena_class_vm(), testArenaSIZE),
"mps_arena_create");
arena = (Arena)mpsArena; /* avoid pun */
die((mps_res_t)BTCreate(&btlo, arena, btSize),
"failed to create low bit table");
die((mps_res_t)BTCreate(&bthi, arena, btSize),
"failed to create high bit table");
btTests(btlo, bthi, btSize);
printf("%s: Conclusion: Failed to find any defects.\n", argv[0]);
return 0;
}
/* C. COPYRIGHT AND LICENSE
*
* Copyright (C) 2001-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
|