xref: /openbsd/gnu/usr.bin/perl/ext/Hash-Util/t/builtin.t (revision 76d0caae)
1#!/usr/bin/perl -Tw
2
3use strict;
4use Test::More;
5
6my @Exported_Funcs;
7BEGIN {
8    @Exported_Funcs = qw( bucket_ratio num_buckets used_buckets );
9    plan tests => 13 + @Exported_Funcs;
10    use_ok 'Hash::Util', @Exported_Funcs;
11}
12foreach my $func (@Exported_Funcs) {
13    can_ok __PACKAGE__, $func;
14}
15
16my %hash;
17
18is(bucket_ratio(%hash), 0, "Empty hash has no bucket_ratio");
19is(num_buckets(%hash), 8, "Empty hash should have eight buckets");
20is(used_buckets(%hash), 0, "Empty hash should have no used buckets");
21
22$hash{1}= 1;
23is(bucket_ratio(%hash), "1/8", "hash has expected bucket_ratio");
24is(num_buckets(%hash), 8, "hash should have eight buckets");
25is(used_buckets(%hash), 1, "hash should have one used buckets");
26
27$hash{$_}= $_ for 2..7;
28
29like(bucket_ratio(%hash), qr!/(?:8|16)!, "hash has expected number of buckets in bucket_ratio");
30my $num= num_buckets(%hash);
31ok(($num == 8 || $num == 16), "hash should have 8 or 16 buckets");
32cmp_ok(used_buckets(%hash), "<", 8, "hash should have one used buckets");
33
34$hash{8}= 8;
35like(bucket_ratio(%hash), qr!/(?:8|16)!, "hash has expected number of buckets in bucket_ratio");
36$num= num_buckets(%hash);
37ok(($num == 8 || $num == 16), "hash should have 8 or 16 buckets");
38cmp_ok(used_buckets(%hash), "<=", 8, "hash should have at most 8 used buckets");
39
40
41