Skip to content

Commit

Permalink
Add 'Common/TypeMapper' with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kachsheev committed Sep 17, 2024
1 parent b6b2b99 commit bde2a1d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/FlameIDE/Common/Traits/Functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,44 @@ struct DoSomeByIndex<T, SIZE, SIZE>
{}
};

///
/// @brief The TypeMappingTrait class
/// @tparam T1
/// @tparam T2
///
template<typename T1, typename T2>
struct TypeMappingTrait
{
using Type1 = T1;
using Type2 = T2;
};

///
/// @brief The TypeMapper class
/// @tparam T
/// @tparam TypeMappingTrait
/// @tparam ENABLE_STATIC_ASSERT
///
template<typename T, typename TypeMappingTrait, bool ENABLE_STATIC_ASSERT = true>
struct TypeMapper
{
using Type = typename ChooseType<
ComparingTypes<T, typename TypeMappingTrait::Type1>::VALUE
, typename TypeMappingTrait::Type2
, typename ChooseType<
ComparingTypes<T, typename TypeMappingTrait::Type2>::VALUE
, typename TypeMappingTrait::Type1
, Empty
>::Type
>::Type;

static_assert(
!ENABLE_STATIC_ASSERT
|| ComparingTypes<Type, Empty>::VALUE == FalseType::VALUE
, "Type does not include in chosen TypeMatchingTrait"
);
};

}

#endif // FLAMEIDE_COMMON_TRAITS_FUCTIONAL_HPP
49 changes: 49 additions & 0 deletions src/Common/Tests/FunctionalTraitTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <FlameIDE/../../src/Common/Tests/FunctionalTraitTest.hpp>

#include <FlameIDE/Common/Traits/Functional.hpp>

namespace flame_ide
{namespace common
{namespace tests
{

FunctionalTraitTest::FunctionalTraitTest() noexcept : ::AbstractTest{ "FunctionalTrait" }
{}

FunctionalTraitTest::~FunctionalTraitTest() noexcept = default;

int FunctionalTraitTest::vStart()
{
CHECK_RESULT_SUCCESS(doTestCase(
"TypeMapper"
, [this] { return typeMapper(); }
));
return ResultType::SUCCESS;
}

int FunctionalTraitTest::typeMapper()
{
using T1 = long long;
using T2 = unsigned long long;
using TestTypeMatchingTrait = TypeMappingTrait<T1, T2>;

using T2Result = TypeMapper<T1, TestTypeMatchingTrait>::Type;
using T1Result = TypeMapper<T2, TestTypeMatchingTrait>::Type;

const bool t1ComparationResult = ComparingTypes<T1, T1Result>::VALUE;
const bool t2ComparationResult = ComparingTypes<T2, T2Result>::VALUE;

IN_CASE_CHECK(t1ComparationResult == true);
IN_CASE_CHECK(t2ComparationResult == true);

using T3 = double;
// using T3Result = TypeMapper<T3, TestTypeMatchingTrait>::Type; // static assert
using T3Result = TypeMapper<T3, TestTypeMatchingTrait, false>::Type;

const bool t3ComparationResult = ComparingTypes<Empty, T3Result>::VALUE;
IN_CASE_CHECK(t3ComparationResult == true);

return ResultType::SUCCESS;
}

}}} // namespace flame_ide::common::tests
26 changes: 26 additions & 0 deletions src/Common/Tests/FunctionalTraitTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef FLAMEIDE_COMMON_TESTS_FUNCTIONALTRAITTEST_HPP
#define FLAMEIDE_COMMON_TESTS_FUNCTIONALTRAITTEST_HPP

#include <tests/Test.hpp>

namespace flame_ide
{namespace common
{namespace tests
{

class FunctionalTraitTest: public ::AbstractTest
{
public:
FunctionalTraitTest() noexcept;
~FunctionalTraitTest() noexcept override;

private:
int vStart() override;

private:
int typeMapper();
};

}}} // namespace flame_ide::common::tests

#endif // FLAMEIDE_COMMON_TESTS_FUNCTIONALTRAITTEST_HPP
2 changes: 2 additions & 0 deletions src/Common/Tests/Sources.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set (SOURCE_LIST
${CMAKE_CURRENT_SOURCE_DIR}/ExpectedTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ExpectedTest.hpp
${CMAKE_CURRENT_SOURCE_DIR}/FunctionalTraitTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/FunctionalTraitTest.hpp
${CMAKE_CURRENT_SOURCE_DIR}/TestAggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestAggregator.hpp
${CMAKE_CURRENT_SOURCE_DIR}/UtilsTest.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/Common/Tests/TestAggregator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <FlameIDE/../../src/Common/Tests/UtilsTest.hpp>
#include <FlameIDE/../../src/Common/Tests/ExpectedTest.hpp>
#include <FlameIDE/../../src/Common/Tests/VoidTypeTest.hpp>
#include <FlameIDE/../../src/Common/Tests/FunctionalTraitTest.hpp>

#include <FlameIDE/../../src/Common/Tests/TestAggregator.hpp>

Expand All @@ -14,6 +15,7 @@ TestAggregator::TestAggregator() : ::TestAggregator("Common")
pushBackTest(std::make_shared<UtilsTest>());
pushBackTest(std::make_shared<ExpectedTest>());
pushBackTest(std::make_shared<VoidTypeTest>());
pushBackTest(std::make_shared<FunctionalTraitTest>());
}

}}} // namespace flame_ide::common::tests

0 comments on commit bde2a1d

Please sign in to comment.