From 54703cb1224e6d67588d8c30b8f120d34dad86b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Thu, 18 Feb 2021 07:08:04 +0200 Subject: [PATCH] unit tests + "last bend" bugfix --- IV/tests.sql | 21 +++++++++++++++++---- IV/wm.sql | 7 +++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/IV/tests.sql b/IV/tests.sql index 8e05135..fe09dbd 100644 --- a/IV/tests.sql +++ b/IV/tests.sql @@ -1,10 +1,23 @@ +create schema if not exists test; \i wm.sql +-- https://stackoverflow.com/questions/19982373/which-tools-libraries-do-you-use-to-unit-test-your-pl-pgsql +CREATE OR REPLACE FUNCTION assert_equals(expected anyelement, actual anyelement) RETURNS void AS $$ +begin + if expected = actual or (expected is null and actual is null) then + --do nothing + else + raise exception 'Assertion Error. Expected <%> but was <%>', expected, actual; + end if; +end $$ LANGUAGE plpgsql; + drop table if exists figures; create table figures (name text, way geometry); insert into figures (name, way) values ('fig3', ST_GeomFromText('LINESTRING(0 0, 12 0, 13 4, 20 2, 20 0, 32 0, 33 10, 38 16, 43 15, 44 10, 44 0, 60 0)')); +insert into figures (name, way) values ('fig3-1', ST_GeomFromText('LINESTRING(0 0, 12 0, 13 4, 20 2, 20 0, 32 0, 33 10, 38 16, 43 15, 44 10, 44 0)')); -drop table if exists bends; -create table bends as (select * from detect_bends((select way from figures where name='fig3'))); - ---insert into figures (name, way) values ('Žeimena', (select st_linemerge(st_union(p.way)) from planet_osm_line p where p.name = 'Žeimena')) +do $$ +begin + perform assert_equals(3::bigint, (select count(1) from detect_bends((select way from figures where name='fig3')))); + perform assert_equals(3::bigint, (select count(1) from detect_bends((select way from figures where name='fig3-1')))); +end $$ language plpgsql; diff --git a/IV/wm.sql b/IV/wm.sql index a6c2194..ba1612e 100644 --- a/IV/wm.sql +++ b/IV/wm.sql @@ -36,5 +36,12 @@ begin end if; prev_sign = cur_sign; end loop; + + -- the last bend may be lost if there is no "final" inflection angle. + -- to avoid that, return the last bend if the last accumulation has >3 + -- vertices. + if (select count(1) from ((select st_dumppoints(bend) as a)) b) >= 3 then + return next; + end if; end $$ language plpgsql;