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