Data-SpatialHash-Shared
view release on metacpan or search on metacpan
t/21-frozen.t view on Meta::CPAN
# ---- refuse a read-write reopen of a sealed file ----
like exception(sub { Data::SpatialHash::Shared->new($path, 2000, 0, 1.0) }),
qr/frozen|read-only/, 'read-write reopen of a sealed file is refused';
# ---- new_from_fd also refuses a sealed file opened read-write ----
{
open(my $fh, '+<', $path) or die "open $path: $!";
like exception(sub { Data::SpatialHash::Shared->new_from_fd(fileno($fh)) }),
qr/frozen|read-only/, 'new_from_fd on a sealed file is refused';
}
# ---- new_readonly rejects a non-frozen file ----
{
my $u = "$dir/unsealed.sph";
{ my $s = Data::SpatialHash::Shared->new($u, 100, 0, 1.0); $s->insert(1, 1, 1); }
like exception(sub { Data::SpatialHash::Shared->new_readonly($u) }),
qr/not frozen/, 'new_readonly on an unsealed file croaks';
}
# ---- new_readonly error paths ----
like exception(sub { Data::SpatialHash::Shared->new_readonly("$dir/does-not-exist.sph") }),
qr/open|No such/, 'new_readonly on a missing path croaks';
like exception(sub { Data::SpatialHash::Shared->new_readonly(undef) }),
qr/required/, 'new_readonly requires a path';
# ---- spherical (geo) world: producer/consumer parity for the geo read paths ----
{
my $gpath = "$dir/geo.sph";
my $geh;
my ($exp_geo_pos, $exp_geo_radius);
{
# cell_size 5km; points spread ~5-55km apart (lat/lon steps of a fraction
# of a radian); query dist 30km -> per-axis cell span well under the cap.
my $g = Data::SpatialHash::Shared->new($gpath, 200, 0, 5_000, sphere => 6_371_000);
for my $i (0 .. 19) {
my $v = $g->insert_geo(0.0005 * ($i - 10), 0.0007 * ($i - 10), 500, 5000 + $i);
$geh = $v if $i == 10;
}
$exp_geo_pos = [ $g->position_geo($geh) ];
$exp_geo_radius = S([ $g->query_geo_radius(0, 0, 500, 30_000) ]);
ok scalar(@$exp_geo_radius) > 1, 'sanity: geo radius query matches more than one point';
$g->freeze;
like exception(sub { $g->insert_geo(0, 0, 0, 1) }), qr/frozen|read-only/, 'insert_geo on frozen geo handle croaks';
like exception(sub { $g->move_geo($geh, 0, 0, 0) }), qr/frozen|read-only/, 'move_geo on frozen geo handle croaks';
}
my $gro = Data::SpatialHash::Shared->new_readonly($gpath);
ok $gro->frozen && $gro->readonly, 'geo read-only view reports frozen/readonly';
my @got_geo_pos = $gro->position_geo($geh);
for my $i (0 .. 2) {
cmp_ok abs($got_geo_pos[$i] - $exp_geo_pos->[$i]), '<', 1e-9, "position_geo[$i] matches producer via read-only view";
}
is_deeply S([ $gro->query_geo_radius(0, 0, 500, 30_000) ]), $exp_geo_radius,
'query_geo_radius matches producer via read-only view';
like exception(sub { $gro->insert_geo(0, 0, 0, 1) }), qr/read-only/, 'insert_geo on read-only geo view croaks';
like exception(sub { $gro->move_geo($geh, 0, 0, 0) }), qr/read-only/, 'move_geo on read-only geo view croaks';
}
done_testing;
# minimal exception helper (avoid a Test::Fatal dependency)
sub exception {
my $code = shift;
my $err;
{ local $@; eval { $code->(); 1 } or $err = $@; }
return $err;
}
( run in 0.848 second using v1.01-cache-2.11-cpan-54e63673c56 )