DGtal 2.1.0
Loading...
Searching...
No Matches
testNumberTraits.cpp
1
27#include <limits>
28
29#include "DGtal/base/BasicTypes.h"
30#include "DGtal/kernel/NumberTraits.h"
31
32#include "DGtalCatch.h"
33
35template <typename T>
40
41template <>
42struct IntegralCstToTag<std::true_type>
43{
45};
46
47template <>
48struct IntegralCstToTag<std::false_type>
49{
51};
52
54template <bool V>
56{
58};
59
60template <>
61struct ValueToTag<true>
62{
64};
65
66template <>
67struct ValueToTag<false>
68{
70};
71
72#define REQUIRE_SAME_TAG(a, b) REQUIRE( (std::is_same<a, typename IntegralCstToTag<b>::type>::value) )
73#define REQUIRE_SAME_VALUE(a, b) REQUIRE( (std::is_same<a, typename ValueToTag<b>::type>::value) )
74#define REQUIRE_SAME_TYPE(a, b) REQUIRE( (std::is_same<a, b>::value) )
75
77template <typename T>
78void checkParamRef(T const&)
79{
80}
81
83template <typename T>
84void checkFundamentalType()
85{
86 using NT = typename DGtal::NumberTraits<T>;
87 using NL = typename std::numeric_limits<T>;
88
89 REQUIRE_SAME_VALUE( typename NT::IsBounded, NL::is_bounded );
90 REQUIRE_SAME_VALUE( typename NT::IsSigned, NL::is_signed );
91 REQUIRE_SAME_VALUE( typename NT::IsUnsigned, ! NL::is_signed );
92 REQUIRE_SAME_VALUE( typename NT::IsIntegral, NL::is_integer );
93 REQUIRE_SAME_VALUE( typename NT::IsSpecialized, true );
94 REQUIRE_SAME_TYPE( typename NT::ReturnType, T );
95 REQUIRE_SAME_TYPE( typename std::decay<typename NT::ParamType>::type, T );
96
97 REQUIRE( NT::ZERO == T(0) );
98 REQUIRE( NT::ONE == T(1) );
99
100 REQUIRE( NT::zero() == T(0) );
101 REQUIRE( NT::one() == T(1) );
102
103 REQUIRE( NT::min() == NL::min() );
104 REQUIRE( NT::max() == NL::max() );
105 REQUIRE( NT::digits() == NL::digits );
106 REQUIRE( NT::isBounded() == (NL::is_bounded ? DGtal::BOUNDED : DGtal::UNBOUNDED) );
107 REQUIRE( NT::isSigned() == (NL::is_signed ? DGtal::SIGNED : DGtal::UNSIGNED) );
108
109 REQUIRE( NT::castToInt64_t(T(3.25)) == 3 );
110 REQUIRE( NT::castToDouble(T(3.25)) == (NL::is_integer ? 3. : 3.25) );
111
112 checkParamRef(NT::ZERO);
113 checkParamRef(NT::ONE);
114}
115
117template <typename T>
118void checkFundamentalIntegerType()
119{
120 checkFundamentalType<T>();
121
122 using NT = typename DGtal::NumberTraits<T>;
123
124 REQUIRE_SAME_TYPE( typename NT::SignedVersion, typename std::make_signed<T>::type );
125 REQUIRE_SAME_TYPE( typename NT::UnsignedVersion, typename std::make_unsigned<T>::type );
126
127 REQUIRE( NT::even(T(42)) == true );
128 REQUIRE( NT::even(T(43)) == false );
129 REQUIRE( NT::odd(T(42)) == false );
130 REQUIRE( NT::odd(T(43)) == true );
131}
132
134template <typename T>
135void checkFundamentalFloatType()
136{
137 checkFundamentalType<T>();
138}
139
140#define TEST_TYPE_TRAITS( test, a ) TEST_CASE( #a ) { test<a>(); }
141#define TEST_FUNDAMENTAL_INTEGER_TYPE( a ) TEST_TYPE_TRAITS( checkFundamentalIntegerType, a )
142#define TEST_FUNDAMENTAL_FLOAT_TYPE( a ) TEST_TYPE_TRAITS( checkFundamentalFloatType, a )
143
144TEST_FUNDAMENTAL_INTEGER_TYPE( signed char )
145TEST_FUNDAMENTAL_INTEGER_TYPE( unsigned char )
146TEST_FUNDAMENTAL_INTEGER_TYPE( signed short )
147TEST_FUNDAMENTAL_INTEGER_TYPE( unsigned short )
148TEST_FUNDAMENTAL_INTEGER_TYPE( signed int )
149TEST_FUNDAMENTAL_INTEGER_TYPE( unsigned int )
150TEST_FUNDAMENTAL_INTEGER_TYPE( signed long int )
151TEST_FUNDAMENTAL_INTEGER_TYPE( unsigned long int )
152TEST_FUNDAMENTAL_INTEGER_TYPE( signed long long int )
153TEST_FUNDAMENTAL_INTEGER_TYPE( unsigned long long int )
154
155TEST_FUNDAMENTAL_INTEGER_TYPE( int8_t )
156TEST_FUNDAMENTAL_INTEGER_TYPE( uint8_t )
157TEST_FUNDAMENTAL_INTEGER_TYPE( int16_t )
158TEST_FUNDAMENTAL_INTEGER_TYPE( uint16_t )
159TEST_FUNDAMENTAL_INTEGER_TYPE( int32_t )
160TEST_FUNDAMENTAL_INTEGER_TYPE( uint32_t )
161TEST_FUNDAMENTAL_INTEGER_TYPE( int64_t )
162TEST_FUNDAMENTAL_INTEGER_TYPE( uint64_t )
163
164TEST_FUNDAMENTAL_FLOAT_TYPE( float )
165TEST_FUNDAMENTAL_FLOAT_TYPE( double )
166TEST_FUNDAMENTAL_FLOAT_TYPE( long double )
167
168
169
170TEST_CASE( "BigInteger" )
171{
172 using T = DGtal::BigInteger;
173 using NT = typename DGtal::NumberTraits<T>;
174
175 REQUIRE_SAME_VALUE( typename NT::IsBounded, false );
176 REQUIRE_SAME_VALUE( typename NT::IsSigned, true );
177 REQUIRE_SAME_VALUE( typename NT::IsUnsigned, true );
178 REQUIRE_SAME_VALUE( typename NT::IsIntegral, true );
179 REQUIRE_SAME_VALUE( typename NT::IsSpecialized, true );
180
181 REQUIRE_SAME_TYPE( typename NT::ReturnType, T );
182 REQUIRE_SAME_TYPE( typename std::decay<NT::ParamType>::type, T );
183
184 REQUIRE( NT::zero() == T(0) );
185 REQUIRE( NT::one() == T(1) );
186
187 REQUIRE( NT::even(T(42)) == true );
188 REQUIRE( NT::even(T(43)) == false );
189 REQUIRE( NT::odd(T(42)) == false );
190 REQUIRE( NT::odd(T(43)) == true );
191
192 REQUIRE( NT::isBounded() == DGtal::UNBOUNDED );
193 REQUIRE( NT::isSigned() == DGtal::SIGNED );
194
195 REQUIRE( NT::castToInt64_t(T(3.25)) == 3 );
196 REQUIRE( NT::castToDouble(T(3.25)) == 3. );
197
198 checkParamRef(NT::ZERO);
199 checkParamRef(NT::ONE);
200}
DGtal is the top-level namespace which contains all DGtal functions and types.
@ UNBOUNDED
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>, boost::multiprecision::et_off > BigInteger
Definition BasicTypes.h:75
STL namespace.
Aim: The traits class for all models of Cinteger.
Transform a std::integral_constant<bool, value> to the corresponding DGtal tag.
Transform a boolean value to the corresponding DGtal tag.
TEST_CASE("int container traits", "[int][traits]")
REQUIRE(domain.isInside(aPoint))