DGtal  1.0.0
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 
35 template <typename T>
36 struct IntegralCstToTag
37 {
38  using type = DGtal::TagUnknown;
39 };
40 
41 template <>
42 struct IntegralCstToTag<std::true_type>
43 {
44  using type = DGtal::TagTrue;
45 };
46 
47 template <>
48 struct IntegralCstToTag<std::false_type>
49 {
50  using type = DGtal::TagFalse;
51 };
52 
54 template <bool V>
55 struct ValueToTag
56 {
57  using type = DGtal::TagUnknown;
58 };
59 
60 template <>
61 struct ValueToTag<true>
62 {
63  using type = DGtal::TagTrue;
64 };
65 
66 template <>
67 struct ValueToTag<false>
68 {
69  using type = DGtal::TagFalse;
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 
77 template <typename T>
78 void checkParamRef(T const&)
79 {
80 }
81 
83 template <typename T>
84 void 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 
117 template <typename T>
118 void 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 
134 template <typename T>
135 void 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 
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 )
154 
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 )
163 
164 TEST_FUNDAMENTAL_FLOAT_TYPE( float )
165 TEST_FUNDAMENTAL_FLOAT_TYPE( double )
166 TEST_FUNDAMENTAL_FLOAT_TYPE( long double )
167 
168 #ifdef WITH_BIGINTEGER
169 
171 TEST_CASE( "BigInteger" )
172 {
173  using T = DGtal::BigInteger;
174  using NT = typename DGtal::NumberTraits<T>;
175 
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 );
181 
182  REQUIRE_SAME_TYPE( typename NT::ReturnType, T );
183  REQUIRE_SAME_TYPE( typename std::decay<NT::ParamType>::type, T );
184 
185  REQUIRE( NT::zero() == T(0) );
186  REQUIRE( NT::one() == T(1) );
187 
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 );
192 
193  REQUIRE( NT::isBounded() == DGtal::UNBOUNDED );
194  REQUIRE( NT::isSigned() == DGtal::SIGNED );
195 
196  REQUIRE( NT::castToInt64_t(T(3.25)) == 3 );
197  REQUIRE( NT::castToDouble(T(3.25)) == 3. );
198 
199  checkParamRef(NT::ZERO);
200  checkParamRef(NT::ONE);
201 }
202 
203 #endif
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
REQUIRE(domain.isInside(aPoint))
TEST_CASE("int container traits", "[int][traits]")
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:531
int max(int a, int b)