Skip to content

Commit c6dbd17

Browse files
committed
Add gtest and Travis CI setup
1 parent 037939d commit c6dbd17

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea/
22
cmake-build-debug/
33
cmake-build-release/
4+
build/
45

56
# Prerequisites
67
*.d

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "submodules/googletest"]
2+
path = submodules/googletest
3+
url = https://github.com/google/googletest/

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dist: focal
2+
language: cpp
3+
addons:
4+
apt:
5+
packages:
6+
- build-essential
7+
8+
script:
9+
- echo "C++ tests"
10+
- mkdir build
11+
- cd build
12+
- cmake ..
13+
- make
14+
- test/graphs_cpp_gtest
15+

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12+
include_directories(.)
13+
1214
add_executable(graphs_test main.cpp)
15+
16+
add_subdirectory(submodules/googletest)
17+
add_subdirectory(test)

submodules/googletest

Submodule googletest added at 389cb68

test/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(BINARY ${CMAKE_PROJECT_NAME}_gtest)
2+
3+
file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.h *.cpp)
4+
5+
set(SOURCES ${TEST_SOURCES})
6+
7+
add_executable(${BINARY} ${TEST_SOURCES})
8+
9+
add_test(NAME ${BINARY} COMMAND ${BINARY})
10+
11+
target_link_libraries(${BINARY} PUBLIC gtest)

test/basic_test.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

test/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "gtest/gtest.h"
2+
3+
int main(int argc, char **argv) {
4+
::testing::InitGoogleTest(&argc, argv);
5+
return RUN_ALL_TESTS();
6+
}

0 commit comments

Comments
 (0)