-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadjacency_list.cpp
124 lines (100 loc) · 3.36 KB
/
adjacency_list.cpp
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
/*
* Copyright (C) 2011 Leo Osvald <leo.osvald@gmail.com>
*
* This file is part of KSP Library.
*
* KSP Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KSP Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <string>
#include <sstream>
#include "../src/adjacency_list.hpp"
#include "../src/graph_builder.hpp"
#include "test.hpp"
#ifndef SKIP_TESTS
namespace ksp {
namespace test {
typedef GraphBuilder<int> MyGraph;
typedef MyGraph::EdgeType MyEdge;
typedef AdjacencyList<> MyAdjList;
struct MyEdgeLess {
bool operator()(const MyEdge& x, const MyEdge& y) const {
return x.tail < y.tail || (x.tail == y.tail &&
(x.head < y.head || (x.head == y.head &&
x.data < y.data)));
}
};
struct MyEdgeEq {
bool operator()(const MyEdge& x, const MyEdge& y) const {
return x.tail == y.tail && x.head == y.head && x.data == y.data;
}
};
void ExpectAdj(MyGraph g, const MyAdjList& a, MyGraph::VertexType v,
MyEdge* exp_first, MyEdge* exp_last) {
std::vector<MyEdge> act_edges(a.first(g.vertex_id(v)),
a.last(g.vertex_id(v)));
std::vector<MyEdge> exp_edges(exp_first, exp_last);
for (std::size_t i = 0; i < exp_edges.size(); ++i) {
exp_edges[i].tail = g.vertex_id(exp_edges[i].tail);
exp_edges[i].head = g.vertex_id(exp_edges[i].head);
}
if (!CheckSortedEq(exp_edges.begin(), exp_edges.end(),
act_edges.begin(), act_edges.end(),
MyEdgeLess(), MyEdgeEq())) {
EXPECT_TRUE(false);
std::cout << " Actual:\n";
for (std::size_t i = 0; i < exp_edges.size(); ++i)
std::cout << act_edges[i] << " ";
std::cout << "\nExpected:\n";
for (std::size_t i = 0; i < exp_edges.size(); ++i)
std::cout << exp_edges[i] << " ";
std::cout << "\n";
}
}
TEST(adjacency_list, simple01) {
MyGraph g;
g.AddVertex(1);
g.AddVertex(2);
g.AddVertex(3);
g.AddEdge(1, 2, 3);
g.AddEdge(3, 1, 4);
g.AddEdge(3, 3, 5);
MyAdjList a(g.edges_begin(), g.edges_end());
MyEdge a1[] = {MyEdge(1, 2, 3)};
ExpectAdj(g, a, 1, a1, a1 + 1);
ExpectAdj(g, a, 2, a1, a1);
MyEdge a3[] = {MyEdge(3, 1, 4), MyEdge(3, 3, 5)};
ExpectAdj(g, a, 3, a3, a3 + 2);
}
TEST(adjacency_list, auto01) {
int n = 100;
MyGraph g;
std::vector<int> vs(n);
for (int i = 0; i < n; ++i)
vs[i] = i;
std::random_shuffle(vs.begin(), vs.end());
for (int i = 0; i < n; ++i)
g.AddVertex(i);
for (int j = 0; j < n; ++j)
for (int i = n - 1; i >= 0; --i)
if (i >= j * j)
g.AddEdge(i, j, 1);
MyAdjList a(g.edges_begin(), g.edges_end());
MyEdge a35[] = {MyEdge(35, 0, 1), MyEdge(35, 1, 1), MyEdge(35, 2, 1),
MyEdge(35, 3, 1), MyEdge(35, 4, 1), MyEdge(35, 5, 1)};
ExpectAdj(g, a, 35, a35, a35 + 6);
}
} // namespace test
} // namespace ksp
#endif