Skip to content

Commit fc5665e

Browse files
authored
Merge pull request #2877 from oddegen/test-topologicalSort
Test topological sort
2 parents 6f4397e + c721e72 commit fc5665e

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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(8) 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 * FROM pgr_topologicalsort(
40+
'SELECT id, source, target, cost, reverse_cost
41+
FROM edges WHERE id > 18'
42+
);
43+
44+
RETURN QUERY
45+
SELECT lives_ok('q1');
46+
47+
RETURN QUERY
48+
SELECT is_empty('q1', 'graph with 0 edge and 0 vertex returns empty');
49+
50+
-- non DAG graph
51+
52+
PREPARE q2 AS
53+
SELECT * FROM pgr_topologicalsort(
54+
'SELECT id, source, target, cost, reverse_cost
55+
FROM edges WHERE id = 7'
56+
);
57+
58+
RETURN QUERY
59+
SELECT throws_ok('q2', 'Graph is not DAG', 'throws is not DAG error');
60+
61+
-- 1 vertex test
62+
63+
PREPARE q3 AS
64+
SELECT * FROM pgr_topologicalsort(
65+
'SELECT id, source, 6 AS target, cost, -1 AS reverse_cost
66+
FROM edges WHERE id = 2'
67+
);
68+
69+
RETURN QUERY
70+
SELECT is_empty('q3', 'graph with one vertex returns empty');
71+
72+
-- 2 vertices test (connected)
73+
74+
PREPARE q4 AS
75+
SELECT * FROM pgr_topologicalsort(
76+
'SELECT id, source, target, cost, -1 AS reverse_cost
77+
FROM edges WHERE id = 7'
78+
);
79+
80+
RETURN QUERY
81+
SELECT set_eq('q4', $$VALUES (1, 3), (2, 7)$$, 'graph with two connected vertices return correct order');
82+
83+
-- 2 vertices test (isolated)
84+
85+
CREATE TABLE two_isolated_vertices_table (
86+
id BIGSERIAL,
87+
source BIGINT,
88+
target BIGINT,
89+
cost FLOAT DEFAULT -1,
90+
reverse_cost FLOAT DEFAULT -1
91+
);
92+
93+
INSERT INTO two_isolated_vertices_table (source, target) VALUES
94+
(2, 2),
95+
(1, 1);
96+
97+
PREPARE q5 AS
98+
SELECT * FROM pgr_topologicalsort(
99+
'SELECT id, source, target, cost, reverse_cost
100+
FROM two_isolated_vertices_table'
101+
);
102+
103+
RETURN QUERY
104+
SELECT is_empty('q5', 'graph with two isolated vertices returns empty');
105+
106+
-- 3 vertices test (a -> b -> c)
107+
108+
PREPARE q6 AS
109+
SELECT * FROM pgr_topologicalsort(
110+
'SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
111+
FROM edges WHERE id <= 2'
112+
);
113+
114+
RETURN QUERY
115+
SELECT set_eq('q6', $$VALUES (1, 5), (2, 6), (3, 10)$$, 'graph with three vertices returns correct order');
116+
117+
-- 3 vertices test (a -> {b, c})
118+
119+
PREPARE q7 AS
120+
SELECT * FROM pgr_topologicalsort(
121+
'SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
122+
FROM edges WHERE source = 10'
123+
);
124+
125+
RETURN QUERY
126+
SELECT set_eq('q7', $$VALUES (1, 10), (2, 15), (3, 11)$$, 'graph with one source and two targets returns correct order');
127+
128+
129+
END
130+
$BODY$
131+
LANGUAGE plpgsql;
132+
133+
SELECT edge_cases();
134+
135+
SELECT * FROM finish();
136+
ROLLBACK;

0 commit comments

Comments
 (0)