DBIO-PostgreSQL-PostGIS
view release on metacpan or search on metacpan
t/codec-wkt-parser.t view on Meta::CPAN
cmp_ok $cpu, '<', 1, "depth $depth is refused fast";
}
{
my $r = $p->parse(nested(32));
ok $r, 'depth 32 parses';
my $depth = 0;
my $g = $r;
while ($g->{type} eq 'geometrycollection') { $depth++; $g = $g->{coords}[0] }
is $depth, 32, '... all 32 levels are there';
is_deeply $g, { type => 'point', coords => [1, 1], is_empty => 0 }, '... down to the point';
my $ok = eval { $p->parse(nested(33)); 1 };
ok !$ok, 'depth 33 throws';
like "$@", qr/WKT nesting too deep/, '... naming the limit';
}
# Wide input costs time in proportion to its length. Growth is measured, not
# the wall clock: the same shape of input at n and at 8n, compared by CPU time
# (see CpuGrowth). Linear gives a ratio near 8, quadratic near 64; the limit
# 24 separates them. The sizes can be set for experiments.
my $N = $ENV{WKT_GROWTH_N} || 25_000;
my $MAX_RATIO = 24;
{
my ($ratio, $small, $big, $r) = growth_ratio(
sub { 'MULTIPOINT(' . join(',', map { "($_ 1)" } 1 .. $_[0]) . ')' },
sub { $p->parse($_[0]) }, $N, 8);
is scalar @{ $r->{coords} }, 8 * $N, 'MULTIPOINT with ' . 8 * $N . ' points';
is_deeply $r->{coords}[8 * $N - 1], [8 * $N, 1], '... the last one is read';
cmp_ok $ratio, '<', $MAX_RATIO, '... in linear time' or diag "ratio $ratio ($small s, $big s)";
($ratio, $small, $big, $r) = growth_ratio(
sub { 'GEOMETRYCOLLECTION(' . join(',', ('POINT(1 2)') x $_[0]) . ')' },
sub { $p->parse($_[0]) }, $N / 2, 8);
is scalar @{ $r->{coords} }, 4 * $N, 'GEOMETRYCOLLECTION with ' . 4 * $N . ' members';
cmp_ok $ratio, '<', $MAX_RATIO, '... in linear time' or diag "ratio $ratio ($small s, $big s)";
}
# A long run of whitespace is read in linear time, wherever it stands.
{
my $W = $ENV{WKT_GROWTH_W} || 1_000_000;
for my $case (
['between ordinates', sub { "POINT(1$_[0]2)" }],
['before a comma', sub { "LINESTRING(1 2$_[0],3 4)" }],
['after a comma', sub { "LINESTRING(1 2,$_[0]3 4)" }],
['around a ring', sub { "POLYGON(($_[0]0 0$_[0],1 0,1 1,0 0$_[0]))" }],
['in a MULTIPOINT', sub { "MULTIPOINT((1$_[0]2)$_[0],(3 4))" }],
) {
my ($name, $shape) = @$case;
my ($ratio, $small, $big, $r) = growth_ratio(
sub { $shape->(' ' x $_[0]) }, sub { $p->parse($_[0]) }, $W, 8);
ok $r, "8 MB of whitespace $name: read";
cmp_ok $ratio, '<', $MAX_RATIO, '... in linear time' or diag "ratio $ratio ($small s, $big s)";
}
}
# Ordinates are numbers, two to four per point; a point list has no empty entry.
for my $wkt ('POINT(abc def)', 'POINT((1 1))', 'POINT(1 2 3 4 5 6)', 'POINT(1 2 3 4 5)',
'POINT(1)', 'POINT(1 x)', 'POINT(1 2e)', 'POINT(1 --2)', 'POINT(0x10 1)',
'POINT(Inf NaN)', 'POINT(1e9999 0)', 'POINT(0 -1e9999)', 'POINT(. 1)', 'LINESTRING(0 0,1)',
'POLYGON((0 0,1 0,1 abc,0 0))', 'MULTIPOINT(1 2,3)', 'MULTIPOINT((1 2),(3 x))',
'MULTILINESTRING((0 0,1 1),(2 2,3 q))', 'MULTIPOLYGON(((0 0,1 0,1 1,z 0)))',
'LINESTRING(1 2,)', 'LINESTRING(0 0,1 1,)', 'LINESTRING(,0 0,1 1)', 'LINESTRING(0 0,,1 1)',
'GEOMETRYCOLLECTION(POINT(a b))') {
my @warn;
local $SIG{__WARN__} = sub { push @warn, @_ };
is $p->parse($wkt), undef, "$wkt: not read";
is scalar @warn, 0, '... without a warning';
}
for my $case (['1e5', 1e5], ['-.5', -0.5], ['3.', 3], ['1E-3', 0.001], ['+1', undef],
['2.5e+2', 250], ['-0', 0], ['.5E3', 500]) {
my ($tok, $want) = @$case;
my $r = $p->parse("POINT($tok 1)");
if (defined $want) {
is $r && $r->{coords}[0], $want, "POINT($tok 1): $tok is read";
} else {
is $r, undef, "POINT($tok 1): a leading plus is not WKT";
}
}
is_deeply $p->parse('LINESTRING(1e0 -.5 3.,0 0 0)')->{coords}, [[1, -0.5, 3], [0, 0, 0]],
'numbers of the several forms in one list';
done_testing;
( run in 1.319 second using v1.01-cache-2.11-cpan-60884a8df76 )