diff -r c92296660262 -r a2d5fd4c309a test/maps_test.cc --- a/test/maps_test.cc Wed Nov 18 14:38:02 2009 +0100 +++ b/test/maps_test.cc Wed Nov 18 14:38:38 2009 +0100 @@ -26,6 +26,7 @@ #include <lemon/smart_graph.h> #include <lemon/adaptors.h> #include <lemon/dfs.h> +#include <algorithm> #include "test_tools.h" @@ -37,9 +38,22 @@ struct B {}; class C { - int x; + int _x; public: - C(int _x) : x(_x) {} + C(int x) : _x(x) {} + int get() const { return _x; } +}; +inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); } +inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); } + +C createC(int x) { return C(x); } + +template <typename T> +class Less { + T _t; +public: + Less(T t): _t(t) {} + bool operator()(const T& t) const { return t < _t; } }; class F { @@ -56,6 +70,14 @@ int binc(int a, B) { return a+1; } +template <typename T> +class Sum { + T& _sum; +public: + Sum(T& sum) : _sum(sum) {} + void operator()(const T& t) { _sum += t; } +}; + typedef ReadMap<A, double> DoubleMap; typedef ReadWriteMap<A, double> DoubleWriteMap; typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; @@ -64,12 +86,6 @@ typedef ReadWriteMap<A, bool> BoolWriteMap; typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; -template<typename Map1, typename Map2, typename ItemIt> -void compareMap(const Map1& map1, const Map2& map2, ItemIt it) { - for (; it != INVALID; ++it) - check(map1[it] == map2[it], "The maps are not equal"); -} - int main() { // Map concepts @@ -494,9 +510,10 @@ check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap"); } - compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))), - targetMap(orienter(gr, constMap<Edge, bool>(false))), - EdgeIt(gr)); + check(mapCompare(gr, + sourceMap(orienter(gr, constMap<Edge, bool>(true))), + targetMap(orienter(gr, constMap<Edge, bool>(false)))), + "Wrong SourceMap or TargetMap"); typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph; Digraph dgr(gr, constMap<Edge, bool>(true)); @@ -800,5 +817,183 @@ check(n == num, "Wrong number"); } + + // Graph map utilities: + // mapMin(), mapMax(), mapMinValue(), mapMaxValue() + // mapFind(), mapFindIf(), mapCount(), mapCountIf() + // mapCopy(), mapCompare(), mapFill() + { + DIGRAPH_TYPEDEFS(SmartDigraph); + + SmartDigraph g; + Node n1 = g.addNode(); + Node n2 = g.addNode(); + Node n3 = g.addNode(); + + SmartDigraph::NodeMap<int> map1(g); + SmartDigraph::ArcMap<char> map2(g); + ConstMap<Node, A> cmap1 = A(); + ConstMap<Arc, C> cmap2 = C(0); + + map1[n1] = 10; + map1[n2] = 5; + map1[n3] = 12; + + // mapMin(), mapMax(), mapMinValue(), mapMaxValue() + check(mapMin(g, map1) == n2, "Wrong mapMin()"); + check(mapMax(g, map1) == n3, "Wrong mapMax()"); + check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()"); + check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()"); + check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()"); + check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()"); + + check(mapMin(g, map2) == INVALID, "Wrong mapMin()"); + check(mapMax(g, map2) == INVALID, "Wrong mapMax()"); + + check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); + check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()"); + + Arc a1 = g.addArc(n1, n2); + Arc a2 = g.addArc(n1, n3); + Arc a3 = g.addArc(n2, n3); + Arc a4 = g.addArc(n3, n1); + + map2[a1] = 'b'; + map2[a2] = 'a'; + map2[a3] = 'b'; + map2[a4] = 'c'; + + // mapMin(), mapMax(), mapMinValue(), mapMaxValue() + check(mapMin(g, map2) == a2, "Wrong mapMin()"); + check(mapMax(g, map2) == a4, "Wrong mapMax()"); + check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()"); + check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()"); + check(mapMinValue(g, map2, std::greater<int>()) == 'c', + "Wrong mapMinValue()"); + check(mapMaxValue(g, map2, std::greater<int>()) == 'a', + "Wrong mapMaxValue()"); + + check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); + check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()"); + check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()"); + + check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2, + "Wrong mapMin()"); + check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4, + "Wrong mapMax()"); + check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'), + "Wrong mapMinValue()"); + check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'), + "Wrong mapMaxValue()"); + + // mapFind(), mapFindIf() + check(mapFind(g, map1, 5) == n2, "Wrong mapFind()"); + check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()"); + check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()"); + check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()"); + check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()"); + check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()"); + + check(mapFindIf(g, map1, Less<int>(7)) == n2, + "Wrong mapFindIf()"); + check(mapFindIf(g, map1, Less<int>(5)) == INVALID, + "Wrong mapFindIf()"); + check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g), + "Wrong mapFindIf()"); + check(mapFindIf(g, map2, Less<char>('a')) == INVALID, + "Wrong mapFindIf()"); + + // mapCount(), mapCountIf() + check(mapCount(g, map1, 5) == 1, "Wrong mapCount()"); + check(mapCount(g, map1, 6) == 0, "Wrong mapCount()"); + check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()"); + check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()"); + check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()"); + check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()"); + check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()"); + + check(mapCountIf(g, map1, Less<int>(11)) == 2, + "Wrong mapCountIf()"); + check(mapCountIf(g, map1, Less<int>(13)) == 3, + "Wrong mapCountIf()"); + check(mapCountIf(g, map1, Less<int>(5)) == 0, + "Wrong mapCountIf()"); + check(mapCountIf(g, map2, Less<char>('d')) == 4, + "Wrong mapCountIf()"); + check(mapCountIf(g, map2, Less<char>('c')) == 3, + "Wrong mapCountIf()"); + check(mapCountIf(g, map2, Less<char>('a')) == 0, + "Wrong mapCountIf()"); + + // MapIt, ConstMapIt +/* +These tests can be used after applying bugfix #330 + typedef SmartDigraph::NodeMap<int>::MapIt MapIt; + typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt; + check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5, + "Wrong NodeMap<>::MapIt"); + check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12, + "Wrong NodeMap<>::MapIt"); + + int sum = 0; + std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum)); + check(sum == 27, "Wrong NodeMap<>::MapIt"); + std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum)); + check(sum == 54, "Wrong NodeMap<>::ConstMapIt"); +*/ + + // mapCopy(), mapCompare(), mapFill() + check(mapCompare(g, map1, map1), "Wrong mapCompare()"); + check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()"); + check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()"); + check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()"); + check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()"); + + SmartDigraph::NodeMap<int> map3(g, 0); + SmartDigraph::ArcMap<char> map4(g, 'a'); + + check(!mapCompare(g, map1, map3), "Wrong mapCompare()"); + check(!mapCompare(g, map2, map4), "Wrong mapCompare()"); + + mapCopy(g, map1, map3); + mapCopy(g, map2, map4); + + check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()"); + check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()"); + + Undirector<SmartDigraph> ug(g); + Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x'); + Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14); + + check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); + check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); + check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); + check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); + + mapCopy(g, map2, umap1); + + check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); + check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); + check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); + check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); + + mapCopy(g, map2, umap1); + mapCopy(g, umap1, map2); + mapCopy(ug, map2, umap1); + mapCopy(ug, umap1, map2); + + check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); + mapCopy(ug, umap1, umap2); + check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); + + check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()"); + mapFill(g, map1, 2); + check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()"); + + check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()"); + mapCopy(g, constMap<Arc>('z'), map2); + check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()"); + } + return 0; }