test/lgf_reader_writer_test.cc
author Balazs Dezso <deba@google.com>
Sat, 27 Oct 2018 13:00:48 +0200
changeset 1203 8c567e298d7f
parent 1030 4936be66d2f5
permissions -rw-r--r--
Paremeter to stop matching calculation when only single node is unmatched
deba@1030
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@1030
     2
 *
deba@1030
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@1030
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
deba@1030
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1030
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1030
     8
 *
deba@1030
     9
 * Permission to use, modify and distribute this software is granted
deba@1030
    10
 * provided that this copyright notice appears in all copies. For
deba@1030
    11
 * precise terms see the accompanying LICENSE file.
deba@1030
    12
 *
deba@1030
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1030
    14
 * express or implied, and with no claim as to its suitability for any
deba@1030
    15
 * purpose.
deba@1030
    16
 *
deba@1030
    17
 */
deba@1030
    18
deba@1030
    19
#include <string>
deba@1030
    20
deba@1030
    21
#include <lemon/concepts/digraph.h>
deba@1030
    22
#include <lemon/concepts/graph.h>
deba@1030
    23
#include <lemon/concepts/bpgraph.h>
deba@1030
    24
deba@1030
    25
#include <lemon/list_graph.h>
deba@1030
    26
#include <lemon/smart_graph.h>
deba@1030
    27
#include <lemon/lgf_reader.h>
deba@1030
    28
deba@1030
    29
#include "test_tools.h"
deba@1030
    30
deba@1030
    31
struct ReaderConverter {
deba@1030
    32
  int operator()(const std::string& str) const {
deba@1030
    33
    return str.length();
deba@1030
    34
  }
deba@1030
    35
};
deba@1030
    36
deba@1030
    37
struct WriterConverter {
deba@1030
    38
  std::string operator()(int value) const {
deba@1030
    39
    return std::string(value, '*');
deba@1030
    40
  }
deba@1030
    41
};
deba@1030
    42
deba@1030
    43
void checkDigraphReaderCompile() {
deba@1030
    44
  typedef lemon::concepts::ExtendableDigraphComponent<
deba@1030
    45
    lemon::concepts::Digraph> Digraph;
deba@1030
    46
  Digraph digraph;
deba@1030
    47
  Digraph::NodeMap<int> node_map(digraph);
deba@1030
    48
  Digraph::ArcMap<int> arc_map(digraph);
deba@1030
    49
  Digraph::Node node;
deba@1030
    50
  Digraph::Arc arc;
deba@1030
    51
  int attr;
deba@1030
    52
deba@1030
    53
  lemon::DigraphReader<Digraph> reader(digraph, "filename");
deba@1030
    54
  reader.nodeMap("node_map", node_map);
deba@1030
    55
  reader.nodeMap("node_map", node_map, ReaderConverter());
deba@1030
    56
  reader.arcMap("arc_map", arc_map);
deba@1030
    57
  reader.arcMap("arc_map", arc_map, ReaderConverter());
deba@1030
    58
  reader.attribute("attr", attr);
deba@1030
    59
  reader.attribute("attr", attr, ReaderConverter());
deba@1030
    60
  reader.node("node", node);
deba@1030
    61
  reader.arc("arc", arc);
deba@1030
    62
deba@1030
    63
  reader.nodes("alt_nodes_caption");
deba@1030
    64
  reader.arcs("alt_arcs_caption");
deba@1030
    65
  reader.attributes("alt_attrs_caption");
deba@1030
    66
deba@1030
    67
  reader.useNodes(node_map);
deba@1030
    68
  reader.useNodes(node_map, WriterConverter());
deba@1030
    69
  reader.useArcs(arc_map);
deba@1030
    70
  reader.useArcs(arc_map, WriterConverter());
deba@1030
    71
deba@1030
    72
  reader.skipNodes();
deba@1030
    73
  reader.skipArcs();
deba@1030
    74
deba@1030
    75
  reader.run();
deba@1030
    76
deba@1030
    77
  lemon::DigraphReader<Digraph> reader2(digraph, std::cin);
deba@1030
    78
}
deba@1030
    79
deba@1030
    80
void checkDigraphWriterCompile() {
deba@1030
    81
  typedef lemon::concepts::Digraph Digraph;
deba@1030
    82
  Digraph digraph;
deba@1030
    83
  Digraph::NodeMap<int> node_map(digraph);
deba@1030
    84
  Digraph::ArcMap<int> arc_map(digraph);
deba@1030
    85
  Digraph::Node node;
deba@1030
    86
  Digraph::Arc arc;
deba@1030
    87
  int attr;
deba@1030
    88
deba@1030
    89
  lemon::DigraphWriter<Digraph> writer(digraph, "filename");
deba@1030
    90
  writer.nodeMap("node_map", node_map);
deba@1030
    91
  writer.nodeMap("node_map", node_map, WriterConverter());
deba@1030
    92
  writer.arcMap("arc_map", arc_map);
deba@1030
    93
  writer.arcMap("arc_map", arc_map, WriterConverter());
deba@1030
    94
  writer.attribute("attr", attr);
deba@1030
    95
  writer.attribute("attr", attr, WriterConverter());
deba@1030
    96
  writer.node("node", node);
deba@1030
    97
  writer.arc("arc", arc);
deba@1030
    98
deba@1030
    99
  writer.nodes("alt_nodes_caption");
deba@1030
   100
  writer.arcs("alt_arcs_caption");
deba@1030
   101
  writer.attributes("alt_attrs_caption");
deba@1030
   102
deba@1030
   103
  writer.skipNodes();
deba@1030
   104
  writer.skipArcs();
deba@1030
   105
deba@1030
   106
  writer.run();
deba@1030
   107
}
deba@1030
   108
deba@1030
   109
void checkGraphReaderCompile() {
deba@1030
   110
  typedef lemon::concepts::ExtendableGraphComponent<
deba@1030
   111
    lemon::concepts::Graph> Graph;
deba@1030
   112
  Graph graph;
deba@1030
   113
  Graph::NodeMap<int> node_map(graph);
deba@1030
   114
  Graph::ArcMap<int> arc_map(graph);
deba@1030
   115
  Graph::EdgeMap<int> edge_map(graph);
deba@1030
   116
  Graph::Node node;
deba@1030
   117
  Graph::Arc arc;
deba@1030
   118
  Graph::Edge edge;
deba@1030
   119
  int attr;
deba@1030
   120
deba@1030
   121
  lemon::GraphReader<Graph> reader(graph, "filename");
deba@1030
   122
  reader.nodeMap("node_map", node_map);
deba@1030
   123
  reader.nodeMap("node_map", node_map, ReaderConverter());
deba@1030
   124
  reader.arcMap("arc_map", arc_map);
deba@1030
   125
  reader.arcMap("arc_map", arc_map, ReaderConverter());
deba@1030
   126
  reader.edgeMap("edge_map", edge_map);
deba@1030
   127
  reader.edgeMap("edge_map", edge_map, ReaderConverter());
deba@1030
   128
  reader.attribute("attr", attr);
deba@1030
   129
  reader.attribute("attr", attr, ReaderConverter());
deba@1030
   130
  reader.node("node", node);
deba@1030
   131
  reader.arc("arc", arc);
deba@1030
   132
deba@1030
   133
  reader.nodes("alt_nodes_caption");
deba@1030
   134
  reader.edges("alt_edges_caption");
deba@1030
   135
  reader.attributes("alt_attrs_caption");
deba@1030
   136
deba@1030
   137
  reader.useNodes(node_map);
deba@1030
   138
  reader.useNodes(node_map, WriterConverter());
deba@1030
   139
  reader.useEdges(edge_map);
deba@1030
   140
  reader.useEdges(edge_map, WriterConverter());
deba@1030
   141
deba@1030
   142
  reader.skipNodes();
deba@1030
   143
  reader.skipEdges();
deba@1030
   144
deba@1030
   145
  reader.run();
deba@1030
   146
deba@1030
   147
  lemon::GraphReader<Graph> reader2(graph, std::cin);
deba@1030
   148
}
deba@1030
   149
deba@1030
   150
void checkGraphWriterCompile() {
deba@1030
   151
  typedef lemon::concepts::Graph Graph;
deba@1030
   152
  Graph graph;
deba@1030
   153
  Graph::NodeMap<int> node_map(graph);
deba@1030
   154
  Graph::ArcMap<int> arc_map(graph);
deba@1030
   155
  Graph::EdgeMap<int> edge_map(graph);
deba@1030
   156
  Graph::Node node;
deba@1030
   157
  Graph::Arc arc;
deba@1030
   158
  Graph::Edge edge;
deba@1030
   159
  int attr;
deba@1030
   160
deba@1030
   161
  lemon::GraphWriter<Graph> writer(graph, "filename");
deba@1030
   162
  writer.nodeMap("node_map", node_map);
deba@1030
   163
  writer.nodeMap("node_map", node_map, WriterConverter());
deba@1030
   164
  writer.arcMap("arc_map", arc_map);
deba@1030
   165
  writer.arcMap("arc_map", arc_map, WriterConverter());
deba@1030
   166
  writer.edgeMap("edge_map", edge_map);
deba@1030
   167
  writer.edgeMap("edge_map", edge_map, WriterConverter());
deba@1030
   168
  writer.attribute("attr", attr);
deba@1030
   169
  writer.attribute("attr", attr, WriterConverter());
deba@1030
   170
  writer.node("node", node);
deba@1030
   171
  writer.arc("arc", arc);
deba@1030
   172
  writer.edge("edge", edge);
deba@1030
   173
deba@1030
   174
  writer.nodes("alt_nodes_caption");
deba@1030
   175
  writer.edges("alt_edges_caption");
deba@1030
   176
  writer.attributes("alt_attrs_caption");
deba@1030
   177
deba@1030
   178
  writer.skipNodes();
deba@1030
   179
  writer.skipEdges();
deba@1030
   180
deba@1030
   181
  writer.run();
deba@1030
   182
deba@1030
   183
  lemon::GraphWriter<Graph> writer2(graph, std::cout);
deba@1030
   184
}
deba@1030
   185
deba@1030
   186
void checkBpGraphReaderCompile() {
deba@1030
   187
  typedef lemon::concepts::ExtendableBpGraphComponent<
deba@1030
   188
    lemon::concepts::BpGraph> BpGraph;
deba@1030
   189
  BpGraph graph;
deba@1030
   190
  BpGraph::NodeMap<int> node_map(graph);
deba@1030
   191
  BpGraph::RedNodeMap<int> red_node_map(graph);
deba@1030
   192
  BpGraph::BlueNodeMap<int> blue_node_map(graph);
deba@1030
   193
  BpGraph::ArcMap<int> arc_map(graph);
deba@1030
   194
  BpGraph::EdgeMap<int> edge_map(graph);
deba@1030
   195
  BpGraph::Node node;
deba@1030
   196
  BpGraph::RedNode red_node;
deba@1030
   197
  BpGraph::BlueNode blue_node;
deba@1030
   198
  BpGraph::Arc arc;
deba@1030
   199
  BpGraph::Edge edge;
deba@1030
   200
  int attr;
deba@1030
   201
deba@1030
   202
  lemon::BpGraphReader<BpGraph> reader(graph, "filename");
deba@1030
   203
  reader.nodeMap("node_map", node_map);
deba@1030
   204
  reader.nodeMap("node_map", node_map, ReaderConverter());
deba@1030
   205
  reader.redNodeMap("red_node_map", red_node_map);
deba@1030
   206
  reader.redNodeMap("red_node_map", red_node_map, ReaderConverter());
deba@1030
   207
  reader.blueNodeMap("blue_node_map", blue_node_map);
deba@1030
   208
  reader.blueNodeMap("blue_node_map", blue_node_map, ReaderConverter());
deba@1030
   209
  reader.arcMap("arc_map", arc_map);
deba@1030
   210
  reader.arcMap("arc_map", arc_map, ReaderConverter());
deba@1030
   211
  reader.edgeMap("edge_map", edge_map);
deba@1030
   212
  reader.edgeMap("edge_map", edge_map, ReaderConverter());
deba@1030
   213
  reader.attribute("attr", attr);
deba@1030
   214
  reader.attribute("attr", attr, ReaderConverter());
deba@1030
   215
  reader.node("node", node);
deba@1030
   216
  reader.redNode("red_node", red_node);
deba@1030
   217
  reader.blueNode("blue_node", blue_node);
deba@1030
   218
  reader.arc("arc", arc);
deba@1030
   219
deba@1030
   220
  reader.nodes("alt_nodes_caption");
deba@1030
   221
  reader.edges("alt_edges_caption");
deba@1030
   222
  reader.attributes("alt_attrs_caption");
deba@1030
   223
deba@1030
   224
  reader.useNodes(node_map);
deba@1030
   225
  reader.useNodes(node_map, WriterConverter());
deba@1030
   226
  reader.useEdges(edge_map);
deba@1030
   227
  reader.useEdges(edge_map, WriterConverter());
deba@1030
   228
deba@1030
   229
  reader.skipNodes();
deba@1030
   230
  reader.skipEdges();
deba@1030
   231
deba@1030
   232
  reader.run();
deba@1030
   233
deba@1030
   234
  lemon::BpGraphReader<BpGraph> reader2(graph, std::cin);
deba@1030
   235
}
deba@1030
   236
deba@1030
   237
void checkBpGraphWriterCompile() {
deba@1030
   238
  typedef lemon::concepts::BpGraph BpGraph;
deba@1030
   239
  BpGraph graph;
deba@1030
   240
  BpGraph::NodeMap<int> node_map(graph);
deba@1030
   241
  BpGraph::RedNodeMap<int> red_node_map(graph);
deba@1030
   242
  BpGraph::BlueNodeMap<int> blue_node_map(graph);
deba@1030
   243
  BpGraph::ArcMap<int> arc_map(graph);
deba@1030
   244
  BpGraph::EdgeMap<int> edge_map(graph);
deba@1030
   245
  BpGraph::Node node;
deba@1030
   246
  BpGraph::RedNode red_node;
deba@1030
   247
  BpGraph::BlueNode blue_node;
deba@1030
   248
  BpGraph::Arc arc;
deba@1030
   249
  BpGraph::Edge edge;
deba@1030
   250
  int attr;
deba@1030
   251
deba@1030
   252
  lemon::BpGraphWriter<BpGraph> writer(graph, "filename");
deba@1030
   253
  writer.nodeMap("node_map", node_map);
deba@1030
   254
  writer.nodeMap("node_map", node_map, WriterConverter());
deba@1030
   255
  writer.redNodeMap("red_node_map", red_node_map);
deba@1030
   256
  writer.redNodeMap("red_node_map", red_node_map, WriterConverter());
deba@1030
   257
  writer.blueNodeMap("blue_node_map", blue_node_map);
deba@1030
   258
  writer.blueNodeMap("blue_node_map", blue_node_map, WriterConverter());
deba@1030
   259
  writer.arcMap("arc_map", arc_map);
deba@1030
   260
  writer.arcMap("arc_map", arc_map, WriterConverter());
deba@1030
   261
  writer.edgeMap("edge_map", edge_map);
deba@1030
   262
  writer.edgeMap("edge_map", edge_map, WriterConverter());
deba@1030
   263
  writer.attribute("attr", attr);
deba@1030
   264
  writer.attribute("attr", attr, WriterConverter());
deba@1030
   265
  writer.node("node", node);
deba@1030
   266
  writer.redNode("red_node", red_node);
deba@1030
   267
  writer.blueNode("blue_node", blue_node);
deba@1030
   268
  writer.arc("arc", arc);
deba@1030
   269
deba@1030
   270
  writer.nodes("alt_nodes_caption");
deba@1030
   271
  writer.edges("alt_edges_caption");
deba@1030
   272
  writer.attributes("alt_attrs_caption");
deba@1030
   273
deba@1030
   274
  writer.skipNodes();
deba@1030
   275
  writer.skipEdges();
deba@1030
   276
deba@1030
   277
  writer.run();
deba@1030
   278
deba@1030
   279
  lemon::BpGraphWriter<BpGraph> writer2(graph, std::cout);
deba@1030
   280
}
deba@1030
   281
deba@1030
   282
void checkDigraphReaderWriter() {
deba@1030
   283
  typedef lemon::SmartDigraph Digraph;
deba@1030
   284
  Digraph digraph;
deba@1030
   285
  Digraph::Node n1 = digraph.addNode();
deba@1030
   286
  Digraph::Node n2 = digraph.addNode();
deba@1030
   287
  Digraph::Node n3 = digraph.addNode();
deba@1030
   288
deba@1030
   289
  Digraph::Arc a1 = digraph.addArc(n1, n2);
deba@1030
   290
  Digraph::Arc a2 = digraph.addArc(n2, n3);
deba@1030
   291
deba@1030
   292
  Digraph::NodeMap<int> node_map(digraph);
deba@1030
   293
  node_map[n1] = 11;
deba@1030
   294
  node_map[n2] = 12;
deba@1030
   295
  node_map[n3] = 13;
deba@1030
   296
deba@1030
   297
  Digraph::ArcMap<int> arc_map(digraph);
deba@1030
   298
  arc_map[a1] = 21;
deba@1030
   299
  arc_map[a2] = 22;
deba@1030
   300
deba@1030
   301
  int attr = 100;
deba@1030
   302
deba@1030
   303
  std::ostringstream os;
deba@1030
   304
  lemon::DigraphWriter<Digraph> writer(digraph, os);
deba@1030
   305
deba@1030
   306
  writer.nodeMap("node_map1", node_map);
deba@1030
   307
  writer.nodeMap("node_map2", node_map, WriterConverter());
deba@1030
   308
  writer.arcMap("arc_map1", arc_map);
deba@1030
   309
  writer.arcMap("arc_map2", arc_map, WriterConverter());
deba@1030
   310
  writer.node("node", n2);
deba@1030
   311
  writer.arc("arc", a1);
deba@1030
   312
  writer.attribute("attr1", attr);
deba@1030
   313
  writer.attribute("attr2", attr, WriterConverter());
deba@1030
   314
deba@1030
   315
  writer.run();
deba@1030
   316
deba@1030
   317
  typedef lemon::ListDigraph ExpDigraph;
deba@1030
   318
  ExpDigraph exp_digraph;
deba@1030
   319
  ExpDigraph::NodeMap<int> exp_node_map1(exp_digraph);
deba@1030
   320
  ExpDigraph::NodeMap<int> exp_node_map2(exp_digraph);
deba@1030
   321
  ExpDigraph::ArcMap<int> exp_arc_map1(exp_digraph);
deba@1030
   322
  ExpDigraph::ArcMap<int> exp_arc_map2(exp_digraph);
deba@1030
   323
  ExpDigraph::Node exp_n2;
deba@1030
   324
  ExpDigraph::Arc exp_a1;
deba@1030
   325
  int exp_attr1;
deba@1030
   326
  int exp_attr2;
deba@1030
   327
deba@1030
   328
  std::istringstream is(os.str());
deba@1030
   329
  lemon::DigraphReader<ExpDigraph> reader(exp_digraph, is);
deba@1030
   330
deba@1030
   331
  reader.nodeMap("node_map1", exp_node_map1);
deba@1030
   332
  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
deba@1030
   333
  reader.arcMap("arc_map1", exp_arc_map1);
deba@1030
   334
  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
deba@1030
   335
  reader.node("node", exp_n2);
deba@1030
   336
  reader.arc("arc", exp_a1);
deba@1030
   337
  reader.attribute("attr1", exp_attr1);
deba@1030
   338
  reader.attribute("attr2", exp_attr2, ReaderConverter());
deba@1030
   339
deba@1030
   340
  reader.run();
deba@1030
   341
deba@1030
   342
  check(lemon::countNodes(exp_digraph) == 3, "Wrong number of nodes");
deba@1030
   343
  check(lemon::countArcs(exp_digraph) == 2, "Wrong number of arcs");
deba@1030
   344
  check(exp_node_map1[exp_n2] == 12, "Wrong map value");
deba@1030
   345
  check(exp_node_map2[exp_n2] == 12, "Wrong map value");
deba@1030
   346
  check(exp_arc_map1[exp_a1] == 21, "Wrong map value");
deba@1030
   347
  check(exp_arc_map2[exp_a1] == 21, "Wrong map value");
deba@1030
   348
  check(exp_attr1 == 100, "Wrong attr value");
deba@1030
   349
  check(exp_attr2 == 100, "Wrong attr value");
deba@1030
   350
}
deba@1030
   351
deba@1030
   352
void checkGraphReaderWriter() {
deba@1030
   353
  typedef lemon::SmartGraph Graph;
deba@1030
   354
  Graph graph;
deba@1030
   355
  Graph::Node n1 = graph.addNode();
deba@1030
   356
  Graph::Node n2 = graph.addNode();
deba@1030
   357
  Graph::Node n3 = graph.addNode();
deba@1030
   358
deba@1030
   359
  Graph::Edge e1 = graph.addEdge(n1, n2);
deba@1030
   360
  Graph::Edge e2 = graph.addEdge(n2, n3);
deba@1030
   361
deba@1030
   362
  Graph::NodeMap<int> node_map(graph);
deba@1030
   363
  node_map[n1] = 11;
deba@1030
   364
  node_map[n2] = 12;
deba@1030
   365
  node_map[n3] = 13;
deba@1030
   366
deba@1030
   367
  Graph::EdgeMap<int> edge_map(graph);
deba@1030
   368
  edge_map[e1] = 21;
deba@1030
   369
  edge_map[e2] = 22;
deba@1030
   370
deba@1030
   371
  Graph::ArcMap<int> arc_map(graph);
deba@1030
   372
  arc_map[graph.direct(e1, true)] = 211;
deba@1030
   373
  arc_map[graph.direct(e1, false)] = 212;
deba@1030
   374
  arc_map[graph.direct(e2, true)] = 221;
deba@1030
   375
  arc_map[graph.direct(e2, false)] = 222;
deba@1030
   376
deba@1030
   377
  int attr = 100;
deba@1030
   378
deba@1030
   379
  std::ostringstream os;
deba@1030
   380
  lemon::GraphWriter<Graph> writer(graph, os);
deba@1030
   381
deba@1030
   382
  writer.nodeMap("node_map1", node_map);
deba@1030
   383
  writer.nodeMap("node_map2", node_map, WriterConverter());
deba@1030
   384
  writer.edgeMap("edge_map1", edge_map);
deba@1030
   385
  writer.edgeMap("edge_map2", edge_map, WriterConverter());
deba@1030
   386
  writer.arcMap("arc_map1", arc_map);
deba@1030
   387
  writer.arcMap("arc_map2", arc_map, WriterConverter());
alpar@1092
   388
  writer.node("node", n2);
deba@1030
   389
  writer.edge("edge", e1);
deba@1030
   390
  writer.arc("arc", graph.direct(e1, false));
deba@1030
   391
  writer.attribute("attr1", attr);
deba@1030
   392
  writer.attribute("attr2", attr, WriterConverter());
deba@1030
   393
deba@1030
   394
  writer.run();
deba@1030
   395
deba@1030
   396
  typedef lemon::ListGraph ExpGraph;
deba@1030
   397
  ExpGraph exp_graph;
deba@1030
   398
  ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
deba@1030
   399
  ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
deba@1030
   400
  ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
deba@1030
   401
  ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
deba@1030
   402
  ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
deba@1030
   403
  ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
deba@1030
   404
  ExpGraph::Node exp_n2;
deba@1030
   405
  ExpGraph::Edge exp_e1;
deba@1030
   406
  ExpGraph::Arc exp_a1;
deba@1030
   407
  int exp_attr1;
deba@1030
   408
  int exp_attr2;
deba@1030
   409
deba@1030
   410
  std::istringstream is(os.str());
deba@1030
   411
  lemon::GraphReader<ExpGraph> reader(exp_graph, is);
deba@1030
   412
deba@1030
   413
  reader.nodeMap("node_map1", exp_node_map1);
deba@1030
   414
  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
deba@1030
   415
  reader.edgeMap("edge_map1", exp_edge_map1);
deba@1030
   416
  reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
deba@1030
   417
  reader.arcMap("arc_map1", exp_arc_map1);
deba@1030
   418
  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
deba@1030
   419
  reader.node("node", exp_n2);
deba@1030
   420
  reader.edge("edge", exp_e1);
deba@1030
   421
  reader.arc("arc", exp_a1);
deba@1030
   422
  reader.attribute("attr1", exp_attr1);
deba@1030
   423
  reader.attribute("attr2", exp_attr2, ReaderConverter());
deba@1030
   424
deba@1030
   425
  reader.run();
deba@1030
   426
deba@1030
   427
  check(lemon::countNodes(exp_graph) == 3, "Wrong number of nodes");
deba@1030
   428
  check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
deba@1030
   429
  check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
deba@1030
   430
  check(exp_node_map1[exp_n2] == 12, "Wrong map value");
deba@1030
   431
  check(exp_node_map2[exp_n2] == 12, "Wrong map value");
deba@1030
   432
  check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
deba@1030
   433
  check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
deba@1030
   434
  check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
deba@1030
   435
  check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
deba@1030
   436
  check(exp_attr1 == 100, "Wrong attr value");
deba@1030
   437
  check(exp_attr2 == 100, "Wrong attr value");
deba@1030
   438
}
deba@1030
   439
deba@1030
   440
void checkBpGraphReaderWriter() {
deba@1030
   441
  typedef lemon::SmartBpGraph Graph;
deba@1030
   442
  Graph graph;
deba@1030
   443
  Graph::RedNode rn1 = graph.addRedNode();
deba@1030
   444
  Graph::RedNode rn2 = graph.addRedNode();
deba@1030
   445
  Graph::RedNode rn3 = graph.addRedNode();
deba@1030
   446
  Graph::BlueNode bn1 = graph.addBlueNode();
deba@1030
   447
  Graph::BlueNode bn2 = graph.addBlueNode();
deba@1030
   448
  Graph::Node n = bn1;
deba@1030
   449
deba@1030
   450
  Graph::Edge e1 = graph.addEdge(rn1, bn1);
deba@1030
   451
  Graph::Edge e2 = graph.addEdge(rn2, bn1);
deba@1030
   452
deba@1030
   453
  Graph::NodeMap<int> node_map(graph);
deba@1030
   454
  node_map[rn1] = 11;
deba@1030
   455
  node_map[rn2] = 12;
deba@1030
   456
  node_map[rn3] = 13;
deba@1030
   457
  node_map[bn1] = 14;
deba@1030
   458
  node_map[bn2] = 15;
deba@1030
   459
deba@1030
   460
  Graph::NodeMap<int> red_node_map(graph);
deba@1030
   461
  red_node_map[rn1] = 411;
deba@1030
   462
  red_node_map[rn2] = 412;
deba@1030
   463
  red_node_map[rn3] = 413;
deba@1030
   464
deba@1030
   465
  Graph::NodeMap<int> blue_node_map(graph);
deba@1030
   466
  blue_node_map[bn1] = 414;
deba@1030
   467
  blue_node_map[bn2] = 415;
deba@1030
   468
deba@1030
   469
  Graph::EdgeMap<int> edge_map(graph);
deba@1030
   470
  edge_map[e1] = 21;
deba@1030
   471
  edge_map[e2] = 22;
deba@1030
   472
deba@1030
   473
  Graph::ArcMap<int> arc_map(graph);
deba@1030
   474
  arc_map[graph.direct(e1, true)] = 211;
deba@1030
   475
  arc_map[graph.direct(e1, false)] = 212;
deba@1030
   476
  arc_map[graph.direct(e2, true)] = 221;
deba@1030
   477
  arc_map[graph.direct(e2, false)] = 222;
deba@1030
   478
deba@1030
   479
  int attr = 100;
deba@1030
   480
deba@1030
   481
  std::ostringstream os;
deba@1030
   482
  lemon::BpGraphWriter<Graph> writer(graph, os);
deba@1030
   483
deba@1030
   484
  writer.nodeMap("node_map1", node_map);
deba@1030
   485
  writer.nodeMap("node_map2", node_map, WriterConverter());
deba@1030
   486
  writer.nodeMap("red_node_map1", red_node_map);
deba@1030
   487
  writer.nodeMap("red_node_map2", red_node_map, WriterConverter());
deba@1030
   488
  writer.nodeMap("blue_node_map1", blue_node_map);
deba@1030
   489
  writer.nodeMap("blue_node_map2", blue_node_map, WriterConverter());
deba@1030
   490
  writer.edgeMap("edge_map1", edge_map);
deba@1030
   491
  writer.edgeMap("edge_map2", edge_map, WriterConverter());
deba@1030
   492
  writer.arcMap("arc_map1", arc_map);
deba@1030
   493
  writer.arcMap("arc_map2", arc_map, WriterConverter());
deba@1030
   494
  writer.node("node", n);
alpar@1092
   495
  writer.redNode("red_node", rn1);
deba@1030
   496
  writer.blueNode("blue_node", bn2);
deba@1030
   497
  writer.edge("edge", e1);
deba@1030
   498
  writer.arc("arc", graph.direct(e1, false));
deba@1030
   499
  writer.attribute("attr1", attr);
deba@1030
   500
  writer.attribute("attr2", attr, WriterConverter());
deba@1030
   501
deba@1030
   502
  writer.run();
deba@1030
   503
deba@1030
   504
  typedef lemon::ListBpGraph ExpGraph;
deba@1030
   505
  ExpGraph exp_graph;
deba@1030
   506
  ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
deba@1030
   507
  ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
deba@1030
   508
  ExpGraph::RedNodeMap<int> exp_red_node_map1(exp_graph);
deba@1030
   509
  ExpGraph::RedNodeMap<int> exp_red_node_map2(exp_graph);
deba@1030
   510
  ExpGraph::BlueNodeMap<int> exp_blue_node_map1(exp_graph);
deba@1030
   511
  ExpGraph::BlueNodeMap<int> exp_blue_node_map2(exp_graph);
deba@1030
   512
  ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
deba@1030
   513
  ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
deba@1030
   514
  ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
deba@1030
   515
  ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
deba@1030
   516
  ExpGraph::Node exp_n;
deba@1030
   517
  ExpGraph::RedNode exp_rn1;
deba@1030
   518
  ExpGraph::BlueNode exp_bn2;
deba@1030
   519
  ExpGraph::Edge exp_e1;
deba@1030
   520
  ExpGraph::Arc exp_a1;
deba@1030
   521
  int exp_attr1;
deba@1030
   522
  int exp_attr2;
deba@1030
   523
deba@1030
   524
  std::istringstream is(os.str());
deba@1030
   525
  lemon::BpGraphReader<ExpGraph> reader(exp_graph, is);
deba@1030
   526
deba@1030
   527
  reader.nodeMap("node_map1", exp_node_map1);
deba@1030
   528
  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
deba@1030
   529
  reader.redNodeMap("red_node_map1", exp_red_node_map1);
deba@1030
   530
  reader.redNodeMap("red_node_map2", exp_red_node_map2, ReaderConverter());
deba@1030
   531
  reader.blueNodeMap("blue_node_map1", exp_blue_node_map1);
deba@1030
   532
  reader.blueNodeMap("blue_node_map2", exp_blue_node_map2, ReaderConverter());
deba@1030
   533
  reader.edgeMap("edge_map1", exp_edge_map1);
deba@1030
   534
  reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
deba@1030
   535
  reader.arcMap("arc_map1", exp_arc_map1);
deba@1030
   536
  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
deba@1030
   537
  reader.node("node", exp_n);
deba@1030
   538
  reader.redNode("red_node", exp_rn1);
deba@1030
   539
  reader.blueNode("blue_node", exp_bn2);
deba@1030
   540
  reader.edge("edge", exp_e1);
deba@1030
   541
  reader.arc("arc", exp_a1);
deba@1030
   542
  reader.attribute("attr1", exp_attr1);
deba@1030
   543
  reader.attribute("attr2", exp_attr2, ReaderConverter());
deba@1030
   544
deba@1030
   545
  reader.run();
deba@1030
   546
deba@1030
   547
  check(lemon::countNodes(exp_graph) == 5, "Wrong number of nodes");
deba@1030
   548
  check(lemon::countRedNodes(exp_graph) == 3, "Wrong number of red nodes");
deba@1030
   549
  check(lemon::countBlueNodes(exp_graph) == 2, "Wrong number of blue nodes");
deba@1030
   550
  check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
deba@1030
   551
  check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
deba@1030
   552
  check(exp_node_map1[exp_n] == 14, "Wrong map value");
deba@1030
   553
  check(exp_node_map2[exp_n] == 14, "Wrong map value");
deba@1030
   554
  check(exp_red_node_map1[exp_rn1] == 411, "Wrong map value");
deba@1030
   555
  check(exp_red_node_map2[exp_rn1] == 411, "Wrong map value");
deba@1030
   556
  check(exp_blue_node_map1[exp_bn2] == 415, "Wrong map value");
deba@1030
   557
  check(exp_blue_node_map2[exp_bn2] == 415, "Wrong map value");
deba@1030
   558
  check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
deba@1030
   559
  check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
deba@1030
   560
  check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
deba@1030
   561
  check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
deba@1030
   562
  check(exp_attr1 == 100, "Wrong attr value");
deba@1030
   563
  check(exp_attr2 == 100, "Wrong attr value");
deba@1030
   564
}
deba@1030
   565
deba@1030
   566
deba@1030
   567
int main() {
deba@1030
   568
  { // Check digrpah
deba@1030
   569
    checkDigraphReaderWriter();
deba@1030
   570
  }
deba@1030
   571
  { // Check graph
deba@1030
   572
    checkGraphReaderWriter();
deba@1030
   573
  }
deba@1030
   574
  { // Check bipartite graph
deba@1030
   575
    checkBpGraphReaderWriter();
deba@1030
   576
  }
deba@1030
   577
  return 0;
deba@1030
   578
}