Skip to content

Commit

Permalink
add templated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredroy committed Nov 15, 2024
1 parent 7734dad commit a16d122
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Sofa/framework/Helper/test/accessor/ReadAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include <sofa/helper/accessor.h>
#include <sofa/type/vector.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Mat.h>

namespace sofa
{
Expand All @@ -46,8 +49,31 @@ TEST(ReadAccessor, VectorTypes)
EXPECT_EQ(accessor.size(), vector.size());
EXPECT_EQ(accessor.empty(), vector.empty());
EXPECT_EQ(accessor.begin(), vector.begin());
EXPECT_EQ(accessor.end(), vector.end());
EXPECT_EQ(accessor.end(), vector.end());
}

template <typename FixedArrayType>
class ReadAccessorFixedArray_test : public ::testing::Test
{
public:
ReadAccessorFixedArray_test() = default;

const FixedArrayType m_array{};
};

using FixedArrayTypes = ::testing::Types <
sofa::type::fixed_array<double, 5>, sofa::type::Vec < 2, float >, sofa::type::Mat<3, 3>>;

TYPED_TEST_SUITE(ReadAccessorFixedArray_test, FixedArrayTypes);

TYPED_TEST(ReadAccessorFixedArray_test, tests )
{
sofa::helper::ReadAccessor accessor(this->m_array);

EXPECT_EQ(TypeParam::static_size, accessor.size());
EXPECT_EQ(this->m_array.size(), accessor.size());
EXPECT_EQ(accessor.begin(), this->m_array.begin());
EXPECT_EQ(accessor.end(), this->m_array.end());
}

}
43 changes: 43 additions & 0 deletions Sofa/framework/Helper/test/accessor/WriteAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include <sofa/helper/accessor.h>
#include <sofa/type/vector.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Mat.h>
#include <string>

namespace sofa
Expand Down Expand Up @@ -104,5 +107,45 @@ TEST(WriteAccessor, VectorTypes)
EXPECT_EQ(pairVector.front().aString, std::string{"hello"});
}

template <typename FixedArrayType>
class WriteAccessorFixedArray_test : public ::testing::Test
{
public:
WriteAccessorFixedArray_test() = default;

FixedArrayType m_array{};
};

using FixedArrayTypes = ::testing::Types <
sofa::type::fixed_array<double, 5>, sofa::type::Vec < 2, float >, sofa::type::Mat<3, 3>>;

TYPED_TEST_SUITE(WriteAccessorFixedArray_test, FixedArrayTypes);

TYPED_TEST(WriteAccessorFixedArray_test, tests )
{
sofa::helper::WriteAccessor accessor(this->m_array);

EXPECT_EQ(TypeParam::static_size, accessor.size());
EXPECT_EQ(this->m_array.size(), accessor.size());
EXPECT_EQ(accessor.begin(), this->m_array.begin());
EXPECT_EQ(accessor.end(), this->m_array.end());

auto copy = this->m_array;
if constexpr (std::is_scalar_v<typename std::decay<decltype(this->m_array[0])>::type>)
{
constexpr auto increment = static_cast<typename std::decay<decltype(this->m_array[0])>::type>(1);
for(auto& v : accessor)
{
v = v + increment;
}

for(typename TypeParam::size_type i = 0 ; i < accessor.size() ; i++)
{
EXPECT_EQ(accessor[i], copy[i]+increment);
}
}

}


}

0 comments on commit a16d122

Please sign in to comment.