123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include "../Tests.hpp"
- #include "core/BitArray.hpp"
- #include "core/Test.hpp"
- static void testSetRead() {
- Core::BitArray bits;
- bits.resize(4, 3);
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- TEST(1, bits.get(0));
- TEST(2, bits.get(1));
- TEST(3, bits.get(2));
- TEST(4, bits.get(3));
- Core::BitArray copy;
- copy = bits;
- TEST(1, copy.get(0));
- TEST(2, copy.get(1));
- TEST(3, copy.get(2));
- TEST(4, copy.get(3));
- Core::BitArray move = Core::move(copy);
- TEST(1, move.get(0));
- TEST(2, move.get(1));
- TEST(3, move.get(2));
- TEST(4, move.get(3));
- }
- static void testBigSetRead() {
- Core::BitArray bits;
- bits.resize(100, 13);
- for(size_t i = 0; i < bits.getLength(); i++) {
- bits.set(i, i);
- }
- for(size_t i = 0; i < bits.getLength(); i++) {
- TEST(i, bits.get(i));
- }
- }
- static void testRandomSetReadResize() {
- constexpr int length = 100;
- u64 data[length];
- Core::BitArray bits;
- bits.resize(100, 13);
- u64 seed = 534;
- for(int k = 0; k < 20; k++) {
- for(u64 i = 0; i < bits.getLength(); i++) {
- seed = seed * 636'455 + 53'453;
- bits.set(i, seed);
- data[i] = seed & (0x1FFF);
- }
- }
- for(size_t i = 0; i < bits.getLength(); i++) {
- TEST(data[i], bits.get(i));
- }
- bits.resize(bits.getLength(), bits.getBits() + 1);
- TEST(14, bits.getBits());
- TEST(100, bits.getLength());
- for(size_t i = 0; i < bits.getLength(); i++) {
- TEST(data[i], bits.get(i));
- }
- }
- static void testReadOnly() {
- Core::BitArray bits;
- bits.resize(4, 3);
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- const Core::BitArray copy = bits;
- TEST(1, copy.get(0));
- TEST(2, copy.get(1));
- TEST(3, copy.get(2));
- TEST(4, copy.get(3));
- }
- static void testSelect() {
- Core::BitArray bits;
- bits.resize(90, 1);
- bits.fill(0);
- bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
- bits.set(32, 1).set(33, 1).set(60, 1);
- TEST(-1, bits.select(0));
- TEST(0, bits.select(1));
- TEST(5, bits.select(2));
- TEST(20, bits.select(3));
- TEST(31, bits.select(4));
- TEST(32, bits.select(5));
- TEST(33, bits.select(6));
- TEST(60, bits.select(7));
- TEST(-1, bits.select(8));
- }
- static void testToString1() {
- Core::BitArray bits;
- bits.resize(4, 3);
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- TEST_STRING("[1, 2, 3, 4]", bits);
- }
- static void testToString2() {
- Core::BitArray bits;
- bits.resize(1, 3);
- bits.set(0, 1);
- TEST_STRING("[1]", bits);
- }
- static void testToString3() {
- Core::BitArray bits;
- TEST_STRING("[]", bits);
- }
- static void testResizeExact() {
- Core::BitArray bits;
- TEST(0, bits.getInternalByteSize());
- // the size in bytes matches the internal storage type
- size_t elements = sizeof(u64);
- bits.resize(elements, 8);
- for(size_t i = 0; i < elements; i++) {
- bits.set(i, i);
- }
- for(size_t i = 0; i < elements; i++) {
- TEST(i, bits.get(i));
- }
- TEST(sizeof(u64), bits.getInternalByteSize());
- }
- static void testInvalidArgument() {
- Core::BitArray bits;
- bits.resize(0, 5);
- TEST(0, bits.getLength());
- TEST(0, bits.getBits());
- bits.resize(5, 0);
- TEST(0, bits.getLength());
- TEST(0, bits.getBits());
- bits.resize(0, 0);
- TEST(0, bits.getLength());
- TEST(0, bits.getBits());
- bits.resize(1, 65);
- TEST(1, bits.getLength());
- TEST(64, bits.getBits());
- bits.resize(5, 68);
- TEST(5, bits.getLength());
- TEST(64, bits.getBits());
- }
- void testBitArray() {
- testBigSetRead();
- testInvalidArgument();
- testRandomSetReadResize();
- testReadOnly();
- testResizeExact();
- testSelect();
- testSetRead();
- testToString1();
- testToString2();
- testToString3();
- }
|