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