DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testIntegralIntervals.cpp File Reference
#include <iostream>
#include <vector>
#include <algorithm>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/IntegralIntervals.h"
#include "DGtalCatch.h"

Go to the source code of this file.

Functions

 SCENARIO ("IntegralIntervals< int > unit tests", "[intervals]")
 
 SCENARIO ("IntegralIntervals< int > set operations tests", "[intervals]")
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2022/07/01

Functions for testing class IntegralIntervals.

This file is part of the DGtal library.

Definition in file testIntegralIntervals.cpp.

Function Documentation

◆ SCENARIO() [1/2]

SCENARIO ( "IntegralIntervals< int > set operations tests"  ,
""  [intervals] 
)

Definition at line 118 of file testIntegralIntervals.cpp.

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 bool A_cup_B_subset_A_cup_B = A_cup_B.includes( A_cup_B );
142 bool A_subset_A_cup_B = A_cup_B.includes( A );
143 bool B_subset_A_cup_B = A_cup_B.includes( B );
144 bool A_cap_B_subset_A_cup_B = A_cup_B.includes( A_cap_B );
145 bool A_minus_B_subset_A = A.includes( A_minus_B );
146 bool A_delta_B_subset_A_cup_B = A_cup_B.includes( A_delta_B );
147 bool A_delta_B_subset_A = A.includes( A_delta_B ); // false
148 bool A_delta_B_subset_B = B.includes( A_delta_B ); // false
149 THEN( "Interval can be constructed from sets" ) {
150 REQUIRE( X.size() == A.size() );
151 REQUIRE( Y.size() == B.size() );
152 }
153 THEN( "Set operations on intervals are correct" ) {
154 REQUIRE( X_cup_Y.size() == A_cup_B.size() );
155 REQUIRE( X_cap_Y.size() == A_cap_B.size() );
156 REQUIRE( X_minus_Y.size() == A_minus_B.size() );
157 REQUIRE( X_delta_Y.size() == A_delta_B.size() );
158 }
159 THEN( "Inclusions are correct" ) {
160 REQUIRE( A_cup_B.includes( A_cup_B ) );
161 REQUIRE( A_cup_B.includes( A ) );
162 REQUIRE( ! A_cup_B.equals( A ) );
163 REQUIRE( ! A.includes( A_cup_B ) );
164 REQUIRE( A_cup_B.includes( B ) );
165 REQUIRE( ! B.includes( A_cup_B ) );
166 REQUIRE( ! B.equals( A_cup_B ) );
167 REQUIRE( A_cup_B.includes( A_cap_B ) );
168 REQUIRE( ! A_cap_B.includes( A_cup_B ) );
169 REQUIRE( A.includes( A_minus_B ) );
170 REQUIRE( ! A_minus_B.includes( A ) );
171 REQUIRE( A_cup_B.includes( A_delta_B ) );
172 REQUIRE( ! A_delta_B.includes( A_cup_B ) );
173 REQUIRE( ! A.includes( A_delta_B ) );
174 REQUIRE( ! B.includes( A_delta_B ) );
175 }
176}
REQUIRE(domain.isInside(aPoint))

References DGtal::IntegralIntervals< TInteger >::insert(), and REQUIRE().

◆ SCENARIO() [2/2]

SCENARIO ( "IntegralIntervals< int > unit tests"  ,
""  [intervals] 
)

Definition at line 47 of file testIntegralIntervals.cpp.

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 i : X ) std::cout << " " << i;
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 i : X ) std::cout << " " << i;
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}

References REQUIRE().