DGtal  1.5.beta
testIntegerConverter.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/kernel/PointVector.h"
33 #include "DGtal/kernel/IntegerConverter.h"
34 #include "DGtalCatch.h"
36 
37 using namespace std;
38 using namespace DGtal;
39 
40 
42 // Functions for testing class IntegerConverter.
44 
45 SCENARIO( "Integer types sizes", "[integer_conversions]" )
46 {
47  WHEN( "Checking integral types" ) {
48  THEN( "Integral types are progressive" ) {
49  CAPTURE( sizeof( int ) );
50  CAPTURE( sizeof( long ) );
51  CAPTURE( sizeof( long long ) );
52  REQUIRE( sizeof( int ) == 4 );
53  REQUIRE( sizeof( int ) <= sizeof( long ) );
54  REQUIRE( sizeof( long ) <= sizeof( long long ) );
55  REQUIRE( sizeof( long long ) == 8 );
56  }
57  }
58 }
59 SCENARIO( "IntegerConverter< 1, int32 >", "[integer_conversions]" )
60 {
61  typedef IntegerConverter< 1, DGtal::int32_t > Converter;
62  DGtal::int32_t small_int32 = 0x12345678;
63  DGtal::int64_t small_int64 = 0x12345678L;
64 #ifdef WITH_BIGINTEGER
65  DGtal::BigInteger small_bigint = 0x12345678;
66 #endif
67  WHEN( "Converting small integers" ) {
68  DGtal::int32_t a = Converter::cast( small_int32 );
69  DGtal::int32_t b = Converter::cast( small_int64 );
70 #ifdef WITH_BIGINTEGER
71  DGtal::int32_t c = Converter::cast( small_bigint );
72 #endif
73  THEN( "Their values are all identical" ) {
74  REQUIRE( a == small_int32 );
75  REQUIRE( a == b );
76 #ifdef WITH_BIGINTEGER
77  REQUIRE( a == c );
78 #endif
79  }
80  }
81  WHEN( "Converting medium integers" ) {
82  DGtal::int64_t medium_int64 = 0x123456789ABCDEFL;
83  DGtal::int32_t a = Converter::cast( medium_int64 );
84  THEN( "The value is lost with a warning" ) {
85  REQUIRE( DGtal::int64_t( a ) != medium_int64 );
86  }
87  }
88 }
89 
90 SCENARIO( "IntegerConverter< 1, int64 >", "[integer_conversions]" )
91 {
92  typedef IntegerConverter< 1, DGtal::int64_t > Converter;
93  DGtal::int32_t medium_int32 = DGtal::int32_t( 0x123456789ABCDEFL );
94  DGtal::int64_t medium_int64 = 0x123456789ABCDEFL;
95  #ifdef WITH_BIGINTEGER
96  DGtal::BigInteger medium_bigint = 0x123456789ABCDEFL;
97  #endif
98  WHEN( "Converting 64bits integers" ) {
99  DGtal::int64_t a = Converter::cast( medium_int32 );
100  DGtal::int64_t b = Converter::cast( medium_int64 );
101  #ifdef WITH_BIGINTEGER
102  DGtal::int64_t c = Converter::cast( medium_bigint );
103  #endif
104  THEN( "Only bigger integers are identical" ) {
105  REQUIRE( a == medium_int32 );
106  REQUIRE( a != b );
107  REQUIRE( b == medium_int64 );
108 #ifdef WITH_BIGINTEGER
109  REQUIRE( b == c );
110  #endif
111  }
112  THEN( "It gives the same results with NumberTraits" ) {
115 #ifdef WITH_BIGINTEGER
117 #endif
118  REQUIRE( a == ap );
119  REQUIRE( b == bp );
120 #ifdef WITH_BIGINTEGER
121  REQUIRE( c == cp );
122 #endif
123  }
124  }
125 }
126 
127 
128 #ifdef WITH_BIGINTEGER
129 SCENARIO( "IntegerConverter< 1, BigInteger >", "[integer_conversions]" )
130 {
132  DGtal::int32_t big_int32 = DGtal::int32_t( 0x123456789ABCDEFL );
133  DGtal::int64_t big_int64 = 0x123456789ABCDEFL;
134  DGtal::BigInteger big_bigint = 0x123456789ABCDEFL;
135  big_int32 *= big_int32;
136  big_int64 *= big_int64;
137  big_bigint *= big_bigint;
138  WHEN( "Converting big integers" ) {
139  DGtal::BigInteger a = Converter::cast( big_int32 );
140  DGtal::BigInteger b = Converter::cast( big_int64 );
141  DGtal::BigInteger c = Converter::cast( big_bigint );
142  DGtal::BigInteger b_prime;
143  detail::mpz_set_sll( b_prime.get_mpz_t(), big_int64 );
144  THEN( "Only bigger integers are identical" ) {
145  REQUIRE( a == big_int32 );
146  REQUIRE( a != b );
147  REQUIRE( b == b_prime );
148  REQUIRE( b != c );
149  REQUIRE( c == big_bigint );
150  }
151  }
152 }
153 #endif
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
----------— INTEGER/POINT CONVERSION SERVICES -----------------—
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:564
CAPTURE(thicknessHV)
SCENARIO("Integer types sizes", "[integer_conversions]")
REQUIRE(domain.isInside(aPoint))