|
| 1 | + |
| 2 | +/*PGR-GNU***************************************************************** |
| 3 | + |
| 4 | +Copyright (c) 2025 pgRouting developers |
| 5 | +Mail: project@pgrouting.org |
| 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.4.0') THEN plan(1) ELSE plan(11) END; |
| 24 | + |
| 25 | + |
| 26 | +CREATE OR REPLACE FUNCTION edge_cases() |
| 27 | +RETURNS SETOF TEXT AS |
| 28 | +$BODY$ |
| 29 | +BEGIN |
| 30 | + |
| 31 | +IF NOT min_version('3.4.0') THEN |
| 32 | + RETURN QUERY |
| 33 | + SELECT skip(1, 'Function is new on 3.4.0'); |
| 34 | + RETURN; |
| 35 | +END IF; |
| 36 | + |
| 37 | +-- 0 edge, 0 vertex test |
| 38 | + |
| 39 | +PREPARE q1 AS |
| 40 | +SELECT id, source, target, cost, reverse_cost |
| 41 | +FROM edges |
| 42 | +WHERE id > 18; |
| 43 | + |
| 44 | +RETURN QUERY |
| 45 | +SELECT is_empty('q1', 'Graph with 0 edge and 0 vertex'); |
| 46 | + |
| 47 | +PREPARE r2 AS |
| 48 | +SELECT * FROM pgr_cuthillMckeeOrdering('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, 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_cuthillMckeeOrdering('q3'); |
| 66 | + |
| 67 | +RETURN QUERY |
| 68 | +SELECT set_eq('r4', $$VALUES (1, 6)$$, '4: Same node returned'); |
| 69 | + |
| 70 | + |
| 71 | +-- 2 vertices test (connected) |
| 72 | + |
| 73 | +PREPARE q5 AS |
| 74 | +SELECT id, source, target, cost, reverse_cost |
| 75 | +FROM edges |
| 76 | +WHERE id = 7; |
| 77 | + |
| 78 | +RETURN QUERY |
| 79 | +SELECT set_eq('q5', $$VALUES (7, 3, 7, 1, 1)$$, 'q5: 3->7'); |
| 80 | + |
| 81 | +PREPARE r6 AS |
| 82 | +SELECT * |
| 83 | +FROM pgr_cuthillMckeeOrdering('q5'); |
| 84 | + |
| 85 | +RETURN QUERY |
| 86 | +SELECT set_eq('r6', $$VALUES (1,3), (2,7)$$, 'Does not matter if 8 comes first or 5'); |
| 87 | + |
| 88 | +-- 2 vertices test (isolated) |
| 89 | + |
| 90 | +CREATE TABLE two_isolated_vertices_table ( |
| 91 | + id BIGSERIAL, |
| 92 | + source BIGINT, |
| 93 | + target BIGINT, |
| 94 | + cost FLOAT, |
| 95 | + reverse_cost FLOAT |
| 96 | +); |
| 97 | + |
| 98 | +INSERT INTO two_isolated_vertices_table (source, target, cost, reverse_cost) VALUES |
| 99 | + (2, 2, -1, 1), |
| 100 | + (1, 1, 1, -1); |
| 101 | + |
| 102 | +PREPARE q7 AS |
| 103 | +SELECT id, source, target, cost, reverse_cost |
| 104 | +FROM two_isolated_vertices_table; |
| 105 | + |
| 106 | +RETURN QUERY |
| 107 | +SELECT set_eq('q7', $$VALUES (1, 2, 2, -1, 1), (2, 1, 1, 1, -1)$$, 'q7: Graph with two isolated vertices 1 and 2'); |
| 108 | + |
| 109 | +PREPARE r8 AS |
| 110 | +SELECT * |
| 111 | +FROM pgr_cuthillMckeeOrdering('q7'); |
| 112 | + |
| 113 | +RETURN QUERY |
| 114 | +SELECT set_eq('r8', $$VALUES (1, 1), (2, 2)$$, 'Showing both vertex of disconnected graph'); |
| 115 | + |
| 116 | +-- 3 vertices test |
| 117 | + |
| 118 | +PREPARE q9 AS |
| 119 | +SELECT id, source, target, cost, reverse_cost |
| 120 | +FROM edges |
| 121 | +WHERE id <= 2; |
| 122 | + |
| 123 | +RETURN QUERY |
| 124 | +SELECT set_eq('q9', $$VALUES (2, 6, 10, -1, 1), (1, 5, 6, 1, 1)$$, 'q9: Graph with three vertices 1, 2 and 3'); |
| 125 | + |
| 126 | +PREPARE r10 AS |
| 127 | +SELECT * |
| 128 | +FROM pgr_cuthillMckeeOrdering('q9'); |
| 129 | + |
| 130 | +RETURN QUERY |
| 131 | +SELECT set_eq('r10', $$VALUES (1, 5), (2, 6), (3, 10)$$, 'Basic test'); |
| 132 | + |
| 133 | + |
| 134 | +-- pgRouting sample data |
| 135 | + |
| 136 | +CREATE TABLE expected_result ( |
| 137 | + seq BIGINT, |
| 138 | + node BIGINT); |
| 139 | + |
| 140 | +INSERT INTO expected_result (seq, node) VALUES |
| 141 | +(1, 13), |
| 142 | +(2, 14), |
| 143 | +(3, 2), |
| 144 | +(4, 4), |
| 145 | +(5, 1), |
| 146 | +(6, 9), |
| 147 | +(7, 3), |
| 148 | +(8, 8), |
| 149 | +(9, 5), |
| 150 | +(10, 7), |
| 151 | +(11, 12), |
| 152 | +(12, 6), |
| 153 | +(13, 11), |
| 154 | +(14, 17), |
| 155 | +(15, 10), |
| 156 | +(16, 16), |
| 157 | +(17, 15); |
| 158 | + |
| 159 | +PREPARE q11 AS |
| 160 | +SELECT * FROM pgr_cuthillMckeeOrdering( |
| 161 | + 'SELECT id, source, target, cost, reverse_cost FROM edges'); |
| 162 | + |
| 163 | +PREPARE r11 AS |
| 164 | +SELECT * FROM expected_result; |
| 165 | + |
| 166 | +IF version() LIKE '%SQL 14%' THEN |
| 167 | + PERFORM todo('test failing with postgres 14',1); |
| 168 | +END IF; |
| 169 | +RETURN QUERY SELECT set_eq('q11','r11','sample data graph of pgRouting'); |
| 170 | + |
| 171 | +END; |
| 172 | +$BODY$ |
| 173 | +LANGUAGE plpgsql; |
| 174 | + |
| 175 | +SELECT edge_cases(); |
| 176 | + |
| 177 | + |
| 178 | +SELECT * FROM finish(); |
| 179 | +ROLLBACK; |
0 commit comments