Data-SortedSet-Shared

 view release on metacpan or  search on metacpan

t/06-strings.t  view on Meta::CPAN

is $z->count, 3, 'pop removed';
is_deeply [$z->pop_max], ['alice', 1600], 'pop_max';
is_deeply [$z->peek_min], ['carol', 100], 'peek_min after pops';
is_deeply [$z->peek_max], ['bob', 1000], 'peek_max';

my $st = $z->stats;
ok exists $st->{set} && exists $st->{keys}, 'stats has set + keys sections';
is $st->{set}{count}, $z->count, 'stats set count matches';
cmp_ok $st->{keys}{count}, '>=', $z->count, 'stats keys count >= members (permanent interning)';

my @each;
$z->each(sub { push @each, [@_] });
is scalar(@each), $z->count, 'each visits every member';
is_deeply [map { $_->[0] } @each], [qw(carol bob)], 'each in score order, decoded to strings';

ok $z->remove("bob"), 'remove returns true';
ok !$z->exists("bob"), 'removed';
ok !$z->remove("bob"), 'remove absent returns false';

# add_many + clear
is $z->add_many([ ['m1', 5], ['m2', 6], 'bad', ['m3', 7] ]), 3, 'add_many counts new (skips malformed)';
$z->clear;
is $z->count, 0, 'clear empties the set';
ok !$z->exists("carol"), 'clear forgot the keys too';

# reopen (file-backed): both backing stores reopen
my $d = "/tmp/sss-$$";
unlink "$d.set", "$d.keys";
{
    my $w = Data::SortedSet::Shared::Strings->new(set => "$d.set", keys => "$d.keys", max => 100);
    $w->add("foo", 5); $w->add("bar", 3);
    $w->sync;
}
{
    my $r = Data::SortedSet::Shared::Strings->new(set => "$d.set", keys => "$d.keys", max => 999);
    is $r->count, 2, 'reopen count';
    is $r->score("foo"), 5, 'reopen score';
    is_deeply [$r->range_by_rank(0, -1)], [qw(bar foo)], 'reopen preserves order + keys';
    $r->unlink;
}
ok !-e "$d.set" && !-e "$d.keys", 'unlink removed both backing files';

# wrap two existing objects
my $w = Data::SortedSet::Shared::Strings->wrap(
    Data::SortedSet::Shared->new(undef, 10),
    Data::Intern::Shared->new(undef, 10),
);
$w->add("zed", 1);
is $w->score("zed"), 1, 'wrap() composes existing objects';

# the parent class's new_strings() convenience constructor (delegates to Strings->new)
{
    require Data::SortedSet::Shared;
    my $z2 = Data::SortedSet::Shared->new_strings(max => 1000);
    isa_ok $z2, 'Data::SortedSet::Shared::Strings', 'new_strings returns a Strings object';
    is $z2->add("x", 10), 1, 'new_strings: add works';
    is $z2->score("x"), 10, 'new_strings: score round-trip';
    is $z2->count, 1, 'new_strings: count';
}

# add_many skips a NaN-scored row WITHOUT interning its key (no ghost slot)
{
    my $z3 = Data::SortedSet::Shared::Strings->new(max => 100);
    my $before = $z3->key_table->count;
    my $n = $z3->add_many([ ["good", 5], ["bad", "NaN"+0], ["good2", 7] ]);
    is $n, 2, 'add_many: NaN-scored row not added';
    ok !$z3->exists("bad"), 'add_many: NaN-row member absent';
    is $z3->key_table->count, $before + 2, 'add_many: NaN-row key not interned (no ghost slot)';
}

{
    my $snap = Data::SortedSet::Shared::Strings->new(max => 10);
    $snap->add_many([ ['a', 1], ['b', 2], ['c', 3] ]);
    my @seen;
    $snap->each(sub {
        push @seen, [ @_ ];
        if (@seen == 1) { $snap->clear; $snap->add('replacement', 99) }
    });
    is_deeply \@seen, [ ['a', 1], ['b', 2], ['c', 3] ],
        'each snapshots strings before a callback clears and reuses ids';
    is_deeply [$snap->range_by_rank(0, -1)], ['replacement'], 'callback mutations remain visible';
    eval { $snap->each(sub { die "callback failed\n" }) };
    like $@, qr/callback failed/, 'each propagates callback exceptions';
    $snap->clear;
    eval { $snap->each(undef) };
    like $@, qr/code ref/, 'each validates callbacks even when empty';
}

done_testing;



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