COIN-OR::LEMON - Graph Library

Ticket #382: 801f8c1c86b5.patch

File 801f8c1c86b5.patch, 6.1 KB (added by Alpar Juttner, 8 years ago)
  • lemon/lgf_reader.h

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1312301614 -7200
    # Node ID 801f8c1c86b5ff7ad83297f8352706ab54d6afdb
    # Parent  e24922c56bc2e6cef6cff65c251ce6000005d00f
    Allow lgf file without Arc maps (#382)
    
    A single '-' character in the @arcs sectio header indicates that
    there is no arc map.
    
    diff --git a/lemon/lgf_reader.h b/lemon/lgf_reader.h
    a b  
    963963        std::string map;
    964964        int index = 0;
    965965        while (_reader_bits::readToken(line, map)) {
     966          if(map == "-")
     967            {
     968              if(index!=0)
     969                throw FormatError("'-' is not allowed as a map name");
     970              else if (line >> std::ws >> c)
     971                throw FormatError("Extra character at the end of line");
     972              else break;
     973            }
    966974          if (maps.find(map) != maps.end()) {
    967975            std::ostringstream msg;
    968976            msg << "Multiple occurence of arc map: " << map;
     
    18031811        std::string map;
    18041812        int index = 0;
    18051813        while (_reader_bits::readToken(line, map)) {
     1814          if(map == "-")
     1815            {
     1816              if(index!=0)
     1817                throw FormatError("'-' is not allowed as a map name");
     1818              else if (line >> std::ws >> c)
     1819                throw FormatError("Extra character at the end of line");
     1820              else break;
     1821            }
    18061822          if (maps.find(map) != maps.end()) {
    18071823            std::ostringstream msg;
    18081824            msg << "Multiple occurence of edge map: " << map;
  • test/CMakeLists.txt

    diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
    a b  
    1818  graph_utils_test
    1919  heap_test
    2020  kruskal_test
     21  lgf_test
    2122  maps_test
    2223  random_test
    2324  path_test
  • test/Makefile.am

    diff --git a/test/Makefile.am b/test/Makefile.am
    a b  
    1818        test/graph_utils_test \
    1919        test/heap_test \
    2020        test/kruskal_test \
     21        test/lgf_test \
    2122        test/maps_test \
    2223        test/random_test \
    2324        test/path_test \
     
    4142test_graph_utils_test_SOURCES = test/graph_utils_test.cc
    4243test_heap_test_SOURCES = test/heap_test.cc
    4344test_kruskal_test_SOURCES = test/kruskal_test.cc
     45test_lgf_test_SOURCES = test/lgf_test.cc
    4446test_maps_test_SOURCES = test/maps_test.cc
    4547test_path_test_SOURCES = test/path_test.cc
    4648test_random_test_SOURCES = test/random_test.cc
  • new file test/lgf_test.cc

    diff --git a/test/lgf_test.cc b/test/lgf_test.cc
    new file mode 100644
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2011
     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 <lemon/list_graph.h>
     20#include <lemon/lgf_reader.h>
     21#include "test_tools.h"
     22
     23using namespace lemon;
     24
     25char test_lgf[] =
     26  "@nodes\n"
     27  "label\n"
     28  "0\n"
     29  "1\n"
     30  "@arcs\n"
     31  "     label\n"
     32  "0 1  0\n"
     33  "1 0  1\n"
     34  "@attributes\n"
     35  "source 0\n"
     36  "target 1\n";
     37
     38char test_lgf_nomap[] =
     39  "@nodes\n"
     40  "label\n"
     41  "0\n"
     42  "1\n"
     43  "@arcs\n"
     44  "     -\n"
     45  "0 1\n";
     46
     47char test_lgf_bad1[] =
     48  "@nodes\n"
     49  "label\n"
     50  "0\n"
     51  "1\n"
     52  "@arcs\n"
     53  "     - another\n"
     54  "0 1\n";
     55
     56char test_lgf_bad2[] =
     57  "@nodes\n"
     58  "label\n"
     59  "0\n"
     60  "1\n"
     61  "@arcs\n"
     62  "     label -\n"
     63  "0 1\n";
     64
     65
     66int main()
     67{
     68  {
     69    ListDigraph d;
     70    ListDigraph::Node s,t;
     71    ListDigraph::ArcMap<int> label(d);
     72    std::istringstream input(test_lgf);
     73    digraphReader(d, input).
     74      node("source", s).
     75      node("target", t).
     76      arcMap("label", label).
     77      run();
     78    check(countNodes(d) == 2,"There should be 2 nodes");
     79    check(countArcs(d) == 2,"There should be 2 arcs");
     80  }
     81  {
     82    ListGraph g;
     83    ListGraph::Node s,t;
     84    ListGraph::EdgeMap<int> label(g);
     85    std::istringstream input(test_lgf);
     86    graphReader(g, input).
     87      node("source", s).
     88      node("target", t).
     89      edgeMap("label", label).
     90      run();
     91    check(countNodes(g) == 2,"There should be 2 nodes");
     92    check(countEdges(g) == 2,"There should be 2 arcs");
     93  }
     94
     95  {
     96    ListDigraph d;
     97    std::istringstream input(test_lgf_nomap);
     98    digraphReader(d, input).
     99      run();
     100    check(countNodes(d) == 2,"There should be 2 nodes");
     101    check(countArcs(d) == 1,"There should be 1 arc");
     102  }
     103  {
     104    ListGraph g;
     105    std::istringstream input(test_lgf_nomap);
     106    graphReader(g, input).
     107      run();
     108    check(countNodes(g) == 2,"There should be 2 nodes");
     109    check(countEdges(g) == 1,"There should be 1 edge");
     110  }
     111
     112  {
     113    ListDigraph d;
     114    std::istringstream input(test_lgf_bad1);
     115    bool ok=false;
     116    try {
     117      digraphReader(d, input).
     118        run();
     119    }
     120    catch (FormatError& error)
     121      {
     122        ok = true;
     123      }
     124    check(ok,"FormatError exception should have occured");
     125  }
     126  {
     127    ListGraph g;
     128    std::istringstream input(test_lgf_bad1);
     129    bool ok=false;
     130    try {
     131      graphReader(g, input).
     132        run();
     133    }
     134    catch (FormatError& error)
     135      {
     136        ok = true;
     137      }
     138    check(ok,"FormatError exception should have occured");
     139  }
     140
     141  {
     142    ListDigraph d;
     143    std::istringstream input(test_lgf_bad2);
     144    bool ok=false;
     145    try {
     146      digraphReader(d, input).
     147        run();
     148    }
     149    catch (FormatError& error)
     150      {
     151        ok = true;
     152      }
     153    check(ok,"FormatError exception should have occured");
     154  }
     155  {
     156    ListGraph g;
     157    std::istringstream input(test_lgf_bad2);
     158    bool ok=false;
     159    try {
     160      graphReader(g, input).
     161        run();
     162    }
     163    catch (FormatError& error)
     164      {
     165        ok = true;
     166      }
     167    check(ok,"FormatError exception should have occured");
     168  }
     169}