6 use Test::More tests => 2 * 20 + 6 + 1;
8 use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/;
11 use Variable::Magic::TestWatcher;
14 [ qw/get set len free copy dup local fetch store exists delete/ ], # clear
17 my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/;
20 check { cast %h, $wiz } { }, 'cast';
23 check { $s = $h{foo} } +{ (fetch => 1) x VMG_UVAR },
24 # (copy => 1) x MGf_COPY # if clear magic
26 is $s, $n{foo}, 'hash: assign element to correctly';
28 $s = check { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, 'exists';
29 ok $s, 'hash: exists correctly';
32 check { %b = %h } { }, 'assign to';
33 is_deeply \%b, \%n, 'hash: assign to correctly';
35 check { $s = \%h } { }, 'reference';
38 check { @b = @h{qw/bar qux/} }
39 +{ (fetch => 2) x VMG_UVAR }, 'slice';
40 # (copy => 2) x MGf_COPY # if clear magic
41 is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly';
43 check { %h = () } { }, 'empty in list context'; # clear => 1
45 check { %h = (a => 1, d => 3); () }
46 +{ (store => 2, copy => 2) x VMG_UVAR }, # clear => 1
47 'assign from list in void context';
49 check { %h = map { $_ => 1 } qw/a b d/; }
50 +{ (exists => 3, store => 3, copy => 3) x VMG_UVAR }, # clear =>1
51 'assign from map in list context';
53 check { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR },
56 check { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR },
59 check { $s = %h } { }, 'buckets';
61 check { @b = keys %h } { }, 'keys';
62 is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly';
64 check { @b = values %h } { }, 'values';
65 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
67 check { while (my ($k, $v) = each %h) { } } { }, 'each';
71 check { cast %b, $wiz } { }, 'cast 2';
72 } { free => 1 }, 'scope end';
74 check { undef %h } { }, 'undef'; # clear => 1
76 check { dispell %h, $wiz } { }, 'dispell';