Skip to content
This repository was archived by the owner on May 19, 2020. It is now read-only.

Conversation

@nyurik
Copy link
Member

@nyurik nyurik commented Mar 24, 2020

Current implementation crashes if the value is a single '-' or '+' char, or in other edge cases like 'e2'.
The code has been rewritten from openmaptiles/openmaptiles#785 (comment) suggestion based on @RhodiumToad suggested code.

This change was fully tested locally using npm test

P.S. Admins, please enable travis-ci build for this repo.

cc @jsanz @sfkeller

Current implementation crashes if the value is a single '-' char.
This change was fully tested locally using `npm test`
@nyurik nyurik changed the title Fix CleanNumeric and CleanInt handling of '-' Fix CleanNumeric and CleanInt edge case crashes Mar 24, 2020
@jsanz
Copy link

jsanz commented Mar 25, 2020

tested locally, and everything works fine, many thanks @nyurik!! It'd definitely be great to enable Travis to have these tests automated.

I'll share my working steps anyways in case anyone is interested:

  1. Start a disposable server, I wanted to use PostgreSQL 12.2 and PostGIS 3.0:
$ docker run --rm -p 5432:5432 \
  -e POSTGRES_USER=j \
  -e POSTGRES_PASS=j \
  -e POSTGRES_DBNAME=testing_postgis_vt_util \
  kartoza/postgis
  1. Load the functions:
$ PGHOST=localhost \
  PGPASSWORD=j \
  PGUSER=j \
  PGDATABASE=testing_postgis_vt_util \
  psql -f postgis-vt-util.sql
  1. Run the tests:
$ PGHOST=localhost \
  PGPASSWORD=j \
  PGUSER=j \
  PGDATABASE=testing_postgis_vt_util \
  POSTGIS_VERSION="3" \
  npm test
Test results
> @mapbox/postgis-vt-util@1.2.1 test /home/j/src/openmaptiles/postgis-vt-util
> test/sql-test.sh

TAP version 13
1..103
ok 1 - Bounds(ST_GeomFromText('LINESTRING(100 100, 300 300)', 3857))
ok 2 - (select array_agg(i) from     (select round(unnest(Bounds(ST_GeomFromText('LINESTRING(100 100, 300 300)', 3857), 4326))::numeric,8) as i) _)
ok 3 - CleanInt(null)
ok 4 - CleanInt('.')
ok 5 - CleanInt('')
ok 6 - CleanInt('-')
ok 7 - CleanInt('+')
ok 8 - CleanInt('foobar')
ok 9 - CleanInt('e')
ok 10 - CleanInt('E')
ok 11 - CleanInt('e2')
ok 12 - CleanInt('E2')
ok 13 - CleanInt('.e')
ok 14 - CleanInt('.E')
ok 15 - CleanInt('1e')
ok 16 - CleanInt('1E')
ok 17 - CleanInt('1.e')
ok 18 - CleanInt('1.E')
ok 19 - CleanInt('.e2')
ok 20 - CleanInt('.E2')
ok 21 - CleanInt('a123')
ok 22 - CleanInt('123a')
ok 23 - CleanInt('9999999999')
ok 24 - CleanInt('-9999999999')
ok 25 - CleanInt('123')
ok 26 - CleanInt('+42')
ok 27 - CleanInt('+42.123')
ok 28 - CleanInt('123.456')
ok 29 - CleanInt('456.789')
ok 30 - CleanInt('  456.789   ')
ok 31 - CleanInt('456.789e2')
ok 32 - CleanInt('-2147483649')
ok 33 - CleanInt('2147483648')
ok 34 - CleanInt('2147483647')
ok 35 - CleanInt('-2147483648')
ok 36 - CleanNumeric(null)
ok 37 - CleanNumeric('.')
ok 38 - CleanNumeric('')
ok 39 - CleanNumeric('-')
ok 40 - CleanNumeric('+')
ok 41 - CleanNumeric('foobar')
ok 42 - CleanNumeric('e')
ok 43 - CleanNumeric('E')
ok 44 - CleanNumeric('e2')
ok 45 - CleanNumeric('E2')
ok 46 - CleanNumeric('.e')
ok 47 - CleanNumeric('.E')
ok 48 - CleanNumeric('1e')
ok 49 - CleanNumeric('1E')
ok 50 - CleanNumeric('1.e')
ok 51 - CleanNumeric('1.E')
ok 52 - CleanNumeric('.e2')
ok 53 - CleanNumeric('.E2')
ok 54 - CleanNumeric('a123')
ok 55 - CleanNumeric('123a')
ok 56 - CleanNumeric('9999999999')
ok 57 - CleanNumeric('-9999999999')
ok 58 - CleanNumeric('123')
ok 59 - CleanNumeric('+42')
ok 60 - CleanNumeric('+42.123')
ok 61 - CleanNumeric('123.456')
ok 62 - CleanNumeric('456.789')
ok 63 - CleanNumeric('  456.789   ')
ok 64 - CleanNumeric('456.789e2')
ok 65 - LabelGrid(ST_GeomFromText('POINT(100 -100)',900913), 64*9.5546285343)
ok 66 - LargestPart(ST_GeomFromText('POLYGON((10 10, 10 20, 20 20, 20 10, 10 10))'))
ok 67 - LargestPart(ST_GeomFromText('MULTIPOLYGON(    ((-20 -20, -20 20, -10 20, -10 -20, -20 -20)),    ((10 10, 10 20, 20 20, 20 10, 10 10)))'))
ok 68 - LargestPart(ST_GeomFromText('LINESTRING(0 0, 10 0)'))
ok 69 - LargestPart(ST_GeomFromText('MULTILINESTRING((0 0, 10 0),(0 10, 20 10))'))
ok 70 - LargestPart(ST_GeomFromText('GEOMETRYCOLLECTION(LINESTRING(0 0, 0 10),    POLYGON((10 10, 10 20, 20 20, 20 10, 10 10)))'))
ok 71 - LineLabel(14, 'Foobar', ST_GeomFromText('POINT(0 0)',900913))
ok 72 - LineLabel(14, 'Foobar', ST_GeomFromText('LINESTRING(0 0, 0 300)',900913))
ok 73 - LineLabel(15, 'Foobar', ST_GeomFromText('LINESTRING(0 0, 0 300)',900913))
ok 74 - round(ST_Length(MakeArc(ST_MakePoint(0,0), ST_MakePoint(20,10), ST_MakePoint(40,0)))::numeric,4)
ok 75 - round(ST_Area(MercBuffer(ST_GeomFromText('LINESTRING(0 0, 1000 1000)', 900913), 500))::numeric,4)
ok 76 - round(ST_Area(MercBuffer(ST_GeomFromText('POINT(0 8500000)', 900913), 500))::numeric,4)
ok 77 - MercDWithin(ST_GeomFromText('POINT(0 0)',3857), ST_GeomFromText('POINT(60 0)',3857), 50.0)
ok 78 - MercDWithin(ST_GeomFromText('POINT(0 8500000)',3857), ST_GeomFromText('POINT(60 8500000)',3857), 50.0)
ok 79 - MercLength(ST_GeomFromText('LINESTRING(0 0, 10000 0)', 900913))
ok 80 - MercLength(ST_GeomFromText('LINESTRING(0 8500000, 10000 8500000)', 900913))
ok 81 - ST_AsText(ST_SnapToGrid(OrientedEnvelope(ST_GeomFromText('LINESTRING(0 0, 10 10, 8 12)')),0.001))
ok 82 - ST_AsText(Sieve(ST_GeomFromText('MULTIPOLYGON(    ((0 0,0 100,100 100,100 0,0 0),(10 10,12 10,12 12,10 12,10 10),(50 50,60 50,60 60,50 60,50 50)),    ((200 200,200 202,202 202,202 200,200 200)),    ((300 300,300 350,350 350,350 300,300 300)))'),10))
ok 83 - round(ST_Area(SmartShrink(ST_Buffer(ST_MakePoint(0,0),5000),0.5,true))::numeric,4)
ok 84 - round(ST_Area(SmartShrink(ST_Buffer(ST_MakePoint(0,0),5000),0.5,false))::numeric,4)
ok 85 - ST_AsText(ST_SnapToGrid(TileBBox(0,0,0),0.01))
ok 86 - ST_AsText(ST_SnapToGrid(TileBBox(11,585,783),0.01))
ok 87 - ST_AsText(ST_SnapToGrid(TileBBox(0,0,0,4326),0.01))
ok 88 - ToPoint(ST_GeomFromText('POINT(0 0)',900913))
ok 89 - ToPoint(ST_GeomFromText('POLYGON EMPTY',900913))
ok 90 - ToPoint(ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))',900913))
ok 91 - ToPoint(ST_GeomFromText('POLYGON((0 0, 10 0, 0 10, 10 10, 0 0))',900913))
ok 92 - ToPoint(ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))',900913))
ok 93 - ToPoint(ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((20 20, 30 20, 30 30, 20 30, 20 20)))',900913))
ok 94 - ST_AsText(ToPoint(ST_GeomFromText('POLYGON((50 5,10 8,10 10,100 190,150 30,150 10,50 5))',900913)))
ok 95 - Z(1000000000)
ok 96 - Z(500000000)
ok 97 - Z(1000)
ok 98 - Z(0)
ok 99 - Z(NULL)
ok 100 - round(ZRes(0)::numeric,4)
ok 101 - round(ZRes(19)::numeric,4)
ok 102 - round(ZRes(0.5)::numeric,4)
ok 103 - ZRes(NULL)
# ok - 103 / 103 tests passed

@nyurik nyurik merged commit 23da437 into openmaptiles:master Mar 31, 2020
@nyurik nyurik deleted the fix-cleannum branch March 31, 2020 17:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants