# HG changeset patch # User alpar # Date 1083569249 0 # Node ID 769f31e9f7b03f7a7f8c0a7ec628c89d1f795b5e # Parent 1b41ebb5fee52ecda5485799671d24b527e16a32 test/graph_test.cc added. It discovered several bugs and warnings in 'include/smart_graph.h', in 'include/skeletons/graph.h' and in 'work/alpar/list_graph.h'. They have also been fixed. diff -r 1b41ebb5fee5 -r 769f31e9f7b0 src/include/skeletons/graph.h --- a/src/include/skeletons/graph.h Fri Apr 30 19:02:40 2004 +0000 +++ b/src/include/skeletons/graph.h Mon May 03 07:27:29 2004 +0000 @@ -1,6 +1,6 @@ // -*- c++ -*- -#ifndef HUGO_GRAPH_H -#define HUGO_GRAPH_H +#ifndef HUGO_SKELETON_GRAPH_H +#define HUGO_SKELETON_GRAPH_H ///\file ///\brief Declaration of GraphSkeleton. @@ -58,13 +58,13 @@ /// Two iterators are equal if and only if they point to the /// same object or both are invalid. - bool operator==(Node n) const { return true; } + bool operator==(Node) const { return true; } /// \sa \ref operator==(Node n) /// - bool operator!=(Node n) const { return true; } + bool operator!=(Node) const { return true; } - bool operator<(Node n) const { return true; } + bool operator<(Node) const { return true; } }; /// This iterator goes through each node. @@ -90,7 +90,7 @@ NodeIt(const GraphSkeleton &G) {} /// @warning The default constructor sets the iterator /// to an undefined value. - NodeIt(const NodeIt &) {} + NodeIt(const NodeIt &n) : Node(n) {} }; @@ -104,9 +104,9 @@ Edge(Invalid) {} /// Two iterators are equal if and only if they point to the /// same object or both are invalid. - bool operator==(Edge n) const { return true; } - bool operator!=(Edge n) const { return true; } - bool operator<(Edge n) const { return true; } + bool operator==(Edge) const { return true; } + bool operator!=(Edge) const { return true; } + bool operator<(Edge) const { return true; } }; /// This iterator goes trough the outgoing edges of a node. @@ -187,9 +187,9 @@ NodeIt &first(NodeIt &i) const { return i;} /// The first incoming edge. - InEdgeIt &first(InEdgeIt &i, Node n) const { return i;} + InEdgeIt &first(InEdgeIt &i, Node) const { return i;} /// The first outgoing edge. - OutEdgeIt &first(OutEdgeIt &i, Node n) const { return i;} + OutEdgeIt &first(OutEdgeIt &i, Node) const { return i;} // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;} /// The first edge of the Graph. EdgeIt &first(EdgeIt &i) const { return i;} @@ -258,7 +258,7 @@ ///Add a new edge to the graph with tail node \c tail ///and head node \c head. ///\return the new edge. - Edge addEdge(Node tail, Node head) { return INVALID;} + Edge addEdge(Node, Node) { return INVALID;} /// Resets the graph. @@ -294,11 +294,11 @@ /// Sets the value associated with node \c i to the value \c t. /// - void set(Node i, T t) {} - /// Gets the value of a node. - T get(Node i) const {return *(T*)0;} //FIXME: Is it necessary - T &operator[](Node i) {return *(T*)0;} - const T &operator[](Node i) const {return *(T*)0;} + void set(Node, T) {} + // Gets the value of a node. + //T get(Node i) const {return *(T*)0;} //FIXME: Is it necessary? + T &operator[](Node) {return *(T*)0;} + const T &operator[](Node) const {return *(T*)0;} /// Updates the map if the graph has been changed @@ -326,9 +326,14 @@ EdgeMap(const GraphSkeleton &G) {} EdgeMap(const GraphSkeleton &G, T t) {} - void set(Edge i, T t) {} - T get(Edge i) const {return *(T*)0;} - T &operator[](Edge i) {return *(T*)0;} + ///\todo It can copy between different types. + /// + template EdgeMap(const EdgeMap &m) {} + + void set(Edge, T) {} + //T get(Edge) const {return *(T*)0;} + T &operator[](Edge) {return *(T*)0;} + const T &operator[](Edge) const {return *(T*)0;} void update() {} void update(T a) {} //FIXME: Is it necessary @@ -391,4 +396,4 @@ // } -#endif // HUGO_GRAPH_H +#endif // HUGO_SKELETON_GRAPH_H diff -r 1b41ebb5fee5 -r 769f31e9f7b0 src/include/smart_graph.h --- a/src/include/smart_graph.h Fri Apr 30 19:02:40 2004 +0000 +++ b/src/include/smart_graph.h Mon May 03 07:27:29 2004 +0000 @@ -80,6 +80,7 @@ public: + class NodeIt; class EdgeIt; class OutEdgeIt; @@ -138,7 +139,17 @@ bool valid(Edge e) const { return e.n!=-1; } bool valid(Node n) const { return n.n!=-1; } + ///\deprecated Use + ///\code + /// e=INVALID; + ///\endcode + ///instead. void setInvalid(Edge &e) { e.n=-1; } + ///\deprecated Use + ///\code + /// e=INVALID; + ///\endcode + ///instead. void setInvalid(Node &n) { n.n=-1; } template It getNext(It it) const @@ -197,7 +208,7 @@ Node(int nn) {n=nn;} public: Node() {} - Node (Invalid i) { n=-1; } + Node (Invalid) { n=-1; } bool operator==(const Node i) const {return n==i.n;} bool operator!=(const Node i) const {return n!=i.n;} bool operator<(const Node i) const {return n &m) : DynMapBase(*m.G), container(m.container) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); } template friend class EdgeMap; @@ -389,7 +400,7 @@ template EdgeMap(const EdgeMap &m) : DynMapBase(*m.G) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); typename std::vector::const_iterator i; for(typename std::vector::const_iterator i=m.container.begin(); i!=m.container.end(); diff -r 1b41ebb5fee5 -r 769f31e9f7b0 src/test/graph_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/graph_test.cc Mon May 03 07:27:29 2004 +0000 @@ -0,0 +1,304 @@ +#include +#include +#include +#include<../work/alpar/list_graph.h> + +/* +This test makes consistency checks of list graph structures. + +G.addNode(), G.addEdge(), G.valid(), G.tail(), G.head() + +*/ + +using namespace hugo; + +// void check(bool rc, const char *msg) { +// if(!rc) { +// std::cerr << msg << std::endl; +// exit(1); +// } +// } + +#define check(rc, msg) \ + if(!rc) { \ + std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \ + exit(1); \ + } else { } \ + + +template void checkCompile(Graph &G) +{ + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::Edge Edge; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::InEdgeIt InEdgeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + + { + Node i; Node j(i); Node k(INVALID); + i=j; + bool b=G.valid(i); b=b; + b=(i==j); b=(i!=j); b=(i m(G); + typename Graph::NodeMap const &cm = m; //Const map + typename Graph::NodeMap mdef(G,12); //Inicialize with default value + typename Graph::NodeMap mm(cm); //Copy + typename Graph::NodeMap dm(cm); //Copy from another type + int v; + v=m[k]; m[k]=v; m.set(k,v); + v=cm[k]; + + m=cm; + dm=cm; //Copy from another type + } + { //bool NodeMap + Node k; + typename Graph::NodeMap m(G); + typename Graph::NodeMap const &cm = m; //Const map + typename Graph::NodeMap mdef(G,12); //Inicialize with default value + typename Graph::NodeMap mm(cm); //Copy + typename Graph::NodeMap dm(cm); //Copy from another type + bool v; + v=m[k]; m[k]=v; m.set(k,v); + v=cm[k]; + + m=cm; + dm=cm; //Copy from another type + } + //EdgeMap tests + { + Edge k; + typename Graph::EdgeMap m(G); + typename Graph::EdgeMap const &cm = m; //Const map + typename Graph::EdgeMap mdef(G,12); //Inicialize with default value + typename Graph::EdgeMap mm(cm); //Copy + typename Graph::EdgeMap dm(cm); //Copy from another type + int v; + v=m[k]; m[k]=v; m.set(k,v); + v=cm[k]; + + m=cm; + dm=cm; //Copy from another type + } + { //bool EdgeMap + Edge k; + typename Graph::EdgeMap m(G); + typename Graph::EdgeMap const &cm = m; //Const map + typename Graph::EdgeMap mdef(G,12); //Inicialize with default value + typename Graph::EdgeMap mm(cm); //Copy + typename Graph::EdgeMap dm(cm); //Copy from another type + bool v; + v=m[k]; m[k]=v; m.set(k,v); + v=cm[k]; + + m=cm; + dm=cm; //Copy from another type + } + +} + + + +template void addPetersen(Graph &G) +{ + std::vector outer, inner; + + for(int i=0;i<5;i++) { + outer.push_back(G.addNode()); + inner.push_back(G.addNode()); + } + + for(int i=0;i<5;i++) { + G.addEdge(outer[i],inner[i]); + G.addEdge(outer[i],outer[(i+1)%5]); + G.addEdge(inner[i],inner[(i+2)%5]); + } +} + +template void checkNodeList(Graph &G, int nn) +{ + typename Graph::NodeIt n(G); + for(int i=0;i void checkEdgeList(Graph &G, int nn) +{ + typedef typename Graph::EdgeIt EdgeIt; + + EdgeIt e(G); + for(int i=0;i void checkOutEdgeList(Graph &G, + typename Graph::Node n, + int nn) +{ + typename Graph::OutEdgeIt e(G,n); + for(int i=0;i void checkInEdgeList(Graph &G, + typename Graph::Node n, + int nn) +{ + typename Graph::InEdgeIt e(G,n); + for(int i=0;i void bidirPetersen(Graph &G) +{ + typedef typename Graph::Edge Edge; + typedef typename Graph::EdgeIt EdgeIt; + + checkEdgeList(G,15); + + std::vector ee; + + for(EdgeIt e(G);G.valid(e);G.next(e)) ee.push_back(e); + + for(typename std::vector::iterator p=ee.begin();p!=ee.end();p++) + G.addEdge(G.head(*p),G.tail(*p)); +} + +template void checkPetersen(Graph &G) +{ + typedef typename Graph::Node Node; + + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::NodeIt NodeIt; + + checkNodeList(G,10); + checkEdgeList(G,30); + + for(NodeIt n(G);G.valid(n);G.next(n)) { + checkInEdgeList(G,n,3); + checkOutEdgeList(G,n,3); + G.next(n); + } +} + +template void checkCompile(GraphSkeleton &); +template void checkCompile(SmartGraph &); +template void checkCompile(SymSmartGraph &); +template void checkCompile(ListGraph &); +template void checkCompile(SymListGraph &); +//Due to some mysterious and some conceptual problems it does not work. +//template void checkCompile >(EdgeSet &); + +int main() +{ + { + SmartGraph G; + addPetersen(G); + bidirPetersen(G); + checkPetersen(G); + } + { + ListGraph G; + addPetersen(G); + bidirPetersen(G); + checkPetersen(G); + } + { + SymSmartGraph G; + addPetersen(G); + checkPetersen(G); + } + { + SymListGraph G; + addPetersen(G); + checkPetersen(G); + } + + //\todo map tests. + //\todo copy constr tests. + + std::cout << __FILE__ ": All tests passed.\n"; + +} diff -r 1b41ebb5fee5 -r 769f31e9f7b0 src/work/alpar/list_graph.h --- a/src/work/alpar/list_graph.h Fri Apr 30 19:02:40 2004 +0000 +++ b/src/work/alpar/list_graph.h Mon May 03 07:27:29 2004 +0000 @@ -316,7 +316,7 @@ Node(int nn) {n=nn;} public: Node() {} - Node (Invalid i) { n=-1; } + Node (Invalid) { n=-1; } bool operator==(const Node i) const {return n==i.n;} bool operator!=(const Node i) const {return n!=i.n;} bool operator<(const Node i) const {return n &m) : DynMapBase(*m.G), container(m.container) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); } template friend class EdgeMap; @@ -513,7 +513,7 @@ template EdgeMap(const EdgeMap &m) : DynMapBase(*m.G) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); typename std::vector::const_iterator i; for(typename std::vector::const_iterator i=m.container.begin(); i!=m.container.end(); @@ -1294,11 +1294,11 @@ it.n=edges[it.n].next_in; } else { - typename NodeGraphType::Node n; - for(n=G.next(edges[it.n].head); - G.valid(n) && nodes[n].first_in == -1; - G.next(n)) ; - it.n = (G.valid(n))?-1:nodes[n].first_in; + NodeIt n; + for(n=next(edges[it.n].head); + valid(n) && nodes[n].first_in == -1; + next(n)) ; + it.n = (valid(n))?-1:nodes[n].first_in; } return it; } @@ -1385,6 +1385,7 @@ // first_node=first_free_node=first_free_edge=-1; // } + public: class Node : public NodeGraphType::Node { friend class EdgeSet; // template friend class NodeMap; @@ -1444,10 +1445,11 @@ friend class EdgeSet; public: EdgeIt(const EdgeSet& G) : Edge() { - typename NodeGraphType::Node m; + // typename NodeGraphType::Node m; + NodeIt m; for(G.first(m); - G.valid(m) && nodes[m].first_in == -1; G.next[m]); - n = G.valid(m)?-1:nodes[m].first_in; + G.valid(m) && G.nodes[m].first_in == -1; G.next(m)); + n = G.valid(m)?-1:G.nodes[m].first_in; } EdgeIt (Invalid i) : Edge(i) { } EdgeIt() : Edge() { } @@ -1514,7 +1516,7 @@ EdgeMap(const EdgeMap &m) : DynMapBase(*m.G), container(m.container) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); } template friend class EdgeMap; @@ -1524,7 +1526,7 @@ template EdgeMap(const EdgeMap &m) : DynMapBase(*m.G) { - G->dyn_node_maps.push_back(this); + G->dyn_edge_maps.push_back(this); typename std::vector::const_iterator i; for(typename std::vector::const_iterator i=m.container.begin(); i!=m.container.end();