29 #include "DGtal/base/BasicTypes.h"
30 #include "DGtal/kernel/NumberTraits.h"
32 #include "DGtalCatch.h"
36 struct IntegralCstToTag
42 struct IntegralCstToTag<std::true_type>
48 struct IntegralCstToTag<std::false_type>
61 struct ValueToTag<true>
67 struct ValueToTag<false>
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) )
78 void checkParamRef(T
const&)
84 void checkFundamentalType()
87 using NL =
typename std::numeric_limits<T>;
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 );
103 REQUIRE( NT::min() == NL::min() );
105 REQUIRE( NT::digits() == NL::digits );
109 REQUIRE( NT::castToInt64_t(T(3.25)) == 3 );
110 REQUIRE( NT::castToDouble(T(3.25)) == (NL::is_integer ? 3. : 3.25) );
112 checkParamRef(NT::ZERO);
113 checkParamRef(NT::ONE);
117 template <
typename T>
118 void checkFundamentalIntegerType()
120 checkFundamentalType<T>();
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 );
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 );
134 template <
typename T>
135 void checkFundamentalFloatType()
137 checkFundamentalType<T>();
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 )
144 TEST_FUNDAMENTAL_INTEGER_TYPE(
signed char )
145 TEST_FUNDAMENTAL_INTEGER_TYPE(
unsigned char )
146 TEST_FUNDAMENTAL_INTEGER_TYPE(
signed short )
147 TEST_FUNDAMENTAL_INTEGER_TYPE(
unsigned short )
148 TEST_FUNDAMENTAL_INTEGER_TYPE(
signed int )
149 TEST_FUNDAMENTAL_INTEGER_TYPE(
unsigned int )
150 TEST_FUNDAMENTAL_INTEGER_TYPE(
signed long int )
151 TEST_FUNDAMENTAL_INTEGER_TYPE(
unsigned long int )
152 TEST_FUNDAMENTAL_INTEGER_TYPE(
signed long long int )
153 TEST_FUNDAMENTAL_INTEGER_TYPE(
unsigned long long int )
155 TEST_FUNDAMENTAL_INTEGER_TYPE( int8_t )
156 TEST_FUNDAMENTAL_INTEGER_TYPE( uint8_t )
157 TEST_FUNDAMENTAL_INTEGER_TYPE( int16_t )
158 TEST_FUNDAMENTAL_INTEGER_TYPE( uint16_t )
159 TEST_FUNDAMENTAL_INTEGER_TYPE( int32_t )
160 TEST_FUNDAMENTAL_INTEGER_TYPE( uint32_t )
161 TEST_FUNDAMENTAL_INTEGER_TYPE( int64_t )
162 TEST_FUNDAMENTAL_INTEGER_TYPE( uint64_t )
164 TEST_FUNDAMENTAL_FLOAT_TYPE(
float )
165 TEST_FUNDAMENTAL_FLOAT_TYPE(
double )
166 TEST_FUNDAMENTAL_FLOAT_TYPE(
long double )
168 #ifdef WITH_BIGINTEGER
176 REQUIRE_SAME_VALUE(
typename NT::IsBounded,
false );
177 REQUIRE_SAME_VALUE(
typename NT::IsSigned,
true );
178 REQUIRE_SAME_VALUE(
typename NT::IsUnsigned,
true );
179 REQUIRE_SAME_VALUE(
typename NT::IsIntegral,
true );
180 REQUIRE_SAME_VALUE(
typename NT::IsSpecialized,
true );
182 REQUIRE_SAME_TYPE(
typename NT::ReturnType, T );
183 REQUIRE_SAME_TYPE(
typename std::decay<NT::ParamType>::type, T );
188 REQUIRE( NT::even(T(42)) ==
true );
189 REQUIRE( NT::even(T(43)) ==
false );
190 REQUIRE( NT::odd(T(42)) ==
false );
191 REQUIRE( NT::odd(T(43)) ==
true );
196 REQUIRE( NT::castToInt64_t(T(3.25)) == 3 );
197 REQUIRE( NT::castToDouble(T(3.25)) == 3. );
199 checkParamRef(NT::ZERO);
200 checkParamRef(NT::ONE);
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Aim: The traits class for all models of Cinteger.
TEST_CASE("int container traits", "[int][traits]")
REQUIRE(domain.isInside(aPoint))