1.1 --- a/test/maps_test.cc Thu Nov 05 10:01:02 2009 +0100
1.2 +++ b/test/maps_test.cc Thu Nov 05 10:23:16 2009 +0100
1.3 @@ -23,6 +23,9 @@
1.4 #include <lemon/concepts/maps.h>
1.5 #include <lemon/maps.h>
1.6 #include <lemon/list_graph.h>
1.7 +#include <lemon/smart_graph.h>
1.8 +#include <lemon/adaptors.h>
1.9 +#include <lemon/dfs.h>
1.10
1.11 #include "test_tools.h"
1.12
1.13 @@ -61,6 +64,12 @@
1.14 typedef ReadWriteMap<A, bool> BoolWriteMap;
1.15 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
1.16
1.17 +template<typename Map1, typename Map2, typename ItemIt>
1.18 +void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
1.19 + for (; it != INVALID; ++it)
1.20 + check(map1[it] == map2[it], "The maps are not equal");
1.21 +}
1.22 +
1.23 int main()
1.24 {
1.25 // Map concepts
1.26 @@ -329,6 +338,10 @@
1.27 // LoggerBoolMap
1.28 {
1.29 typedef std::vector<int> vec;
1.30 + checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
1.31 + checkConcept<WriteMap<int, bool>,
1.32 + LoggerBoolMap<std::back_insert_iterator<vec> > >();
1.33 +
1.34 vec v1;
1.35 vec v2(10);
1.36 LoggerBoolMap<std::back_insert_iterator<vec> >
1.37 @@ -348,6 +361,155 @@
1.38 for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
1.39 it != map2.end(); ++it )
1.40 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
1.41 +
1.42 + typedef ListDigraph Graph;
1.43 + DIGRAPH_TYPEDEFS(Graph);
1.44 + Graph gr;
1.45 +
1.46 + Node n0 = gr.addNode();
1.47 + Node n1 = gr.addNode();
1.48 + Node n2 = gr.addNode();
1.49 + Node n3 = gr.addNode();
1.50 +
1.51 + gr.addArc(n3, n0);
1.52 + gr.addArc(n3, n2);
1.53 + gr.addArc(n0, n2);
1.54 + gr.addArc(n2, n1);
1.55 + gr.addArc(n0, n1);
1.56 +
1.57 + {
1.58 + std::vector<Node> v;
1.59 + dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1.60 +
1.61 + check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
1.62 + "Something is wrong with LoggerBoolMap");
1.63 + }
1.64 + {
1.65 + std::vector<Node> v(countNodes(gr));
1.66 + dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
1.67 +
1.68 + check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
1.69 + "Something is wrong with LoggerBoolMap");
1.70 + }
1.71 + }
1.72 +
1.73 + // IdMap, RangeIdMap
1.74 + {
1.75 + typedef ListDigraph Graph;
1.76 + DIGRAPH_TYPEDEFS(Graph);
1.77 +
1.78 + checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
1.79 + checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
1.80 + checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
1.81 + checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
1.82 +
1.83 + Graph gr;
1.84 + IdMap<Graph, Node> nmap(gr);
1.85 + IdMap<Graph, Arc> amap(gr);
1.86 + RangeIdMap<Graph, Node> nrmap(gr);
1.87 + RangeIdMap<Graph, Arc> armap(gr);
1.88 +
1.89 + Node n0 = gr.addNode();
1.90 + Node n1 = gr.addNode();
1.91 + Node n2 = gr.addNode();
1.92 +
1.93 + Arc a0 = gr.addArc(n0, n1);
1.94 + Arc a1 = gr.addArc(n0, n2);
1.95 + Arc a2 = gr.addArc(n2, n1);
1.96 + Arc a3 = gr.addArc(n2, n0);
1.97 +
1.98 + check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
1.99 + check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
1.100 + check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
1.101 +
1.102 + check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
1.103 + check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
1.104 + check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
1.105 + check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
1.106 +
1.107 + check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
1.108 + check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
1.109 +
1.110 + check(nrmap.size() == 3 && armap.size() == 4,
1.111 + "Wrong RangeIdMap::size()");
1.112 +
1.113 + check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
1.114 + check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
1.115 + check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
1.116 +
1.117 + check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
1.118 + check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
1.119 + check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
1.120 + check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
1.121 +
1.122 + check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
1.123 + check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
1.124 +
1.125 + gr.erase(n1);
1.126 +
1.127 + if (nrmap[n0] == 1) nrmap.swap(n0, n2);
1.128 + nrmap.swap(n2, n0);
1.129 + if (armap[a1] == 1) armap.swap(a1, a3);
1.130 + armap.swap(a3, a1);
1.131 +
1.132 + check(nrmap.size() == 2 && armap.size() == 2,
1.133 + "Wrong RangeIdMap::size()");
1.134 +
1.135 + check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
1.136 + check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
1.137 +
1.138 + check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
1.139 + check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
1.140 +
1.141 + check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
1.142 + check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
1.143 + }
1.144 +
1.145 + // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
1.146 + {
1.147 + typedef ListGraph Graph;
1.148 + GRAPH_TYPEDEFS(Graph);
1.149 +
1.150 + checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
1.151 + checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
1.152 + checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
1.153 + checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
1.154 + checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
1.155 + checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
1.156 +
1.157 + Graph gr;
1.158 + Node n0 = gr.addNode();
1.159 + Node n1 = gr.addNode();
1.160 + Node n2 = gr.addNode();
1.161 +
1.162 + gr.addEdge(n0,n1);
1.163 + gr.addEdge(n1,n2);
1.164 + gr.addEdge(n0,n2);
1.165 + gr.addEdge(n2,n1);
1.166 + gr.addEdge(n1,n2);
1.167 + gr.addEdge(n0,n1);
1.168 +
1.169 + for (EdgeIt e(gr); e != INVALID; ++e) {
1.170 + check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
1.171 + check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
1.172 + }
1.173 +
1.174 + compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
1.175 + targetMap(orienter(gr, constMap<Edge, bool>(false))),
1.176 + EdgeIt(gr));
1.177 +
1.178 + typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
1.179 + Digraph dgr(gr, constMap<Edge, bool>(true));
1.180 + OutDegMap<Digraph> odm(dgr);
1.181 + InDegMap<Digraph> idm(dgr);
1.182 +
1.183 + check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
1.184 + check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
1.185 +
1.186 + gr.addEdge(n2, n0);
1.187 +
1.188 + check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
1.189 + check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
1.190 }
1.191
1.192 // CrossRefMap
1.193 @@ -357,6 +519,73 @@
1.194
1.195 checkConcept<ReadWriteMap<Node, int>,
1.196 CrossRefMap<Graph, Node, int> >();
1.197 + checkConcept<ReadWriteMap<Node, bool>,
1.198 + CrossRefMap<Graph, Node, bool> >();
1.199 + checkConcept<ReadWriteMap<Node, double>,
1.200 + CrossRefMap<Graph, Node, double> >();
1.201 +
1.202 + Graph gr;
1.203 + typedef CrossRefMap<Graph, Node, char> CRMap;
1.204 + CRMap map(gr);
1.205 +
1.206 + Node n0 = gr.addNode();
1.207 + Node n1 = gr.addNode();
1.208 + Node n2 = gr.addNode();
1.209 +
1.210 + map.set(n0, 'A');
1.211 + map.set(n1, 'B');
1.212 + map.set(n2, 'C');
1.213 +
1.214 + check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
1.215 + "Wrong CrossRefMap");
1.216 + check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
1.217 + "Wrong CrossRefMap");
1.218 + check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
1.219 + "Wrong CrossRefMap");
1.220 + check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
1.221 + "Wrong CrossRefMap::count()");
1.222 +
1.223 + CRMap::ValueIt it = map.beginValue();
1.224 + check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
1.225 + it == map.endValue(), "Wrong value iterator");
1.226 +
1.227 + map.set(n2, 'A');
1.228 +
1.229 + check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
1.230 + "Wrong CrossRefMap");
1.231 + check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
1.232 + check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
1.233 + check(map('C') == INVALID && map.inverse()['C'] == INVALID,
1.234 + "Wrong CrossRefMap");
1.235 + check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
1.236 + "Wrong CrossRefMap::count()");
1.237 +
1.238 + it = map.beginValue();
1.239 + check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
1.240 + it == map.endValue(), "Wrong value iterator");
1.241 +
1.242 + map.set(n0, 'C');
1.243 +
1.244 + check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
1.245 + "Wrong CrossRefMap");
1.246 + check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
1.247 + check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
1.248 + check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
1.249 + check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
1.250 + "Wrong CrossRefMap::count()");
1.251 +
1.252 + it = map.beginValue();
1.253 + check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
1.254 + it == map.endValue(), "Wrong value iterator");
1.255 + }
1.256 +
1.257 + // CrossRefMap
1.258 + {
1.259 + typedef SmartDigraph Graph;
1.260 + DIGRAPH_TYPEDEFS(Graph);
1.261 +
1.262 + checkConcept<ReadWriteMap<Node, int>,
1.263 + CrossRefMap<Graph, Node, int> >();
1.264
1.265 Graph gr;
1.266 typedef CrossRefMap<Graph, Node, char> CRMap;
1.267 @@ -383,6 +612,193 @@
1.268 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
1.269 it == map.endValue(), "Wrong value iterator");
1.270 }
1.271 +
1.272 + // Iterable bool map
1.273 + {
1.274 + typedef SmartGraph Graph;
1.275 + typedef SmartGraph::Node Item;
1.276
1.277 + typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
1.278 + checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
1.279 +
1.280 + const int num = 10;
1.281 + Graph g;
1.282 + std::vector<Item> items;
1.283 + for (int i = 0; i < num; ++i) {
1.284 + items.push_back(g.addNode());
1.285 + }
1.286 +
1.287 + Ibm map1(g, true);
1.288 + int n = 0;
1.289 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
1.290 + check(map1[static_cast<Item>(it)], "Wrong TrueIt");
1.291 + ++n;
1.292 + }
1.293 + check(n == num, "Wrong number");
1.294 +
1.295 + n = 0;
1.296 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
1.297 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
1.298 + ++n;
1.299 + }
1.300 + check(n == num, "Wrong number");
1.301 + check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
1.302 + check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
1.303 +
1.304 + map1[items[5]] = true;
1.305 +
1.306 + n = 0;
1.307 + for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
1.308 + check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
1.309 + ++n;
1.310 + }
1.311 + check(n == num, "Wrong number");
1.312 +
1.313 + map1[items[num / 2]] = false;
1.314 + check(map1[items[num / 2]] == false, "Wrong map value");
1.315 +
1.316 + n = 0;
1.317 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
1.318 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
1.319 + ++n;
1.320 + }
1.321 + check(n == num - 1, "Wrong number");
1.322 +
1.323 + n = 0;
1.324 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
1.325 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
1.326 + ++n;
1.327 + }
1.328 + check(n == 1, "Wrong number");
1.329 +
1.330 + map1[items[0]] = false;
1.331 + check(map1[items[0]] == false, "Wrong map value");
1.332 +
1.333 + map1[items[num - 1]] = false;
1.334 + check(map1[items[num - 1]] == false, "Wrong map value");
1.335 +
1.336 + n = 0;
1.337 + for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
1.338 + check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
1.339 + ++n;
1.340 + }
1.341 + check(n == num - 3, "Wrong number");
1.342 + check(map1.trueNum() == num - 3, "Wrong number");
1.343 +
1.344 + n = 0;
1.345 + for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
1.346 + check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
1.347 + ++n;
1.348 + }
1.349 + check(n == 3, "Wrong number");
1.350 + check(map1.falseNum() == 3, "Wrong number");
1.351 + }
1.352 +
1.353 + // Iterable int map
1.354 + {
1.355 + typedef SmartGraph Graph;
1.356 + typedef SmartGraph::Node Item;
1.357 + typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
1.358 +
1.359 + checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
1.360 +
1.361 + const int num = 10;
1.362 + Graph g;
1.363 + std::vector<Item> items;
1.364 + for (int i = 0; i < num; ++i) {
1.365 + items.push_back(g.addNode());
1.366 + }
1.367 +
1.368 + Iim map1(g);
1.369 + check(map1.size() == 0, "Wrong size");
1.370 +
1.371 + for (int i = 0; i < num; ++i) {
1.372 + map1[items[i]] = i;
1.373 + }
1.374 + check(map1.size() == num, "Wrong size");
1.375 +
1.376 + for (int i = 0; i < num; ++i) {
1.377 + Iim::ItemIt it(map1, i);
1.378 + check(static_cast<Item>(it) == items[i], "Wrong value");
1.379 + ++it;
1.380 + check(static_cast<Item>(it) == INVALID, "Wrong value");
1.381 + }
1.382 +
1.383 + for (int i = 0; i < num; ++i) {
1.384 + map1[items[i]] = i % 2;
1.385 + }
1.386 + check(map1.size() == 2, "Wrong size");
1.387 +
1.388 + int n = 0;
1.389 + for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
1.390 + check(map1[static_cast<Item>(it)] == 0, "Wrong value");
1.391 + ++n;
1.392 + }
1.393 + check(n == (num + 1) / 2, "Wrong number");
1.394 +
1.395 + for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
1.396 + check(map1[static_cast<Item>(it)] == 1, "Wrong value");
1.397 + ++n;
1.398 + }
1.399 + check(n == num, "Wrong number");
1.400 +
1.401 + }
1.402 +
1.403 + // Iterable value map
1.404 + {
1.405 + typedef SmartGraph Graph;
1.406 + typedef SmartGraph::Node Item;
1.407 + typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
1.408 +
1.409 + checkConcept<ReadWriteMap<Item, double>, Ivm>();
1.410 +
1.411 + const int num = 10;
1.412 + Graph g;
1.413 + std::vector<Item> items;
1.414 + for (int i = 0; i < num; ++i) {
1.415 + items.push_back(g.addNode());
1.416 + }
1.417 +
1.418 + Ivm map1(g, 0.0);
1.419 + check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
1.420 + check(*map1.beginValue() == 0.0, "Wrong value");
1.421 +
1.422 + for (int i = 0; i < num; ++i) {
1.423 + map1.set(items[i], static_cast<double>(i));
1.424 + }
1.425 + check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
1.426 +
1.427 + for (int i = 0; i < num; ++i) {
1.428 + Ivm::ItemIt it(map1, static_cast<double>(i));
1.429 + check(static_cast<Item>(it) == items[i], "Wrong value");
1.430 + ++it;
1.431 + check(static_cast<Item>(it) == INVALID, "Wrong value");
1.432 + }
1.433 +
1.434 + for (Ivm::ValueIt vit = map1.beginValue();
1.435 + vit != map1.endValue(); ++vit) {
1.436 + check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
1.437 + "Wrong ValueIt");
1.438 + }
1.439 +
1.440 + for (int i = 0; i < num; ++i) {
1.441 + map1.set(items[i], static_cast<double>(i % 2));
1.442 + }
1.443 + check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
1.444 +
1.445 + int n = 0;
1.446 + for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
1.447 + check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
1.448 + ++n;
1.449 + }
1.450 + check(n == (num + 1) / 2, "Wrong number");
1.451 +
1.452 + for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
1.453 + check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
1.454 + ++n;
1.455 + }
1.456 + check(n == num, "Wrong number");
1.457 +
1.458 + }
1.459 return 0;
1.460 }