#include #include #include #include #include #include template void Check(const char* title); template void CheckBitOps(const char* title); template void CheckConstructor(const char* title); template void CheckCopyMoveSwap(const char* title); SCENARIO("Bitset", "[CORE][BITSET]") { Check("Bitset made of 8bits blocks"); Check("Bitset made of 16bits blocks"); Check("Bitset made of 32bits blocks"); Check("Bitset made of 64bits blocks"); } template void Check(const char* title) { CheckConstructor(title); CheckCopyMoveSwap(title); CheckBitOps(title); } template void CheckBitOps(const char* title) { SECTION(title) { GIVEN("Two bitsets") { Nz::Bitset first("01001"); Nz::Bitset second("10111"); WHEN("We perform operators") { Nz::Bitset andBitset = first & second; Nz::Bitset orBitset = first | second; Nz::Bitset xorBitset = first ^ second; THEN("They should operate as logical operators") { CHECK(andBitset == Nz::Bitset("00001")); CHECK(orBitset == Nz::Bitset("11111")); CHECK(xorBitset == Nz::Bitset("11110")); } } } } } template void CheckConstructor(const char* title) { SECTION(title) { GIVEN("Allocate and constructor") { Nz::Bitset bitset(3, false); THEN("Capacity is 3 and size is 3") { CHECK(bitset.GetSize() == 3); CHECK(bitset.GetCapacity() >= 3); } } GIVEN("Iterator and default constructor") { Nz::String anotherDataString("0101"); Nz::Bitset defaultByte; Nz::Bitset anotherData(anotherDataString.GetConstBuffer()); WHEN("We assign 'anotherData'") { defaultByte = anotherDataString; CHECK(anotherData == defaultByte); CHECK(defaultByte.GetSize() == 4); CHECK(defaultByte.GetCapacity() >= 4); CHECK(anotherData.GetSize() == 4); CHECK(anotherData.GetCapacity() >= 4); } } } } template void CheckCopyMoveSwap(const char* title) { SECTION(title) { GIVEN("Copy and Move constructor") { Nz::Bitset originalArray(3, true); WHEN("We copy") { Nz::Bitset copyBitset(originalArray); THEN("We get a copy") { CHECK(copyBitset == originalArray); AND_WHEN("We modify one") { for (std::size_t i = 0; i < copyBitset.GetSize(); ++i) copyBitset[i] = false; THEN("They are no more equal") { CHECK(copyBitset != originalArray); CHECK(copyBitset == Nz::Bitset(3, false)); } } } } WHEN("We move") { Nz::Bitset moveBitset(std::move(originalArray)); THEN("These results are expected") { CHECK(moveBitset == Nz::Bitset(3, true)); CHECK(originalArray.GetCapacity() == 0); } } } GIVEN("Three bitsets") { Nz::Bitset first("01001"); Nz::Bitset second("10110"); Nz::Bitset third; WHEN("We swap first and third, then second and third and finally third and first") { Nz::Bitset oldFirst(first); Nz::Bitset oldSecond(second); first.Swap(third); std::swap(second, third); third.Swap(first); THEN("First and second have been swapped and third is still empty.") { CHECK(oldFirst == second); CHECK(oldSecond == first); CHECK(third.GetSize() == 0); } } } } }