Skip to content
Snippets Groups Projects
Commit 5907c128 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add tests for CastArray

parent 53c3ad8b
No related branches found
No related tags found
1 merge request!63Feature/utils coverage
......@@ -8,7 +8,7 @@
#include <iostream>
template <typename DataType, typename CastDataType>
class CastArray
class [[nodiscard]] CastArray
{
public:
using data_type = CastDataType;
......@@ -20,15 +20,13 @@ class CastArray
public:
PUGS_INLINE
const size_t&
size() const
const size_t& size() const
{
return m_size;
}
PUGS_INLINE
CastDataType&
operator[](size_t i) const
CastDataType& operator[](size_t i) const
{
Assert(i < m_size);
return m_values[i];
......@@ -40,11 +38,10 @@ class CastArray
PUGS_INLINE
CastArray& operator=(CastArray&&) = default;
PUGS_INLINE
CastArray(const Array<DataType>& array)
explicit CastArray(const Array<DataType>& array)
: m_array(array),
m_size(sizeof(DataType) * array.size() / sizeof(CastDataType)),
m_values((array.size() == 0) ? nullptr : reinterpret_cast<CastDataType*>(&(array[0])))
m_values((array.size() == 0) ? nullptr : reinterpret_cast<CastDataType*>(&(static_cast<DataType&>(array[0]))))
{
static_assert((std::is_const_v<CastDataType> and std::is_const_v<DataType>) or (not std::is_const_v<DataType>),
"CastArray cannot remove const attribute");
......@@ -54,9 +51,8 @@ class CastArray
}
}
PUGS_INLINE
CastArray(DataType& value)
: m_size(sizeof(DataType) / sizeof(CastDataType)), m_values(reinterpret_cast<CastDataType*>(&(value)))
explicit CastArray(DataType & value)
: m_size(sizeof(DataType) / sizeof(CastDataType)), m_values(reinterpret_cast<CastDataType*>(&value))
{
static_assert((std::is_const_v<CastDataType> and std::is_const_v<DataType>) or (not std::is_const_v<DataType>),
"CastArray cannot remove const attribute");
......
......@@ -49,6 +49,7 @@ add_executable (unit_tests
test_BuiltinFunctionEmbedder.cpp
test_BuiltinFunctionEmbedderTable.cpp
test_BuiltinFunctionProcessor.cpp
test_CastArray.cpp
test_CG.cpp
test_ContinueProcessor.cpp
test_ConcatExpressionProcessor.cpp
......
#ifndef TEST_CAST_ARRAY_HPP
#define TEST_CAST_ARRAY_HPP
#include <catch2/catch.hpp>
#include <utils/ArrayUtils.hpp>
#include <utils/CastArray.hpp>
// clazy:excludeall=non-pod-global-static
TEST_CASE("CastArray", "[utils]")
{
SECTION("explicit cast Array -> CastArray")
{
Array<double> x_double{5};
x_double[0] = 1;
x_double[1] = 2;
x_double[2] = 3;
x_double[3] = 4;
x_double[4] = 5;
CastArray<double, char> x_char{x_double};
REQUIRE(x_char.size() * sizeof(char) == x_double.size() * sizeof(double));
Array<char> y_char{x_char.size()};
for (size_t i = 0; i < x_char.size(); ++i) {
y_char[i] = x_char[i];
}
CastArray<char, double> y_double{y_char};
REQUIRE(y_char.size() * sizeof(char) == y_double.size() * sizeof(double));
REQUIRE(&(y_double[0]) != &(x_double[0]));
for (size_t i = 0; i < y_double.size(); ++i) {
REQUIRE(y_double[i] == x_double[i]);
}
}
SECTION("explicit cast value -> CastArray")
{
double x = 3;
CastArray<double, char> x_char(x);
REQUIRE(x_char.size() * sizeof(char) == sizeof(double));
}
SECTION("invalid cast array")
{
Array<char> x_char{13};
REQUIRE_THROWS_WITH((CastArray<char, double>{x_char}),
"unexpected error: cannot cast array to the chosen data type");
}
SECTION("cast array utilities")
{
SECTION("Array -> CastArray")
{
Array<double> x_double{5};
x_double[0] = 1.3;
x_double[1] = 3.2;
x_double[2] = -4;
x_double[3] = 6.2;
x_double[4] = -1.6;
CastArray<double, short> x_short{x_double};
auto x_short_from = cast_array_to<short>::from(x_double);
REQUIRE(x_short_from.size() == x_short.size());
for (size_t i = 0; i < x_short_from.size(); ++i) {
REQUIRE(x_short_from[i] == x_short[i]);
}
}
SECTION("Value -> CastArray")
{
double x = 3.14;
CastArray<double, short> x_short{x};
auto x_short_from = cast_value_to<short>::from(x);
REQUIRE(x_short_from.size() == x_short.size());
for (size_t i = 0; i < x_short_from.size(); ++i) {
REQUIRE(x_short_from[i] == x_short[i]);
}
}
}
}
#endif // TEST_CAST_ARRAY_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment