test/maps_test.cc
changeset 722 b52189c479fb
parent 720 6e8c27ee9079
parent 694 71939d63ae77
child 723 acdd0bd75a55
equal deleted inserted replaced
17:2ff349d2cf21 18:e591948109cf
    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/list_graph.h>
       
    26 #include <lemon/smart_graph.h>
    26 
    27 
    27 #include "test_tools.h"
    28 #include "test_tools.h"
    28 
    29 
    29 using namespace lemon;
    30 using namespace lemon;
    30 using namespace lemon::concepts;
    31 using namespace lemon::concepts;
   492     it = map.beginValue();
   493     it = map.beginValue();
   493     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   494     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   494           it == map.endValue(), "Wrong value iterator");
   495           it == map.endValue(), "Wrong value iterator");
   495   }
   496   }
   496 
   497 
       
   498   // Iterable bool map
       
   499   {
       
   500     typedef SmartGraph Graph;
       
   501     typedef SmartGraph::Node Item;
       
   502 
       
   503     typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
       
   504     checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
       
   505 
       
   506     const int num = 10;
       
   507     Graph g;
       
   508     std::vector<Item> items;
       
   509     for (int i = 0; i < num; ++i) {
       
   510       items.push_back(g.addNode());
       
   511     }
       
   512 
       
   513     Ibm map1(g, true);
       
   514     int n = 0;
       
   515     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   516       check(map1[static_cast<Item>(it)], "Wrong TrueIt");
       
   517       ++n;
       
   518     }
       
   519     check(n == num, "Wrong number");
       
   520 
       
   521     n = 0;
       
   522     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
       
   523         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
       
   524         ++n;
       
   525     }
       
   526     check(n == num, "Wrong number");
       
   527     check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
       
   528     check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
       
   529 
       
   530     map1[items[5]] = true;
       
   531 
       
   532     n = 0;
       
   533     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
       
   534         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
       
   535         ++n;
       
   536     }
       
   537     check(n == num, "Wrong number");
       
   538 
       
   539     map1[items[num / 2]] = false;
       
   540     check(map1[items[num / 2]] == false, "Wrong map value");
       
   541 
       
   542     n = 0;
       
   543     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   544         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
       
   545         ++n;
       
   546     }
       
   547     check(n == num - 1, "Wrong number");
       
   548 
       
   549     n = 0;
       
   550     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
       
   551         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
       
   552         ++n;
       
   553     }
       
   554     check(n == 1, "Wrong number");
       
   555 
       
   556     map1[items[0]] = false;
       
   557     check(map1[items[0]] == false, "Wrong map value");
       
   558 
       
   559     map1[items[num - 1]] = false;
       
   560     check(map1[items[num - 1]] == false, "Wrong map value");
       
   561 
       
   562     n = 0;
       
   563     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
       
   564         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
       
   565         ++n;
       
   566     }
       
   567     check(n == num - 3, "Wrong number");
       
   568     check(map1.trueNum() == num - 3, "Wrong number");
       
   569 
       
   570     n = 0;
       
   571     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
       
   572         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
       
   573         ++n;
       
   574     }
       
   575     check(n == 3, "Wrong number");
       
   576     check(map1.falseNum() == 3, "Wrong number");
       
   577   }
       
   578 
       
   579   // Iterable int map
       
   580   {
       
   581     typedef SmartGraph Graph;
       
   582     typedef SmartGraph::Node Item;
       
   583     typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
       
   584 
       
   585     checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
       
   586 
       
   587     const int num = 10;
       
   588     Graph g;
       
   589     std::vector<Item> items;
       
   590     for (int i = 0; i < num; ++i) {
       
   591       items.push_back(g.addNode());
       
   592     }
       
   593 
       
   594     Iim map1(g);
       
   595     check(map1.size() == 0, "Wrong size");
       
   596 
       
   597     for (int i = 0; i < num; ++i) {
       
   598       map1[items[i]] = i;
       
   599     }
       
   600     check(map1.size() == num, "Wrong size");
       
   601 
       
   602     for (int i = 0; i < num; ++i) {
       
   603       Iim::ItemIt it(map1, i);
       
   604       check(static_cast<Item>(it) == items[i], "Wrong value");
       
   605       ++it;
       
   606       check(static_cast<Item>(it) == INVALID, "Wrong value");
       
   607     }
       
   608 
       
   609     for (int i = 0; i < num; ++i) {
       
   610       map1[items[i]] = i % 2;
       
   611     }
       
   612     check(map1.size() == 2, "Wrong size");
       
   613 
       
   614     int n = 0;
       
   615     for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
       
   616       check(map1[static_cast<Item>(it)] == 0, "Wrong value");
       
   617       ++n;
       
   618     }
       
   619     check(n == (num + 1) / 2, "Wrong number");
       
   620 
       
   621     for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
       
   622       check(map1[static_cast<Item>(it)] == 1, "Wrong value");
       
   623       ++n;
       
   624     }
       
   625     check(n == num, "Wrong number");
       
   626 
       
   627   }
       
   628 
       
   629   // Iterable value map
       
   630   {
       
   631     typedef SmartGraph Graph;
       
   632     typedef SmartGraph::Node Item;
       
   633     typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
       
   634 
       
   635     checkConcept<ReadWriteMap<Item, double>, Ivm>();
       
   636 
       
   637     const int num = 10;
       
   638     Graph g;
       
   639     std::vector<Item> items;
       
   640     for (int i = 0; i < num; ++i) {
       
   641       items.push_back(g.addNode());
       
   642     }
       
   643 
       
   644     Ivm map1(g, 0.0);
       
   645     check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
       
   646     check(*map1.beginValue() == 0.0, "Wrong value");
       
   647 
       
   648     for (int i = 0; i < num; ++i) {
       
   649       map1.set(items[i], static_cast<double>(i));
       
   650     }
       
   651     check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
       
   652 
       
   653     for (int i = 0; i < num; ++i) {
       
   654       Ivm::ItemIt it(map1, static_cast<double>(i));
       
   655       check(static_cast<Item>(it) == items[i], "Wrong value");
       
   656       ++it;
       
   657       check(static_cast<Item>(it) == INVALID, "Wrong value");
       
   658     }
       
   659 
       
   660     for (Ivm::ValueIterator vit = map1.beginValue();
       
   661          vit != map1.endValue(); ++vit) {
       
   662       check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
       
   663             "Wrong ValueIterator");
       
   664     }
       
   665 
       
   666     for (int i = 0; i < num; ++i) {
       
   667       map1.set(items[i], static_cast<double>(i % 2));
       
   668     }
       
   669     check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
       
   670 
       
   671     int n = 0;
       
   672     for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
       
   673       check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
       
   674       ++n;
       
   675     }
       
   676     check(n == (num + 1) / 2, "Wrong number");
       
   677 
       
   678     for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
       
   679       check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
       
   680       ++n;
       
   681     }
       
   682     check(n == num, "Wrong number");
       
   683 
       
   684   }
   497   return 0;
   685   return 0;
   498 }
   686 }