test/matrix_maps_test.cc
author alpar
Fri, 20 Oct 2006 12:05:54 +0000
changeset 2256 b22dfb6c5ff3
parent 2039 dacc4ce9474d
child 2260 4274224f8a7d
permissions -rw-r--r--
Graph imlementations actually provide ReferenceMaps.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <iostream>
    20 #include <vector>
    21 
    22 #include <lemon/concept_check.h>
    23 
    24 #include <lemon/concept/matrix_maps.h>
    25 #include <lemon/concept/maps.h>
    26 #include <lemon/concept/graph.h>
    27 
    28 #include <lemon/matrix_maps.h>
    29 
    30 #include <lemon/smart_graph.h>
    31 
    32 #include "test_tools.h"
    33 #include "graph_test.h"
    34 #include "map_test.h"
    35 
    36 
    37 using namespace lemon;
    38 using namespace lemon::concept;
    39 
    40 int main() {
    41   typedef SmartGraph Graph;
    42   typedef Graph::Node Node;
    43   typedef Graph::Edge Edge;
    44 
    45   { // checking MatrixMap for int
    46     typedef DynamicMatrixMap<Graph, Node, int> IntMatrixMap;
    47     checkConcept<ReferenceMatrixMap<Node, Node, int, 
    48       IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
    49       IntMatrixMap>();
    50 
    51   }
    52 
    53   { // checking MatrixMap for bool
    54     typedef DynamicMatrixMap<Graph, Node, bool> BoolMatrixMap;
    55     checkConcept<ReferenceMatrixMap<Node, Node, bool, 
    56       BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
    57       BoolMatrixMap>();
    58 
    59   }
    60 
    61   {
    62     Graph graph;
    63     typedef DynamicMatrixMap<Graph, Node, int> IntMatrixMap;
    64     IntMatrixMap matrix(graph);
    65     for (int i = 0; i < 10; ++i) {
    66       graph.addNode();
    67     }
    68     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
    69       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
    70 	int val = rnd[100];
    71 	matrix.set(it, jt, val);
    72 	check(matrix(it, jt) == val, "Wrong assign");
    73 	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
    74 	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
    75       }
    76     }
    77     const IntMatrixMap& cm = matrix;
    78     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
    79       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
    80 	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
    81 	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
    82       }
    83     }
    84   }
    85 
    86   { // checking MatrixMap for int
    87     typedef DynamicSymMatrixMap<Graph, Node, int> IntMatrixMap;
    88     checkConcept<ReferenceMatrixMap<Node, Node, int, 
    89       IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
    90       IntMatrixMap>();
    91 
    92   }
    93 
    94   { // checking MatrixMap for bool
    95     typedef DynamicSymMatrixMap<Graph, Node, bool> BoolMatrixMap;
    96     checkConcept<ReferenceMatrixMap<Node, Node, bool, 
    97       BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
    98       BoolMatrixMap>();
    99 
   100   }
   101 
   102   {
   103     Graph graph;
   104     typedef DynamicSymMatrixMap<Graph, Node, int> IntMatrixMap;
   105     IntMatrixMap matrix(graph);
   106     for (int i = 0; i < 10; ++i) {
   107       graph.addNode();
   108     }
   109     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
   110       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
   111 	int val = rnd[100];
   112 	matrix.set(it, jt, val);
   113 	check(matrix(it, jt) == val, "Wrong assign");
   114 	check(matrix(jt, it) == val, "Wrong assign");
   115 	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
   116 	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
   117       }
   118     }
   119     const IntMatrixMap& cm = matrix;
   120     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
   121       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
   122 	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
   123 	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
   124       }
   125     }
   126   }
   127 
   128   { // checking MatrixMap for int
   129     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, int> IntMatrixMap;
   130     checkConcept<ReferenceMatrixMap<Node, Edge, int, 
   131       IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
   132       IntMatrixMap>();
   133 
   134   }
   135 
   136   { // checking MatrixMap for bool
   137     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, bool> BoolMatrixMap;
   138     checkConcept<ReferenceMatrixMap<Node, Edge, bool, 
   139       BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
   140       BoolMatrixMap>();
   141 
   142   }
   143 
   144   {
   145     Graph graph1, graph2;
   146     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, int> IntMatrixMap;
   147     IntMatrixMap matrix(graph1, graph2);
   148     for (int i = 0; i < 10; ++i) {
   149       graph1.addNode();
   150     }
   151     graph2.addNode();
   152     for (int i = 0; i < 20; ++i) {
   153       graph2.addEdge(Graph::NodeIt(graph2), Graph::NodeIt(graph2));
   154     }
   155     for (Graph::NodeIt it(graph1); it != INVALID; ++it) {
   156       for (Graph::EdgeIt jt(graph2); jt != INVALID; ++jt) {
   157 	int val = rnd[100];
   158 	matrix.set(it, jt, val);
   159 	check(matrix(it, jt) == val, "Wrong assign");
   160 	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
   161 	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
   162       }
   163     }
   164     const IntMatrixMap& cm = matrix;
   165     for (Graph::NodeIt it(graph1); it != INVALID; ++it) {
   166       for (Graph::EdgeIt jt(graph2); jt != INVALID; ++jt) {
   167 	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
   168 	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
   169       }
   170     }
   171   }
   172 
   173   { // checking MatrixMap for int
   174     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, int> IntMatrixMap;
   175     checkConcept<ReferenceMatrixMap<Node, Node, int, 
   176       IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
   177       IntMatrixMap>();
   178 
   179   }
   180 
   181   { // checking MatrixMap for bool
   182     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, bool> BoolMatrixMap;
   183     checkConcept<ReferenceMatrixMap<Node, Node, bool, 
   184       BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
   185       BoolMatrixMap>();
   186 
   187   }
   188 
   189   {
   190     Graph graph;
   191     typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, int> IntMatrixMap;
   192     IntMatrixMap matrix(graph, graph);
   193     for (int i = 0; i < 10; ++i) {
   194       graph.addNode();
   195     }
   196     for (int i = 0; i < 20; ++i) {
   197       graph.addEdge(Graph::NodeIt(graph), Graph::NodeIt(graph));
   198     }
   199     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
   200       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
   201 	int val = rnd[100];
   202 	matrix.set(it, jt, val);
   203 	check(matrix(it, jt) == val, "Wrong assign");
   204 	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
   205 	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
   206       }
   207     }
   208     const IntMatrixMap& cm = matrix;
   209     for (Graph::NodeIt it(graph); it != INVALID; ++it) {
   210       for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
   211 	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
   212 	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
   213       }
   214     }
   215   }
   216 
   217   std::cout << __FILE__ ": All tests passed.\n";
   218 
   219   return 0;
   220 }