123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "tests/BitArrayTests.h"
- #include "data/BitArray.h"
- #include "test/Test.h"
- using String = Core::ArrayString<128>;
- template<typename T>
- static String build(Core::Test& test, const T& t) {
- String s;
- test.checkFalse(s.append(t), "append works");
- return s;
- }
- static void testSetRead(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(4, 3), "resize works 1");
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- test.checkEqual(1, bits.get(0), "set and read correct value 1");
- test.checkEqual(2, bits.get(1), "set and read correct value 2");
- test.checkEqual(3, bits.get(2), "set and read correct value 3");
- test.checkEqual(4, bits.get(3), "set and read correct value 4");
- }
- static void testOutOfBoundsSetRead(Core::Test& test) {
- Core::BitArray bits;
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- test.checkEqual(0, bits.get(0), "set and read default value 1");
- test.checkEqual(0, bits.get(1), "set and read default value 2");
- test.checkEqual(0, bits.get(2), "set and read default value 3");
- test.checkEqual(0, bits.get(3), "set and read default value 4");
- }
- static void testBigSetRead(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(100, 13), "resize works 2");
- for(int i = 0; i < bits.getLength(); i++) {
- bits.set(i, i);
- }
- for(int i = 0; i < bits.getLength(); i++) {
- test.checkEqual(i, bits.get(i),
- "set and read correct value over long array");
- }
- }
- static void testRandomSetReadResize(Core::Test& test) {
- const int length = 100;
- int data[length];
- Core::BitArray bits;
- test.checkFalse(bits.resize(100, 13), "resize works 3");
- int seed = 534;
- for(int k = 0; k < 20; k++) {
- for(int i = 0; i < bits.getLength(); i++) {
- seed = seed * 636455 + 53453;
- bits.set(i, seed & (0x1FFF));
- data[i] = seed & (0x1FFF);
- }
- }
- for(int i = 0; i < bits.getLength(); i++) {
- test.checkEqual(data[i], bits.get(i),
- "set and read correct value with random input");
- }
- test.checkFalse(bits.resize(bits.getLength(), bits.getBits() + 1),
- "resize works 4");
- test.checkEqual(14, bits.getBits(), "corrects bits after resize");
- test.checkEqual(100, bits.getLength(), "correct length after resize");
- for(int i = 0; i < bits.getLength(); i++) {
- test.checkEqual(
- data[i], bits.get(i),
- "set and read correct value with random input after resize");
- }
- }
- static void testReadOnly(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(4, 3), "resize works 5");
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- Core::BitArray copy;
- test.checkFalse(copy.copyFrom(bits), "copy works");
- const Core::BitArray bits2 = Core::move(copy);
- test.checkEqual(1, bits2.get(0), "can read from const 1");
- test.checkEqual(2, bits2.get(1), "can read from const 2");
- test.checkEqual(3, bits2.get(2), "can read from const 3");
- test.checkEqual(4, bits2.get(3), "can read from const 4");
- }
- static void testSelect(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(90, 1), "resize works 6");
- 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.checkEqual(-1, bits.select(-1), "select -1 bit");
- test.checkEqual(-1, bits.select(0), "select 0 bit");
- test.checkEqual(0, bits.select(1), "select 1 bit");
- test.checkEqual(5, bits.select(2), "select 2 bit");
- test.checkEqual(20, bits.select(3), "select 3 bit");
- test.checkEqual(31, bits.select(4), "select 4 bit");
- test.checkEqual(32, bits.select(5), "select 5 bit");
- test.checkEqual(33, bits.select(6), "select 6 bit");
- test.checkEqual(60, bits.select(7), "select 7 bit");
- test.checkEqual(-1, bits.select(8), "select 8 bit");
- }
- static void testToString1(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(4, 3), "resize works 7");
- bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
- test.checkEqual(build(test, "[1, 2, 3, 4]"), build(test, bits),
- "bit array to string 1");
- }
- static void testToString2(Core::Test& test) {
- Core::BitArray bits;
- test.checkFalse(bits.resize(1, 3), "resize works 8");
- bits.set(0, 1);
- test.checkEqual(build(test, "[1]"), build(test, bits),
- "bit array to string 2");
- }
- static void testToString3(Core::Test& test) {
- Core::BitArray bits;
- test.checkEqual(build(test, "[]"), build(test, bits),
- "bit array to string 3");
- }
- void Core::BitArrayTests::test() {
- Core::Test test("Core::BitArray");
- testSetRead(test);
- testOutOfBoundsSetRead(test);
- testBigSetRead(test);
- testRandomSetReadResize(test);
- testReadOnly(test);
- testSelect(test);
- testToString1(test);
- testToString2(test);
- testToString3(test);
- test.finalize();
- }
|