1
+ #include " gtest/gtest.h"
2
+ #include < memory>
3
+ #include < fstream>
4
+ #include " Graph.h"
5
+
6
+ // graph source of tinyG.txt
7
+ const char * tinyG = R"( 13
8
+ 13
9
+ 0 5
10
+ 4 3
11
+ 0 1
12
+ 9 12
13
+ 6 4
14
+ 5 4
15
+ 0 2
16
+ 11 12
17
+ 9 10
18
+ 0 6
19
+ 7 8
20
+ 9 11
21
+ 5 3
22
+ )" ;
23
+
24
+ // read graph from constant
25
+ std::unique_ptr<graph::Graph> readGraph () {
26
+ std::istringstream iss (tinyG);
27
+ std::unique_ptr<graph::Graph> pGraph = std::make_unique<graph::AdjacencyListGraph>(iss);
28
+ return pGraph;
29
+ }
30
+
31
+ // not testing everything yet (WIP)
32
+
33
+ TEST (basic_test, basic_ops) { // NOLINT
34
+ auto pGraph = readGraph ();
35
+
36
+ EXPECT_NO_THROW (pGraph->toString ());
37
+ EXPECT_EQ (degree (*pGraph, 0 ), 4 );
38
+ EXPECT_EQ (degree (*pGraph, 1 ), 1 );
39
+ EXPECT_ANY_THROW (degree (*pGraph, 20 ));
40
+ EXPECT_EQ (graph::maxDegree (*pGraph), 4 );
41
+ EXPECT_EQ (graph::avgDegree (*pGraph), 2 );
42
+ EXPECT_EQ (graph::numSelfLoops (*pGraph), 0 );
43
+ }
44
+
45
+ TEST (basic_test, dfs) { // NOLINT
46
+ auto pGraph = readGraph ();
47
+
48
+ EXPECT_ANY_THROW (graph::depth_first_search::fromVertexToAll (*pGraph, 30 ));
49
+ const auto dfs_from_0 = graph::depth_first_search::fromVertexToAll (*pGraph, 0 );
50
+ EXPECT_FALSE (dfs_from_0.hasPathTo (10 ));
51
+ EXPECT_TRUE (dfs_from_0.hasPathTo (3 ));
52
+ EXPECT_GE (dfs_from_0.pathTo (3 ).size (), 3 );
53
+ }
54
+
55
+ TEST (basic_test, bfs) { // NOLINT
56
+ auto pGraph = readGraph ();
57
+
58
+ EXPECT_ANY_THROW (graph::breadth_first_search::fromVertexToAll (*pGraph, 30 ));
59
+ const auto bfs_from_0 = graph::breadth_first_search::fromVertexToAll (*pGraph, 0 );
60
+ EXPECT_FALSE (bfs_from_0.hasPathTo (10 ));
61
+ EXPECT_TRUE (bfs_from_0.hasPathTo (3 ));
62
+ EXPECT_EQ (bfs_from_0.pathTo (3 ).size (), 3 );
63
+ }
0 commit comments