test/maps_test.cc
changeset 766 ba79e8d64448
parent 731 7b1a6e963018
parent 741 71939d63ae77
child 773 3fc2a801c39e
     1.1 --- a/test/maps_test.cc	Thu Aug 20 20:34:30 2009 +0200
     1.2 +++ b/test/maps_test.cc	Fri Sep 25 09:33:09 2009 +0200
     1.3 @@ -22,6 +22,7 @@
     1.4  #include <lemon/concept_check.h>
     1.5  #include <lemon/concepts/maps.h>
     1.6  #include <lemon/maps.h>
     1.7 +#include <lemon/smart_graph.h>
     1.8  
     1.9  #include "test_tools.h"
    1.10  
    1.11 @@ -349,5 +350,226 @@
    1.12        check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
    1.13    }
    1.14  
    1.15 +  // CrossRefMap
    1.16 +  {
    1.17 +    typedef SmartDigraph Graph;
    1.18 +    DIGRAPH_TYPEDEFS(Graph);
    1.19 +
    1.20 +    checkConcept<ReadWriteMap<Node, int>,
    1.21 +                 CrossRefMap<Graph, Node, int> >();
    1.22 +    
    1.23 +    Graph gr;
    1.24 +    typedef CrossRefMap<Graph, Node, char> CRMap;
    1.25 +    typedef CRMap::ValueIterator ValueIt;
    1.26 +    CRMap map(gr);
    1.27 +    
    1.28 +    Node n0 = gr.addNode();
    1.29 +    Node n1 = gr.addNode();
    1.30 +    Node n2 = gr.addNode();
    1.31 +    
    1.32 +    map.set(n0, 'A');
    1.33 +    map.set(n1, 'B');
    1.34 +    map.set(n2, 'C');
    1.35 +    map.set(n2, 'A');
    1.36 +    map.set(n0, 'C');
    1.37 +
    1.38 +    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
    1.39 +          "Wrong CrossRefMap");
    1.40 +    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
    1.41 +    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
    1.42 +    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
    1.43 +
    1.44 +    ValueIt it = map.beginValue();
    1.45 +    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
    1.46 +          it == map.endValue(), "Wrong value iterator");
    1.47 +  }
    1.48 +  
    1.49 +  // Iterable bool map
    1.50 +  {
    1.51 +    typedef SmartGraph Graph;
    1.52 +    typedef SmartGraph::Node Item;
    1.53 +
    1.54 +    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
    1.55 +    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
    1.56 +
    1.57 +    const int num = 10;
    1.58 +    Graph g;
    1.59 +    std::vector<Item> items;
    1.60 +    for (int i = 0; i < num; ++i) {
    1.61 +      items.push_back(g.addNode());
    1.62 +    }
    1.63 +
    1.64 +    Ibm map1(g, true);
    1.65 +    int n = 0;
    1.66 +    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
    1.67 +      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
    1.68 +      ++n;
    1.69 +    }
    1.70 +    check(n == num, "Wrong number");
    1.71 +
    1.72 +    n = 0;
    1.73 +    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
    1.74 +        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
    1.75 +        ++n;
    1.76 +    }
    1.77 +    check(n == num, "Wrong number");
    1.78 +    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
    1.79 +    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
    1.80 +
    1.81 +    map1[items[5]] = true;
    1.82 +
    1.83 +    n = 0;
    1.84 +    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
    1.85 +        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
    1.86 +        ++n;
    1.87 +    }
    1.88 +    check(n == num, "Wrong number");
    1.89 +
    1.90 +    map1[items[num / 2]] = false;
    1.91 +    check(map1[items[num / 2]] == false, "Wrong map value");
    1.92 +
    1.93 +    n = 0;
    1.94 +    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
    1.95 +        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
    1.96 +        ++n;
    1.97 +    }
    1.98 +    check(n == num - 1, "Wrong number");
    1.99 +
   1.100 +    n = 0;
   1.101 +    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
   1.102 +        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
   1.103 +        ++n;
   1.104 +    }
   1.105 +    check(n == 1, "Wrong number");
   1.106 +
   1.107 +    map1[items[0]] = false;
   1.108 +    check(map1[items[0]] == false, "Wrong map value");
   1.109 +
   1.110 +    map1[items[num - 1]] = false;
   1.111 +    check(map1[items[num - 1]] == false, "Wrong map value");
   1.112 +
   1.113 +    n = 0;
   1.114 +    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
   1.115 +        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
   1.116 +        ++n;
   1.117 +    }
   1.118 +    check(n == num - 3, "Wrong number");
   1.119 +    check(map1.trueNum() == num - 3, "Wrong number");
   1.120 +
   1.121 +    n = 0;
   1.122 +    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
   1.123 +        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
   1.124 +        ++n;
   1.125 +    }
   1.126 +    check(n == 3, "Wrong number");
   1.127 +    check(map1.falseNum() == 3, "Wrong number");
   1.128 +  }
   1.129 +
   1.130 +  // Iterable int map
   1.131 +  {
   1.132 +    typedef SmartGraph Graph;
   1.133 +    typedef SmartGraph::Node Item;
   1.134 +    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
   1.135 +
   1.136 +    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
   1.137 +
   1.138 +    const int num = 10;
   1.139 +    Graph g;
   1.140 +    std::vector<Item> items;
   1.141 +    for (int i = 0; i < num; ++i) {
   1.142 +      items.push_back(g.addNode());
   1.143 +    }
   1.144 +
   1.145 +    Iim map1(g);
   1.146 +    check(map1.size() == 0, "Wrong size");
   1.147 +
   1.148 +    for (int i = 0; i < num; ++i) {
   1.149 +      map1[items[i]] = i;
   1.150 +    }
   1.151 +    check(map1.size() == num, "Wrong size");
   1.152 +
   1.153 +    for (int i = 0; i < num; ++i) {
   1.154 +      Iim::ItemIt it(map1, i);
   1.155 +      check(static_cast<Item>(it) == items[i], "Wrong value");
   1.156 +      ++it;
   1.157 +      check(static_cast<Item>(it) == INVALID, "Wrong value");
   1.158 +    }
   1.159 +
   1.160 +    for (int i = 0; i < num; ++i) {
   1.161 +      map1[items[i]] = i % 2;
   1.162 +    }
   1.163 +    check(map1.size() == 2, "Wrong size");
   1.164 +
   1.165 +    int n = 0;
   1.166 +    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
   1.167 +      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
   1.168 +      ++n;
   1.169 +    }
   1.170 +    check(n == (num + 1) / 2, "Wrong number");
   1.171 +
   1.172 +    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
   1.173 +      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
   1.174 +      ++n;
   1.175 +    }
   1.176 +    check(n == num, "Wrong number");
   1.177 +
   1.178 +  }
   1.179 +
   1.180 +  // Iterable value map
   1.181 +  {
   1.182 +    typedef SmartGraph Graph;
   1.183 +    typedef SmartGraph::Node Item;
   1.184 +    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
   1.185 +
   1.186 +    checkConcept<ReadWriteMap<Item, double>, Ivm>();
   1.187 +
   1.188 +    const int num = 10;
   1.189 +    Graph g;
   1.190 +    std::vector<Item> items;
   1.191 +    for (int i = 0; i < num; ++i) {
   1.192 +      items.push_back(g.addNode());
   1.193 +    }
   1.194 +
   1.195 +    Ivm map1(g, 0.0);
   1.196 +    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
   1.197 +    check(*map1.beginValue() == 0.0, "Wrong value");
   1.198 +
   1.199 +    for (int i = 0; i < num; ++i) {
   1.200 +      map1.set(items[i], static_cast<double>(i));
   1.201 +    }
   1.202 +    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
   1.203 +
   1.204 +    for (int i = 0; i < num; ++i) {
   1.205 +      Ivm::ItemIt it(map1, static_cast<double>(i));
   1.206 +      check(static_cast<Item>(it) == items[i], "Wrong value");
   1.207 +      ++it;
   1.208 +      check(static_cast<Item>(it) == INVALID, "Wrong value");
   1.209 +    }
   1.210 +
   1.211 +    for (Ivm::ValueIterator vit = map1.beginValue();
   1.212 +         vit != map1.endValue(); ++vit) {
   1.213 +      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
   1.214 +            "Wrong ValueIterator");
   1.215 +    }
   1.216 +
   1.217 +    for (int i = 0; i < num; ++i) {
   1.218 +      map1.set(items[i], static_cast<double>(i % 2));
   1.219 +    }
   1.220 +    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
   1.221 +
   1.222 +    int n = 0;
   1.223 +    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
   1.224 +      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
   1.225 +      ++n;
   1.226 +    }
   1.227 +    check(n == (num + 1) / 2, "Wrong number");
   1.228 +
   1.229 +    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
   1.230 +      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
   1.231 +      ++n;
   1.232 +    }
   1.233 +    check(n == num, "Wrong number");
   1.234 +
   1.235 +  }
   1.236    return 0;
   1.237  }