test/maps_test.cc
changeset 729 be48a648d28f
parent 684 7b1a6e963018
parent 694 71939d63ae77
child 726 3fc2a801c39e
equal deleted inserted replaced
13:4a6b01c1896b 16:330cf283f414
    20 #include <set>
    20 #include <set>
    21 
    21 
    22 #include <lemon/concept_check.h>
    22 #include <lemon/concept_check.h>
    23 #include <lemon/concepts/maps.h>
    23 #include <lemon/concepts/maps.h>
    24 #include <lemon/maps.h>
    24 #include <lemon/maps.h>
    25 #include <lemon/list_graph.h>
    25 #include <lemon/smart_graph.h>
    26 
    26 
    27 #include "test_tools.h"
    27 #include "test_tools.h"
    28 
    28 
    29 using namespace lemon;
    29 using namespace lemon;
    30 using namespace lemon::concepts;
    30 using namespace lemon::concepts;
   347     int i = 0;
   347     int i = 0;
   348     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   348     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   349           it != map2.end(); ++it )
   349           it != map2.end(); ++it )
   350       check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
   350       check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
   351   }
   351   }
   352   
   352 
   353   // CrossRefMap
   353   // CrossRefMap
   354   {
   354   {
   355     typedef ListDigraph Graph;
   355     typedef SmartDigraph Graph;
   356     DIGRAPH_TYPEDEFS(Graph);
   356     DIGRAPH_TYPEDEFS(Graph);
   357 
   357 
   358     checkConcept<ReadWriteMap<Node, int>,
   358     checkConcept<ReadWriteMap<Node, int>,
   359                  CrossRefMap<Graph, Node, int> >();
   359                  CrossRefMap<Graph, Node, int> >();
   360     
   360     
   381 
   381 
   382     ValueIt it = map.beginValue();
   382     ValueIt it = map.beginValue();
   383     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   383     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   384           it == map.endValue(), "Wrong value iterator");
   384           it == map.endValue(), "Wrong value iterator");
   385   }
   385   }
   386 
   386   
       
   387   // Iterable bool map
       
   388   {
       
   389     typedef SmartGraph Graph;
       
   390     typedef SmartGraph::Node Item;
       
   391 
       
   392     typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
       
   393     checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
       
   394 
       
   395     const int num = 10;
       
   396     Graph g;
       
   397     std::vector<Item> items;
       
   398     for (int i = 0; i < num; ++i) {
       
   399       items.push_back(g.addNode());
       
   400     }
       
   401 
       
   402     Ibm map1(g, true);
       
   403     int n = 0;
       
   404     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   405       check(map1[static_cast<Item>(it)], "Wrong TrueIt");
       
   406       ++n;
       
   407     }
       
   408     check(n == num, "Wrong number");
       
   409 
       
   410     n = 0;
       
   411     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
       
   412         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
       
   413         ++n;
       
   414     }
       
   415     check(n == num, "Wrong number");
       
   416     check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
       
   417     check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
       
   418 
       
   419     map1[items[5]] = true;
       
   420 
       
   421     n = 0;
       
   422     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
       
   423         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
       
   424         ++n;
       
   425     }
       
   426     check(n == num, "Wrong number");
       
   427 
       
   428     map1[items[num / 2]] = false;
       
   429     check(map1[items[num / 2]] == false, "Wrong map value");
       
   430 
       
   431     n = 0;
       
   432     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   433         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
       
   434         ++n;
       
   435     }
       
   436     check(n == num - 1, "Wrong number");
       
   437 
       
   438     n = 0;
       
   439     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
       
   440         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
       
   441         ++n;
       
   442     }
       
   443     check(n == 1, "Wrong number");
       
   444 
       
   445     map1[items[0]] = false;
       
   446     check(map1[items[0]] == false, "Wrong map value");
       
   447 
       
   448     map1[items[num - 1]] = false;
       
   449     check(map1[items[num - 1]] == false, "Wrong map value");
       
   450 
       
   451     n = 0;
       
   452     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   453         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
       
   454         ++n;
       
   455     }
       
   456     check(n == num - 3, "Wrong number");
       
   457     check(map1.trueNum() == num - 3, "Wrong number");
       
   458 
       
   459     n = 0;
       
   460     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
       
   461         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
       
   462         ++n;
       
   463     }
       
   464     check(n == 3, "Wrong number");
       
   465     check(map1.falseNum() == 3, "Wrong number");
       
   466   }
       
   467 
       
   468   // Iterable int map
       
   469   {
       
   470     typedef SmartGraph Graph;
       
   471     typedef SmartGraph::Node Item;
       
   472     typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
       
   473 
       
   474     checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
       
   475 
       
   476     const int num = 10;
       
   477     Graph g;
       
   478     std::vector<Item> items;
       
   479     for (int i = 0; i < num; ++i) {
       
   480       items.push_back(g.addNode());
       
   481     }
       
   482 
       
   483     Iim map1(g);
       
   484     check(map1.size() == 0, "Wrong size");
       
   485 
       
   486     for (int i = 0; i < num; ++i) {
       
   487       map1[items[i]] = i;
       
   488     }
       
   489     check(map1.size() == num, "Wrong size");
       
   490 
       
   491     for (int i = 0; i < num; ++i) {
       
   492       Iim::ItemIt it(map1, i);
       
   493       check(static_cast<Item>(it) == items[i], "Wrong value");
       
   494       ++it;
       
   495       check(static_cast<Item>(it) == INVALID, "Wrong value");
       
   496     }
       
   497 
       
   498     for (int i = 0; i < num; ++i) {
       
   499       map1[items[i]] = i % 2;
       
   500     }
       
   501     check(map1.size() == 2, "Wrong size");
       
   502 
       
   503     int n = 0;
       
   504     for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
       
   505       check(map1[static_cast<Item>(it)] == 0, "Wrong value");
       
   506       ++n;
       
   507     }
       
   508     check(n == (num + 1) / 2, "Wrong number");
       
   509 
       
   510     for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
       
   511       check(map1[static_cast<Item>(it)] == 1, "Wrong value");
       
   512       ++n;
       
   513     }
       
   514     check(n == num, "Wrong number");
       
   515 
       
   516   }
       
   517 
       
   518   // Iterable value map
       
   519   {
       
   520     typedef SmartGraph Graph;
       
   521     typedef SmartGraph::Node Item;
       
   522     typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
       
   523 
       
   524     checkConcept<ReadWriteMap<Item, double>, Ivm>();
       
   525 
       
   526     const int num = 10;
       
   527     Graph g;
       
   528     std::vector<Item> items;
       
   529     for (int i = 0; i < num; ++i) {
       
   530       items.push_back(g.addNode());
       
   531     }
       
   532 
       
   533     Ivm map1(g, 0.0);
       
   534     check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
       
   535     check(*map1.beginValue() == 0.0, "Wrong value");
       
   536 
       
   537     for (int i = 0; i < num; ++i) {
       
   538       map1.set(items[i], static_cast<double>(i));
       
   539     }
       
   540     check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
       
   541 
       
   542     for (int i = 0; i < num; ++i) {
       
   543       Ivm::ItemIt it(map1, static_cast<double>(i));
       
   544       check(static_cast<Item>(it) == items[i], "Wrong value");
       
   545       ++it;
       
   546       check(static_cast<Item>(it) == INVALID, "Wrong value");
       
   547     }
       
   548 
       
   549     for (Ivm::ValueIterator vit = map1.beginValue();
       
   550          vit != map1.endValue(); ++vit) {
       
   551       check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
       
   552             "Wrong ValueIterator");
       
   553     }
       
   554 
       
   555     for (int i = 0; i < num; ++i) {
       
   556       map1.set(items[i], static_cast<double>(i % 2));
       
   557     }
       
   558     check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
       
   559 
       
   560     int n = 0;
       
   561     for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
       
   562       check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
       
   563       ++n;
       
   564     }
       
   565     check(n == (num + 1) / 2, "Wrong number");
       
   566 
       
   567     for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
       
   568       check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
       
   569       ++n;
       
   570     }
       
   571     check(n == num, "Wrong number");
       
   572 
       
   573   }
   387   return 0;
   574   return 0;
   388 }
   575 }