DGtal  1.4.beta
testIntegralIntervals.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <vector>
33 #include <algorithm>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/IntegralIntervals.h"
36 #include "DGtalCatch.h"
38 
39 using namespace std;
40 using namespace DGtal;
41 
42 
44 // Functions for testing class IntegralIntervals.
46 
47 SCENARIO( "IntegralIntervals< int > unit tests", "[intervals]" )
48 {
49  typedef int Integer;
50  typedef IntegralIntervals< Integer > Intervals;
51 
52  Intervals V;
53  WHEN( "Inserting many intervals" ) {
54  int nb = 1000;
55  int nb_ok = 0;
56  int nb_count_ok = 0;
57  std::set< int > X;
58  for ( int i = 0; i < nb; i++ )
59  {
60  int f = rand() % 1000;
61  int l = std::min( f + rand() % 10, 999 );
62  // std::cout << "V + (" << f << "," << l << ") = ";
63  V.insert( f, l );
64  for ( int k = f; k <= l; k++ ) X.insert( k );
65  nb_ok += V.isValid() ? 1 : 0;
66  nb_count_ok += ( V.size() == X.size() ) ? 1 : 0;
67  if ( ! V.isValid() )
68  std::cout << V << " => " << ( V.isValid() ? "OK" : "ERROR" ) << std::endl;
69  if ( V.size() != X.size() )
70  {
71  std::cout << "Bad count #V=" << V.size() << " #X=" << X.size()
72  << std::endl;
73  for ( auto j : X ) std::cout << " " << j;
74  std::cout << std::endl;
75  break;
76  }
77  }
78  THEN( "The object remains consistent." ) {
79  REQUIRE( nb == nb_ok );
80  REQUIRE( nb == nb_count_ok );
81  }
82  }
83 
84  WHEN( "Erasing many intervals" ) {
85  int nb = 1000;
86  int nb_ok = 0;
87  int nb_count_ok = 0;
88  std::set< int > X;
89  V.insert( 0, 999 );
90  for ( int i = 0; i < 1000; i++ ) X.insert( i );
91  for ( int i = 0; i < nb; i++ )
92  {
93  int f = rand() % 1000;
94  int l = std::min( f + rand() % 10, 999 );
95  // std::cout << "V + (" << f << "," << l << ") = ";
96  V.erase( f, l );
97  for ( int k = f; k <= l; k++ ) X.erase( k );
98  nb_ok += V.isValid() ? 1 : 0;
99  nb_count_ok += ( V.size() == X.size() ) ? 1 : 0;
100  if ( ! V.isValid() )
101  std::cout << V << " => " << ( V.isValid() ? "OK" : "ERROR" ) << std::endl;
102  if ( V.size() != X.size() )
103  {
104  std::cout << "Bad count #V=" << V.size() << " #X=" << X.size()
105  << std::endl;
106  for ( auto j : X ) std::cout << " " << j;
107  std::cout << std::endl;
108  break;
109  }
110  }
111  THEN( "The object remains consistent." ) {
112  REQUIRE( nb == nb_ok );
113  REQUIRE( nb == nb_count_ok );
114  }
115  }
116 }
117 
118 SCENARIO( "IntegralIntervals< int > set operations tests", "[intervals]" )
119 {
120  typedef int Integer;
121  typedef IntegralIntervals< Integer > Intervals;
122 
123  std::set< int > X,Y;
124  for ( int i = 0; i < 1000; i++ ) X.insert( rand() % 1000 );
125  for ( int i = 0; i < 1000; i++ ) Y.insert( rand() % 1000 );
126  Intervals A( X.cbegin(), X.cend() );
127  Intervals B( Y.cbegin(), Y.cend() );
128  std::vector< int > X_cup_Y, X_cap_Y, X_minus_Y, X_delta_Y;
129  std::set_union( X.cbegin(), X.cend(), Y.cbegin(), Y.cend(),
130  std::back_inserter( X_cup_Y ) );
131  std::set_intersection( X.cbegin(), X.cend(), Y.cbegin(), Y.cend(),
132  std::back_inserter( X_cap_Y ) );
133  std::set_difference( X.cbegin(), X.cend(), Y.cbegin(), Y.cend(),
134  std::back_inserter( X_minus_Y ) );
135  std::set_symmetric_difference( X.cbegin(), X.cend(), Y.cbegin(), Y.cend(),
136  std::back_inserter( X_delta_Y ) );
137  Intervals A_cup_B = A.set_union( B );
138  Intervals A_cap_B = A.set_intersection( B );
139  Intervals A_minus_B = A.set_difference( B );
140  Intervals A_delta_B = A.set_symmetric_difference( B );
141  THEN( "Interval can be constructed from sets" ) {
142  REQUIRE( X.size() == A.size() );
143  REQUIRE( Y.size() == B.size() );
144  }
145  THEN( "Set operations on intervals are correct" ) {
146  REQUIRE( X_cup_Y.size() == A_cup_B.size() );
147  REQUIRE( X_cap_Y.size() == A_cap_B.size() );
148  REQUIRE( X_minus_Y.size() == A_minus_B.size() );
149  REQUIRE( X_delta_Y.size() == A_delta_B.size() );
150  }
151  THEN( "Inclusions are correct" ) {
152  REQUIRE( A_cup_B.includes( A_cup_B ) );
153  REQUIRE( A_cup_B.includes( A ) );
154  REQUIRE( ! A_cup_B.equals( A ) );
155  REQUIRE( ! A.includes( A_cup_B ) );
156  REQUIRE( A_cup_B.includes( B ) );
157  REQUIRE( ! B.includes( A_cup_B ) );
158  REQUIRE( ! B.equals( A_cup_B ) );
159  REQUIRE( A_cup_B.includes( A_cap_B ) );
160  REQUIRE( ! A_cap_B.includes( A_cup_B ) );
161  REQUIRE( A.includes( A_minus_B ) );
162  REQUIRE( ! A_minus_B.includes( A ) );
163  REQUIRE( A_cup_B.includes( A_delta_B ) );
164  REQUIRE( ! A_delta_B.includes( A_cup_B ) );
165  REQUIRE( ! A.includes( A_delta_B ) );
166  REQUIRE( ! B.includes( A_delta_B ) );
167  }
168 }
DGtal is the top-level namespace which contains all DGtal functions and types.
SCENARIO("IntegralIntervals< int > unit tests", "[intervals]")
REQUIRE(domain.isInside(aPoint))