DGtal 1.3.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>
36struct IntegralCstToTag
37{
38 using type = DGtal::TagUnknown;
39};
40
41template <>
42struct IntegralCstToTag<std::true_type>
43{
44 using type = DGtal::TagTrue;
45};
46
47template <>
48struct IntegralCstToTag<std::false_type>
49{
50 using type = DGtal::TagFalse;
51};
52
54template <bool V>
55struct ValueToTag
56{
57 using type = DGtal::TagUnknown;
58};
59
60template <>
61struct ValueToTag<true>
62{
63 using type = DGtal::TagTrue;
64};
65
66template <>
67struct 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
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#ifdef WITH_BIGINTEGER
169
171TEST_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
@ UNBOUNDED
Definition: NumberTraits.h:54
@ BOUNDED
Definition: NumberTraits.h:54
@ UNSIGNED
Definition: NumberTraits.h:57
@ SIGNED
Definition: NumberTraits.h:57
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
STL namespace.
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:564
TEST_CASE("int container traits", "[int][traits]")
REQUIRE(domain.isInside(aPoint))