-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
- Loading branch information
saq10002
committed
Oct 11, 2014
1 parent
b5719e3
commit ff33a38
Showing
7 changed files
with
235 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.