Skip to content

Commit c723816

Browse files
committed
(pgtap) adding topologicalSort edge_cases test
1 parent d317eb6 commit c723816

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
2+
/*PGR-GNU*****************************************************************
3+
4+
Copyright (c) 2018 pgRouting developers
5+
6+
7+
------
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
********************************************************************PGR-GNU*/
20+
BEGIN;
21+
22+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
23+
SELECT CASE WHEN NOT min_version('3.0.0') THEN plan(1) ELSE plan(14) END;
24+
25+
CREATE OR REPLACE FUNCTION edge_cases()
26+
RETURNS SETOF TEXT AS
27+
$BODY$
28+
BEGIN
29+
30+
IF NOT min_version('3.0.0') THEN
31+
RETURN QUERY
32+
SELECT skip(1, 'Function is new on 3.0.0');
33+
RETURN;
34+
END IF;
35+
36+
-- empty graph
37+
38+
PREPARE q1 AS
39+
SELECT id, source, target, cost, reverse_cost
40+
FROM edges
41+
WHERE id > 18;
42+
43+
RETURN QUERY
44+
SELECT is_empty('q1', 'Graph with 0 edge and 0 vertex');
45+
46+
PREPARE r2 AS
47+
SELECT *
48+
FROM pgr_topologicalsort('q1');
49+
50+
RETURN QUERY
51+
SELECT is_empty('r2', 'Graph with 0 edge and 0 vertex -> Empty row is returned');
52+
53+
-- 1 vertex test
54+
55+
PREPARE q3 AS
56+
SELECT id, source, 6 AS target, cost, -1 AS reverse_cost
57+
FROM edges
58+
WHERE id = 2;
59+
60+
RETURN QUERY
61+
SELECT set_eq('q3', $$VALUES (2, 6, 6, -1, -1)$$, 'q3: Graph with one vertex');
62+
63+
PREPARE r4 AS
64+
SELECT *
65+
FROM pgr_topologicalsort('q3');
66+
67+
RETURN QUERY
68+
SELECT is_empty('r4', 'r4: Graph with one vertex -> NULL returned');
69+
70+
-- 2 vertices test (non DAG)
71+
72+
PREPARE q5 AS
73+
SELECT id, source, target, cost, reverse_cost
74+
FROM edges
75+
WHERE id = 7;
76+
77+
RETURN QUERY
78+
SELECT set_eq('q5', $$VALUES (7, 3, 7, 1, 1)$$, 'q5: Graph with two vertices (3->7 && 7->3)');
79+
80+
PREPARE r6 AS
81+
SELECT *
82+
FROM pgr_topologicalsort('q5');
83+
84+
RETURN QUERY
85+
SELECT throws_ok('r6', 'Graph is not DAG', 'r6: Graph with two vertices -> throws error');
86+
87+
-- 2 vertices test (connected)
88+
89+
PREPARE q7 AS
90+
SELECT id, source, target, cost, -1 AS reverse_cost
91+
FROM edges
92+
WHERE id = 7;
93+
94+
RETURN QUERY
95+
SELECT set_eq('q7', $$VALUES (7, 3, 7, 1, -1)$$, 'q7: Graph with two vertices (3->7)');
96+
97+
PREPARE r8 AS
98+
SELECT *
99+
FROM pgr_topologicalsort('q7');
100+
101+
RETURN QUERY
102+
SELECT set_eq('r8', $$VALUES (1, 3), (2, 7)$$, 'r8: Graph with two vertices -> return in order');
103+
104+
-- 2 vertices test (isolated)
105+
106+
CREATE TABLE two_isolated_vertices_table (
107+
id BIGSERIAL,
108+
source BIGINT,
109+
target BIGINT,
110+
cost FLOAT DEFAULT -1,
111+
reverse_cost FLOAT DEFAULT -1
112+
);
113+
114+
INSERT INTO two_isolated_vertices_table (source, target) VALUES
115+
(2, 2),
116+
(1, 1);
117+
118+
PREPARE q9 AS
119+
SELECT id, source, target, cost, reverse_cost
120+
FROM two_isolated_vertices_table;
121+
122+
RETURN QUERY
123+
SELECT set_eq('q9', $$VALUES (1, 2, 2, -1, -1), (2, 1, 1, -1, -1)$$, 'q9: Graph with two isolated vertices 1 and 2');
124+
125+
PREPARE r10 AS
126+
SELECT *
127+
FROM pgr_topologicalsort('q9');
128+
129+
RETURN QUERY
130+
SELECT is_empty('r10', 'r10: Graph with two isolated vertices -> NULL returned');
131+
132+
-- 3 vertices test
133+
134+
PREPARE q11 AS
135+
SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
136+
FROM edges
137+
WHERE id <= 2;
138+
139+
RETURN QUERY
140+
SELECT set_eq('q11', $$VALUES (2, 6, 10, 1, -1), (1, 5, 6, 1, -1)$$, 'q9: Graph with three vertices (5->6->10)');
141+
142+
PREPARE r12 AS
143+
SELECT *
144+
FROM pgr_topologicalsort('q11');
145+
146+
RETURN QUERY
147+
SELECT set_eq('r12', $$VALUES (1, 5), (2, 6), (3, 10)$$, 'r12: Graph with three vertices -> return in order');
148+
149+
-- 3 vertices test (a -> b, a -> c)
150+
151+
PREPARE q13 AS
152+
SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
153+
FROM edges
154+
WHERE source = 10;
155+
156+
RETURN QUERY
157+
SELECT set_eq('q13', $$VALUES (3, 10, 15, 1, -1), (5, 10, 11, 1, -1)$$, 'q13: Graph with three vertices (10 -> 11 && 10 -> 15)');
158+
159+
PREPARE r14 AS
160+
SELECT *
161+
FROM pgr_topologicalsort('q13');
162+
163+
RETURN QUERY
164+
SELECT set_eq('r14', $$VALUES (1, 10), (2, 15), (3, 11)$$, 'r14: Graph with three vertices -> return in order');
165+
166+
167+
END
168+
$BODY$
169+
LANGUAGE plpgsql;
170+
171+
SELECT edge_cases();
172+
173+
SELECT * FROM finish();
174+
ROLLBACK;

0 commit comments

Comments
 (0)