test/tsp_test.cc
changeset 1037 d3dcc49e6403
parent 1035 07682e24c4e8
child 1092 dceba191c00d
     1.1 --- a/test/tsp_test.cc	Sun Jan 09 15:06:55 2011 +0100
     1.2 +++ b/test/tsp_test.cc	Thu Feb 28 17:13:14 2013 +0100
     1.3 @@ -61,21 +61,23 @@
     1.4  // }
     1.5  
     1.6  // Checks tour validity
     1.7 -bool checkTour(const FullGraph &gr, const std::vector<FullGraph::Node> &p) {
     1.8 +template <typename Container>
     1.9 +bool checkTour(const FullGraph &gr, const Container &p) {
    1.10    FullGraph::NodeMap<bool> used(gr, false);
    1.11    
    1.12 -  int nodes = 0;
    1.13 -  for (int i = 0; i < int(p.size()); ++i) {
    1.14 -    if (used[p[i]]) return false;
    1.15 -    used[p[i]] = true;
    1.16 -    ++nodes;
    1.17 +  int node_cnt = 0;
    1.18 +  for (typename Container::const_iterator it = p.begin(); it != p.end(); ++it) {
    1.19 +    FullGraph::Node node = *it;
    1.20 +    if (used[node]) return false;
    1.21 +    used[node] = true;
    1.22 +    ++node_cnt;
    1.23    }
    1.24    
    1.25 -  return (nodes == gr.nodeNum());
    1.26 +  return (node_cnt == gr.nodeNum());
    1.27  }
    1.28  
    1.29  // Checks tour validity
    1.30 -bool checkTour(const FullGraph &gr, const Path<FullGraph> &p) {
    1.31 +bool checkTourPath(const FullGraph &gr, const Path<FullGraph> &p) {
    1.32    FullGraph::NodeMap<bool> used(gr, false);
    1.33    
    1.34    if (!checkPath(gr, p)) return false;
    1.35 @@ -134,21 +136,24 @@
    1.36      check(alg.run() == esize, alg_name + ": Wrong total cost");
    1.37      check(alg.tourCost() == esize, alg_name + ": Wrong total cost");
    1.38  
    1.39 -    std::list<Node> list;
    1.40 -    std::vector<Node> vec;
    1.41 -    alg.tourNodes(list);
    1.42 -    alg.tourNodes(vec);
    1.43 -    check(list.size() == nsize, alg_name + ": Wrong node sequence");
    1.44 -    check(vec.size() == nsize,  alg_name + ": Wrong node sequence");
    1.45 -    check(alg.tourNodes().size() == nsize, alg_name + ": Wrong node sequence");
    1.46 -    check(checkTour(g, vec), alg_name + ": Wrong node sequence");
    1.47 -    check(checkCost(g, vec, constMap<Edge, int>(1), esize),
    1.48 +    std::list<Node> list1(nsize), list2;
    1.49 +    std::vector<Node> vec1(nsize), vec2;
    1.50 +    alg.tourNodes(list1.begin());
    1.51 +    alg.tourNodes(vec1.begin());
    1.52 +    alg.tourNodes(std::front_inserter(list2));
    1.53 +    alg.tourNodes(std::back_inserter(vec2));
    1.54 +    check(checkTour(g, alg.tourNodes()), alg_name + ": Wrong node sequence");
    1.55 +    check(checkTour(g, list1), alg_name + ": Wrong node sequence");
    1.56 +    check(checkTour(g, vec1), alg_name + ": Wrong node sequence");
    1.57 +    check(checkTour(g, list2), alg_name + ": Wrong node sequence");
    1.58 +    check(checkTour(g, vec2), alg_name + ": Wrong node sequence");
    1.59 +    check(checkCost(g, vec1, constMap<Edge, int>(1), esize),
    1.60        alg_name + ": Wrong tour cost");
    1.61  
    1.62      SimplePath<FullGraph> path;
    1.63      alg.tour(path);
    1.64      check(path.length() == esize, alg_name + ": Wrong tour");
    1.65 -    check(checkTour(g, path), alg_name + ": Wrong tour");
    1.66 +    check(checkTourPath(g, path), alg_name + ": Wrong tour");
    1.67      check(checkCost(g, path, constMap<Edge, int>(1), esize),
    1.68        alg_name + ": Wrong tour cost");
    1.69    }
    1.70 @@ -180,14 +185,14 @@
    1.71      check(alg.run() > 0, alg_name + ": Wrong total cost");
    1.72  
    1.73      std::vector<Node> vec;
    1.74 -    alg.tourNodes(vec);
    1.75 +    alg.tourNodes(std::back_inserter(vec));
    1.76      check(checkTour(g, vec), alg_name + ": Wrong node sequence");
    1.77      check(checkCost(g, vec, cost, alg.tourCost()),
    1.78        alg_name + ": Wrong tour cost");
    1.79  
    1.80      SimplePath<FullGraph> path;
    1.81      alg.tour(path);
    1.82 -    check(checkTour(g, path), alg_name + ": Wrong tour");
    1.83 +    check(checkTourPath(g, path), alg_name + ": Wrong tour");
    1.84      check(checkCost(g, path, cost, alg.tourCost()),
    1.85        alg_name + ": Wrong tour cost");
    1.86