File: AllPathsSearchTest.cpp

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (181 lines) | stat: -rw-r--r-- 5,276 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Graph/Path.h"
#include "Graph/AllPathsSearch.h"
#include "Common/UnorderedMap.h"
#include <boost/graph/adjacency_list.hpp>
#include <gtest/gtest.h>
#include <set>

using namespace std;

typedef boost::adjacency_list<boost::vecS, boost::vecS,
	boost::bidirectionalS> Graph;

// note: vertex_descriptor for adjacency_list<> is int
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
// note: edge_descriptor for adjacency_list<> is int
typedef boost::graph_traits<Graph>::edge_descriptor Edge;

namespace {

class AllPathsSearchTest : public ::testing::Test {

protected:

	Graph disconnectedGraph;
	Graph simpleAcyclicGraph;
	Graph simpleCyclicGraph;
	Graph multiPathGraph;

	AllPathsSearchTest() {

		add_edge(0, 1, disconnectedGraph);
		add_vertex(disconnectedGraph);

		add_edge(0, 1, simpleAcyclicGraph);
		add_edge(0, 2, simpleAcyclicGraph);
		add_edge(2, 3, simpleAcyclicGraph);

		add_edge(0, 1, simpleCyclicGraph);
		add_edge(0, 4, simpleCyclicGraph);
		add_edge(1, 2, simpleCyclicGraph);
		add_edge(2, 1, simpleCyclicGraph);
		add_edge(1, 3, simpleCyclicGraph);

		add_edge(0, 1, multiPathGraph);
		add_edge(1, 2, multiPathGraph);
		add_edge(1, 3, multiPathGraph);
		add_edge(2, 3, multiPathGraph);
		add_edge(3, 4, multiPathGraph);
		add_edge(3, 5, multiPathGraph);
		add_edge(4, 5, multiPathGraph);
		add_edge(5, 6, multiPathGraph);

	}

};

TEST_F(AllPathsSearchTest, UnreachableGoal)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(disconnectedGraph, 0, 2);

	EXPECT_EQ(NO_PATH, result.resultCode);
	EXPECT_TRUE(result.paths.empty());
}

TEST_F(AllPathsSearchTest, StartNodeEqualsGoal)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(simpleAcyclicGraph, 0, 0);

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(1u, result.paths.size());
	ASSERT_EQ("0", result.paths[0].str());
}

TEST_F(AllPathsSearchTest, SinglePath)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(simpleAcyclicGraph, 0, 3, 1, 2, 2, NO_LIMIT);

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(1u, result.paths.size());
	ASSERT_EQ("0,2,3", result.paths[0].str());
}

TEST_F(AllPathsSearchTest, MultiPathGraph)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(multiPathGraph, 0, 6, 4, 4, 6, NO_LIMIT);

	set<string> expectedPaths;
	expectedPaths.insert("0,1,3,5,6");
	expectedPaths.insert("0,1,2,3,5,6");
	expectedPaths.insert("0,1,3,4,5,6");
	expectedPaths.insert("0,1,2,3,4,5,6");

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(4u, result.paths.size());

	// check that each path is one of the expected ones
	for (unsigned i = 0; i < 4; i++)
		ASSERT_TRUE(expectedPaths.find(result.paths[i].str()) !=
			expectedPaths.end());

	// check that each path is unique
	for (unsigned i = 0; i < 3; i++)
		ASSERT_TRUE(result.paths[i].str() != result.paths[i+1].str());
}

TEST_F(AllPathsSearchTest, RespectsMaxPathsLimit)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(multiPathGraph, 0, 6, 3, NO_LIMIT, NO_LIMIT, NO_LIMIT);
	EXPECT_EQ(TOO_MANY_PATHS, result.resultCode);
}

TEST_F(AllPathsSearchTest, RespectsMaxDepthLimit)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(multiPathGraph, 0, 6, 4, 4, 5, NO_LIMIT);

	// We expect the fourth path ("0,1,2,3,4,5,6")
	// to be excluded by the max depth limit.  Note that
	// the depth of the start node is 0, and so a
	// path of length 7 reaches depth 6.

	set<string> expectedPaths;
	expectedPaths.insert("0,1,3,5,6");
	expectedPaths.insert("0,1,2,3,5,6");
	expectedPaths.insert("0,1,3,4,5,6");

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(3u, result.paths.size());

	// check that each path is one of the expected ones
	for (unsigned i = 0; i < 3; i++)
		ASSERT_TRUE(expectedPaths.find(result.paths[i].str()) != expectedPaths.end());

	// check that each path is unique
	for (unsigned i = 0; i < 2; i++)
		ASSERT_TRUE(result.paths[i].str() != result.paths[i+1].str());
}

TEST_F(AllPathsSearchTest, RespectsMinDepthLimit)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(multiPathGraph, 0, 6, 4, 5, 6, NO_LIMIT);

	// We expect the shortest path ("0,1,3,4,6")
	// to be excluded by the min depth limit.  Note that
	// the depth of the start node is 0, and so a
	// path of length 5 reaches depth 4.

	set<string> expectedPaths;
	expectedPaths.insert("0,1,2,3,5,6");
	expectedPaths.insert("0,1,3,4,5,6");
	expectedPaths.insert("0,1,2,3,4,5,6");

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(3u, result.paths.size());

	// check that each path is one of the expected ones
	for (unsigned i = 0; i < 3; i++)
		ASSERT_TRUE(expectedPaths.find(result.paths[i].str()) != expectedPaths.end());

	// check that each path is unique
	for (unsigned i = 0; i < 2; i++)
		ASSERT_TRUE(result.paths[i].str() != result.paths[i+1].str());
}

TEST_F(AllPathsSearchTest, PathContainsCycle)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(simpleCyclicGraph, 0, 3,
		NO_LIMIT, 0, NO_LIMIT, NO_LIMIT);
	EXPECT_EQ(PATH_CONTAINS_CYCLE, result.resultCode);
}

TEST_F(AllPathsSearchTest, IgnoreCycleNotOnPath)
{
	AllPathsSearchResult<Vertex> result = allPathsSearch(simpleCyclicGraph, 0, 4,
		NO_LIMIT, 0, NO_LIMIT, NO_LIMIT);

	EXPECT_EQ(FOUND_PATH, result.resultCode);
	ASSERT_EQ(1u, result.paths.size());
	ASSERT_EQ("0,4", result.paths.front().str());
}

}