Skip to content

Commit

Permalink
Add ZenUnit::RandomSpan<T>(), ZenUnit::RandomNonEmptySpan<T>(), and Z…
Browse files Browse the repository at this point in the history
…enUnit::RandomSpanWithSize<T>(size_t)
  • Loading branch information
NeilJustice committed Sep 30, 2024
1 parent 026c7c6 commit ed10dc7
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ZenUnitAndMetalMock/ZenUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,9 @@ namespace ZenUnit
template<typename T> constexpr bool is_pair_v = false;
template<typename T1, typename T2> constexpr bool is_pair_v<std::pair<T1, T2>> = true;

template<typename T> constexpr bool is_span_v = false;
template<typename T> constexpr bool is_span_v<std::span<T>> = true;

template<typename T> constexpr bool is_vector_v = false;
template<typename T> constexpr bool is_vector_v<std::vector<T>> = true;

Expand Down Expand Up @@ -7584,6 +7587,16 @@ or change TEST(TestName) to TESTNXN(TestName, ...), where N can be 1 through 10,
}
};

template<typename T>
class Equalizer<std::span<T>>
{
public:
static void AssertEqual(const std::span<T>& expectedSpan, const std::span<T>& actualSpan)
{
SPANS_ARE_EQUAL(expectedSpan, actualSpan);
}
};

template<typename T>
class Equalizer<std::vector<T>>
{
Expand Down Expand Up @@ -7922,6 +7935,32 @@ or change TEST(TestName) to TESTNXN(TestName, ...), where N can be 1 through 10,
return randomTuple;
}

template<typename T>
std::span<T> RandomSpanWithSize(size_t size)
{
T* elements = new T[size]; // By-design memory leak to return a span that points to valid memory
for (size_t i = 0; i < size; ++i)
{
elements[i] = Random<T>();
}
std::span<T> randomSpan(elements, size);
return randomSpan;
}

template<typename T>
std::span<T> RandomSpan()
{
const std::size_t randomSpanSize = RandomBetween<size_t>(0, 3);
return RandomSpanWithSize<T>(randomSpanSize);
}

template<typename T>
std::span<T> RandomNonEmptySpan()
{
const std::size_t randomSpanSize = RandomBetween<size_t>(1, 3);
return RandomSpanWithSize<T>(randomSpanSize);
}

template<typename T>
std::vector<T> RandomVectorWithSize(size_t size)
{
Expand Down Expand Up @@ -8102,6 +8141,11 @@ or change TEST(TestName) to TESTNXN(TestName, ...), where N can be 1 through 10,
std::pair<typename T::first_type, typename T::second_type> randomPair = RandomPair<typename T::first_type, typename T::second_type>();
return randomPair;
}
else if constexpr (is_span_v<T>)
{
std::span<typename T::value_type> randomSpan = RandomSpan<typename T::value_type>();
return randomSpan;
}
else if constexpr (is_vector_v<T>)
{
std::vector<typename T::value_type> randomVector = RandomVector<typename T::value_type>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "pch.h"

namespace ZenUnit
{
TESTS(is_span_vTests)
AFACT(is_span_v_TIsNotAVector_ValueIsFalse)
AFACT(is_span_v_TIsAVector_ValueIsTrue)
EVIDENCE

TEST(is_span_v_TIsNotAVector_ValueIsFalse)
{
IS_FALSE(is_span_v<int>);
IS_FALSE(is_span_v<string>);
}

TEST(is_span_v_TIsAVector_ValueIsTrue)
{
IS_TRUE(is_span_v<span<int>>);
IS_TRUE(is_span_v<span<string>>);
}

RUN_TESTS(is_span_vTests)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ namespace ZenUnit
const string defaultString;
IS_DEFAULT_VALUE(defaultString);

const span<int> defaultSpan;
IS_DEFAULT_VALUE(defaultSpan);

const vector<int> defaultVector;
IS_DEFAULT_VALUE(defaultVector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace ZenUnit
AFACT(FloatIsZero_ThrowsAnomaly)
AFACT(DoubleIsZero_ThrowsAnomaly)
AFACT(StringIsEmptyString_ThrowsAnomaly)
AFACT(IntSpanIsEmpty_ThrowsAnomaly)
AFACT(IntVectorIsEmpty_ThrowsAnomaly)
AFACT(UserTypeIsDefaultValue_ThrowsAnomaly)
AFACT(UserTypeIsDefaultValue_ThrowsAnomaly__MessagesTestCase)
Expand Down Expand Up @@ -39,6 +40,10 @@ namespace ZenUnit
const string nonDefaultString = ZenUnit::Random<string>();
IS_NOT_DEFAULT_VALUE(nonDefaultString);

const vector<int> ints = { 1, 2, 3 };
const span<const int> nonDefaultSpan(ints.cbegin(), 3);
IS_NOT_DEFAULT_VALUE(nonDefaultSpan);

const vector<int> nonDefaultVector = { 1, 2, 3 };
IS_NOT_DEFAULT_VALUE(nonDefaultVector);

Expand Down Expand Up @@ -82,6 +87,19 @@ namespace ZenUnit
"File.cpp(1)"));
}

TEST(IntSpanIsEmpty_ThrowsAnomaly)
{
const span<int> defaultIntSpan;
const string expectedIntSpanTypeName = *Type::GetName<span<int>>();
THROWS_EXCEPTION(IS_NOT_DEFAULT_VALUE(defaultIntSpan), Anomaly, TestUtil::NewlineConcat("",
" Failed: IS_NOT_DEFAULT_VALUE(defaultIntSpan)",
"Expected: Not T{}",
" Actual: " + expectedIntSpanTypeName + " (size 0):",
"{",
"}",
"File.cpp(1)"));
}

TEST(IntVectorIsEmpty_ThrowsAnomaly)
{
const vector<int> defaultIntVector;
Expand Down
36 changes: 36 additions & 0 deletions ZenUnitUtilsAndAssertionTests/StaticComponents/RandomSpanTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pch.h"

namespace ZenUnit
{
template<typename T>
TEMPLATE_TESTS(RandomSpanTests, T)
FACTS(RandomSpanWithSize_ReturnsASpanWithSpecifiedSize)
AFACT(RandomSpan_ReturnsASpanWithSizeBetween0And2)
AFACT(RandomNonEmptySpan_ReturnsASpanWithSizeBetween1And3)
EVIDENCE

TEST1X1(RandomSpanWithSize_ReturnsASpanWithSpecifiedSize,
size_t size,
0ULL,
1ULL,
5ULL)
{
const span<T> randomSpanWithSize = ZenUnit::RandomSpanWithSize<T>(size);
ARE_EQUAL(size, randomSpanWithSize.size());
}

TEST(RandomSpan_ReturnsASpanWithSizeBetween0And2)
{
const span<T> randomSpan = ZenUnit::RandomSpan<T>();
IS_TRUE(randomSpan.size() <= 3);
}

TEST(RandomNonEmptySpan_ReturnsASpanWithSizeBetween1And3)
{
const span<T> randomNonEmptySpan = ZenUnit::RandomNonEmptySpan<T>();
IS_TRUE(randomNonEmptySpan.size() >= 1 && randomNonEmptySpan.size() <= 3);
}

RUN_TEMPLATE_TESTS(RandomSpanTests, int)
THEN_RUN_TEMPLATE_TESTS(RandomSpanTests, string)
}
10 changes: 10 additions & 0 deletions ZenUnitUtilsAndAssertionTests/StaticComponents/RandomTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ZenUnit
AFACT(Random_AllIntegerTypes_ReturnsRandomValueBetweenMinAndMaxForThatType)
AFACT(Random_TIsEnum_ReturnsRandomEnumBetween0AndEnumMaxValue)
AFACT(Random_TIsAPair_ReturnsRandomPair)
AFACT(Random_TIsASpan_ReturnsRandomSpanOfTWithSizeLessThanOrEqualTo3)
AFACT(Random_TIsAVector_ReturnsRandomVectorOfTWithSizeLessThanOrEqualTo3)
AFACT(Random_TIsAnUnorderedMap_ReturnsRandomUnorderedMap)
AFACT(Random_TIsAnOrderedSet_ReturnsRandomOrderedSet)
Expand Down Expand Up @@ -88,6 +89,15 @@ namespace ZenUnit
const pair<double, char> randomDoubleCharPair = Random<pair<double, char>>();
}

TEST(Random_TIsASpan_ReturnsRandomSpanOfTWithSizeLessThanOrEqualTo3)
{
const span<int> randomIntSpan = Random<span<int>>();
IS_TRUE(randomIntSpan.size() <= 3);

const span<string> randomStringSpan = Random<span<string>>();
IS_TRUE(randomStringSpan.size() <= 3);
}

TEST(Random_TIsAVector_ReturnsRandomVectorOfTWithSizeLessThanOrEqualTo3)
{
const vector<int> randomIntVector = Random<vector<int>>();
Expand Down

0 comments on commit ed10dc7

Please sign in to comment.