1.1 --- a/test/maps_test.cc Wed Nov 18 14:38:02 2009 +0100
1.2 +++ b/test/maps_test.cc Wed Nov 18 14:38:38 2009 +0100
1.3 @@ -26,6 +26,7 @@
1.4 #include <lemon/smart_graph.h>
1.5 #include <lemon/adaptors.h>
1.6 #include <lemon/dfs.h>
1.7 +#include <algorithm>
1.8
1.9 #include "test_tools.h"
1.10
1.11 @@ -37,9 +38,22 @@
1.12 struct B {};
1.13
1.14 class C {
1.15 - int x;
1.16 + int _x;
1.17 public:
1.18 - C(int _x) : x(_x) {}
1.19 + C(int x) : _x(x) {}
1.20 + int get() const { return _x; }
1.21 +};
1.22 +inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); }
1.23 +inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); }
1.24 +
1.25 +C createC(int x) { return C(x); }
1.26 +
1.27 +template <typename T>
1.28 +class Less {
1.29 + T _t;
1.30 +public:
1.31 + Less(T t): _t(t) {}
1.32 + bool operator()(const T& t) const { return t < _t; }
1.33 };
1.34
1.35 class F {
1.36 @@ -56,6 +70,14 @@
1.37
1.38 int binc(int a, B) { return a+1; }
1.39
1.40 +template <typename T>
1.41 +class Sum {
1.42 + T& _sum;
1.43 +public:
1.44 + Sum(T& sum) : _sum(sum) {}
1.45 + void operator()(const T& t) { _sum += t; }
1.46 +};
1.47 +
1.48 typedef ReadMap<A, double> DoubleMap;
1.49 typedef ReadWriteMap<A, double> DoubleWriteMap;
1.50 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
1.51 @@ -64,12 +86,6 @@
1.52 typedef ReadWriteMap<A, bool> BoolWriteMap;
1.53 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
1.54
1.55 -template<typename Map1, typename Map2, typename ItemIt>
1.56 -void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
1.57 - for (; it != INVALID; ++it)
1.58 - check(map1[it] == map2[it], "The maps are not equal");
1.59 -}
1.60 -
1.61 int main()
1.62 {
1.63 // Map concepts
1.64 @@ -494,9 +510,10 @@
1.65 check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
1.66 }
1.67
1.68 - compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
1.69 - targetMap(orienter(gr, constMap<Edge, bool>(false))),
1.70 - EdgeIt(gr));
1.71 + check(mapCompare(gr,
1.72 + sourceMap(orienter(gr, constMap<Edge, bool>(true))),
1.73 + targetMap(orienter(gr, constMap<Edge, bool>(false)))),
1.74 + "Wrong SourceMap or TargetMap");
1.75
1.76 typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
1.77 Digraph dgr(gr, constMap<Edge, bool>(true));
1.78 @@ -800,5 +817,183 @@
1.79 check(n == num, "Wrong number");
1.80
1.81 }
1.82 +
1.83 + // Graph map utilities:
1.84 + // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
1.85 + // mapFind(), mapFindIf(), mapCount(), mapCountIf()
1.86 + // mapCopy(), mapCompare(), mapFill()
1.87 + {
1.88 + DIGRAPH_TYPEDEFS(SmartDigraph);
1.89 +
1.90 + SmartDigraph g;
1.91 + Node n1 = g.addNode();
1.92 + Node n2 = g.addNode();
1.93 + Node n3 = g.addNode();
1.94 +
1.95 + SmartDigraph::NodeMap<int> map1(g);
1.96 + SmartDigraph::ArcMap<char> map2(g);
1.97 + ConstMap<Node, A> cmap1 = A();
1.98 + ConstMap<Arc, C> cmap2 = C(0);
1.99 +
1.100 + map1[n1] = 10;
1.101 + map1[n2] = 5;
1.102 + map1[n3] = 12;
1.103 +
1.104 + // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
1.105 + check(mapMin(g, map1) == n2, "Wrong mapMin()");
1.106 + check(mapMax(g, map1) == n3, "Wrong mapMax()");
1.107 + check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()");
1.108 + check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()");
1.109 + check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()");
1.110 + check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()");
1.111 +
1.112 + check(mapMin(g, map2) == INVALID, "Wrong mapMin()");
1.113 + check(mapMax(g, map2) == INVALID, "Wrong mapMax()");
1.114 +
1.115 + check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
1.116 + check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()");
1.117 +
1.118 + Arc a1 = g.addArc(n1, n2);
1.119 + Arc a2 = g.addArc(n1, n3);
1.120 + Arc a3 = g.addArc(n2, n3);
1.121 + Arc a4 = g.addArc(n3, n1);
1.122 +
1.123 + map2[a1] = 'b';
1.124 + map2[a2] = 'a';
1.125 + map2[a3] = 'b';
1.126 + map2[a4] = 'c';
1.127 +
1.128 + // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
1.129 + check(mapMin(g, map2) == a2, "Wrong mapMin()");
1.130 + check(mapMax(g, map2) == a4, "Wrong mapMax()");
1.131 + check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()");
1.132 + check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()");
1.133 + check(mapMinValue(g, map2, std::greater<int>()) == 'c',
1.134 + "Wrong mapMinValue()");
1.135 + check(mapMaxValue(g, map2, std::greater<int>()) == 'a',
1.136 + "Wrong mapMaxValue()");
1.137 +
1.138 + check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
1.139 + check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()");
1.140 + check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()");
1.141 +
1.142 + check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2,
1.143 + "Wrong mapMin()");
1.144 + check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4,
1.145 + "Wrong mapMax()");
1.146 + check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'),
1.147 + "Wrong mapMinValue()");
1.148 + check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'),
1.149 + "Wrong mapMaxValue()");
1.150 +
1.151 + // mapFind(), mapFindIf()
1.152 + check(mapFind(g, map1, 5) == n2, "Wrong mapFind()");
1.153 + check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()");
1.154 + check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()");
1.155 + check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()");
1.156 + check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()");
1.157 + check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()");
1.158 +
1.159 + check(mapFindIf(g, map1, Less<int>(7)) == n2,
1.160 + "Wrong mapFindIf()");
1.161 + check(mapFindIf(g, map1, Less<int>(5)) == INVALID,
1.162 + "Wrong mapFindIf()");
1.163 + check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g),
1.164 + "Wrong mapFindIf()");
1.165 + check(mapFindIf(g, map2, Less<char>('a')) == INVALID,
1.166 + "Wrong mapFindIf()");
1.167 +
1.168 + // mapCount(), mapCountIf()
1.169 + check(mapCount(g, map1, 5) == 1, "Wrong mapCount()");
1.170 + check(mapCount(g, map1, 6) == 0, "Wrong mapCount()");
1.171 + check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()");
1.172 + check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()");
1.173 + check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()");
1.174 + check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()");
1.175 + check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()");
1.176 +
1.177 + check(mapCountIf(g, map1, Less<int>(11)) == 2,
1.178 + "Wrong mapCountIf()");
1.179 + check(mapCountIf(g, map1, Less<int>(13)) == 3,
1.180 + "Wrong mapCountIf()");
1.181 + check(mapCountIf(g, map1, Less<int>(5)) == 0,
1.182 + "Wrong mapCountIf()");
1.183 + check(mapCountIf(g, map2, Less<char>('d')) == 4,
1.184 + "Wrong mapCountIf()");
1.185 + check(mapCountIf(g, map2, Less<char>('c')) == 3,
1.186 + "Wrong mapCountIf()");
1.187 + check(mapCountIf(g, map2, Less<char>('a')) == 0,
1.188 + "Wrong mapCountIf()");
1.189 +
1.190 + // MapIt, ConstMapIt
1.191 +/*
1.192 +These tests can be used after applying bugfix #330
1.193 + typedef SmartDigraph::NodeMap<int>::MapIt MapIt;
1.194 + typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt;
1.195 + check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5,
1.196 + "Wrong NodeMap<>::MapIt");
1.197 + check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12,
1.198 + "Wrong NodeMap<>::MapIt");
1.199 +
1.200 + int sum = 0;
1.201 + std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum));
1.202 + check(sum == 27, "Wrong NodeMap<>::MapIt");
1.203 + std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum));
1.204 + check(sum == 54, "Wrong NodeMap<>::ConstMapIt");
1.205 +*/
1.206 +
1.207 + // mapCopy(), mapCompare(), mapFill()
1.208 + check(mapCompare(g, map1, map1), "Wrong mapCompare()");
1.209 + check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()");
1.210 + check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()");
1.211 + check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()");
1.212 + check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()");
1.213 +
1.214 + SmartDigraph::NodeMap<int> map3(g, 0);
1.215 + SmartDigraph::ArcMap<char> map4(g, 'a');
1.216 +
1.217 + check(!mapCompare(g, map1, map3), "Wrong mapCompare()");
1.218 + check(!mapCompare(g, map2, map4), "Wrong mapCompare()");
1.219 +
1.220 + mapCopy(g, map1, map3);
1.221 + mapCopy(g, map2, map4);
1.222 +
1.223 + check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()");
1.224 + check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()");
1.225 +
1.226 + Undirector<SmartDigraph> ug(g);
1.227 + Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x');
1.228 + Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14);
1.229 +
1.230 + check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
1.231 + check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
1.232 + check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
1.233 + check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
1.234 +
1.235 + mapCopy(g, map2, umap1);
1.236 +
1.237 + check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
1.238 + check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
1.239 + check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
1.240 + check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
1.241 +
1.242 + mapCopy(g, map2, umap1);
1.243 + mapCopy(g, umap1, map2);
1.244 + mapCopy(ug, map2, umap1);
1.245 + mapCopy(ug, umap1, map2);
1.246 +
1.247 + check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
1.248 + mapCopy(ug, umap1, umap2);
1.249 + check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
1.250 +
1.251 + check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()");
1.252 + mapFill(g, map1, 2);
1.253 + check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()");
1.254 +
1.255 + check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()");
1.256 + mapCopy(g, constMap<Arc>('z'), map2);
1.257 + check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()");
1.258 + }
1.259 +
1.260 return 0;
1.261 }