1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
4  *   info_at_agrum_dot_org
5  *
6  *  This library is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #include <iostream>
23 #include <string>
24 
25 #include <gumtest/AgrumTestSuite.h>
26 #include <gumtest/testsuite_utils.h>
27 #include <ressources/include/countedAlloc.h>
28 
29 #include <agrum/tools/core/bijection.h>
30 #include <agrum/tools/graphs/graphElements.h>
31 #include <agrum/tools/variables/labelizedVariable.h>
32 
33 namespace gum_tests {
34 
35   class BijectionTestSuite: public CxxTest::TestSuite {
36     public:
test_constructors1()37     void test_constructors1() {
38       gum::Bijection< int, int > bijection;
39 
40       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(1, 2));
41       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(3, 4));
42       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(5, 6));
43 
44       TS_ASSERT_THROWS(bijection.insert(5, 6), gum::DuplicateElement)
45       TS_ASSERT_THROWS(bijection.insert(5, 7), gum::DuplicateElement)
46       TS_ASSERT_THROWS(bijection.insert(7, 6), gum::DuplicateElement)
47 
48       gum::Bijection< int, int > bijection2(bijection);
49       TS_ASSERT_EQUALS(bijection2.size(), (gum::Size)(gum::Size)3)
50 
51       gum::Bijection< int, int, DebugCountedAlloc< int > > bij_bis(bijection);
52       bij_bis.insert(8, 10);
53       TS_ASSERT_EQUALS(bij_bis.size(), (gum::Size)(gum::Size)4)
54 
55       gum::Bijection< int, int >* bijection3 = new gum::Bijection< int, int >;
56       bijection3->insert(1, 2);
57       bijection3->insert(3, 3);
58 
59       gum::Bijection< int, int > bijection4 = bijection;
60       bijection4                            = *bijection3;
61       delete bijection3;
62       TS_ASSERT_EQUALS(bijection4.first(2), 1)
63       TS_ASSERT_EQUALS(bijection4.first(3), 3)
64       TS_ASSERT_EQUALS(bijection4.second(1), 2)
65       TS_ASSERT_EQUALS(bijection4.second(3), 3)
66 
67       gum::Bijection< int, int, DebugCountedAlloc< int > > bij_ter(std::move(bij_bis));
68       TS_ASSERT_EQUALS(bij_ter.size(), (gum::Size)(gum::Size)4)
69 
70       gum::Bijection< int, int > bij5{std::pair< int, int >(3, 4), std::pair< int, int >(5, 6)};
71       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)(gum::Size)2)
72 
73       bij5 = bijection;
74       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)(gum::Size)3)
75       TS_ASSERT(bij5.existsFirst(5))
76       TS_ASSERT(bij5.existsFirst(3))
77 
78       bij5 = bij_ter;
79       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)(gum::Size)4)
80       TS_ASSERT(bij5.existsFirst(8))
81 
82       bij5 = std::move(bijection);
83       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)3)
84     }
85 
test_constructors2()86     void test_constructors2() {
87       gum::Bijection< std::string, std::string > bijection;
88 
89       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("a", "ba"));
90       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("b", "bb"));
91       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("c", "bc"));
92 
93       TS_ASSERT_THROWS(bijection.insert("b", "d"), gum::DuplicateElement)
94       TS_ASSERT_THROWS(bijection.insert("b", "bb"), gum::DuplicateElement)
95       TS_ASSERT_THROWS(bijection.insert("e", "bc"), gum::DuplicateElement)
96 
97       gum::Bijection< std::string, std::string > bijection2(bijection);
98       TS_ASSERT_EQUALS(bijection2.size(), (gum::Size)3)
99 
100       gum::Bijection< std::string, std::string, DebugCountedAlloc< int > > bij_bis(bijection);
101       bij_bis.insert("d", "bd");
102       TS_ASSERT_EQUALS(bij_bis.size(), (gum::Size)4)
103 
104       gum::Bijection< std::string, std::string >* bijection3
105          = new gum::Bijection< std::string, std::string >;
106       bijection3->insert("a", "b");
107       bijection3->insert("b", "a");
108 
109       gum::Bijection< std::string, std::string > bijection4 = bijection;
110       bijection4                                            = *bijection3;
111       delete bijection3;
112       TS_ASSERT_EQUALS(bijection4.first("a"), "b")
113       TS_ASSERT_EQUALS(bijection4.first("b"), "a")
114       TS_ASSERT_EQUALS(bijection4.second("a"), "b")
115       TS_ASSERT_EQUALS(bijection4.second("b"), "a")
116 
117       gum::Bijection< std::string, std::string, DebugCountedAlloc< int > > bij_ter(
118          std::move(bij_bis));
119       TS_ASSERT_EQUALS(bij_ter.size(), (gum::Size)4)
120 
121       gum::Bijection< std::string, std::string > bij5{
122          std::pair< std::string, std::string >("3", "4"),
123          std::pair< std::string, std::string >("5", "6")};
124       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)2)
125 
126       bij5 = bijection;
127       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)3)
128       TS_ASSERT(bij5.existsFirst("a"))
129       TS_ASSERT(bij5.existsFirst("c"))
130 
131       bij5 = bij_ter;
132       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)4)
133       TS_ASSERT(bij5.existsFirst("d"))
134 
135       bij5 = std::move(bijection);
136       TS_ASSERT_EQUALS(bij5.size(), (gum::Size)3)
137     }
138 
testMoves()139     void testMoves() {
140       gum::Bijection< int, int > bij1;
141       gum::Bijection< int, int > bij2;
142       gum::Bijection< int, int > bij3;
143       gum::Bijection< int, int > bij4;
144 
145       TS_GUM_ASSERT_THROWS_NOTHING(bij1.insert(1, 2));
146       TS_GUM_ASSERT_THROWS_NOTHING(bij1.insert(3, 4));
147       TS_GUM_ASSERT_THROWS_NOTHING(bij1.insert(5, 6));
148 
149       TS_GUM_ASSERT_THROWS_NOTHING(bij2.insert(7, 8));
150       TS_GUM_ASSERT_THROWS_NOTHING(bij2.insert(9, 10));
151 
152       TS_GUM_ASSERT_THROWS_NOTHING(bij3.insert(1, 2));
153 
154       bij4 = std::move(bij3);
155       bij3 = std::move(bij2);
156       bij2 = std::move(bij1);
157 
158       TS_ASSERT_EQUALS(bij2.size(), (gum::Size)3)
159       TS_ASSERT_EQUALS(bij2.second(3), 4)
160     }
161 
testAccess1()162     void testAccess1() {
163       gum::Bijection< int, int > bijection;
164 
165       int x1 = 1, x2 = 2;
166       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(x1, x2));
167       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(3, 4));
168       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(5, 6));
169 
170       TS_ASSERT_EQUALS(bijection.first(2), 1)
171       TS_ASSERT_EQUALS(bijection.first(4), 3)
172       TS_ASSERT_EQUALS(bijection.first(6), 5)
173       TS_ASSERT_EQUALS(bijection.second(1), 2)
174       TS_ASSERT_EQUALS(bijection.second(3), 4)
175       TS_ASSERT_EQUALS(bijection.second(5), 6)
176       TS_ASSERT(bijection.existsFirst(1))
177       TS_ASSERT(bijection.existsFirst(3))
178       TS_ASSERT(bijection.existsFirst(5))
179       TS_ASSERT(bijection.existsSecond(2))
180       TS_ASSERT(bijection.existsSecond(4))
181       TS_ASSERT(bijection.existsSecond(6))
182       TS_ASSERT_EQUALS(bijection.size(), (gum::Size)3)
183       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst(1));
184       TS_ASSERT(!bijection.existsSecond(2))
185       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst(3));
186       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseSecond(6));
187       TS_ASSERT(bijection.empty())
188 
189       bijection.insert(1, 2);
190       bijection.insert(3, 4);
191       bijection.insert(5, 6);
192 
193       TS_ASSERT(!bijection.empty())
194 
195       bijection.clear();
196       TS_ASSERT(bijection.empty())
197     }
198 
testAccess2()199     void testAccess2() {
200       gum::Bijection< std::string, std::string > bijection;
201 
202       std::string x1 = "1", x2 = "2";
203       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(x1, x2));
204       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("3", "4"));
205       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("5", "6"));
206 
207       TS_ASSERT_EQUALS(bijection.first("2"), "1")
208       TS_ASSERT_EQUALS(bijection.first("4"), "3")
209       TS_ASSERT_EQUALS(bijection.first("6"), "5")
210       TS_ASSERT_EQUALS(bijection.second("1"), "2")
211       TS_ASSERT_EQUALS(bijection.second("3"), "4")
212       TS_ASSERT_EQUALS(bijection.second("5"), "6")
213       TS_ASSERT(bijection.existsFirst("1"))
214       TS_ASSERT(bijection.existsFirst("3"))
215       TS_ASSERT(bijection.existsFirst("5"))
216       TS_ASSERT(bijection.existsSecond("2"))
217       TS_ASSERT(bijection.existsSecond("4"))
218       TS_ASSERT(bijection.existsSecond("6"))
219       TS_ASSERT_EQUALS(bijection.size(), (gum::Size)3)
220       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst("1"));
221       TS_ASSERT(!bijection.existsSecond("2"))
222       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst("3"));
223       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseSecond("6"));
224       TS_ASSERT(bijection.empty())
225 
226       bijection.insert("1", "2");
227       bijection.insert("3", "4");
228       bijection.insert("5", "6");
229 
230       TS_ASSERT(!bijection.empty())
231 
232       bijection.clear();
233       TS_ASSERT(bijection.empty())
234     }
235 
testEmplace1()236     void testEmplace1() {
237       gum::Bijection< std::string, std::string > bijection;
238 
239       TS_GUM_ASSERT_THROWS_NOTHING(bijection.emplace("3", "4"));
240       TS_GUM_ASSERT_THROWS_NOTHING(bijection.emplace("5", "6"));
241       TS_ASSERT(bijection.existsFirst("3"))
242     }
243 
testEmplace2()244     void testEmplace2() {
245       gum::Bijection< int, int > bijection;
246 
247       TS_GUM_ASSERT_THROWS_NOTHING(bijection.emplace(3, 4));
248       TS_GUM_ASSERT_THROWS_NOTHING(bijection.emplace(5, 6));
249       TS_ASSERT(bijection.existsFirst(3))
250     }
251 
test_ph()252     void test_ph() {
253       gum::Bijection< int, int > carre;
254       carre.insert(1, 1);
255       carre.insert(2, 4);
256       carre.insert(3, 9);
257 
258       try {
259         carre.insert(4, 1);
260       } catch (gum::DuplicateElement&) {}
261 
262       TS_ASSERT(!carre.existsFirst(4))
263     }
264 
testResize()265     void testResize() {
266       gum::Bijection< std::string, std::string > bijection(2);
267 
268       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("1", "2"));
269       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("3", "4"));
270       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("5", "6"));
271       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("6", "3"));
272       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("7", "5"));
273       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("8", "9"));
274       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("9", "7"));
275       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert("2", "1"));
276 
277       bijection.resize(2);
278 
279       TS_ASSERT_DIFFERS(bijection.capacity(), (gum::Size)2)
280     }
281 
testResize2()282     void testResize2() {
283       gum::Bijection< int, int > bijection((gum::Size)2);
284 
285       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(1, 2));
286       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(3, 4));
287       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(5, 6));
288       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(6, 3));
289       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(7, 5));
290       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(8, 9));
291       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(9, 7));
292       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert(2, 1));
293 
294       bijection.resize(2);
295 
296       TS_ASSERT_DIFFERS(bijection.capacity(), (gum::Size)2)
297     }
298 
testIterators1()299     void testIterators1() {
300       gum::Bijection< int, int > bijection;
301 
302       bijection.insert(1, 2);
303       bijection.insert(3, 4);
304       bijection.insert(5, 6);
305       bijection.insert(6, 3);
306       bijection.insert(7, 5);
307       bijection.insert(8, 9);
308       bijection.insert(9, 7);
309       bijection.insert(2, 1);
310 
311       auto iter1 = bijection.begin();
312       auto iter2 = bijection.end();
313 
314       gum::Size nb = 0;
315 
316       for (auto iter = bijection.begin(); iter != bijection.end(); ++iter, ++nb) {}
317 
318       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)8)
319 
320       bijection.eraseFirst(1);
321       bijection.eraseFirst(5);
322       bijection.eraseFirst(6);
323       bijection.eraseFirst(9);
324 
325       nb = 0;
326 
327       for (; iter1 != iter2; ++iter1, ++nb) {}
328 
329       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
330 
331       nb        = 0;
332       auto iter = iter2;
333 
334       for (iter = bijection.begin(); iter != iter2; ++iter, ++nb) {}
335 
336       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
337 
338       iter = bijection.begin();
339       nb   = iter.first();
340       nb   = iter.second();
341     }
342 
testIterators2()343     void testIterators2() {
344       gum::Bijection< int, int > bijection;
345 
346       bijection.insert(1, 2);
347       bijection.insert(3, 4);
348       bijection.insert(5, 6);
349       bijection.insert(6, 3);
350       bijection.insert(7, 5);
351       bijection.insert(8, 9);
352       bijection.insert(9, 7);
353       bijection.insert(2, 1);
354 
355       gum::Bijection< int, int >::iterator iter1 = bijection.begin();
356       gum::Bijection< int, int >::iterator iter2 = bijection.end();
357 
358       gum::Size nb = 0;
359 
360       for (gum::Bijection< int, int >::iterator iter = bijection.begin(); iter != bijection.end();
361            ++iter, ++nb) {}
362 
363       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)8)
364 
365       bijection.eraseFirst(1);
366       bijection.eraseFirst(5);
367       bijection.eraseFirst(6);
368       bijection.eraseFirst(9);
369 
370       nb = 0;
371 
372       for (; iter1 != iter2; ++iter1, ++nb) {}
373 
374       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
375 
376       nb                                        = 0;
377       gum::Bijection< int, int >::iterator iter = iter2;
378 
379       for (iter = bijection.begin(); iter != iter2; ++iter, ++nb) {}
380 
381       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
382 
383       iter = bijection.begin();
384       nb   = iter.first();
385       nb   = iter.second();
386     }
387 
testIterators3()388     void testIterators3() {
389       gum::Bijection< std::string, std::string > bijection;
390 
391       bijection.insert("1", "2");
392       bijection.insert("3", "4");
393       bijection.insert("5", "6");
394       bijection.insert("6", "3");
395       bijection.insert("7", "5");
396       bijection.insert("8", "9");
397       bijection.insert("9", "7");
398       bijection.insert("2", "1");
399 
400       auto iter1 = bijection.begin();
401       auto iter2 = bijection.end();
402 
403       gum::Size nb = 0;
404 
405       for (auto iter = bijection.begin(); iter != bijection.end(); ++iter, ++nb) {}
406 
407       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)8)
408 
409       bijection.eraseFirst("1");
410       bijection.eraseFirst("5");
411       bijection.eraseFirst("6");
412       bijection.eraseFirst("9");
413 
414       nb = 0;
415 
416       for (; iter1 != iter2; ++iter1, ++nb) {}
417 
418       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
419 
420       nb        = 0;
421       auto iter = iter2;
422 
423       for (iter = bijection.begin(); iter != iter2; ++iter, ++nb) {}
424 
425       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
426 
427       iter            = bijection.begin();
428       std::string str = iter.first();
429       str             = iter.second();
430     }
431 
testIterators4()432     void testIterators4() {
433       gum::Bijection< std::string, std::string > bijection;
434 
435       bijection.insert("1", "2");
436       bijection.insert("3", "4");
437       bijection.insert("5", "6");
438       bijection.insert("6", "3");
439       bijection.insert("7", "5");
440       bijection.insert("8", "9");
441       bijection.insert("9", "7");
442       bijection.insert("2", "1");
443 
444       gum::Bijection< std::string, std::string >::iterator iter1 = bijection.begin();
445       gum::Bijection< std::string, std::string >::iterator iter2 = bijection.end();
446 
447       gum::Size nb = 0;
448 
449       for (gum::Bijection< std::string, std::string >::iterator iter = bijection.begin();
450            iter != bijection.end();
451            ++iter, ++nb) {}
452 
453       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)8)
454 
455       bijection.eraseFirst("1");
456       bijection.eraseFirst("5");
457       bijection.eraseFirst("6");
458       bijection.eraseFirst("9");
459 
460       nb = 0;
461 
462       for (; iter1 != iter2; ++iter1, ++nb) {}
463 
464       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
465 
466       nb                                                        = 0;
467       gum::Bijection< std::string, std::string >::iterator iter = iter2;
468 
469       for (iter = bijection.begin(); iter != iter2; ++iter, ++nb) {}
470 
471       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)4)
472 
473       iter            = bijection.begin();
474       std::string str = iter.first();
475       str             = iter.second();
476     }
477 
test_constructorsStar()478     void test_constructorsStar() {
479       gum::Bijection< int*, int* > bijection;
480 
481       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)1, (int*)2));
482       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)3, (int*)4));
483       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)5, (int*)6));
484 
485       TS_ASSERT_THROWS(bijection.insert((int*)5, (int*)6), gum::DuplicateElement)
486       TS_ASSERT_THROWS(bijection.insert((int*)5, (int*)7), gum::DuplicateElement)
487       TS_ASSERT_THROWS(bijection.insert((int*)7, (int*)6), gum::DuplicateElement)
488 
489       gum::Bijection< int*, int* > bijection2 = bijection;
490       TS_ASSERT_EQUALS(bijection2.size(), (gum::Size)3)
491 
492       gum::Bijection< int*, int* >* bijection3 = new gum::Bijection< int*, int* >;
493       bijection3->insert((int*)1, (int*)2);
494       bijection3->insert((int*)3, (int*)3);
495 
496       gum::Bijection< int*, int* > bijection4 = bijection;
497       bijection4                              = *bijection3;
498       delete bijection3;
499       TS_ASSERT_EQUALS(bijection4.first((int*)2), (int*)1)
500       TS_ASSERT_EQUALS(bijection4.first((int*)3), (int*)3)
501       TS_ASSERT_EQUALS(bijection4.second((int*)1), (int*)2)
502       TS_ASSERT_EQUALS(bijection4.second((int*)3), (int*)3)
503     }
504 
testAccessStar()505     void testAccessStar() {
506       gum::Bijection< int*, int* > bijection;
507 
508       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)1, (int*)2));
509       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)3, (int*)4));
510       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)5, (int*)6));
511 
512       TS_ASSERT_EQUALS(bijection.first((int*)2), (int*)1)
513       TS_ASSERT_EQUALS(bijection.first((int*)4), (int*)3)
514       TS_ASSERT_EQUALS(bijection.first((int*)6), (int*)5)
515       TS_ASSERT_EQUALS(bijection.second((int*)1), (int*)2)
516       TS_ASSERT_EQUALS(bijection.second((int*)3), (int*)4)
517       TS_ASSERT_EQUALS(bijection.second((int*)5), (int*)6)
518       TS_ASSERT(bijection.existsFirst((int*)1))
519       TS_ASSERT(bijection.existsFirst((int*)3))
520       TS_ASSERT(bijection.existsFirst((int*)5))
521       TS_ASSERT(bijection.existsSecond((int*)2))
522       TS_ASSERT(bijection.existsSecond((int*)4))
523       TS_ASSERT(bijection.existsSecond((int*)6))
524       TS_ASSERT_EQUALS(bijection.size(), (gum::Size)3)
525       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst((int*)1));
526       TS_ASSERT(!bijection.existsSecond((int*)2))
527       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseFirst((int*)3));
528       TS_GUM_ASSERT_THROWS_NOTHING(bijection.eraseSecond((int*)6));
529       TS_ASSERT(bijection.empty())
530 
531       bijection.insert((int*)1, (int*)2);
532       bijection.insert((int*)3, (int*)4);
533       bijection.insert((int*)5, (int*)6);
534 
535       TS_ASSERT(!bijection.empty())
536 
537       bijection.clear();
538       TS_ASSERT(bijection.empty())
539     }
540 
test_phStar()541     void test_phStar() {
542       gum::Bijection< int*, int* > carre;
543       carre.insert((int*)1, (int*)1);
544       carre.insert((int*)2, (int*)4);
545       carre.insert((int*)3, (int*)9);
546 
547       try {
548         carre.insert((int*)4, (int*)1);
549       } catch (gum::DuplicateElement&) {}
550 
551       TS_ASSERT(!carre.existsFirst((int*)4))
552     }
553 
testResizeStar()554     void testResizeStar() {
555       gum::Bijection< int*, int* > bijection(2);
556 
557       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)1, (int*)2));
558       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)3, (int*)4));
559       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)5, (int*)6));
560       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)6, (int*)3));
561       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)7, (int*)5));
562       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)8, (int*)9));
563       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)9, (int*)7));
564       TS_GUM_ASSERT_THROWS_NOTHING(bijection.insert((int*)2, (int*)1));
565 
566       bijection.resize(2);
567 
568       TS_ASSERT_DIFFERS(bijection.capacity(), (gum::Size)2)
569     }
570 
testIteratorsStar()571     void testIteratorsStar() {
572       gum::Bijection< int*, int* > bijection;
573 
574       bijection.insert((int*)1, (int*)2);
575       bijection.insert((int*)3, (int*)4);
576       bijection.insert((int*)5, (int*)6);
577       bijection.insert((int*)6, (int*)3);
578       bijection.insert((int*)7, (int*)5);
579       bijection.insert((int*)8, (int*)9);
580       bijection.insert((int*)9, (int*)7);
581       bijection.insert((int*)2, (int*)1);
582 
583       auto iter1 = bijection.begin();
584       auto iter2 = bijection.end();
585 
586       gum::Size nb = 0;
587 
588       for (auto iter = bijection.begin(); iter != bijection.end(); ++iter, ++nb) {}
589 
590       TS_ASSERT_EQUALS(nb, (gum::Size)(gum::Size)8)
591 
592       bijection.eraseFirst((int*)1);
593       bijection.eraseFirst((int*)5);
594       bijection.eraseFirst((int*)6);
595       bijection.eraseFirst((int*)9);
596 
597       nb = 0;
598 
599       for (; iter1 != iter2; ++iter1, ++nb) {}
600 
601       TS_ASSERT_EQUALS(nb, (gum::Size)4)
602 
603       nb        = 0;
604       auto iter = iter2;
605 
606       for (iter = bijection.begin(); iter != iter2; ++iter, ++nb) {}
607 
608       TS_ASSERT_EQUALS(nb, (gum::Size)4)
609 
610       iter = bijection.begin();
611       iter.first();
612       iter.second();
613     }
614 
testcopyConstructor()615     void testcopyConstructor() {
616       gum::Bijection< gum::NodeId, const gum::LabelizedVariable* > b;
617       b.insert(1, new gum::LabelizedVariable("A"));
618       b.insert(2, new gum::LabelizedVariable("B"));
619       b.insert(3, new gum::LabelizedVariable("C"));
620       b.insert(4, new gum::LabelizedVariable("D"));
621       b.insert(5, new gum::LabelizedVariable("E"));
622 
623       gum::Bijection< gum::NodeId, const gum::LabelizedVariable* > copy(b);
624 
625       for (auto iter = b.begin(); iter != b.end(); ++iter) {
626         delete iter.second();
627       }
628     }
629   };
630 }   // namespace gum_tests
631