diff --git a/YASI_12/YASI_12.vcxproj b/YASI_12/YASI_12.vcxproj index 3d9ae4b..cf38240 100644 --- a/YASI_12/YASI_12.vcxproj +++ b/YASI_12/YASI_12.vcxproj @@ -74,6 +74,7 @@ + @@ -91,6 +92,7 @@ + diff --git a/YASI_12/YASI_12.vcxproj.filters b/YASI_12/YASI_12.vcxproj.filters index ccbf8b5..b0988e2 100644 --- a/YASI_12/YASI_12.vcxproj.filters +++ b/YASI_12/YASI_12.vcxproj.filters @@ -200,5 +200,11 @@ Header Files\Data Structures\Hash Table + + Header Files\Data Structures\Graph + + + Header Files + \ No newline at end of file diff --git a/YASI_12/alg.graph.h b/YASI_12/alg.graph.h index d6909a9..cc502dc 100644 --- a/YASI_12/alg.graph.h +++ b/YASI_12/alg.graph.h @@ -2,6 +2,10 @@ #include "common.h" #include "ds.graph.h" #include "ds.doublylinkedlist.h" + +// enable-disable testing classes in this file +#include "test.this.module.h" + using namespace std; namespace yasi{ @@ -17,7 +21,7 @@ namespace graph{ template // graph class BFS{ ////////////// enable testing ////////////// - friend class BFSTest; + FRIEND_TEST_CLASS( BFSTest); public: typedef typename G::VertexType VertexType; typedef typename G::EdgeType EdgeType; @@ -125,154 +129,6 @@ namespace graph{ - class BFSTest : public Test { - typedef Graph G; - typedef G::VertexType VertexType; - - template - class MyBFS : public BFS < G > { - ////////// enable testing ///////// - friend class BSFTest; - protected: - typedef BFS parent; - typedef typename parent::VertexType VertexType; - typedef typename parent::EdgeType EdgeType; - - stringstream visitSequence; - ///////////// override visit methods ///////////// - virtual void visitVertexBeforeSchedulingNeighbors(VertexType* v) override { - visitSequence << v->label << " "; - } - virtual void visitVertexAfterSchedulingNeighbors(VertexType* v) override {}; - virtual void visitEdge(EdgeType* e) override {}; - public: - MyBFS() : parent(){ - } - virtual ~MyBFS(){ - - } - string getVisitSequence() const{ - return visitSequence.str(); - } - }; - - - public: - void simpleGraphs(){ - - { - SCOPED_TRACE("path"); - string strPath = - "1 2 0.5\n" - "2 3 0.5\n" - "3 4 0.5\n" - "4 5 0.5\n" - ; - G g; - g.loadFromString(strPath); - string expectedVisitSequence = "1 2 3 4 5 "; - int bfsParent[] = {-1, 1, 2, 3, 4}; - - MyBFS bfs; - VertexType* src = *(g.getVertexList()->begin()); - bfs.search(g, src); - string actualVisitSequence = bfs.getVisitSequence(); - ASSERT_EQ(expectedVisitSequence, actualVisitSequence); - // bfs tree - for (int i = 1; i < g.numVertices(); i++){ - ASSERT_EQ(i - 1, bfs.pBfsParent[i]); - } - } - { - SCOPED_TRACE("binary tree"); - string strBalancedBinaryTree = - "1 2 0.5\n" - "1 3 0.5\n" - "2 4 0.5\n" - "2 5 0.5\n" - "3 6 0.5\n" - "3 7 0.5\n" - ; - G g; - g.loadFromString(strBalancedBinaryTree); - string expectedVisitSequence = "1 2 3 4 5 6 7 "; - - MyBFS bfs; - VertexType* src = *(g.getVertexList()->begin()); - bfs.search(g, src); - string actualVisitSequence = bfs.getVisitSequence(); - ASSERT_EQ(expectedVisitSequence, actualVisitSequence); - // bfs tree - for (int i = 1; i < g.numVertices(); i++){ - int parentId = (i%2) ? i/2 : i/2 - 1; - ASSERT_EQ(parentId, bfs.pBfsParent[i]); - } - } - - { - SCOPED_TRACE("cycle"); - string strCycle = - "1 2 0.5\n" - "2 3 0.5\n" - "3 4 0.5\n" - "4 1 0.5\n" - ; - G g; - g.loadFromString(strCycle); - string expectedVisitSequence = "1 2 3 4 "; - - MyBFS bfs; - VertexType* src = *(g.getVertexList()->begin()); - bfs.search(g, src); - string actualVisitSequence = bfs.getVisitSequence(); - ASSERT_EQ(expectedVisitSequence, actualVisitSequence); - // bfs tree - for (int i = 1; i < g.numVertices(); i++){ - ASSERT_EQ(i - 1, bfs.pBfsParent[i]); - } - } - { - SCOPED_TRACE("complete graph K5"); - string strK5 = - "1 5 0.5\n" - "1 2 0.5\n" - "1 4 0.5\n" - "1 3 0.5\n" - "2 1 0.5\n" - "2 3 0.5\n" - "2 4 0.5\n" - "2 5 0.5\n" - "3 2 0.5\n" - "3 1 0.5\n" - "3 4 0.5\n" - "3 5 0.5\n" - "4 2 0.5\n" - "4 3 0.5\n" - "4 1 0.5\n" - "4 5 0.5\n" - "5 2 0.5\n" - "5 3 0.5\n" - "5 4 0.5\n" - "5 1 0.5\n" - ; - G g; - g.loadFromString(strK5); - string expectedVisitSequence = "1 5 2 4 3 "; - - MyBFS bfs; - VertexType* src = *(g.getVertexList()->begin()); - bfs.search(g, src); - string actualVisitSequence = bfs.getVisitSequence(); - ASSERT_EQ(expectedVisitSequence, actualVisitSequence); - // bfs tree - const int expectedBfsParent[] = {-1, 0, 0, 0, 0}; // first vertex is parent of all others - ASSERT_EQ(true, arrcmp(expectedBfsParent, bfs.pBfsParent, g.numVertices())); - } - } - }; - - ADD_TEST_F(BFSTest, simpleGraphs); - } // namespace graph } } \ No newline at end of file diff --git a/YASI_12/alg.graph.test.h b/YASI_12/alg.graph.test.h new file mode 100644 index 0000000..535abaf --- /dev/null +++ b/YASI_12/alg.graph.test.h @@ -0,0 +1,162 @@ +#pragma once + +#if YASI_TEST_THIS_MODULE != 1 +#define YASI_TEST_THIS_MODULE 1 +#endif +#include "alg.graph.h" + +namespace yasi{ + namespace alg{ + + namespace graph{ + + class BFSTest : public Test { + typedef Graph G; + typedef G::VertexType VertexType; + + template + class MyBFS : public BFS < G > { + ////////// enable testing ///////// + FRIEND_TEST_CLASS(BFSTest); + protected: + typedef BFS parent; + typedef typename parent::VertexType VertexType; + typedef typename parent::EdgeType EdgeType; + + stringstream visitSequence; + ///////////// override visit methods ///////////// + virtual void visitVertexBeforeSchedulingNeighbors(VertexType* v) override { + visitSequence << v->label << " "; + } + virtual void visitVertexAfterSchedulingNeighbors(VertexType* v) override {}; + virtual void visitEdge(EdgeType* e) override {}; + public: + MyBFS() : parent(){ + } + virtual ~MyBFS(){ + + } + string getVisitSequence() const{ + return visitSequence.str(); + } + }; + + + public: + void simpleGraphs(){ + + { + SCOPED_TRACE("path"); + string strPath = + "1 2 0.5\n" + "2 3 0.5\n" + "3 4 0.5\n" + "4 5 0.5\n" + ; + G g; + g.loadFromString(strPath); + string expectedVisitSequence = "1 2 3 4 5 "; + int bfsParent[] = { -1, 1, 2, 3, 4 }; + + MyBFS bfs; + VertexType* src = *(g.getVertexList()->begin()); + bfs.search(g, src); + string actualVisitSequence = bfs.getVisitSequence(); + ASSERT_EQ(expectedVisitSequence, actualVisitSequence); + // bfs tree + for (int i = 1; i < g.numVertices(); i++){ + ASSERT_EQ(i - 1, bfs.pBfsParent[i]); + } + } + { + SCOPED_TRACE("binary tree"); + string strBalancedBinaryTree = + "1 2 0.5\n" + "1 3 0.5\n" + "2 4 0.5\n" + "2 5 0.5\n" + "3 6 0.5\n" + "3 7 0.5\n" + ; + G g; + g.loadFromString(strBalancedBinaryTree); + string expectedVisitSequence = "1 2 3 4 5 6 7 "; + + MyBFS bfs; + VertexType* src = *(g.getVertexList()->begin()); + bfs.search(g, src); + string actualVisitSequence = bfs.getVisitSequence(); + ASSERT_EQ(expectedVisitSequence, actualVisitSequence); + // bfs tree + for (int i = 1; i < g.numVertices(); i++){ + int parentId = (i % 2) ? i / 2 : i / 2 - 1; + ASSERT_EQ(parentId, bfs.pBfsParent[i]); + } + } + + { + SCOPED_TRACE("cycle"); + string strCycle = + "1 2 0.5\n" + "2 3 0.5\n" + "3 4 0.5\n" + "4 1 0.5\n" + ; + G g; + g.loadFromString(strCycle); + string expectedVisitSequence = "1 2 3 4 "; + + MyBFS bfs; + VertexType* src = *(g.getVertexList()->begin()); + bfs.search(g, src); + string actualVisitSequence = bfs.getVisitSequence(); + ASSERT_EQ(expectedVisitSequence, actualVisitSequence); + // bfs tree + for (int i = 1; i < g.numVertices(); i++){ + ASSERT_EQ(i - 1, bfs.pBfsParent[i]); + } + } + { + SCOPED_TRACE("complete graph K5"); + string strK5 = + "1 5 0.5\n" + "1 2 0.5\n" + "1 4 0.5\n" + "1 3 0.5\n" + "2 1 0.5\n" + "2 3 0.5\n" + "2 4 0.5\n" + "2 5 0.5\n" + "3 2 0.5\n" + "3 1 0.5\n" + "3 4 0.5\n" + "3 5 0.5\n" + "4 2 0.5\n" + "4 3 0.5\n" + "4 1 0.5\n" + "4 5 0.5\n" + "5 2 0.5\n" + "5 3 0.5\n" + "5 4 0.5\n" + "5 1 0.5\n" + ; + G g; + g.loadFromString(strK5); + string expectedVisitSequence = "1 5 2 4 3 "; + + MyBFS bfs; + VertexType* src = *(g.getVertexList()->begin()); + bfs.search(g, src); + string actualVisitSequence = bfs.getVisitSequence(); + ASSERT_EQ(expectedVisitSequence, actualVisitSequence); + // bfs tree + const int expectedBfsParent[] = { -1, 0, 0, 0, 0 }; // first vertex is parent of all others + ASSERT_EQ(true, arrcmp(expectedBfsParent, bfs.pBfsParent, g.numVertices())); + } + } + }; + + ADD_TEST_F(BFSTest, simpleGraphs); + } + } +} \ No newline at end of file diff --git a/YASI_12/ds.graph.h b/YASI_12/ds.graph.h index c05719b..b4bd394 100644 --- a/YASI_12/ds.graph.h +++ b/YASI_12/ds.graph.h @@ -4,6 +4,10 @@ #include #include "ds.doublylinkedlist.h" #include "ds.IntLinearProbingHashTable.h" + +// enable-disable testing classes in this file +#include "test.this.module.h" + using namespace std; namespace yasi{ @@ -57,7 +61,7 @@ class IGraph{ template class Graph : public IGraph{ /////////////// enable testing ///////////// - friend class GraphTest; + FRIEND_TEST_CLASS( GraphTest); public: typedef Vertex VertexType; typedef typename VertexType::LabelType VertexLabelType; @@ -220,47 +224,6 @@ class Graph : public IGraph{ template ostream& operator<<(ostream& out, const Graph& g){ out << g.toString(); return out; } -class GraphTest : public yasi::Test{ -public: - void loadFromString(){ - Graph g; - typedef Graph::VertexIterator VertexIterator; - - // simple triangle - string triangle = - "; this is comment\n" - "1 2 12\n" - "1 3 13\n" - "\n" - "; some other comments\n" - "2 3 23\n" - "; finishing comments"; - const string strTriangle = - "; Number of vertices\n" - "3\n" - "; Number of edges\n" - "3\n" - "; List of edges\n" - "1 2 12\n" - "1 3 13\n" - "2 3 23\n" - ; - g.loadFromString(triangle); - - // test - ASSERT_EQ(3, g.numVertices()); - ASSERT_EQ(3, g.numEdges()); - // now make sure that all id's are different, and they are in the range [1..n-1] - for (VertexIterator i = g.vertexList.begin(); i != g.vertexList.end(); i++){ - ASSERT_LE(0, (*i)->id); - ASSERT_GT(g.numVertices(), (*i)->id); - } - ASSERT_EQ(strTriangle, g.toString()); - } - -}; - -ADD_TEST_F(GraphTest, loadFromString); } // namespace ds } // namespace yasi \ No newline at end of file diff --git a/YASI_12/ds.graph.test.h b/YASI_12/ds.graph.test.h new file mode 100644 index 0000000..fa1d348 --- /dev/null +++ b/YASI_12/ds.graph.test.h @@ -0,0 +1,53 @@ +#pragma once + +#if YASI_TEST_THIS_MODULE != 1 +#define YASI_TEST_THIS_MODULE 1 +#endif +#include "ds.graph.h" + +namespace yasi{ + namespace ds{ + + class GraphTest : public yasi::Test{ + public: + void loadFromString(){ + Graph g; + typedef Graph::VertexIterator VertexIterator; + + // simple triangle + string triangle = + "; this is comment\n" + "1 2 12\n" + "1 3 13\n" + "\n" + "; some other comments\n" + "2 3 23\n" + "; finishing comments"; + const string strTriangle = + "; Number of vertices\n" + "3\n" + "; Number of edges\n" + "3\n" + "; List of edges\n" + "1 2 12\n" + "1 3 13\n" + "2 3 23\n" + ; + g.loadFromString(triangle); + + // test + ASSERT_EQ(3, g.numVertices()); + ASSERT_EQ(3, g.numEdges()); + // now make sure that all id's are different, and they are in the range [1..n-1] + for (VertexIterator i = g.vertexList.begin(); i != g.vertexList.end(); i++){ + ASSERT_LE(0, (*i)->id); + ASSERT_GT(g.numVertices(), (*i)->id); + } + ASSERT_EQ(strTriangle, g.toString()); + } + + }; + + ADD_TEST_F(GraphTest, loadFromString); + } +} \ No newline at end of file diff --git a/YASI_12/main.cpp b/YASI_12/main.cpp index 163949a..19e0ed9 100644 --- a/YASI_12/main.cpp +++ b/YASI_12/main.cpp @@ -1,9 +1,5 @@ #include "common.h" -//#include "ds.intlinearprobinghashtable.h" -//#include "ds.linearprobinghashtable.h" -//#include "ds.hopscotchhashtable.h" -//#include "ds.graph.h" //#include "alg.graph.h" //#include "Sorter.h" @@ -33,6 +29,8 @@ using namespace std; #include "ds.linearprobinghashtable.test.h" #include "ds.intlinearprobinghashtable.test.h" #include "ds.HopScotchHashTable.test.h" +#include "ds.graph.test.h" +#include "alg.graph.test.h" int testAll(int* argc, char** argv){