Data-HashMap

 view release on metacpan or  search on metacpan

t/22-bugfixes.t  view on Meta::CPAN

    is($CountOnce::n, 1, 'II put_ttl FETCHes a tied ttl once');
    my $s = Data::HashMap::SS->new();
    $CountOnce::n = 0;
    hm_ss_put_ttl $s, 'k', 'v', $t;
    is($CountOnce::n, 1, 'SS put_ttl FETCHes a tied ttl once');

    my $f;
    tie $f, 'FlipFlop';
    local @FlipFlop::v = (30, -3);
    my $fm = Data::HashMap::II->new(0, $f);
    is($fm->ttl, 30, 'ttl uses a single settled read (no flip-flop bypass)');

    my $bad;
    tie $bad, 'FlipFlop';
    local @FlipFlop::v = ('abc');
    eval { Data::HashMap::II->new(0, $bad) };
    like($@, qr/must be a number/, 'tied non-numeric ttl croaks like a plain one');
}

{
    package CountBad;
    our $n;
    sub TIESCALAR { bless {}, $_[0] }
    sub FETCH { $n++; 'abc' }
    sub STORE { }
    package main;

    my $bad;
    tie $bad, 'CountBad';
    $CountBad::n = 0;
    eval { Data::HashMap::II->new(0, $bad) };
    like($@, qr/must be a number/, 'tied non-numeric ttl croaks');
    is($CountBad::n, 1, 'croak path reads a tied ttl once (message uses settled value)');
}

# ---- clear() removes what a value's DESTROY adds, as %h = () does ----
{
    package ReinsClear { sub DESTROY { $main::ReinsClearMap->put($_, 're') for 90000 .. 90040 } }
    our $ReinsClearMap;
    for my $max (0, 2) {
        my $m = $ReinsClearMap = Data::HashMap::IA->new($max);
        hm_ia_put $m, $_, 'v' for 1 .. 5;
        hm_ia_put $m, 1, bless {}, 'ReinsClear';
        hm_ia_clear $m;
        my @keys = hm_ia_keys $m;
        is(hm_ia_size $m, 0, "clear (max_size $max): entries a DESTROY adds are cleared too");
        is(scalar @keys, 0, "clear (max_size $max): no live slot is left");
        hm_ia_put $m, 100 + $_, "v$_" for 1 .. 3;
        @keys = hm_ia_keys $m;
        is(hm_ia_size $m, scalar @keys, "clear (max_size $max): size matches keys after refill");
    }
    undef $ReinsClearMap;

    package DropMap { sub DESTROY { undef $main::DropMap } }
    our $DropMap = Data::HashMap::SA->new;
    $DropMap->put("k$_", bless {}, 'DropMap') for 1 .. 8;
    hm_sa_clear $DropMap;
    ok(!defined $DropMap, 'clear survives a DESTROY that frees the map');
}

# ---- NaN croaks wherever a number is expected ----
{
    my $nan = 0 + "nan";
    my $m = Data::HashMap::II->new();
    eval { hm_ii_put $m, $nan, 1 };
    like($@, qr/cannot use NaN/, 'II put: NaN key croaks');
    eval { hm_ii_put $m, 1, $nan };
    like($@, qr/cannot use NaN/, 'II put: NaN value croaks');
    eval { hm_ii_put $m, "nan", 1 };
    like($@, qr/cannot use NaN/, 'II put: "nan" string key croaks');
    eval { Data::HashMap::II->new(0, $nan) };
    like($@, qr/ttl must be a number/, 'II new: NaN ttl croaks');
    eval { Data::HashMap::II->new($nan) };
    like($@, qr/max_size must be a number/, 'II new: NaN max_size croaks');
    eval { Data::HashMap::II->new(2, 0, $nan) };
    like($@, qr/lru_skip must be a number/, 'II new: NaN lru_skip croaks');
    eval { hm_ii_put_ttl $m, 2, 2, $nan };
    like($@, qr/ttl must be a number/, 'II put_ttl: NaN ttl croaks');
    eval { hm_ii_reserve $m, $nan };
    like($@, qr/NaN is not a valid count/, 'II reserve: NaN count croaks');
    eval { hm_ii_drain $m, $nan };
    like($@, qr/NaN is not a valid count/, 'II drain: NaN count croaks');
    eval { hm_ii_incr_by $m, 1, $nan };
    like($@, qr/cannot use NaN/, 'II incr_by: NaN delta croaks');
    my $saturated = Data::HashMap::II->new(0, 9**9**9);
    is($saturated->ttl, 4294967295, 'II new: Inf ttl saturates to UINT32_MAX');
}

# ---- int conversion reads a tied scalar once, on success and croak paths ----
{
    package CountKey;
    our ($n, $v);
    sub TIESCALAR { bless {}, $_[0] }
    sub FETCH { $n++; $v }
    sub STORE { }
    package main;

    my $t;
    tie $t, 'CountKey';
    my $m = Data::HashMap::II->new();
    $CountKey::v = 4.5; $CountKey::n = 0;
    hm_ii_put $m, $t, 9;
    is($CountKey::n, 1, 'II put: tied NV key FETCHed once');
    is(hm_ii_get $m, 4, 9, 'II put: tied NV key converts correctly');

    $CountKey::v = 7; $CountKey::n = 0;
    hm_ii_put $m, $t, 1;
    is($CountKey::n, 1, 'II put: tied IV key FETCHed once');

    my $m16 = Data::HashMap::I16->new();
    $CountKey::v = 100000; $CountKey::n = 0;
    eval { hm_i16_put $m16, $t, 1 };
    like($@, qr/out of int16 range/, 'I16 put: tied out-of-range key croaks');
    is($CountKey::n, 1, 'I16 put: croak path reads tied key once');

    $CountKey::v = 1e30; $CountKey::n = 0;
    eval { hm_i16_put $m16, $t, 1 };
    like($@, qr/out of int16 range/, 'I16 put: tied NV out-of-range key croaks');
    is($CountKey::n, 1, 'I16 put: NV croak path reads tied key once');

    $CountKey::v = 'nan';
    eval { hm_ii_put $m, $t, 1 };
    like($@, qr/cannot use NaN/, 'II put: tied NaN key croaks');
    $CountKey::v = 5;
    ok(eval { hm_ii_put $m, $t, 1; 1 }, 'II put: a tied key judged on its new value, not the last FETCH');
}

# ---- new/thaw on an instance use its real class, not a stringified ref ----
{
    my $m = Data::HashMap::II->new;
    $m->put(1, 10);
    my $blob = $m->freeze;

    my $n = $m->new;
    is(ref($n), 'Data::HashMap::II', 'instance->new blesses into the real class');
    $n->put(2, 20);
    is($n->get(2), 20, 'instance->new returns a usable map');

    my $t = $m->thaw($blob);
    is(ref($t), 'Data::HashMap::II', 'instance->thaw blesses into the real class');
    is($t->get(1), 10, 'instance->thaw returns a usable map');

    package My::HM::II; our @ISA = ('Data::HashMap::II'); package main;
    my $sub = My::HM::II->thaw($blob);
    is(ref($sub), 'My::HM::II', 'thaw as a subclass blesses into the subclass');
    is($sub->get(1), 10, 'subclass thaw is usable');
}

{
    package ClearLookup;
    our ($map, $missed);
    sub DESTROY {
        $missed++ for grep { !$map->exists($_) } $map->keys;
    }
    package main;

    for my $v (qw(IA SA I16A I32A)) {
        for my $max (0, 100) {
            local $ClearLookup::map = "Data::HashMap::$v"->new($max);
            local $ClearLookup::missed = 0;
            my $m = $ClearLookup::map;
            $m->put($_, bless({}, 'ClearLookup')) for 1 .. 70;
            $m->clear;
            is($ClearLookup::missed, 0, "$v clear ($max): DESTROY can look up every remaining key");
            is($m->size, 0, "$v clear ($max): no entries remain");
        }
    }
}

done_testing;



( run in 1.053 second using v1.01-cache-2.11-cpan-60884a8df76 )