Radium Engine  1.7.2
Loading...
Searching...
No Matches
typeutils.cpp
1#include <Core/Containers/VectorArray.hpp>
2#include <Core/Types.hpp>
3#include <Core/Utils/Color.hpp>
4#include <Core/Utils/TypesUtils.hpp>
5#include <catch2/catch_test_macros.hpp>
6
7namespace TypeTests {
8struct TypeName_struct {};
9
10struct SimpleStruct {
11 std::vector<std::string> strings; // cppcheck-suppress unusedStructMember
12 bool boolean; // cppcheck-suppress unusedStructMember
13};
14using PairAlias = std::pair<std::string, bool>;
15
16} // namespace TypeTests
17TEST_CASE( "Core/Utils/TypesUtils", "[unittests][Core][Utils][TypesUtils]" ) {
18 SECTION( "Demangle from typename" ) {
19 using Ra::Core::Utils::demangleType;
20 using Ra::Core::Utils::simplifiedDemangledType;
21
22 REQUIRE( demangleType<int>() == "int" );
23 REQUIRE( demangleType<float>() == "float" );
24 REQUIRE( demangleType<uint>() == "unsigned int" );
25 REQUIRE( demangleType<size_t>() == "unsigned long" );
26
27 REQUIRE( demangleType( std::vector<int> {} ) == "std::vector<int, std::allocator<int>>" );
28 REQUIRE( demangleType<std::vector<int>>() == "std::vector<int, std::allocator<int>>" );
29 REQUIRE( demangleType( std::type_index( typeid( std::vector<int> ) ) ) ==
30 "std::vector<int, std::allocator<int>>" );
31
32 REQUIRE( demangleType( std::vector<float> {} ) ==
33 "std::vector<float, std::allocator<float>>" );
34 REQUIRE( demangleType<std::vector<float>>() ==
35 "std::vector<float, std::allocator<float>>" );
36 REQUIRE( demangleType( std::type_index( typeid( std::vector<float> ) ) ) ==
37 "std::vector<float, std::allocator<float>>" );
38
39 std::string string_vector_type =
40 "std::vector<std::basic_string<char, std::char_traits<char>, "
41 "std::allocator<char>>, std::allocator<std::basic_string<char, "
42 "std::char_traits<char>, std::allocator<char>>>>";
43 REQUIRE( demangleType( std::vector<std::string> {} ) == string_vector_type );
44 REQUIRE( demangleType<std::vector<std::string>>() == string_vector_type );
45 REQUIRE( demangleType( std::type_index( typeid( std::vector<std::string> ) ) ) ==
46 string_vector_type );
47 std::string simplified_string_vector_type = "vector<string>";
48 REQUIRE( simplifiedDemangledType( std::vector<std::string> {} ) ==
49 simplified_string_vector_type );
50 REQUIRE( simplifiedDemangledType<std::vector<std::string>>() ==
51 simplified_string_vector_type );
52 REQUIRE( simplifiedDemangledType( std::type_index( typeid( std::vector<std::string> ) ) ) ==
53 simplified_string_vector_type );
54
56 "std::unordered_map<std::basic_string<char, std::char_traits<char>, "
57 "std::allocator<char>>, TypeTests::SimpleStruct, "
58 "std::hash<std::basic_string<char, std::char_traits<char>, "
59 "std::allocator<char>>>, std::equal_to<std::basic_string<char, "
60 "std::char_traits<char>, std::allocator<char>>>, "
61 "std::allocator<std::pair<std::basic_string<char, "
62 "std::char_traits<char>, "
63 "std::allocator<char>> const, TypeTests::SimpleStruct>>>" );
64
65 REQUIRE( demangleType<TypeTests::TypeName_struct>() == "TypeTests::TypeName_struct" );
66 REQUIRE( demangleType<TypeTests::SimpleStruct>() == "TypeTests::SimpleStruct" );
67
68 REQUIRE( demangleType<TypeTests::PairAlias>() ==
69 "std::pair<std::basic_string<char, std::char_traits<char>, "
70 "std::allocator<char>>, bool>" );
71 REQUIRE( demangleType<std::vector<TypeTests::PairAlias>>() ==
72 "std::vector<std::pair<std::basic_string<char, std::char_traits"
73 "<char>, std::allocator<char>>, bool>, std::allocator<std::pair<std::"
74 "basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>>>" );
75
76 REQUIRE( simplifiedDemangledType<std::vector<float>>() == "vector<Scalar>" );
77 REQUIRE(
79 "unordered_map<string, TypeTests::SimpleStruct>" );
80 REQUIRE( simplifiedDemangledType<TypeTests::PairAlias>() == "pair<string, bool>" );
81 REQUIRE( simplifiedDemangledType<std::vector<TypeTests::PairAlias>>() ==
82 "vector<pair<string, bool>>" );
83 }
84
85 SECTION( "Demangle from instance" ) {
86 using Ra::Core::Utils::demangleType;
87
88 int i { 1 };
89 float f { 2 };
90 unsigned int u { 3 };
91 size_t s { 4 };
92
93 REQUIRE( demangleType( i ) == "int" );
94 REQUIRE( demangleType( f ) == "float" );
95 REQUIRE( demangleType( u ) == "unsigned int" );
96 REQUIRE( demangleType( s ) == "unsigned long" );
97
98#ifndef _WIN32
99 // this segfault on windows due to out_of_bound exception. why ???
101 auto demangledName = demangleType( v );
102 REQUIRE( demangledName == "std::vector<int, std::allocator<int>>" );
103#endif
104 TypeTests::TypeName_struct tns;
105 auto demangledNameFromStruct = demangleType( tns );
106 REQUIRE( demangledNameFromStruct == "TypeTests::TypeName_struct" );
107 }
108
109 SECTION( "Type traits" ) {
110 using namespace Ra::Core::Utils;
111 REQUIRE( is_container<Scalar>::value == false );
112 REQUIRE( is_container<Ra::Core::Vector3>::value == false );
113 REQUIRE( is_container<Ra::Core::Utils::Color>::value == false );
114 REQUIRE( is_container<Ra::Core::VectorArray<Ra::Core::Vector3>>::value == true );
115 REQUIRE( is_container<std::array<unsigned int, 2>>::value == true );
116 REQUIRE( is_container<std::map<size_t, std::string>>::value == true );
117 REQUIRE( is_container<std::vector<Scalar>>::value == true );
118 }
119}
This class implements ContainerIntrospectionInterface for AlignedStdVector.