gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Get rid of StdMap in unionfind_test.cc
0 1 0
default
1 file changed with 45 insertions and 55 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <iostream>
20 20

	
21
#include <lemon/list_graph.h>
21 22
#include <lemon/maps.h>
22 23
#include <lemon/unionfind.h>
23 24
#include "test_tools.h"
24 25

	
25 26
using namespace lemon;
26 27
using namespace std;
27 28

	
28
typedef UnionFindEnum<StdMap<int, int> > UFE;
29

	
30
void print(UFE const &ufe) {
31
  cout << "Print the classes of the structure:" << endl;
32
  int i = 1;
33
  for (UFE::ClassIt cit(ufe); cit != INVALID; ++cit) {
34
    cout << "  " << i << " (" << cit << "):" << flush;
35
    for (UFE::ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
36
      cout << " " << iit << flush;
37
    }
38
    cout << endl;
39
    i++;
40
  }
41
  cout << "done" << endl;
42
}
43

	
29
typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE;
44 30

	
45 31
int main() {
46
  StdMap<int, int> base;
32
  ListGraph g;
33
  ListGraph::NodeMap<int> base(g);
47 34
  UFE U(base);
35
  vector<ListGraph::Node> n;
36
  
37
  for(int i=0;i<20;i++) n.push_back(g.addNode());
48 38

	
49
  U.insert(1);
50
  U.insert(2);
39
  U.insert(n[1]);
40
  U.insert(n[2]);
51 41

	
52
  check(U.join(1,2) != -1,"Test failed.");
42
  check(U.join(n[1],n[2]) != -1,"Test failed.");
53 43

	
54
  U.insert(3);
55
  U.insert(4);
56
  U.insert(5);
57
  U.insert(6);
58
  U.insert(7);
44
  U.insert(n[3]);
45
  U.insert(n[4]);
46
  U.insert(n[5]);
47
  U.insert(n[6]);
48
  U.insert(n[7]);
59 49

	
60 50

	
61
  check(U.join(1,4) != -1,"Test failed.");
62
  check(U.join(2,4) == -1,"Test failed.");
63
  check(U.join(3,5) != -1,"Test failed.");
51
  check(U.join(n[1],n[4]) != -1,"Test failed.");
52
  check(U.join(n[2],n[4]) == -1,"Test failed.");
53
  check(U.join(n[3],n[5]) != -1,"Test failed.");
64 54

	
65 55

	
66
  U.insert(8,U.find(5));
56
  U.insert(n[8],U.find(n[5]));
67 57

	
68 58

	
69
  check(U.size(U.find(4)) == 3,"Test failed.");
70
  check(U.size(U.find(5)) == 3,"Test failed.");
71
  check(U.size(U.find(6)) == 1,"Test failed.");
72
  check(U.size(U.find(2)) == 3,"Test failed.");
59
  check(U.size(U.find(n[4])) == 3,"Test failed.");
60
  check(U.size(U.find(n[5])) == 3,"Test failed.");
61
  check(U.size(U.find(n[6])) == 1,"Test failed.");
62
  check(U.size(U.find(n[2])) == 3,"Test failed.");
73 63

	
74 64

	
75
  U.insert(9);
76
  U.insert(10,U.find(9));
65
  U.insert(n[9]);
66
  U.insert(n[10],U.find(n[9]));
77 67

	
78 68

	
79
  check(U.join(8,10) != -1,"Test failed.");
69
  check(U.join(n[8],n[10]) != -1,"Test failed.");
80 70

	
81 71

	
82
  check(U.size(U.find(4)) == 3,"Test failed.");
83
  check(U.size(U.find(9)) == 5,"Test failed.");
72
  check(U.size(U.find(n[4])) == 3,"Test failed.");
73
  check(U.size(U.find(n[9])) == 5,"Test failed.");
84 74

	
85
  check(U.size(U.find(8)) == 5,"Test failed.");
75
  check(U.size(U.find(n[8])) == 5,"Test failed.");
86 76

	
87
  U.erase(9);
88
  U.erase(1);
77
  U.erase(n[9]);
78
  U.erase(n[1]);
89 79

	
90
  check(U.size(U.find(10)) == 4,"Test failed.");
91
  check(U.size(U.find(2)) == 2,"Test failed.");
80
  check(U.size(U.find(n[10])) == 4,"Test failed.");
81
  check(U.size(U.find(n[2])) == 2,"Test failed.");
92 82

	
93
  U.erase(6);
94
  U.split(U.find(8));
83
  U.erase(n[6]);
84
  U.split(U.find(n[8]));
95 85

	
96 86

	
97
  check(U.size(U.find(4)) == 2,"Test failed.");
98
  check(U.size(U.find(3)) == 1,"Test failed.");
99
  check(U.size(U.find(2)) == 2,"Test failed.");
87
  check(U.size(U.find(n[4])) == 2,"Test failed.");
88
  check(U.size(U.find(n[3])) == 1,"Test failed.");
89
  check(U.size(U.find(n[2])) == 2,"Test failed.");
100 90

	
101 91

	
102
  check(U.join(3,4) != -1,"Test failed.");
103
  check(U.join(2,4) == -1,"Test failed.");
92
  check(U.join(n[3],n[4]) != -1,"Test failed.");
93
  check(U.join(n[2],n[4]) == -1,"Test failed.");
104 94

	
105 95

	
106
  check(U.size(U.find(4)) == 3,"Test failed.");
107
  check(U.size(U.find(3)) == 3,"Test failed.");
108
  check(U.size(U.find(2)) == 3,"Test failed.");
96
  check(U.size(U.find(n[4])) == 3,"Test failed.");
97
  check(U.size(U.find(n[3])) == 3,"Test failed.");
98
  check(U.size(U.find(n[2])) == 3,"Test failed.");
109 99

	
110
  U.eraseClass(U.find(4));
111
  U.eraseClass(U.find(7));
100
  U.eraseClass(U.find(n[4]));
101
  U.eraseClass(U.find(n[7]));
112 102

	
113 103
  return 0;
114 104
}
0 comments (0 inline)