From fe94940552b9317f87a124f70c0974d7850c40de Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Wed, 8 Sep 2021 12:19:39 +0000 Subject: [PATCH] More tests for assigning to keys %hash 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.) --- t/op/each.t | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/t/op/each.t b/t/op/each.t index 3d49e5d96863..875622da12cb 100644 --- a/t/op/each.t +++ b/t/op/each.t @@ -10,8 +10,6 @@ BEGIN { set_up_inc('../lib'); } -plan tests => 55; - my %h; $h{'abc'} = 'ABC'; $h{'def'} = 'DEF'; @@ -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); @@ -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();