UnitTests/Bitset: Test with different block size
This commit is contained in:
parent
cd0e9d97b8
commit
91010fffa6
|
|
@ -1,117 +1,159 @@
|
|||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Block> void Check(const char* title);
|
||||
template<typename Block> void CheckBitOps(const char* title);
|
||||
template<typename Block> void CheckConstructor(const char* title);
|
||||
template<typename Block> void CheckCopyMoveSwap(const char* title);
|
||||
|
||||
SCENARIO("Bitset", "[CORE][BITSET]")
|
||||
{
|
||||
GIVEN("Allocate and constructor")
|
||||
Check<Nz::UInt8>("Bitset made of 8bits blocks");
|
||||
Check<Nz::UInt16>("Bitset made of 16bits blocks");
|
||||
Check<Nz::UInt32>("Bitset made of 32bits blocks");
|
||||
Check<Nz::UInt64>("Bitset made of 64bits blocks");
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
void Check(const char* title)
|
||||
{
|
||||
CheckConstructor<Block>(title);
|
||||
CheckCopyMoveSwap<Block>(title);
|
||||
|
||||
CheckBitOps<Block>(title);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
void CheckBitOps(const char* title)
|
||||
{
|
||||
SECTION(title)
|
||||
{
|
||||
Nz::Bitset<> bitset(3, false);
|
||||
|
||||
THEN("Capacity is 3 and size is 3")
|
||||
GIVEN("Two bitsets")
|
||||
{
|
||||
CHECK(bitset.GetSize() == 3);
|
||||
CHECK(bitset.GetCapacity() >= 3);
|
||||
}
|
||||
}
|
||||
Nz::Bitset<Block> first("01001");
|
||||
Nz::Bitset<Block> second("10111");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
GIVEN("Copy and Move constructor")
|
||||
{
|
||||
Nz::Bitset<> originalArray(3, true);
|
||||
|
||||
WHEN("We copy")
|
||||
{
|
||||
Nz::Bitset<> copyBitset(originalArray);
|
||||
|
||||
THEN("We get a copy")
|
||||
WHEN("We perform operators")
|
||||
{
|
||||
CHECK(copyBitset == originalArray);
|
||||
Nz::Bitset<Block> andBitset = first & second;
|
||||
Nz::Bitset<Block> orBitset = first | second;
|
||||
Nz::Bitset<Block> xorBitset = first ^ second;
|
||||
|
||||
AND_WHEN("We modify one")
|
||||
THEN("They should operate as logical operators")
|
||||
{
|
||||
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));
|
||||
}
|
||||
CHECK(andBitset == Nz::Bitset<Block>("00001"));
|
||||
CHECK(orBitset == Nz::Bitset<Block>("11111"));
|
||||
CHECK(xorBitset == Nz::Bitset<Block>("11110"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We move")
|
||||
{
|
||||
Nz::Bitset<> moveBitset(std::move(originalArray));
|
||||
|
||||
THEN("These results are expected")
|
||||
{
|
||||
REQUIRE(moveBitset == Nz::Bitset<>(3, true));
|
||||
REQUIRE(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.")
|
||||
{
|
||||
REQUIRE(oldFirst == second);
|
||||
REQUIRE(oldSecond == first);
|
||||
REQUIRE(third.GetSize() == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
{
|
||||
REQUIRE(andBitset == Nz::Bitset<>("00001"));
|
||||
REQUIRE(orBitset == Nz::Bitset<>("11111"));
|
||||
REQUIRE(xorBitset == Nz::Bitset<>("11110"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template<typename Block>
|
||||
void CheckConstructor(const char* title)
|
||||
{
|
||||
SECTION(title)
|
||||
{
|
||||
GIVEN("Allocate and constructor")
|
||||
{
|
||||
Nz::Bitset<Block> 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<Block> defaultByte;
|
||||
Nz::Bitset<Block> 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<typename Block>
|
||||
void CheckCopyMoveSwap(const char* title)
|
||||
{
|
||||
SECTION(title)
|
||||
{
|
||||
GIVEN("Copy and Move constructor")
|
||||
{
|
||||
Nz::Bitset<Block> originalArray(3, true);
|
||||
|
||||
WHEN("We copy")
|
||||
{
|
||||
Nz::Bitset<Block> 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<Block>(3, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We move")
|
||||
{
|
||||
Nz::Bitset<Block> moveBitset(std::move(originalArray));
|
||||
|
||||
THEN("These results are expected")
|
||||
{
|
||||
CHECK(moveBitset == Nz::Bitset<Block>(3, true));
|
||||
CHECK(originalArray.GetCapacity() == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GIVEN("Three bitsets")
|
||||
{
|
||||
Nz::Bitset<Block> first("01001");
|
||||
Nz::Bitset<Block> second("10110");
|
||||
Nz::Bitset<Block> third;
|
||||
|
||||
WHEN("We swap first and third, then second and third and finally third and first")
|
||||
{
|
||||
Nz::Bitset<Block> oldFirst(first);
|
||||
Nz::Bitset<Block> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue