35#include <unordered_set>
36#include "DGtal/base/Common.h"
37#include "DGtal/base/SetFunctions.h"
38#include "DGtalCatch.h"
44using DGtal::functions::setops::operator&;
45using DGtal::functions::setops::operator-;
46using DGtal::functions::setops::operator^;
50TEMPLATE_TEST_CASE(
"SetFunctions module unit tests",
"[set_functions]",
54 std::unordered_set<int> )
57 int S1[ 10 ] = { 4, 15, 20, 17, 9, 7, 13, 12, 1, 3 };
58 int S2[ 6 ] = { 17, 14, 19, 2, 3, 4 };
59 TestType C1( S1, S1 + 10 );
60 TestType C2( S2, S2 + 6 );
61 TestType C1_minus_C2 = C1 - C2;
62 REQUIRE( C1_minus_C2.size() == 7 );
63 TestType C2_minus_C1 = C2 - C1;
64 REQUIRE( C2_minus_C1.size() == 3 );
65 REQUIRE( ( C1_minus_C2 - C1 ).size() == 0 );
66 REQUIRE( ( C2_minus_C1 - C2 ).size() == 0 );
67 REQUIRE( ( C1_minus_C2 - C2 ).size() == C1_minus_C2.size() );
68 REQUIRE( ( C2_minus_C1 - C1 ).size() == C2_minus_C1.size() );
71 REQUIRE( C1_union_C2.size() == 13 );
72 REQUIRE( C1_union_C2.size() == C2_union_C1.size() );
73 REQUIRE( ( DGtal::functions::setops::operator|(C1_minus_C2 , C2) ).size() == (DGtal::functions::setops::operator|(C2_minus_C1 , C1) ).size() );
75 TestType C1_intersection_C2 = C1 & C2;
76 TestType C2_intersection_C1 = C2 & C1;
77 REQUIRE( C1_intersection_C2.size() == 3 );
78 REQUIRE( C1_intersection_C2.size() == C2_intersection_C1.size() );
80 REQUIRE( ( DGtal::functions::setops::operator|(DGtal::functions::setops::operator|(C1_minus_C2 , C1_intersection_C2) , C2_minus_C1) ).size() == C1_union_C2.size() );
82 TestType C1_symdiff_C2 = C1 ^ C2;
83 TestType C2_symdiff_C1 = C2 ^ C1;
84 REQUIRE( C1_symdiff_C2.size() == C2_symdiff_C1.size() );
85 REQUIRE( C1_symdiff_C2.size() == ( C1_union_C2 - C1_intersection_C2 ).size() );
86 REQUIRE( C1_symdiff_C2.size() == ( DGtal::functions::setops::operator|(C1_minus_C2 , C2_minus_C1) ).size() );
88 REQUIRE(
isEqual( C1_symdiff_C2, C1_union_C2 - C1_intersection_C2 ) );
89 REQUIRE(
isEqual( C1_symdiff_C2, DGtal::functions::setops::operator|(C1_minus_C2 , C2_minus_C1) ) );
90 REQUIRE(
isEqual( DGtal::functions::setops::operator|(DGtal::functions::setops::operator|(C1_minus_C2 , C1_intersection_C2) , C2_minus_C1), C1_union_C2 ) );
105static const int NB = 10000;
106static std::default_random_engine generator;
107static std::uniform_int_distribution<int> distribution(1,NB);
111 return distribution(generator);
115TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator | (sequences)",
"[set_functions]",
118 typedef typename TestType::size_type
Size;
121 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
122 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
123 TestType A( S1.begin(), S1.end() );
124 TestType B( S2.begin(), S2.end() );
126 std::random_device rd;
127 std::mt19937 g(rd());
129 std::shuffle( A.begin(), A.end(), g);
130 std::shuffle( B.begin(), B.end(), g);
132 SECTION(
" - benchmark set operators |" )
136 Size size_A = A.size();
137 Size size_B = B.size();
138 Size size_AorB = AorB.size();
139 REQUIRE( size_AorB >= std::max( size_A, size_B ) );
142TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator | (sets)",
"[set_functions]",
144 std::unordered_set<int> )
146 typedef typename TestType::size_type
Size;
148 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
150 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
151 TestType A( S1.begin(), S1.end() );
152 TestType B( S2.begin(), S2.end() );
155 SECTION(
" - benchmark set operators |" )
159 Size size_A = A.size();
160 Size size_B = B.size();
161 Size size_AorB = AorB.size();
162 REQUIRE( size_AorB >= std::max( size_A, size_B ) );
166TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator & (sequences)",
"[set_functions]",
169 typedef typename TestType::size_type
Size;
171 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
173 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
174 TestType A( S1.begin(), S1.end() );
175 TestType B( S2.begin(), S2.end() );
177 std::random_device rd;
178 std::mt19937 g(rd());
180 std::shuffle( A.begin(), A.end(), g );
181 std::shuffle( B.begin(), B.end(), g );
183 SECTION(
" - benchmark set operators &" )
187 Size size_A = A.size();
188 Size size_B = B.size();
189 Size size_AandB = AandB.size();
190 REQUIRE( size_AandB <= std::min( size_A, size_B ) );
193TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator & (sets)",
"[set_functions]",
195 std::unordered_set<int> )
197 typedef typename TestType::size_type
Size;
199 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
201 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
202 TestType A( S1.begin(), S1.end() );
203 TestType B( S2.begin(), S2.end() );
206 SECTION(
" - benchmark set operators &" )
210 Size size_A = A.size();
211 Size size_B = B.size();
212 Size size_AandB = AandB.size();
213 REQUIRE( size_AandB <= std::min( size_A, size_B ) );
218TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator - (sequences)",
"[set_functions]",
221 typedef typename TestType::size_type
Size;
223 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
225 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
226 TestType A( S1.begin(), S1.end() );
227 TestType B( S2.begin(), S2.end() );
229 std::random_device rd;
230 std::mt19937 g(rd());
232 std::shuffle( A.begin(), A.end(), g );
233 std::shuffle( B.begin(), B.end(), g );
235 SECTION(
" - benchmark set operators -" )
239 Size size_A = A.size();
240 Size size_B = B.size();
241 Size size_AminusB = AminusB.size();
242 REQUIRE( size_AminusB <= size_A );
243 boost::ignore_unused_variable_warning(size_B);
246TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator - (sets)",
"[set_functions]",
248 std::unordered_set<int> )
250 typedef typename TestType::size_type
Size;
252 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
254 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
255 TestType A( S1.begin(), S1.end() );
256 TestType B( S2.begin(), S2.end() );
259 SECTION(
" - benchmark set operators -" )
263 Size size_A = A.size();
264 Size size_B = B.size();
265 Size size_AminusB = AminusB.size();
266 REQUIRE( size_AminusB <= size_A );
267 boost::ignore_unused_variable_warning(size_B);
272TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator ^ (sequences)",
"[set_functions]",
275 typedef typename TestType::size_type
Size;
277 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
279 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
280 TestType A( S1.begin(), S1.end() );
281 TestType B( S2.begin(), S2.end() );
283 std::random_device rd;
284 std::mt19937 g(rd());
286 std::shuffle( A.begin(), A.end(), g );
287 std::shuffle( B.begin(), B.end(), g );
289 SECTION(
" - benchmark set operators ^" )
293 Size size_A = A.size();
294 Size size_B = B.size();
295 Size size_AxorB = AxorB.size();
296 REQUIRE( size_AxorB <= std::max( size_A, size_B ) );
299TEMPLATE_TEST_CASE(
"SetFunctions benchmark operator ^ (sets)",
"[set_functions]",
301 std::unordered_set<int> )
303 typedef typename TestType::size_type
Size;
305 for (
int i = 0; i < NB; ++i ) S1.insert( randomNB( ) );
307 for (
int i = 0; i < NB; ++i ) S2.insert( randomNB( ) );
308 TestType A( S1.begin(), S1.end() );
309 TestType B( S2.begin(), S2.end() );
312 SECTION(
" - benchmark set operators ^" )
316 Size size_A = A.size();
317 Size size_B = B.size();
318 Size size_AxorB = AxorB.size();
319 REQUIRE( size_AxorB <= std::max( size_A, size_B ) );
Container operator|(const Container &S1, const Container &S2)
functions namespace gathers all DGtal functionsxs.
bool isEqual(const Container &S1, const Container &S2)
bool isSubset(const Container &S1, const Container &S2)
DGtal is the top-level namespace which contains all DGtal functions and types.
HalfEdgeDataStructure::Size Size
SECTION("Testing constant forward iterators")
REQUIRE(domain.isInside(aPoint))