File: edge_cases.pg

package info (click to toggle)
pgrouting 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,332 kB
  • sloc: cpp: 21,315; sql: 10,419; ansic: 9,795; perl: 1,142; sh: 919; javascript: 314; xml: 182; makefile: 29
file content (124 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download
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
/* :file: This file is part of the pgRouting project.
:copyright: Copyright (c) 2025-2026 pgRouting developers
:license: Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 */


BEGIN;

UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
SELECT CASE WHEN min_lib_version('3.4.0') THEN plan(8) ELSE plan(7) END;

CREATE OR REPLACE FUNCTION edge_cases()
RETURNS SETOF TEXT AS
$BODY$
BEGIN

IF NOT min_version('3.0.0') THEN
  RETURN QUERY
  SELECT skip(1, 'Function is new on 3.0.0');
  RETURN;
END IF;

-- empty graph

PREPARE q1 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, target, cost, reverse_cost
    FROM edges WHERE id > 18'
  );

RETURN QUERY
SELECT lives_ok('q1');

RETURN QUERY
SELECT is_empty('q1', 'graph with 0 edge and 0 vertex returns empty');

-- non DAG graph

PREPARE q2 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, target, cost, reverse_cost
    FROM edges WHERE id = 7'
  );

IF min_lib_version('3.4.0') THEN
  /* On v3.4 there is an additional check */
  RETURN QUERY SELECT throws_ok('q2', 'Graph is not DAG', 'throws is not DAG error');
END IF;

-- 1 vertex test

PREPARE q3 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, 6 AS target, cost, -1 AS reverse_cost
    FROM edges WHERE id = 2'
  );

RETURN QUERY
SELECT is_empty('q3', 'graph with one vertex returns empty');

-- 2 vertices test (connected)

PREPARE q4 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, target, cost, -1 AS reverse_cost
    FROM edges WHERE id = 7'
  );

RETURN QUERY
SELECT set_eq('q4', $$VALUES (1, 3), (2, 7)$$, 'graph with two connected vertices return correct order');

-- 2 vertices test (isolated)

CREATE TABLE two_isolated_vertices_table (
  id BIGSERIAL,
  source BIGINT,
  target BIGINT,
  cost FLOAT DEFAULT -1,
  reverse_cost FLOAT DEFAULT -1
);

INSERT INTO two_isolated_vertices_table (source, target) VALUES
  (2, 2),
  (1, 1);

PREPARE q5 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, target, cost, reverse_cost
    FROM two_isolated_vertices_table'
  );

RETURN QUERY
SELECT is_empty('q5', 'graph with two isolated vertices returns empty');

-- 3 vertices test (a -> b -> c)

PREPARE q6 AS
 SELECT * FROM pgr_topologicalsort(
   'SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
   FROM edges WHERE id <= 2'
 );

RETURN QUERY
SELECT set_eq('q6', $$VALUES (1, 5), (2, 6), (3, 10)$$, 'graph with three vertices returns correct order');

-- 3 vertices test (a -> {b, c})

PREPARE q7 AS
  SELECT * FROM pgr_topologicalsort(
    'SELECT id, source, target, 1 AS cost, -1 AS reverse_cost
    FROM edges WHERE source = 10'
  );

RETURN QUERY
SELECT set_eq('q7', $$VALUES (1, 10), (2, 15), (3, 11)$$, 'graph with one source and two targets returns correct order');


END
$BODY$
LANGUAGE plpgsql;

SELECT edge_cases();

SELECT * FROM finish();
ROLLBACK;