DGtal  1.2.0
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  DGtal::BigInteger small_bigint = 0x12345678;
65  WHEN( "Converting small integers" ) {
66  DGtal::int32_t a = Converter::cast( small_int32 );
67  DGtal::int32_t b = Converter::cast( small_int64 );
68  DGtal::int32_t c = Converter::cast( small_bigint );
69  THEN( "Their values are all identical" ) {
70  REQUIRE( a == small_int32 );
71  REQUIRE( a == b );
72  REQUIRE( a == c );
73  }
74  }
75  WHEN( "Converting medium integers" ) {
76  DGtal::int64_t medium_int64 = 0x123456789ABCDEFL;
77  DGtal::int32_t a = Converter::cast( medium_int64 );
78  THEN( "The value is lost with a warning" ) {
79  REQUIRE( DGtal::int64_t( a ) != medium_int64 );
80  }
81  }
82 }
83 
84 SCENARIO( "IntegerConverter< 1, int64 >", "[integer_conversions]" )
85 {
86  typedef IntegerConverter< 1, DGtal::int64_t > Converter;
87  DGtal::int32_t medium_int32 = DGtal::int32_t( 0x123456789ABCDEFL );
88  DGtal::int64_t medium_int64 = 0x123456789ABCDEFL;
89  DGtal::BigInteger medium_bigint = 0x123456789ABCDEFL;
90  WHEN( "Converting 64bits integers" ) {
91  DGtal::int64_t a = Converter::cast( medium_int32 );
92  DGtal::int64_t b = Converter::cast( medium_int64 );
93  DGtal::int64_t c = Converter::cast( medium_bigint );
94  THEN( "Only bigger integers are identical" ) {
95  REQUIRE( a == medium_int32 );
96  REQUIRE( a != b );
97  REQUIRE( b == medium_int64 );
98  REQUIRE( b == c );
99  }
100  THEN( "It gives the same results with NumberTraits" ) {
104  REQUIRE( a == ap );
105  REQUIRE( b == bp );
106  REQUIRE( c == cp );
107  }
108  }
109 }
110 
111 SCENARIO( "IntegerConverter< 1, BigInteger >", "[integer_conversions]" )
112 {
114  DGtal::int32_t big_int32 = DGtal::int32_t( 0x123456789ABCDEFL );
115  DGtal::int64_t big_int64 = 0x123456789ABCDEFL;
116  DGtal::BigInteger big_bigint = 0x123456789ABCDEFL;
117  big_int32 *= big_int32;
118  big_int64 *= big_int64;
119  big_bigint *= big_bigint;
120  WHEN( "Converting big integers" ) {
121  DGtal::BigInteger a = Converter::cast( big_int32 );
122  DGtal::BigInteger b = Converter::cast( big_int64 );
123  DGtal::BigInteger c = Converter::cast( big_bigint );
124  DGtal::BigInteger b_prime;
125  detail::mpz_set_sll( b_prime.get_mpz_t(), big_int64 );
126  THEN( "Only bigger integers are identical" ) {
127  REQUIRE( a == big_int32 );
128  REQUIRE( a != b );
129  REQUIRE( b == b_prime );
130  REQUIRE( b != c );
131  REQUIRE( c == big_bigint );
132  }
133  }
134 }
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:533
CAPTURE(thicknessHV)
SCENARIO("Integer types sizes", "[integer_conversions]")
REQUIRE(domain.isInside(aPoint))