Skip to content

Commit

Permalink
More tests for assigning to keys %hash
Browse files Browse the repository at this point in the history
Change some calls from is()/isnt() to cmp_ok() to confirm that values
change in the expected direction, rather than merely being "different".

Re-order some calls to is()/cmp_ok() so that the "expected" value is second.

Don't assume that a hash can always hold `num_buckets()/2` keys.
(The test had previously assumed that assigning this value to keys wouldn't
change the hash size. Instead use the current number of keys, as clearly
the hash is already holding that many keys.)
  • Loading branch information
nwc10 committed Sep 8, 2021
1 parent 034242a commit a00dd64
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions t/op/each.t
Expand Up @@ -10,8 +10,6 @@ BEGIN {
set_up_inc('../lib');
}

plan tests => 55;

my %h;
$h{'abc'} = 'ABC';
$h{'def'} = 'DEF';
Expand Down Expand Up @@ -110,19 +108,32 @@ $total = 0;
$total += $key while $key = each %hash;
is ($total, 100, "test values keys resets iterator");

is (keys(%hash), 10, "keys (%hash)");
SKIP: {
skip "no Hash::Util on miniperl", 3, if is_miniperl;
skip "no Hash::Util on miniperl", 8, if is_miniperl;
require Hash::Util;
sub Hash::Util::num_buckets (\%);

my $size = Hash::Util::num_buckets(%hash);
keys(%hash) = $size / 2;
is ($size, Hash::Util::num_buckets(%hash),
cmp_ok($size, '>=', keys %hash, 'sanity check - more buckets than keys');
%hash = ();
is(Hash::Util::num_buckets(%hash), $size,
"size doesn't change when hash is emptied");

%hash = split /, /, 'Pugh, Pugh, Barney McGrew, Cuthbert, Dibble, Grubb';
is (keys(%hash), 3, "now 3 keys");
# 3 keys won't be enough to trigger any "must grow" criteria:
is(Hash::Util::num_buckets(%hash), $size,
"size doesn't change with 3 keys");

keys(%hash) = keys(%hash);
is (Hash::Util::num_buckets(%hash), $size,
"assign to keys does not shrink hash bucket array");
is (keys(%hash), 3, "still 3 keys");
keys(%hash) = $size + 100;
isnt ($size, Hash::Util::num_buckets(%hash),
"assignment to keys of a number not large enough does not change size");
is (keys(%hash), 10, "keys (%hash)");
cmp_ok(Hash::Util::num_buckets(%hash), '>', $size,
"assign to keys will grow hash bucket array");
is (keys(%hash), 3, "but still 3 keys");
}

@::tests = (&next_test, &next_test, &next_test);
Expand Down Expand Up @@ -285,3 +296,5 @@ is "$a $b", "f 7", 'each in list assignment';
$a = 7;
($a, $b) = (3, values %h2);
is "$a $b", "3 7", 'values in list assignment';

done_testing();

0 comments on commit a00dd64

Please sign in to comment.