123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "../Tests.h"
- #include "core/BitArray.h"
- static void testSetRead() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 4, 3);
- setBitsBitArray(&bits, 0, 1);
- setBitsBitArray(&bits, 1, 2);
- setBitsBitArray(&bits, 2, 3);
- setBitsBitArray(&bits, 3, 4);
- CORE_TEST_U64(1, getBitsBitArray(&bits, 0));
- CORE_TEST_U64(2, getBitsBitArray(&bits, 1));
- CORE_TEST_U64(3, getBitsBitArray(&bits, 2));
- CORE_TEST_U64(4, getBitsBitArray(&bits, 3));
- destroyBitArray(&bits);
- }
- static void testBigSetRead() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 100, 13);
- for(size_t i = 0; i < bits.length; i++) {
- setBitsBitArray(&bits, i, i);
- }
- for(size_t i = 0; i < bits.length; i++) {
- CORE_TEST_U64(i, getBitsBitArray(&bits, i));
- }
- destroyBitArray(&bits);
- }
- static void testRandomSetReadResize() {
- u64 data[100];
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 100, 13);
- u64 seed = 534;
- for(int k = 0; k < 20; k++) {
- for(u64 i = 0; i < bits.length; i++) {
- seed = seed * 636455 + 53453;
- setBitsBitArray(&bits, i, seed);
- data[i] = seed & 0x1FFF;
- }
- }
- for(size_t i = 0; i < bits.length; i++) {
- CORE_TEST_U64(data[i], getBitsBitArray(&bits, i));
- }
- resizeBitArray(&bits, bits.length, bits.bits + 1u);
- CORE_TEST_U64(14, bits.bits);
- CORE_TEST_U64(100, bits.length);
- for(size_t i = 0; i < bits.length; i++) {
- CORE_TEST_U64(data[i], getBitsBitArray(&bits, i));
- }
- destroyBitArray(&bits);
- }
- static void testCopy() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 4, 3);
- setBitsBitArray(&bits, 0, 1);
- setBitsBitArray(&bits, 1, 2);
- setBitsBitArray(&bits, 2, 3);
- setBitsBitArray(&bits, 3, 4);
- BitArray copy = BIT_ARRAY;
- copyBitArray(©, &bits);
- copyBitArray(©, ©);
- CORE_TEST_U64(1, getBitsBitArray(©, 0));
- CORE_TEST_U64(2, getBitsBitArray(©, 1));
- CORE_TEST_U64(3, getBitsBitArray(©, 2));
- CORE_TEST_U64(4, getBitsBitArray(©, 3));
- destroyBitArray(©);
- destroyBitArray(&bits);
- }
- static void testSelect() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 90, 1);
- fillBitArray(&bits, 0);
- setBitsBitArray(&bits, 0, 1);
- setBitsBitArray(&bits, 5, 1);
- setBitsBitArray(&bits, 20, 1);
- setBitsBitArray(&bits, 31, 1);
- setBitsBitArray(&bits, 32, 1);
- setBitsBitArray(&bits, 33, 1);
- setBitsBitArray(&bits, 60, 1);
- CORE_TEST_I64(-1, selectBitsBitArray(&bits, 0));
- CORE_TEST_I64(0, selectBitsBitArray(&bits, 1));
- CORE_TEST_I64(5, selectBitsBitArray(&bits, 2));
- CORE_TEST_I64(20, selectBitsBitArray(&bits, 3));
- CORE_TEST_I64(31, selectBitsBitArray(&bits, 4));
- CORE_TEST_I64(32, selectBitsBitArray(&bits, 5));
- CORE_TEST_I64(33, selectBitsBitArray(&bits, 6));
- CORE_TEST_I64(60, selectBitsBitArray(&bits, 7));
- CORE_TEST_I64(-1, selectBitsBitArray(&bits, 8));
- destroyBitArray(&bits);
- }
- static void testToString1() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 4, 3);
- setBitsBitArray(&bits, 0, 1);
- setBitsBitArray(&bits, 1, 2);
- setBitsBitArray(&bits, 2, 3);
- setBitsBitArray(&bits, 3, 4);
- char buffer[128];
- size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
- CORE_TEST_SIZE(12, n);
- CORE_TEST_STRING("[1, 2, 3, 4]", buffer);
- destroyBitArray(&bits);
- }
- static void testToString2() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 1, 3);
- setBitsBitArray(&bits, 0, 1);
- char buffer[128];
- size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
- CORE_TEST_SIZE(3, n);
- CORE_TEST_STRING("[1]", buffer);
- destroyBitArray(&bits);
- }
- static void testToString3() {
- BitArray bits = BIT_ARRAY;
- char buffer[128];
- size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
- CORE_TEST_SIZE(2, n);
- CORE_TEST_STRING("[]", buffer);
- destroyBitArray(&bits);
- }
- static void testResizeExact() {
- BitArray bits = BIT_ARRAY;
- CORE_TEST_U64(0, getBytesBitArray(&bits));
- // the size in bytes matches the internal storage type
- size_t elements = sizeof(u64);
- resizeBitArray(&bits, elements, 8);
- for(size_t i = 0; i < elements; i++) {
- setBitsBitArray(&bits, i, i);
- }
- for(size_t i = 0; i < elements; i++) {
- CORE_TEST_U64(i, getBitsBitArray(&bits, i));
- }
- CORE_TEST_U64(sizeof(u64), getBytesBitArray(&bits));
- destroyBitArray(&bits);
- }
- static void testMove() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 8, 8);
- for(size_t i = 0; i < bits.length; i++) {
- setBitsBitArray(&bits, i, i);
- }
- BitArray m = BIT_ARRAY;
- moveBitArray(&m, &bits);
- moveBitArray(&m, &m);
- CORE_TEST_U64(8, m.length);
- for(size_t i = 0; i < m.length; i++) {
- CORE_TEST_U64(i, getBitsBitArray(&m, i));
- }
- destroyBitArray(&m);
- destroyBitArray(&bits);
- }
- static void testInvalidArgument() {
- BitArray bits = BIT_ARRAY;
- resizeBitArray(&bits, 0, 5);
- CORE_TEST_SIZE(0, bits.length);
- CORE_TEST_SIZE(0, bits.bits);
- resizeBitArray(&bits, 5, 0);
- CORE_TEST_SIZE(0, bits.length);
- CORE_TEST_SIZE(0, bits.bits);
- resizeBitArray(&bits, 0, 0);
- CORE_TEST_SIZE(0, bits.length);
- CORE_TEST_SIZE(0, bits.bits);
- resizeBitArray(&bits, 1, 65);
- CORE_TEST_SIZE(1, bits.length);
- CORE_TEST_SIZE(64, bits.bits);
- resizeBitArray(&bits, 5, 68);
- CORE_TEST_SIZE(5, bits.length);
- CORE_TEST_SIZE(64, bits.bits);
- destroyBitArray(&bits);
- }
- void coreTestBitArray() {
- testSetRead();
- testBigSetRead();
- testRandomSetReadResize();
- testCopy();
- testSelect();
- testToString1();
- testToString2();
- testToString3();
- testResizeExact();
- testMove();
- testInvalidArgument();
- }
|