Skip to content

Commit

Permalink
197 unit tests for tefixedarray (#353)
Browse files Browse the repository at this point in the history
* Create TEFixedArray unit test, included array test in the cmake
* Completed the fixe array unit tests.
  • Loading branch information
aregtech authored Aug 2, 2024
1 parent d570ee5 commit 17a9f44
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 11 deletions.
12 changes: 4 additions & 8 deletions framework/areg/base/TEFixedArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,9 @@ template< typename VALUE >
inline bool TEFixedArray<VALUE>::contains(const VALUE& elemSearch, uint32_t startAt /*= 0*/) const
{
bool result = false;
for (uint32_t i = startAt; i < mElemCount; ++i)
for (uint32_t i = startAt; (result == false) && (i < mElemCount); ++i)
{
if (mValueList[i] == elemSearch)
{
result = true;
break;
}
result = mValueList[i] == elemSearch;
}

return result;
Expand Down Expand Up @@ -512,11 +508,11 @@ template< typename VALUE >
inline uint32_t TEFixedArray<VALUE>::find(const VALUE& elemSearch, uint32_t startAt /* = 0 */) const
{
uint32_t result = static_cast<uint32_t>(NECommon::INVALID_INDEX);
for (uint32_t i = 0; i < mElemCount; ++i)
for (uint32_t i = startAt; i < mElemCount; ++i)
{
if (elemSearch == mValueList[i])
{
result = static_cast<int>(i);
result = i;
break;
}
}
Expand Down
123 changes: 120 additions & 3 deletions tests/units/FixedArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
TEST(FixedArrayTest, TestConstructors)
{
using FixedArray = TEFixedArray<int>;
constexpr uint32_t elemCount{ 10u };

FixedArray empty, notEmpty(10);
FixedArray empty, notEmpty(elemCount);

EXPECT_TRUE(empty.isEmpty());
EXPECT_FALSE(notEmpty.isEmpty());
EXPECT_EQ(notEmpty.getSize(), elemCount);

for (uint32_t i = 0; i < notEmpty.getSize(); ++i)
{
Expand Down Expand Up @@ -66,15 +68,130 @@ TEST(FixedArrayTest, TestConstructors)
EXPECT_TRUE(src.isEmpty());
EXPECT_FALSE(toMove.isEmpty());
EXPECT_EQ(toCopy, toMove);

toMove.clear();
EXPECT_TRUE(toMove.isEmpty());
EXPECT_EQ(toMove.getSize(), 0u);
}

/**
* \brief Testing copy and move methods of Fixed Array
**/
TEST(FixedArrayTest, TestCopyMove)
{
using FixedArray = TEFixedArray<int>;
constexpr uint32_t elemCount{ 10u };

FixedArray empty, notEmpty(elemCount);

for (uint32_t i = 0; i < notEmpty.getSize(); ++i)
{
notEmpty[i] = static_cast<int>(i);
}

empty.copy(notEmpty);
EXPECT_EQ(empty, notEmpty);

FixedArray toCopy;
toCopy.copy(notEmpty);
EXPECT_FALSE(notEmpty.isEmpty());
EXPECT_FALSE(toCopy.isEmpty());
EXPECT_EQ(empty, toCopy);

FixedArray toMove;
toMove.move(std::move(notEmpty));
EXPECT_TRUE(notEmpty.isEmpty());
EXPECT_FALSE(toMove.isEmpty());
EXPECT_EQ(toCopy, toMove);

FixedArray src(15);
for (uint32_t i = 0; i < src.getSize(); ++i)
{
src[i] = 10 + static_cast<int>(i);
}

toCopy.copy(src);
EXPECT_FALSE(src.isEmpty());
EXPECT_FALSE(toCopy.isEmpty());
EXPECT_EQ(src, toCopy);
EXPECT_NE(empty, toCopy);

toMove.move(std::move(src));
EXPECT_TRUE(src.isEmpty());
EXPECT_FALSE(toMove.isEmpty());
EXPECT_EQ(toCopy, toMove);

toMove.clear();
EXPECT_TRUE(toMove.isEmpty());
EXPECT_EQ(toMove.getSize(), 0u);
}

/**
* \brief Test values of the fixed array
**/
TEST(FixedArrayTest, TestValues)
{
using FixedArray = TEFixedArray<int>;
constexpr uint32_t elemCount{ 10u };
constexpr int32_t arr[]{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
constexpr int len{ MACRO_ARRAYLEN(arr) };

FixedArray notEmpty(elemCount);
const int* dst = notEmpty.getValues();
const int* src = arr;
for (uint32_t i = elemCount; i > 0; -- i)
{
EXPECT_TRUE(notEmpty.isValidIndex(elemCount - i));
notEmpty.setAt(elemCount - i, *src ++);
}

EXPECT_TRUE(::memcmp(dst, arr, elemCount * sizeof(int)) == 0);
}

/**
* \brief Testing searching methods like 'find' and 'contains' of fixed array
**/
TEST(FixedArrayTest, TestSearchElem)
{
using FixedArray = TEFixedArray<int>;
constexpr uint32_t elemCount{ 10u };

FixedArray notEmpty(elemCount);
for (uint32_t i = 0; i < notEmpty.getSize(); ++i)
{
notEmpty[i] = static_cast<int>(i);
}

for (uint32_t i = 0; i < notEmpty.getSize(); ++i)
{
EXPECT_TRUE(notEmpty.contains(i, 0));
EXPECT_TRUE(notEmpty.contains(i, i));
EXPECT_FALSE(notEmpty.contains(i, i + 1));
EXPECT_FALSE(notEmpty.contains(i + elemCount, 0));

EXPECT_EQ(notEmpty.find(i, 0), i);
EXPECT_EQ(notEmpty.find(i, i), i);
EXPECT_EQ(notEmpty.find(i, i + elemCount), static_cast<uint32_t>(NECommon::INVALID_INDEX));
EXPECT_EQ(notEmpty.find(i + elemCount, 0), static_cast<uint32_t>(NECommon::INVALID_INDEX));
}
}

TEST(FixedArrayTest, TestSearches)
/**
* \brief Testing the position of the element
**/
TEST(FixedArrayTest, TestElemPosition)
{
using FixedArray = TEFixedArray<int>;
constexpr uint32_t elemCount{ 10u };

FixedArray empty, notEmpty(10);
FixedArray notEmpty(elemCount);
for (uint32_t i = 0; i < notEmpty.getSize(); ++i)
{
notEmpty[i] = static_cast<int>(i);
EXPECT_EQ(notEmpty.getAt(i), static_cast<int>(i));
EXPECT_EQ(notEmpty.getAt(i), notEmpty.valueAtPosition(i));
}

EXPECT_EQ(notEmpty.firstEntry(), 0u);
EXPECT_EQ(notEmpty.lastEntry(), elemCount - 1u);
}

0 comments on commit 17a9f44

Please sign in to comment.