Skip to content

Commit 416536c

Browse files
committed
breadth first search tests added
1 parent 5418570 commit 416536c

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Headers/0003_Graph/0001_BreadthFirstSearch.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#pragma once
22

3-
#include<map>
43
#include<list>
4+
#include<map>
5+
#include<string>
56
using namespace std;
67
enum color { WHITE, GRAY, BLACK };
8+
79
class Node
810
{
911
public:
@@ -24,4 +26,5 @@ class BFSGraph
2426
public:
2527
void PushUndirectedEdge(char valueU, char valueV);
2628
void BFS(char value);
29+
string ShowBFSResult();
2730
};

SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
2+
#include<iostream>
23
#include<queue>
4+
#include<string>
35
using namespace std;
46

57
Node::Node(char value)
@@ -65,4 +67,14 @@ void BFSGraph::PushUndirectedEdge(char valueU, char valueV)
6567
void BFSGraph::BFS(char value)
6668
{
6769
this->BreadthFirstSearch(this->_nodeMap[value]);
70+
}
71+
72+
string BFSGraph::ShowBFSResult()
73+
{
74+
string result = "";
75+
for (auto value : this->_nodeMap)
76+
{
77+
result = result + " " + value.first + "(" + to_string(value.second->distance) + ")";
78+
}
79+
return result;
6880
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <gtest/gtest.h>
2+
#include<string>
3+
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
4+
5+
// Demonstrate some basic assertions.
6+
namespace BreadthFirstSearchTest
7+
{
8+
TEST(BFSTesting, ShowBFSResultTest) {
9+
BFSGraph graph;
10+
11+
graph.PushUndirectedEdge('s', 'r');
12+
graph.PushUndirectedEdge('s', 'w');
13+
graph.PushUndirectedEdge('r', 'v');
14+
graph.PushUndirectedEdge('w', 't');
15+
graph.PushUndirectedEdge('w', 'x');
16+
graph.PushUndirectedEdge('t', 'x');
17+
graph.PushUndirectedEdge('t', 'u');
18+
graph.PushUndirectedEdge('x', 'u');
19+
graph.PushUndirectedEdge('x', 'y');
20+
graph.PushUndirectedEdge('u', 'y');
21+
22+
graph.BFS('s');
23+
24+
string actualResult = graph.ShowBFSResult();
25+
string expectedResult = " r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
26+
EXPECT_EQ(actualResult, expectedResult);
27+
}
28+
}

0 commit comments

Comments
 (0)