test/edge_set_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 11 Aug 2013 22:32:16 +0200
branch1.3
changeset 1293 bc0fc7384c50
parent 1259 8b2d4e5d96e4
permissions -rw-r--r--
Merge bugfix #471 and #472 to branch 1.3
deba@491
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@491
     2
 *
deba@491
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@491
     4
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
deba@491
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@491
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@491
     8
 *
deba@491
     9
 * Permission to use, modify and distribute this software is granted
deba@491
    10
 * provided that this copyright notice appears in all copies. For
deba@491
    11
 * precise terms see the accompanying LICENSE file.
deba@491
    12
 *
deba@491
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@491
    14
 * express or implied, and with no claim as to its suitability for any
deba@491
    15
 * purpose.
deba@491
    16
 *
deba@491
    17
 */
deba@491
    18
deba@491
    19
#include <iostream>
deba@491
    20
#include <vector>
deba@491
    21
deba@491
    22
#include <lemon/concepts/digraph.h>
deba@491
    23
#include <lemon/concepts/graph.h>
deba@491
    24
#include <lemon/concept_check.h>
deba@491
    25
deba@491
    26
#include <lemon/list_graph.h>
deba@491
    27
deba@491
    28
#include <lemon/edge_set.h>
deba@491
    29
deba@491
    30
#include "graph_test.h"
deba@491
    31
#include "test_tools.h"
deba@491
    32
deba@491
    33
using namespace lemon;
deba@491
    34
deba@491
    35
void checkSmartArcSet() {
deba@559
    36
  checkConcept<concepts::Digraph, SmartArcSet<ListDigraph> >();
deba@491
    37
deba@491
    38
  typedef ListDigraph Digraph;
deba@491
    39
  typedef SmartArcSet<Digraph> ArcSet;
deba@491
    40
deba@491
    41
  Digraph digraph;
deba@491
    42
  Digraph::Node
deba@491
    43
    n1 = digraph.addNode(),
deba@491
    44
    n2 = digraph.addNode();
deba@491
    45
deba@491
    46
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
alpar@1257
    47
  ::lemon::ignore_unused_variable_warning(ga1);
deba@491
    48
deba@491
    49
  ArcSet arc_set(digraph);
deba@491
    50
deba@491
    51
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
alpar@1257
    52
  ::lemon::ignore_unused_variable_warning(ga2);
deba@491
    53
deba@491
    54
  checkGraphNodeList(arc_set, 2);
deba@491
    55
  checkGraphArcList(arc_set, 0);
deba@491
    56
deba@491
    57
  Digraph::Node
deba@491
    58
    n3 = digraph.addNode();
deba@491
    59
  checkGraphNodeList(arc_set, 3);
deba@491
    60
  checkGraphArcList(arc_set, 0);
deba@491
    61
deba@491
    62
  ArcSet::Arc a1 = arc_set.addArc(n1, n2);
deba@491
    63
  check(arc_set.source(a1) == n1 && arc_set.target(a1) == n2, "Wrong arc");
deba@491
    64
  checkGraphNodeList(arc_set, 3);
deba@491
    65
  checkGraphArcList(arc_set, 1);
deba@491
    66
deba@491
    67
  checkGraphOutArcList(arc_set, n1, 1);
deba@491
    68
  checkGraphOutArcList(arc_set, n2, 0);
deba@491
    69
  checkGraphOutArcList(arc_set, n3, 0);
deba@491
    70
deba@491
    71
  checkGraphInArcList(arc_set, n1, 0);
deba@491
    72
  checkGraphInArcList(arc_set, n2, 1);
deba@491
    73
  checkGraphInArcList(arc_set, n3, 0);
deba@491
    74
deba@491
    75
  checkGraphConArcList(arc_set, 1);
deba@491
    76
deba@491
    77
  ArcSet::Arc a2 = arc_set.addArc(n2, n1),
deba@491
    78
    a3 = arc_set.addArc(n2, n3),
deba@491
    79
    a4 = arc_set.addArc(n2, n3);
alpar@1257
    80
  ::lemon::ignore_unused_variable_warning(a2,a3,a4);
alpar@1157
    81
deba@491
    82
  checkGraphNodeList(arc_set, 3);
deba@491
    83
  checkGraphArcList(arc_set, 4);
deba@491
    84
deba@491
    85
  checkGraphOutArcList(arc_set, n1, 1);
deba@491
    86
  checkGraphOutArcList(arc_set, n2, 3);
deba@491
    87
  checkGraphOutArcList(arc_set, n3, 0);
deba@491
    88
deba@491
    89
  checkGraphInArcList(arc_set, n1, 1);
deba@491
    90
  checkGraphInArcList(arc_set, n2, 1);
deba@491
    91
  checkGraphInArcList(arc_set, n3, 2);
deba@491
    92
deba@491
    93
  checkGraphConArcList(arc_set, 4);
deba@491
    94
deba@491
    95
  checkNodeIds(arc_set);
deba@491
    96
  checkArcIds(arc_set);
deba@491
    97
  checkGraphNodeMap(arc_set);
deba@491
    98
  checkGraphArcMap(arc_set);
deba@491
    99
deba@491
   100
  check(arc_set.valid(), "Wrong validity");
deba@491
   101
  digraph.erase(n1);
deba@491
   102
  check(!arc_set.valid(), "Wrong validity");
deba@491
   103
}
deba@491
   104
deba@491
   105
void checkListArcSet() {
deba@559
   106
  checkConcept<concepts::Digraph, SmartArcSet<ListDigraph> >();
deba@491
   107
deba@491
   108
  typedef ListDigraph Digraph;
deba@491
   109
  typedef ListArcSet<Digraph> ArcSet;
deba@491
   110
deba@491
   111
  Digraph digraph;
deba@491
   112
  Digraph::Node
deba@491
   113
    n1 = digraph.addNode(),
deba@491
   114
    n2 = digraph.addNode();
deba@491
   115
deba@491
   116
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
alpar@1257
   117
  ::lemon::ignore_unused_variable_warning(ga1);
deba@491
   118
deba@491
   119
  ArcSet arc_set(digraph);
deba@491
   120
deba@491
   121
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
alpar@1257
   122
  ::lemon::ignore_unused_variable_warning(ga2);
deba@491
   123
deba@491
   124
  checkGraphNodeList(arc_set, 2);
deba@491
   125
  checkGraphArcList(arc_set, 0);
deba@491
   126
deba@491
   127
  Digraph::Node
deba@491
   128
    n3 = digraph.addNode();
deba@491
   129
  checkGraphNodeList(arc_set, 3);
deba@491
   130
  checkGraphArcList(arc_set, 0);
deba@491
   131
deba@491
   132
  ArcSet::Arc a1 = arc_set.addArc(n1, n2);
deba@491
   133
  check(arc_set.source(a1) == n1 && arc_set.target(a1) == n2, "Wrong arc");
deba@491
   134
  checkGraphNodeList(arc_set, 3);
deba@491
   135
  checkGraphArcList(arc_set, 1);
deba@491
   136
deba@491
   137
  checkGraphOutArcList(arc_set, n1, 1);
deba@491
   138
  checkGraphOutArcList(arc_set, n2, 0);
deba@491
   139
  checkGraphOutArcList(arc_set, n3, 0);
deba@491
   140
deba@491
   141
  checkGraphInArcList(arc_set, n1, 0);
deba@491
   142
  checkGraphInArcList(arc_set, n2, 1);
deba@491
   143
  checkGraphInArcList(arc_set, n3, 0);
deba@491
   144
deba@491
   145
  checkGraphConArcList(arc_set, 1);
deba@491
   146
deba@491
   147
  ArcSet::Arc a2 = arc_set.addArc(n2, n1),
deba@491
   148
    a3 = arc_set.addArc(n2, n3),
deba@491
   149
    a4 = arc_set.addArc(n2, n3);
alpar@1257
   150
  ::lemon::ignore_unused_variable_warning(a2,a3,a4);
alpar@1157
   151
deba@491
   152
  checkGraphNodeList(arc_set, 3);
deba@491
   153
  checkGraphArcList(arc_set, 4);
deba@491
   154
deba@491
   155
  checkGraphOutArcList(arc_set, n1, 1);
deba@491
   156
  checkGraphOutArcList(arc_set, n2, 3);
deba@491
   157
  checkGraphOutArcList(arc_set, n3, 0);
deba@491
   158
deba@491
   159
  checkGraphInArcList(arc_set, n1, 1);
deba@491
   160
  checkGraphInArcList(arc_set, n2, 1);
deba@491
   161
  checkGraphInArcList(arc_set, n3, 2);
deba@491
   162
deba@491
   163
  checkGraphConArcList(arc_set, 4);
deba@491
   164
deba@491
   165
  checkNodeIds(arc_set);
deba@491
   166
  checkArcIds(arc_set);
deba@491
   167
  checkGraphNodeMap(arc_set);
deba@491
   168
  checkGraphArcMap(arc_set);
deba@491
   169
deba@491
   170
  digraph.erase(n1);
deba@491
   171
deba@491
   172
  checkGraphNodeList(arc_set, 2);
deba@491
   173
  checkGraphArcList(arc_set, 2);
deba@491
   174
deba@491
   175
  checkGraphOutArcList(arc_set, n2, 2);
deba@491
   176
  checkGraphOutArcList(arc_set, n3, 0);
deba@491
   177
deba@491
   178
  checkGraphInArcList(arc_set, n2, 0);
deba@491
   179
  checkGraphInArcList(arc_set, n3, 2);
deba@491
   180
deba@491
   181
  checkNodeIds(arc_set);
deba@491
   182
  checkArcIds(arc_set);
deba@491
   183
  checkGraphNodeMap(arc_set);
deba@491
   184
  checkGraphArcMap(arc_set);
deba@491
   185
deba@491
   186
  checkGraphConArcList(arc_set, 2);
deba@491
   187
}
deba@491
   188
deba@491
   189
void checkSmartEdgeSet() {
deba@559
   190
  checkConcept<concepts::Digraph, SmartEdgeSet<ListDigraph> >();
deba@491
   191
deba@491
   192
  typedef ListDigraph Digraph;
deba@491
   193
  typedef SmartEdgeSet<Digraph> EdgeSet;
deba@491
   194
deba@491
   195
  Digraph digraph;
deba@491
   196
  Digraph::Node
deba@491
   197
    n1 = digraph.addNode(),
deba@491
   198
    n2 = digraph.addNode();
deba@491
   199
deba@491
   200
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
alpar@1257
   201
  ::lemon::ignore_unused_variable_warning(ga1);
deba@491
   202
deba@491
   203
  EdgeSet edge_set(digraph);
deba@491
   204
deba@491
   205
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
alpar@1257
   206
  ::lemon::ignore_unused_variable_warning(ga2);
deba@491
   207
deba@491
   208
  checkGraphNodeList(edge_set, 2);
deba@491
   209
  checkGraphArcList(edge_set, 0);
deba@491
   210
  checkGraphEdgeList(edge_set, 0);
deba@491
   211
deba@491
   212
  Digraph::Node
deba@491
   213
    n3 = digraph.addNode();
deba@491
   214
  checkGraphNodeList(edge_set, 3);
deba@491
   215
  checkGraphArcList(edge_set, 0);
deba@491
   216
  checkGraphEdgeList(edge_set, 0);
deba@491
   217
deba@491
   218
  EdgeSet::Edge e1 = edge_set.addEdge(n1, n2);
deba@491
   219
  check((edge_set.u(e1) == n1 && edge_set.v(e1) == n2) ||
deba@491
   220
        (edge_set.v(e1) == n1 && edge_set.u(e1) == n2), "Wrong edge");
deba@491
   221
  checkGraphNodeList(edge_set, 3);
deba@491
   222
  checkGraphArcList(edge_set, 2);
deba@491
   223
  checkGraphEdgeList(edge_set, 1);
deba@491
   224
deba@491
   225
  checkGraphOutArcList(edge_set, n1, 1);
deba@491
   226
  checkGraphOutArcList(edge_set, n2, 1);
deba@491
   227
  checkGraphOutArcList(edge_set, n3, 0);
deba@491
   228
deba@491
   229
  checkGraphInArcList(edge_set, n1, 1);
deba@491
   230
  checkGraphInArcList(edge_set, n2, 1);
deba@491
   231
  checkGraphInArcList(edge_set, n3, 0);
deba@491
   232
deba@491
   233
  checkGraphIncEdgeList(edge_set, n1, 1);
deba@491
   234
  checkGraphIncEdgeList(edge_set, n2, 1);
deba@491
   235
  checkGraphIncEdgeList(edge_set, n3, 0);
deba@491
   236
deba@491
   237
  checkGraphConEdgeList(edge_set, 1);
deba@491
   238
  checkGraphConArcList(edge_set, 2);
deba@491
   239
deba@491
   240
  EdgeSet::Edge e2 = edge_set.addEdge(n2, n1),
deba@491
   241
    e3 = edge_set.addEdge(n2, n3),
deba@491
   242
    e4 = edge_set.addEdge(n2, n3);
alpar@1257
   243
  ::lemon::ignore_unused_variable_warning(e2,e3,e4);
alpar@1157
   244
deba@491
   245
  checkGraphNodeList(edge_set, 3);
deba@491
   246
  checkGraphEdgeList(edge_set, 4);
deba@491
   247
deba@491
   248
  checkGraphOutArcList(edge_set, n1, 2);
deba@491
   249
  checkGraphOutArcList(edge_set, n2, 4);
deba@491
   250
  checkGraphOutArcList(edge_set, n3, 2);
deba@491
   251
deba@491
   252
  checkGraphInArcList(edge_set, n1, 2);
deba@491
   253
  checkGraphInArcList(edge_set, n2, 4);
deba@491
   254
  checkGraphInArcList(edge_set, n3, 2);
deba@491
   255
deba@491
   256
  checkGraphIncEdgeList(edge_set, n1, 2);
deba@491
   257
  checkGraphIncEdgeList(edge_set, n2, 4);
deba@491
   258
  checkGraphIncEdgeList(edge_set, n3, 2);
deba@491
   259
deba@491
   260
  checkGraphConEdgeList(edge_set, 4);
deba@491
   261
  checkGraphConArcList(edge_set, 8);
deba@491
   262
deba@491
   263
  checkArcDirections(edge_set);
deba@491
   264
deba@491
   265
  checkNodeIds(edge_set);
deba@491
   266
  checkArcIds(edge_set);
deba@491
   267
  checkEdgeIds(edge_set);
deba@491
   268
  checkGraphNodeMap(edge_set);
deba@491
   269
  checkGraphArcMap(edge_set);
deba@491
   270
  checkGraphEdgeMap(edge_set);
deba@491
   271
deba@491
   272
  check(edge_set.valid(), "Wrong validity");
deba@491
   273
  digraph.erase(n1);
deba@491
   274
  check(!edge_set.valid(), "Wrong validity");
deba@491
   275
}
deba@491
   276
deba@491
   277
void checkListEdgeSet() {
deba@559
   278
  checkConcept<concepts::Digraph, ListEdgeSet<ListDigraph> >();
deba@491
   279
deba@491
   280
  typedef ListDigraph Digraph;
deba@491
   281
  typedef ListEdgeSet<Digraph> EdgeSet;
deba@491
   282
deba@491
   283
  Digraph digraph;
deba@491
   284
  Digraph::Node
deba@491
   285
    n1 = digraph.addNode(),
deba@491
   286
    n2 = digraph.addNode();
deba@491
   287
deba@491
   288
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
alpar@1257
   289
  ::lemon::ignore_unused_variable_warning(ga1);
deba@491
   290
deba@491
   291
  EdgeSet edge_set(digraph);
deba@491
   292
deba@491
   293
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
alpar@1257
   294
  ::lemon::ignore_unused_variable_warning(ga2);
deba@491
   295
deba@491
   296
  checkGraphNodeList(edge_set, 2);
deba@491
   297
  checkGraphArcList(edge_set, 0);
deba@491
   298
  checkGraphEdgeList(edge_set, 0);
deba@491
   299
deba@491
   300
  Digraph::Node
deba@491
   301
    n3 = digraph.addNode();
deba@491
   302
  checkGraphNodeList(edge_set, 3);
deba@491
   303
  checkGraphArcList(edge_set, 0);
deba@491
   304
  checkGraphEdgeList(edge_set, 0);
deba@491
   305
deba@491
   306
  EdgeSet::Edge e1 = edge_set.addEdge(n1, n2);
deba@491
   307
  check((edge_set.u(e1) == n1 && edge_set.v(e1) == n2) ||
deba@491
   308
        (edge_set.v(e1) == n1 && edge_set.u(e1) == n2), "Wrong edge");
deba@491
   309
  checkGraphNodeList(edge_set, 3);
deba@491
   310
  checkGraphArcList(edge_set, 2);
deba@491
   311
  checkGraphEdgeList(edge_set, 1);
deba@491
   312
deba@491
   313
  checkGraphOutArcList(edge_set, n1, 1);
deba@491
   314
  checkGraphOutArcList(edge_set, n2, 1);
deba@491
   315
  checkGraphOutArcList(edge_set, n3, 0);
deba@491
   316
deba@491
   317
  checkGraphInArcList(edge_set, n1, 1);
deba@491
   318
  checkGraphInArcList(edge_set, n2, 1);
deba@491
   319
  checkGraphInArcList(edge_set, n3, 0);
deba@491
   320
deba@491
   321
  checkGraphIncEdgeList(edge_set, n1, 1);
deba@491
   322
  checkGraphIncEdgeList(edge_set, n2, 1);
deba@491
   323
  checkGraphIncEdgeList(edge_set, n3, 0);
deba@491
   324
deba@491
   325
  checkGraphConEdgeList(edge_set, 1);
deba@491
   326
  checkGraphConArcList(edge_set, 2);
deba@491
   327
deba@491
   328
  EdgeSet::Edge e2 = edge_set.addEdge(n2, n1),
deba@491
   329
    e3 = edge_set.addEdge(n2, n3),
deba@491
   330
    e4 = edge_set.addEdge(n2, n3);
alpar@1257
   331
  ::lemon::ignore_unused_variable_warning(e2,e3,e4);
alpar@1157
   332
deba@491
   333
  checkGraphNodeList(edge_set, 3);
deba@491
   334
  checkGraphEdgeList(edge_set, 4);
deba@491
   335
deba@491
   336
  checkGraphOutArcList(edge_set, n1, 2);
deba@491
   337
  checkGraphOutArcList(edge_set, n2, 4);
deba@491
   338
  checkGraphOutArcList(edge_set, n3, 2);
deba@491
   339
deba@491
   340
  checkGraphInArcList(edge_set, n1, 2);
deba@491
   341
  checkGraphInArcList(edge_set, n2, 4);
deba@491
   342
  checkGraphInArcList(edge_set, n3, 2);
deba@491
   343
deba@491
   344
  checkGraphIncEdgeList(edge_set, n1, 2);
deba@491
   345
  checkGraphIncEdgeList(edge_set, n2, 4);
deba@491
   346
  checkGraphIncEdgeList(edge_set, n3, 2);
deba@491
   347
deba@491
   348
  checkGraphConEdgeList(edge_set, 4);
deba@491
   349
  checkGraphConArcList(edge_set, 8);
deba@491
   350
deba@491
   351
  checkArcDirections(edge_set);
deba@491
   352
deba@491
   353
  checkNodeIds(edge_set);
deba@491
   354
  checkArcIds(edge_set);
deba@491
   355
  checkEdgeIds(edge_set);
deba@491
   356
  checkGraphNodeMap(edge_set);
deba@491
   357
  checkGraphArcMap(edge_set);
deba@491
   358
  checkGraphEdgeMap(edge_set);
deba@491
   359
deba@491
   360
  digraph.erase(n1);
deba@491
   361
deba@491
   362
  checkGraphNodeList(edge_set, 2);
deba@491
   363
  checkGraphArcList(edge_set, 4);
deba@491
   364
  checkGraphEdgeList(edge_set, 2);
deba@491
   365
deba@491
   366
  checkGraphOutArcList(edge_set, n2, 2);
deba@491
   367
  checkGraphOutArcList(edge_set, n3, 2);
deba@491
   368
deba@491
   369
  checkGraphInArcList(edge_set, n2, 2);
deba@491
   370
  checkGraphInArcList(edge_set, n3, 2);
deba@491
   371
deba@491
   372
  checkGraphIncEdgeList(edge_set, n2, 2);
deba@491
   373
  checkGraphIncEdgeList(edge_set, n3, 2);
deba@491
   374
deba@491
   375
  checkNodeIds(edge_set);
deba@491
   376
  checkArcIds(edge_set);
deba@491
   377
  checkEdgeIds(edge_set);
deba@491
   378
  checkGraphNodeMap(edge_set);
deba@491
   379
  checkGraphArcMap(edge_set);
deba@491
   380
  checkGraphEdgeMap(edge_set);
deba@491
   381
deba@491
   382
  checkGraphConEdgeList(edge_set, 2);
deba@491
   383
  checkGraphConArcList(edge_set, 4);
deba@491
   384
deba@491
   385
}
deba@491
   386
deba@491
   387
deba@491
   388
int main() {
deba@491
   389
deba@491
   390
  checkSmartArcSet();
deba@491
   391
  checkListArcSet();
deba@491
   392
  checkSmartEdgeSet();
deba@491
   393
  checkListEdgeSet();
deba@491
   394
deba@491
   395
  return 0;
deba@491
   396
}