Fix wrong iteration in ListGraph snapshot, part II. (#598)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
21 #include <lemon/concepts/digraph.h>
22 #include <lemon/concepts/graph.h>
23 #include <lemon/concepts/bpgraph.h>
25 #include <lemon/list_graph.h>
26 #include <lemon/smart_graph.h>
27 #include <lemon/lgf_reader.h>
29 #include "test_tools.h"
31 struct ReaderConverter {
32 int operator()(const std::string& str) const {
37 struct WriterConverter {
38 std::string operator()(int value) const {
39 return std::string(value, '*');
43 void checkDigraphReaderCompile() {
44 typedef lemon::concepts::ExtendableDigraphComponent<
45 lemon::concepts::Digraph> Digraph;
47 Digraph::NodeMap<int> node_map(digraph);
48 Digraph::ArcMap<int> arc_map(digraph);
53 lemon::DigraphReader<Digraph> reader(digraph, "filename");
54 reader.nodeMap("node_map", node_map);
55 reader.nodeMap("node_map", node_map, ReaderConverter());
56 reader.arcMap("arc_map", arc_map);
57 reader.arcMap("arc_map", arc_map, ReaderConverter());
58 reader.attribute("attr", attr);
59 reader.attribute("attr", attr, ReaderConverter());
60 reader.node("node", node);
61 reader.arc("arc", arc);
63 reader.nodes("alt_nodes_caption");
64 reader.arcs("alt_arcs_caption");
65 reader.attributes("alt_attrs_caption");
67 reader.useNodes(node_map);
68 reader.useNodes(node_map, WriterConverter());
69 reader.useArcs(arc_map);
70 reader.useArcs(arc_map, WriterConverter());
77 lemon::DigraphReader<Digraph> reader2(digraph, std::cin);
80 void checkDigraphWriterCompile() {
81 typedef lemon::concepts::Digraph Digraph;
83 Digraph::NodeMap<int> node_map(digraph);
84 Digraph::ArcMap<int> arc_map(digraph);
89 lemon::DigraphWriter<Digraph> writer(digraph, "filename");
90 writer.nodeMap("node_map", node_map);
91 writer.nodeMap("node_map", node_map, WriterConverter());
92 writer.arcMap("arc_map", arc_map);
93 writer.arcMap("arc_map", arc_map, WriterConverter());
94 writer.attribute("attr", attr);
95 writer.attribute("attr", attr, WriterConverter());
96 writer.node("node", node);
97 writer.arc("arc", arc);
99 writer.nodes("alt_nodes_caption");
100 writer.arcs("alt_arcs_caption");
101 writer.attributes("alt_attrs_caption");
109 void checkGraphReaderCompile() {
110 typedef lemon::concepts::ExtendableGraphComponent<
111 lemon::concepts::Graph> Graph;
113 Graph::NodeMap<int> node_map(graph);
114 Graph::ArcMap<int> arc_map(graph);
115 Graph::EdgeMap<int> edge_map(graph);
121 lemon::GraphReader<Graph> reader(graph, "filename");
122 reader.nodeMap("node_map", node_map);
123 reader.nodeMap("node_map", node_map, ReaderConverter());
124 reader.arcMap("arc_map", arc_map);
125 reader.arcMap("arc_map", arc_map, ReaderConverter());
126 reader.edgeMap("edge_map", edge_map);
127 reader.edgeMap("edge_map", edge_map, ReaderConverter());
128 reader.attribute("attr", attr);
129 reader.attribute("attr", attr, ReaderConverter());
130 reader.node("node", node);
131 reader.arc("arc", arc);
133 reader.nodes("alt_nodes_caption");
134 reader.edges("alt_edges_caption");
135 reader.attributes("alt_attrs_caption");
137 reader.useNodes(node_map);
138 reader.useNodes(node_map, WriterConverter());
139 reader.useEdges(edge_map);
140 reader.useEdges(edge_map, WriterConverter());
147 lemon::GraphReader<Graph> reader2(graph, std::cin);
150 void checkGraphWriterCompile() {
151 typedef lemon::concepts::Graph Graph;
153 Graph::NodeMap<int> node_map(graph);
154 Graph::ArcMap<int> arc_map(graph);
155 Graph::EdgeMap<int> edge_map(graph);
161 lemon::GraphWriter<Graph> writer(graph, "filename");
162 writer.nodeMap("node_map", node_map);
163 writer.nodeMap("node_map", node_map, WriterConverter());
164 writer.arcMap("arc_map", arc_map);
165 writer.arcMap("arc_map", arc_map, WriterConverter());
166 writer.edgeMap("edge_map", edge_map);
167 writer.edgeMap("edge_map", edge_map, WriterConverter());
168 writer.attribute("attr", attr);
169 writer.attribute("attr", attr, WriterConverter());
170 writer.node("node", node);
171 writer.arc("arc", arc);
172 writer.edge("edge", edge);
174 writer.nodes("alt_nodes_caption");
175 writer.edges("alt_edges_caption");
176 writer.attributes("alt_attrs_caption");
183 lemon::GraphWriter<Graph> writer2(graph, std::cout);
186 void checkBpGraphReaderCompile() {
187 typedef lemon::concepts::ExtendableBpGraphComponent<
188 lemon::concepts::BpGraph> BpGraph;
190 BpGraph::NodeMap<int> node_map(graph);
191 BpGraph::RedNodeMap<int> red_node_map(graph);
192 BpGraph::BlueNodeMap<int> blue_node_map(graph);
193 BpGraph::ArcMap<int> arc_map(graph);
194 BpGraph::EdgeMap<int> edge_map(graph);
196 BpGraph::RedNode red_node;
197 BpGraph::BlueNode blue_node;
202 lemon::BpGraphReader<BpGraph> reader(graph, "filename");
203 reader.nodeMap("node_map", node_map);
204 reader.nodeMap("node_map", node_map, ReaderConverter());
205 reader.redNodeMap("red_node_map", red_node_map);
206 reader.redNodeMap("red_node_map", red_node_map, ReaderConverter());
207 reader.blueNodeMap("blue_node_map", blue_node_map);
208 reader.blueNodeMap("blue_node_map", blue_node_map, ReaderConverter());
209 reader.arcMap("arc_map", arc_map);
210 reader.arcMap("arc_map", arc_map, ReaderConverter());
211 reader.edgeMap("edge_map", edge_map);
212 reader.edgeMap("edge_map", edge_map, ReaderConverter());
213 reader.attribute("attr", attr);
214 reader.attribute("attr", attr, ReaderConverter());
215 reader.node("node", node);
216 reader.redNode("red_node", red_node);
217 reader.blueNode("blue_node", blue_node);
218 reader.arc("arc", arc);
220 reader.nodes("alt_nodes_caption");
221 reader.edges("alt_edges_caption");
222 reader.attributes("alt_attrs_caption");
224 reader.useNodes(node_map);
225 reader.useNodes(node_map, WriterConverter());
226 reader.useEdges(edge_map);
227 reader.useEdges(edge_map, WriterConverter());
234 lemon::BpGraphReader<BpGraph> reader2(graph, std::cin);
237 void checkBpGraphWriterCompile() {
238 typedef lemon::concepts::BpGraph BpGraph;
240 BpGraph::NodeMap<int> node_map(graph);
241 BpGraph::RedNodeMap<int> red_node_map(graph);
242 BpGraph::BlueNodeMap<int> blue_node_map(graph);
243 BpGraph::ArcMap<int> arc_map(graph);
244 BpGraph::EdgeMap<int> edge_map(graph);
246 BpGraph::RedNode red_node;
247 BpGraph::BlueNode blue_node;
252 lemon::BpGraphWriter<BpGraph> writer(graph, "filename");
253 writer.nodeMap("node_map", node_map);
254 writer.nodeMap("node_map", node_map, WriterConverter());
255 writer.redNodeMap("red_node_map", red_node_map);
256 writer.redNodeMap("red_node_map", red_node_map, WriterConverter());
257 writer.blueNodeMap("blue_node_map", blue_node_map);
258 writer.blueNodeMap("blue_node_map", blue_node_map, WriterConverter());
259 writer.arcMap("arc_map", arc_map);
260 writer.arcMap("arc_map", arc_map, WriterConverter());
261 writer.edgeMap("edge_map", edge_map);
262 writer.edgeMap("edge_map", edge_map, WriterConverter());
263 writer.attribute("attr", attr);
264 writer.attribute("attr", attr, WriterConverter());
265 writer.node("node", node);
266 writer.redNode("red_node", red_node);
267 writer.blueNode("blue_node", blue_node);
268 writer.arc("arc", arc);
270 writer.nodes("alt_nodes_caption");
271 writer.edges("alt_edges_caption");
272 writer.attributes("alt_attrs_caption");
279 lemon::BpGraphWriter<BpGraph> writer2(graph, std::cout);
282 void checkDigraphReaderWriter() {
283 typedef lemon::SmartDigraph Digraph;
285 Digraph::Node n1 = digraph.addNode();
286 Digraph::Node n2 = digraph.addNode();
287 Digraph::Node n3 = digraph.addNode();
289 Digraph::Arc a1 = digraph.addArc(n1, n2);
290 Digraph::Arc a2 = digraph.addArc(n2, n3);
292 Digraph::NodeMap<int> node_map(digraph);
297 Digraph::ArcMap<int> arc_map(digraph);
303 std::ostringstream os;
304 lemon::DigraphWriter<Digraph> writer(digraph, os);
306 writer.nodeMap("node_map1", node_map);
307 writer.nodeMap("node_map2", node_map, WriterConverter());
308 writer.arcMap("arc_map1", arc_map);
309 writer.arcMap("arc_map2", arc_map, WriterConverter());
310 writer.node("node", n2);
311 writer.arc("arc", a1);
312 writer.attribute("attr1", attr);
313 writer.attribute("attr2", attr, WriterConverter());
317 typedef lemon::ListDigraph ExpDigraph;
318 ExpDigraph exp_digraph;
319 ExpDigraph::NodeMap<int> exp_node_map1(exp_digraph);
320 ExpDigraph::NodeMap<int> exp_node_map2(exp_digraph);
321 ExpDigraph::ArcMap<int> exp_arc_map1(exp_digraph);
322 ExpDigraph::ArcMap<int> exp_arc_map2(exp_digraph);
323 ExpDigraph::Node exp_n2;
324 ExpDigraph::Arc exp_a1;
328 std::istringstream is(os.str());
329 lemon::DigraphReader<ExpDigraph> reader(exp_digraph, is);
331 reader.nodeMap("node_map1", exp_node_map1);
332 reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
333 reader.arcMap("arc_map1", exp_arc_map1);
334 reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
335 reader.node("node", exp_n2);
336 reader.arc("arc", exp_a1);
337 reader.attribute("attr1", exp_attr1);
338 reader.attribute("attr2", exp_attr2, ReaderConverter());
342 check(lemon::countNodes(exp_digraph) == 3, "Wrong number of nodes");
343 check(lemon::countArcs(exp_digraph) == 2, "Wrong number of arcs");
344 check(exp_node_map1[exp_n2] == 12, "Wrong map value");
345 check(exp_node_map2[exp_n2] == 12, "Wrong map value");
346 check(exp_arc_map1[exp_a1] == 21, "Wrong map value");
347 check(exp_arc_map2[exp_a1] == 21, "Wrong map value");
348 check(exp_attr1 == 100, "Wrong attr value");
349 check(exp_attr2 == 100, "Wrong attr value");
352 void checkGraphReaderWriter() {
353 typedef lemon::SmartGraph Graph;
355 Graph::Node n1 = graph.addNode();
356 Graph::Node n2 = graph.addNode();
357 Graph::Node n3 = graph.addNode();
359 Graph::Edge e1 = graph.addEdge(n1, n2);
360 Graph::Edge e2 = graph.addEdge(n2, n3);
362 Graph::NodeMap<int> node_map(graph);
367 Graph::EdgeMap<int> edge_map(graph);
371 Graph::ArcMap<int> arc_map(graph);
372 arc_map[graph.direct(e1, true)] = 211;
373 arc_map[graph.direct(e1, false)] = 212;
374 arc_map[graph.direct(e2, true)] = 221;
375 arc_map[graph.direct(e2, false)] = 222;
379 std::ostringstream os;
380 lemon::GraphWriter<Graph> writer(graph, os);
382 writer.nodeMap("node_map1", node_map);
383 writer.nodeMap("node_map2", node_map, WriterConverter());
384 writer.edgeMap("edge_map1", edge_map);
385 writer.edgeMap("edge_map2", edge_map, WriterConverter());
386 writer.arcMap("arc_map1", arc_map);
387 writer.arcMap("arc_map2", arc_map, WriterConverter());
388 writer.node("node", n2);
389 writer.edge("edge", e1);
390 writer.arc("arc", graph.direct(e1, false));
391 writer.attribute("attr1", attr);
392 writer.attribute("attr2", attr, WriterConverter());
396 typedef lemon::ListGraph ExpGraph;
398 ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
399 ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
400 ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
401 ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
402 ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
403 ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
404 ExpGraph::Node exp_n2;
405 ExpGraph::Edge exp_e1;
406 ExpGraph::Arc exp_a1;
410 std::istringstream is(os.str());
411 lemon::GraphReader<ExpGraph> reader(exp_graph, is);
413 reader.nodeMap("node_map1", exp_node_map1);
414 reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
415 reader.edgeMap("edge_map1", exp_edge_map1);
416 reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
417 reader.arcMap("arc_map1", exp_arc_map1);
418 reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
419 reader.node("node", exp_n2);
420 reader.edge("edge", exp_e1);
421 reader.arc("arc", exp_a1);
422 reader.attribute("attr1", exp_attr1);
423 reader.attribute("attr2", exp_attr2, ReaderConverter());
427 check(lemon::countNodes(exp_graph) == 3, "Wrong number of nodes");
428 check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
429 check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
430 check(exp_node_map1[exp_n2] == 12, "Wrong map value");
431 check(exp_node_map2[exp_n2] == 12, "Wrong map value");
432 check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
433 check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
434 check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
435 check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
436 check(exp_attr1 == 100, "Wrong attr value");
437 check(exp_attr2 == 100, "Wrong attr value");
440 void checkBpGraphReaderWriter() {
441 typedef lemon::SmartBpGraph Graph;
443 Graph::RedNode rn1 = graph.addRedNode();
444 Graph::RedNode rn2 = graph.addRedNode();
445 Graph::RedNode rn3 = graph.addRedNode();
446 Graph::BlueNode bn1 = graph.addBlueNode();
447 Graph::BlueNode bn2 = graph.addBlueNode();
450 Graph::Edge e1 = graph.addEdge(rn1, bn1);
451 Graph::Edge e2 = graph.addEdge(rn2, bn1);
453 Graph::NodeMap<int> node_map(graph);
460 Graph::NodeMap<int> red_node_map(graph);
461 red_node_map[rn1] = 411;
462 red_node_map[rn2] = 412;
463 red_node_map[rn3] = 413;
465 Graph::NodeMap<int> blue_node_map(graph);
466 blue_node_map[bn1] = 414;
467 blue_node_map[bn2] = 415;
469 Graph::EdgeMap<int> edge_map(graph);
473 Graph::ArcMap<int> arc_map(graph);
474 arc_map[graph.direct(e1, true)] = 211;
475 arc_map[graph.direct(e1, false)] = 212;
476 arc_map[graph.direct(e2, true)] = 221;
477 arc_map[graph.direct(e2, false)] = 222;
481 std::ostringstream os;
482 lemon::BpGraphWriter<Graph> writer(graph, os);
484 writer.nodeMap("node_map1", node_map);
485 writer.nodeMap("node_map2", node_map, WriterConverter());
486 writer.nodeMap("red_node_map1", red_node_map);
487 writer.nodeMap("red_node_map2", red_node_map, WriterConverter());
488 writer.nodeMap("blue_node_map1", blue_node_map);
489 writer.nodeMap("blue_node_map2", blue_node_map, WriterConverter());
490 writer.edgeMap("edge_map1", edge_map);
491 writer.edgeMap("edge_map2", edge_map, WriterConverter());
492 writer.arcMap("arc_map1", arc_map);
493 writer.arcMap("arc_map2", arc_map, WriterConverter());
494 writer.node("node", n);
495 writer.redNode("red_node", rn1);
496 writer.blueNode("blue_node", bn2);
497 writer.edge("edge", e1);
498 writer.arc("arc", graph.direct(e1, false));
499 writer.attribute("attr1", attr);
500 writer.attribute("attr2", attr, WriterConverter());
504 typedef lemon::ListBpGraph ExpGraph;
506 ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
507 ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
508 ExpGraph::RedNodeMap<int> exp_red_node_map1(exp_graph);
509 ExpGraph::RedNodeMap<int> exp_red_node_map2(exp_graph);
510 ExpGraph::BlueNodeMap<int> exp_blue_node_map1(exp_graph);
511 ExpGraph::BlueNodeMap<int> exp_blue_node_map2(exp_graph);
512 ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
513 ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
514 ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
515 ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
516 ExpGraph::Node exp_n;
517 ExpGraph::RedNode exp_rn1;
518 ExpGraph::BlueNode exp_bn2;
519 ExpGraph::Edge exp_e1;
520 ExpGraph::Arc exp_a1;
524 std::istringstream is(os.str());
525 lemon::BpGraphReader<ExpGraph> reader(exp_graph, is);
527 reader.nodeMap("node_map1", exp_node_map1);
528 reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
529 reader.redNodeMap("red_node_map1", exp_red_node_map1);
530 reader.redNodeMap("red_node_map2", exp_red_node_map2, ReaderConverter());
531 reader.blueNodeMap("blue_node_map1", exp_blue_node_map1);
532 reader.blueNodeMap("blue_node_map2", exp_blue_node_map2, ReaderConverter());
533 reader.edgeMap("edge_map1", exp_edge_map1);
534 reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
535 reader.arcMap("arc_map1", exp_arc_map1);
536 reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
537 reader.node("node", exp_n);
538 reader.redNode("red_node", exp_rn1);
539 reader.blueNode("blue_node", exp_bn2);
540 reader.edge("edge", exp_e1);
541 reader.arc("arc", exp_a1);
542 reader.attribute("attr1", exp_attr1);
543 reader.attribute("attr2", exp_attr2, ReaderConverter());
547 check(lemon::countNodes(exp_graph) == 5, "Wrong number of nodes");
548 check(lemon::countRedNodes(exp_graph) == 3, "Wrong number of red nodes");
549 check(lemon::countBlueNodes(exp_graph) == 2, "Wrong number of blue nodes");
550 check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
551 check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
552 check(exp_node_map1[exp_n] == 14, "Wrong map value");
553 check(exp_node_map2[exp_n] == 14, "Wrong map value");
554 check(exp_red_node_map1[exp_rn1] == 411, "Wrong map value");
555 check(exp_red_node_map2[exp_rn1] == 411, "Wrong map value");
556 check(exp_blue_node_map1[exp_bn2] == 415, "Wrong map value");
557 check(exp_blue_node_map2[exp_bn2] == 415, "Wrong map value");
558 check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
559 check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
560 check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
561 check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
562 check(exp_attr1 == 100, "Wrong attr value");
563 check(exp_attr2 == 100, "Wrong attr value");
569 checkDigraphReaderWriter();
572 checkGraphReaderWriter();
574 { // Check bipartite graph
575 checkBpGraphReaderWriter();