Skip to content

Commit

Permalink
Separated test from graph.
Browse files Browse the repository at this point in the history
Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
  • Loading branch information
saq10002 committed Oct 11, 2014
1 parent b5719e3 commit ff33a38
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 195 deletions.
2 changes: 2 additions & 0 deletions YASI_12/YASI_12.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="alg.graph.h" />
<ClInclude Include="alg.graph.test.h" />
<ClInclude Include="common.h" />
<ClInclude Include="ds.arraybinarytree.h" />
<ClInclude Include="ds.arraybinarytree.test.h" />
Expand All @@ -91,6 +92,7 @@
<ClInclude Include="ds.doublylinkedlist.h" />
<ClInclude Include="ds.doublylinkedlist.test.h" />
<ClInclude Include="ds.graph.h" />
<ClInclude Include="ds.graph.test.h" />
<ClInclude Include="ds.hashtablebase.h" />
<ClInclude Include="ds.HopScotchHashTable.h" />
<ClInclude Include="ds.HopScotchHashTable.test.h" />
Expand Down
6 changes: 6 additions & 0 deletions YASI_12/YASI_12.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,11 @@
<ClInclude Include="ds.HopScotchHashTable.test.h">
<Filter>Header Files\Data Structures\Hash Table</Filter>
</ClInclude>
<ClInclude Include="ds.graph.test.h">
<Filter>Header Files\Data Structures\Graph</Filter>
</ClInclude>
<ClInclude Include="alg.graph.test.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
154 changes: 5 additions & 149 deletions YASI_12/alg.graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -17,7 +21,7 @@ namespace graph{
template<class G> // graph
class BFS{
////////////// enable testing //////////////
friend class BFSTest;
FRIEND_TEST_CLASS( BFSTest);
public:
typedef typename G::VertexType VertexType;
typedef typename G::EdgeType EdgeType;
Expand Down Expand Up @@ -125,154 +129,6 @@ namespace graph{



class BFSTest : public Test {
typedef Graph<int,char> G;
typedef G::VertexType VertexType;

template<class G>
class MyBFS : public BFS < G > {
////////// enable testing /////////
friend class BSFTest;
protected:
typedef BFS<G> 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<G> 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<G> 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<G> 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<G> 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<int>(expectedBfsParent, bfs.pBfsParent, g.numVertices()));
}
}
};

ADD_TEST_F(BFSTest, simpleGraphs);

} // namespace graph
}
}
162 changes: 162 additions & 0 deletions YASI_12/alg.graph.test.h
Original file line number Diff line number Diff line change
@@ -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<int, char> G;
typedef G::VertexType VertexType;

template<class G>
class MyBFS : public BFS < G > {
////////// enable testing /////////
FRIEND_TEST_CLASS(BFSTest);
protected:
typedef BFS<G> 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<G> 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<G> 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<G> 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<G> 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<int>(expectedBfsParent, bfs.pBfsParent, g.numVertices()));
}
}
};

ADD_TEST_F(BFSTest, simpleGraphs);
}
}
}
Loading

0 comments on commit ff33a38

Please sign in to comment.