| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Geo::Hash; |
| 5 |
|
| 6 |
my @tests = ( |
| 7 |
{ |
| 8 |
hash => 'ezs42', |
| 9 |
pos => [ 42.6, -5.6 ], |
| 10 |
eps => 0.01, |
| 11 |
}, |
| 12 |
{ |
| 13 |
hash => 'mh7w', |
| 14 |
pos => [ -20, 50 ], |
| 15 |
eps => 0.1, |
| 16 |
}, |
| 17 |
{ |
| 18 |
hash => 't3b9m', |
| 19 |
pos => [ 10.1, 57.2 ], |
| 20 |
eps => 0.1, |
| 21 |
}, |
| 22 |
{ |
| 23 |
hash => 'c2b25ps', |
| 24 |
pos => [ 49.26, -123.26 ], |
| 25 |
eps => 0.01, |
| 26 |
}, |
| 27 |
{ |
| 28 |
hash => '80021bgm', |
| 29 |
pos => [ 0.005, -179.567 ], |
| 30 |
eps => 0.001, |
| 31 |
}, |
| 32 |
{ |
| 33 |
hash => 'k484ht99h2', |
| 34 |
pos => [ -30.55555, 0.2 ], |
| 35 |
eps => 0.00001, |
| 36 |
}, |
| 37 |
{ |
| 38 |
hash => '8buh2w4pnt', |
| 39 |
pos => [ 5.00001, -140.6 ], |
| 40 |
eps => 0.00001, |
| 41 |
}, |
| 42 |
); |
| 43 |
|
| 44 |
ok my $gh = Geo::Hash->new, "created new Geo::Hash object"; |
| 45 |
isa_ok $gh, 'Geo::Hash'; |
| 46 |
|
| 47 |
for my $test ( @tests ) { |
| 48 |
my ( $hash, $pos, $eps ) = @{$test}{qw(hash pos eps)}; |
| 49 |
is $gh->encode( @$pos, length $hash ), $hash, "$hash: encode"; |
| 50 |
|
| 51 |
{ |
| 52 |
my @got = $gh->decode( $hash ); |
| 53 |
ok abs( $got[$_] - $pos->[$_] ) < $eps, "$hash: decode $_" |
| 54 |
for 0 .. 1; |
| 55 |
} |
| 56 |
|
| 57 |
{ |
| 58 |
my $enc_hash = $gh->encode( @$pos ); |
| 59 |
ok abs( length( $enc_hash ) - length( $hash ) ) <= 1, |
| 60 |
"$hash: auto precision"; |
| 61 |
# diag "@$pos ($hash) -> $enc_hash"; |
| 62 |
my @got = $gh->decode( $enc_hash ); |
| 63 |
ok abs( $got[$_] - $pos->[$_] ) < $eps, "$hash: decode $_" |
| 64 |
for 0 .. 1; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
=for FUTURE |
| 69 |
|
| 70 |
my @bad_cases = ( |
| 71 |
{ |
| 72 |
pos => [ '35.21.03.342', '138.34.45.725' ], |
| 73 |
}, |
| 74 |
{ |
| 75 |
pos => [ '112', '138.34.45.725' ], |
| 76 |
}, |
| 77 |
{ |
| 78 |
pos => [ '35.21.03.342', '95' ], |
| 79 |
} |
| 80 |
); |
| 81 |
for my $test ( @bad_cases ) { |
| 82 |
my ( $pos ) = @{$test}{qw( pos )}; |
| 83 |
eval { |
| 84 |
$gh->encode( @$pos ); |
| 85 |
}; |
| 86 |
like $@, qr/encode\(\) only works on degrees, not dms values/, |
| 87 |
"@$pos is not encodable and dies with an error"; |
| 88 |
} |
| 89 |
|
| 90 |
=cut |
| 91 |
|
| 92 |
done_testing; |