Skip to content

Commit dc023cd

Browse files
committed
unit helper added for vector of pair
1 parent a5dbcdd commit dc023cd

File tree

10 files changed

+90
-22
lines changed

10 files changed

+90
-22
lines changed

Headers/0003_Graph/0001_BreadthFirstSearch.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include<list>
44
#include<map>
5+
#include<utility>
56
#include<string>
7+
#include<vector>
68
using namespace std;
79
enum color { WHITE, GRAY, BLACK };
810

@@ -26,5 +28,5 @@ class BFSGraph
2628
public:
2729
void PushUndirectedEdge(char valueU, char valueV);
2830
void BFS(char value);
29-
string ShowBFSResult();
31+
vector<pair<char, int>> ShowBFSResult();
3032
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include<list>
4+
#include<map>
5+
#include<string>
6+
using namespace std;
7+
enum color { WHITE, GRAY, BLACK };
8+
9+
class Node
10+
{
11+
public:
12+
char data;
13+
int distance;
14+
int color;
15+
Node* parent;
16+
Node(char value);
17+
};
18+
19+
class DFSGraph
20+
{
21+
private:
22+
map<Node*, list<Node*>> _adjlist;
23+
map<char, Node*> _nodeMap;
24+
Node* MakeOrFindNode(char value);
25+
void DepthFirstSearch(Node* node);
26+
public:
27+
void PushUndirectedEdge(char valueU, char valueV);
28+
void DFS(char value);
29+
string ShowDFSResult();
30+
};

SourceCodes/0003_Graph/0001_BreadthFirstSearch.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
22
#include<iostream>
33
#include<queue>
4+
#include<vector>
5+
#include<utility>
46
#include<string>
57
#include<climits>
68
using namespace std;
@@ -70,12 +72,12 @@ void BFSGraph::BFS(char value)
7072
this->BreadthFirstSearch(this->_nodeMap[value]);
7173
}
7274

73-
string BFSGraph::ShowBFSResult()
75+
vector<pair<char, int>> BFSGraph::ShowBFSResult()
7476
{
75-
string result = "";
76-
for (auto &node : this->_nodeMap)
77+
vector<pair<char, int>> result;
78+
for (auto& node : this->_nodeMap)
7779
{
78-
result = result + " " + node.first + "(" + to_string(node.second->distance) + ")";
80+
result.push_back(make_pair(node.first, node.second->distance));
7981
}
8082
return result;
8183
}

SourceCodes/0003_Graph/0002_DepthFirstSearch.cc

Whitespace-only changes.

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Specify the source files
22
set(0003GRAPH_SOURCES
33
0001_BreadthFirstSearch.cc
4+
0002_DepthFirstSearch.cc
45
)
56

67
# Create a library target

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ template<typename T>
99
class UnitTestHelper
1010
{
1111
public:
12-
string VerifyVectorResult(vector<T> vector)
12+
string VerifyVectorResult(vector<T>& vector)
1313
{
1414
string result = "";
15-
for (auto iterator : vector)
15+
for (auto& iterator : vector)
1616
{
1717
result += to_string(iterator) + " ";
1818
}
1919
result.pop_back();
2020
return result;
2121
}
22-
};
22+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include<iostream>
4+
#include<vector>
5+
#include<utility>
6+
#include<string>
7+
using namespace std;
8+
9+
template<typename T1, typename T2>
10+
class UnitTestHelperVectorOfPair
11+
{
12+
public:
13+
string VerifyVectorOfPair(vector<pair<T1,T2>> vector)
14+
{
15+
string result = "";
16+
for (auto& iterator : vector)
17+
{
18+
result += string(1, iterator.first) + "(" + to_string(iterator.second) + ")" + " ";
19+
}
20+
21+
if (!result.empty())
22+
{
23+
result.pop_back();
24+
}
25+
return result;
26+
}
27+
};

Tests/0003_Graph/0001_BreadthFirstSearchTest.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <gtest/gtest.h>
22
#include<string>
33
#include "../Headers/0003_Graph/0001_BreadthFirstSearch.h"
4+
#include "../0000_CommonUtilities/UnitTestHelperVectorOfPair.h"
45

5-
// Demonstrate some basic assertions.
66
namespace BreadthFirstSearchTest
77
{
8+
UnitTestHelperVectorOfPair<char, int> unitTestHelperVectorOfPair;
9+
810
TEST(BFSTesting, ShowBFSResultTest01)
911
{
1012
BFSGraph graph;
@@ -22,8 +24,8 @@ namespace BreadthFirstSearchTest
2224

2325
graph.BFS('s');
2426

25-
string actualResult = graph.ShowBFSResult();
26-
string expectedResult = " r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
27+
string actualResult = unitTestHelperVectorOfPair.VerifyVectorOfPair(graph.ShowBFSResult());
28+
string expectedResult = "r(1) s(0) t(2) u(3) v(2) w(1) x(2) y(3)";
2729
EXPECT_EQ(actualResult, expectedResult);
2830
}
2931

@@ -35,8 +37,8 @@ namespace BreadthFirstSearchTest
3537

3638
graph.BFS('s');
3739

38-
string actualResult = graph.ShowBFSResult();
39-
string expectedResult = " r(1) s(0)";
40+
string actualResult = unitTestHelperVectorOfPair.VerifyVectorOfPair(graph.ShowBFSResult());
41+
string expectedResult = "r(1) s(0)";
4042
EXPECT_EQ(actualResult, expectedResult);
4143
}
4244
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0002_DepthFirstSearch.h"
3+
4+
namespace DepthFirstSearchTest
5+
{
6+
7+
}

Tests/0003_Graph/CMakeLists.txt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ FetchContent_MakeAvailable(googletest)
1111
enable_testing()
1212

1313
add_executable(
14-
0003-Graph-Tests
15-
0001_BreadthFirstSearchTest.cc)
16-
17-
target_link_libraries(
18-
0003-Graph-Tests
19-
GTest::gtest_main
14+
0003GraphTests
15+
0001_BreadthFirstSearchTest.cc
16+
0002_DepthFirstSearchTest.cc
2017
)
2118

2219
target_link_libraries(
23-
0003-Graph-Tests
20+
0003GraphTests
21+
GTest::gtest_main
2422
0003GRAPH
2523
)
2624

27-
2825
include(GoogleTest)
29-
gtest_discover_tests(0003-Graph-Tests)
26+
gtest_discover_tests(0003GraphTests)

0 commit comments

Comments
 (0)