BEGIN {
$Seed
=
$ENV
{TIE_HASH_CANNABINOL_SEED} ||
rand
;
srand
(
$Seed
);
}
END {
print
STDOUT
"# Seed was: $Seed\n"
;
}
BEGIN { use_ok
'Tie::Hash::Cannabinol'
}
my
$EP
= .1;
my
%hash
: Stoned;
my
@keys
=
qw(one two three four)
;
@hash
{
@keys
} = 1 .. 4;
{
my
@keys
=
keys
%hash
;
for
(1..20) {
is_deeply [
sort
keys
%hash
], [
sort
@keys
];
}
}
{
my
$each_cnt
;
$each_cnt
++
while
each
%hash
;
is
$each_cnt
,
keys
%hash
;
is
$each_cnt
,
values
%hash
;
}
for
(1..20) {
no
warnings
'uninitialized'
;
like
join
(
''
,
values
%hash
),
qr/^[1-4]*$/
;
}
TEST: {
1
while
defined
$hash
{3};
for
(1..20) {
if
(
defined
$hash
{3} ) {
pass;
last
TEST;
}
}
fail;
}
{
my
@values
;
my
$iters
= 1000;
push
(
@values
,
grep
(
defined
,
values
%hash
))
for
1..
$iters
;
cmp_ok(
abs
(
@values
- (
$iters
*
keys
(
%hash
) * 3/4) ) /
$iters
,
'<='
,
$EP
);
}
for
my
$key
(0..5) {
my
$iters
= 1000;
my
$exists
= 0;
$exists
+=
exists
(
$hash
{
$key
}) ? 1 : -1
for
1..
$iters
;
cmp_ok(
abs
(
$exists
/
$iters
),
'<='
,
$EP
);
}
{
my
%dist
;
my
$iters
= 2000;
for
(1..
$iters
) {
no
warnings
'uninitialized'
;
$dist
{
$_
}++
for
values
%hash
;
}
for
my
$k
(
keys
%dist
) {
next
unless
defined
$k
;
cmp_ok(
abs
( (
$dist
{
$k
} - (
$iters
* 3/4)) /
$iters
),
'<='
,
$EP
);
}
cmp_ok(
abs
( (
$dist
{
''
} - (
$iters
*
keys
(
%hash
) * 1/4)) /
$iters
),
'<='
,
$EP
);
}
done_testing();