int main(int argc, char **argv) { mt_s *mtctx = mtInit(time(NULL)); // Test the bitPackedArray by writing a bunch of random gibberish // to it, and see if it's the same. u32bit *pos = new u32bit [testSize]; u64bit *val = new u64bit [testSize]; u64bit *ans = new u64bit [arrySize]; bitPackedArray *ARR = new bitPackedArray(wordSize, 16); u32bit fail = u32bitZERO; #if 1 fprintf(stderr, "Touching the end of the array and clearing.\n"); //ARR->set(arrySize, 0); //ARR->clear(); fprintf(stderr, "Generating random test data.\n"); // Hit every element first, just to do it for (u32bit i=0; i<arrySize; i++) { pos[i] = i; val[i] = mtRandom64(mtctx); val[i] &= u64bitMASK(wordSize); ans[pos[i]] = val[i]; } // Then hit random elements, with replacement, looking for bugs for (u32bit i=arrySize; i<testSize; i++) { pos[i] = mtRandom32(mtctx) % arrySize; val[i] = mtRandom64(mtctx); val[i] &= u64bitMASK(wordSize); ans[pos[i]] = val[i]; } fprintf(stderr, "Filling array.\n"); for (u32bit i=0; i<testSize; i++) ARR->set(pos[i], val[i]); fprintf(stderr, "Validating array.\n"); for (u32bit i=0; i<arrySize; i++) if (ARR->get(i) != ans[i]) { fprintf(stderr, "FAIL at i="u32bitFMT"\n", i); fail++; if (fail > 1024) { fprintf(stderr, "bitPackedArray has errors, aborting!\n"); return(1); } } if (fail) { fprintf(stderr, "bitPackedArray had "u32bitFMT" errors.\n", fail); return(1); } fprintf(stderr, "OK!\n"); #endif delete ARR; delete [] pos; delete [] val; delete [] ans; // // // for (u32bit testNum=0; testNum<32; testNum++) { u32bit thisTestSize = 0; u32bit thisWordSize = 0; // Test a BIG heap the first iteration. if (testNum == 0) { thisTestSize = 857353; //23987153; thisWordSize = 63; fprintf(stderr, "Building heap "u32bitFMT" (wordsize="u32bitFMT" testsize="u32bitFMT").\n", testNum, thisWordSize, thisTestSize); } else { thisTestSize = (mtRandom64(mtctx) % (2 * testNum)) * 1024 + 1024; thisWordSize = (mtRandom64(mtctx) % 63) + 1; } u32bit blockSize = mtRandom64(mtctx) % 32 + 1; bitPackedHeap *HEAP = new bitPackedHeap(thisWordSize, blockSize); val = new u64bit [thisTestSize]; for (u32bit i=0; i<thisTestSize; i++) { val[i] = mtRandom64(mtctx); val[i] &= u64bitMASK(thisWordSize); HEAP->add(val[i]); } fprintf(stderr, "Testing heap "u32bitFMT" (wordsize="u32bitFMT" testsize="u32bitFMT").\n", testNum, thisWordSize, thisTestSize); qsort(val, thisTestSize, sizeof(u64bit), u64bitcompare); for (u32bit i=0; i<thisTestSize; i++) { u64bit h = HEAP->get(); //fprintf(stderr, "val["u32bitFMT"]="u64bitFMT" -- HEAP="u64bitFMT"\n", i, val[i], h); if (val[i] != h) { fprintf(stderr, "val["u32bitFMT"]="u64bitFMT" !! HEAP="u64bitFMT"\n", i, val[i], h); fail++; if (fail > 25) { fprintf(stderr, "bitPackedHeap has errors, aborting!\n"); return(1); } } } if (fail) { fprintf(stderr, "bitPackedHeap had "u32bitFMT" errors.!\n", fail); return(1); } delete HEAP; delete [] val; } fprintf(stderr, "OK!\n"); return(fail); }
void testFibonacciEncoding(void) { time_t mtseed = time(0L); mt_s *mtctx = 0L; uint32 iterations = TEST_LENGTH / 4; uint64 *bits = new uint64 [3 * iterations]; uint64 bpos = uint64ZERO; uint32 failed = 0; uint32 errors = 0; fprintf(stderr, "Starting test of fibonacci encoding\n"); bpos = uint64ZERO; mtctx = mtInit(mtseed); failed = 0; for (uint32 j=0; j < iterations; j++) { uint64 siz1 = (mtRandom32(mtctx) % 63) + 1; uint64 val1 = mtRandom64(mtctx) & uint64MASK(siz1); uint64 siz2 = siz1; setFibonacciEncodedNumber(bits, bpos, &siz1, val1); uint64 val2 = getFibonacciEncodedNumber(bits, bpos, &siz2); if ((val1 != val2) || (siz1 != siz2)) { fprintf(stderr, "fibEnc #1 failed on "uint32FMT": got "uint64FMT" expected "uint64FMT"\n", j, val2, val1); failed++; } bpos += siz1; } if (failed) { fprintf(stderr, "fibEnc #1 failed "uint32FMT" times.\n", failed); errors++; } bpos = uint64ZERO; mtctx = mtInit(mtseed); failed = 0; for (uint32 j=0; j < iterations; j++) { uint64 siz1 = (mtRandom32(mtctx) % 63) + 1; uint64 val1 = mtRandom64(mtctx) & uint64MASK(siz1); uint64 val2 = getFibonacciEncodedNumber(bits, bpos, &siz1); if (val1 != val2) { fprintf(stderr, "fibEnc #2 failed on "uint32FMT": got "uint64FMT" expected "uint64FMT"\n", j, val2, val1); failed++; } bpos += siz1; } if (failed) { fprintf(stderr, "fibEnc #2 failed "uint32FMT" times.\n", failed); errors++; } delete [] bits; if (errors) exit(1); }
void testBinaryEncoding(void) { time_t mtseed = time(0L); mt_s *mtctx = 0L; uint32 iterations = TEST_LENGTH; uint64 *bits = new uint64 [iterations + 2]; uint64 bpos = uint64ZERO; uint64 *V = new uint64 [iterations]; uint64 *C = new uint64 [iterations]; uint64 *S = new uint64 [iterations]; uint32 failed = 0; uint32 errors = 0; fprintf(stderr, "Starting test of binary encoding\n"); bpos = uint64ZERO; mtctx = mtInit(mtseed); // Build some values to stuff into the bits for (uint32 j=0; j < iterations; j++) { S[j] = (mtRandom32(mtctx) % 63) + 1; V[j] = mtRandom64(mtctx) & uint64MASK(S[j]); //fprintf(stderr, "[%2d] S="uint64FMT" V="uint64HEX"\n", j, S[j], V[j]); } // Stuff them in, in blocks of some size. At the same time, decode // (this has found bugs in the past). failed = 0; for (uint32 j=0; j < iterations; ) { uint64 num = (mtRandom32(mtctx) % 8); if (j + num > iterations) num = iterations - j; if (num == 0) { setDecodedValue(bits, bpos, S[j], V[j]); C[j] = getDecodedValue(bits, bpos, S[j]); //fprintf(stderr, "[%2d] V="uint64HEX" C="uint64HEX" single\n", j, V[j], C[j]); bpos += S[j]; } else { uint64 newp1 = setDecodedValues(bits, bpos, num, S+j, V+j); uint64 newp2 = getDecodedValues(bits, bpos, num, S+j, C+j); if (newp1 != newp2) { // not perfect; we should be checking the values too, but we do that later. for (uint32 x=0; x<num; x++) fprintf(stderr, "[%2d] #1 V="uint64HEX" C="uint64HEX" multiple "uint32FMT" %s\n", j+x, V[j+x], C[j+x], num, (V[j+x] == C[j+x]) ? "" : "FAILED"); failed++; } bpos = newp2; } j += num; if (num == 0) j++; } if (failed) { fprintf(stderr, "binEncoding #1 failed encoding "uint32FMT" times.\n", failed); errors++; } // Check that V == C failed = 0; for (uint32 j=0; j<iterations; j++) { if (V[j] != C[j]) { fprintf(stderr, "[%2d] #2 V="uint64HEX" C="uint64HEX" S="uint32FMT"\n", j, V[j], C[j], S[j]); failed++; } } if (failed) { fprintf(stderr, "binEncoding #2 failed encode/decode "uint32FMT" times.\n", failed); errors++; } // Decode independently, with different nums bpos = 0; // reset to start of bits for (uint32 j=0; j < iterations; ) { uint64 num = (mtRandom32(mtctx) % 8); if (j + num > iterations) num = iterations - j; if (num == 0) { C[j] = getDecodedValue(bits, bpos, S[j]); bpos += S[j]; } else { bpos = getDecodedValues(bits, bpos, num, S+j, C+j); } j += num; if (num == 0) j++; } // Check that V == C failed = 0; for (uint32 j=0; j<iterations; j++) { if (V[j] != C[j]) { fprintf(stderr, "[%2d] #3 V="uint64HEX" C="uint64HEX" S="uint32FMT"\n", j, V[j], C[j], S[j]); failed++; } } if (failed) { fprintf(stderr, "binEncoding #3 failed decoding "uint32FMT" times.\n", failed); errors++; } // Clean. delete [] bits; delete [] V; delete [] C; delete [] S; if (errors) exit(1); }
void testBinaryEncodingPrePost(void) { time_t mtseed = time(0L); mt_s *mtctx = 0L; uint32 iterations = TEST_LENGTH; uint64 *bits = new uint64 [2 * iterations]; uint64 bpos = uint64ZERO; uint32 siz1 = uint64ZERO; uint64 val1 = uint64ZERO; uint64 val2 = uint64ZERO; fprintf(stderr, "Starting test of binary encoding pre/post increment\n"); bpos = uint64ZERO; mtctx = mtInit(mtseed); for (uint32 j=0; j < iterations; j++) { siz1 = (mtRandom32(mtctx) % 63) + 1; val1 = mtRandom64(mtctx) & uint64MASK(siz1); setDecodedValue(bits, bpos, siz1, val1); val2 = postDecrementDecodedValue(bits, bpos, siz1); if (val2 != val1) { fprintf(stderr, "postDec1 failed: got "uint64FMT" expected "uint64FMT" siz="uint32FMT"\n", val2, val1, siz1); exit(1); } val2 = getDecodedValue(bits, bpos, siz1) + 1; val2 &= uint64MASK(siz1); if (val2 != val1) { fprintf(stderr, "postDec2 failed: got "uint64FMT" expected "uint64FMT" siz="uint32FMT"\n", val2, val1, siz1); exit(1); } val2 = preDecrementDecodedValue(bits, bpos, siz1) + 2; val2 &= uint64MASK(siz1); if (val2 != val1) { fprintf(stderr, "preDec failed: got "uint64FMT" expected "uint64FMT" siz="uint32FMT"\n", val2, val1, siz1); exit(1); } val2 = postIncrementDecodedValue(bits, bpos, siz1) + 2; val2 &= uint64MASK(siz1); if (val2 != val1) { fprintf(stderr, "postInc failed: got "uint64FMT" expected "uint64FMT"\n", val2+2, val1-2); exit(1); } val2 = getDecodedValue(bits, bpos, siz1) + 1; val2 &= uint64MASK(siz1); if (val2 != val1) { fprintf(stderr, "postInc2 failed: got "uint64FMT" expected "uint64FMT" siz="uint32FMT"\n", val2, val1, siz1); exit(1); } val2 = preIncrementDecodedValue(bits, bpos, siz1); // Should be back to original value, so no mask if (val2 != val1) { fprintf(stderr, "preInc failed: got "uint64FMT" expected "uint64FMT"\n", val2, val1); exit(1); } switch (j % 4) { case 0: val2 = postDecrementDecodedValue(bits, bpos, siz1); break; case 1: val2 = preDecrementDecodedValue(bits, bpos, siz1); break; case 2: val2 = postIncrementDecodedValue(bits, bpos, siz1); break; case 3: val2 = preIncrementDecodedValue(bits, bpos, siz1); break; } bpos += siz1; } bpos = uint64ZERO; mtctx = mtInit(mtseed); //for (j=0; j < iterations; j++) { //} delete [] bits; }