]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
This is 0.64
[perl/modules/Variable-Magic.git] / t / 32-hash.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => (2 * 27 + 9) + 2 * (2 * 5 + 5) + 1;
7
8 use Variable::Magic qw<
9  cast dispell
10  VMG_UVAR
11  VMG_COMPAT_HASH_DELETE_NOUVAR_VOID
12 >;
13
14 use lib 't/lib';
15 use Variable::Magic::TestWatcher;
16
17 my $wiz = init_watcher
18         [ qw<get set len clear free copy dup local fetch store exists delete> ],
19         'hash';
20
21 my %n = map { $_ => int rand 1000 } qw<foo bar baz qux>;
22 my %h = %n;
23
24 watch { cast %h, $wiz } { }, 'cast';
25
26 my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
27                        'assign element to';
28 is $s, $n{foo}, 'hash: assign element to correctly';
29
30 my %b;
31 watch { %b = %h } { }, 'assign to';
32 is_deeply \%b, \%n, 'hash: assign to correctly';
33
34 $s = watch { \%h } { }, 'reference';
35
36 my @b = watch { @h{qw<bar qux>} }
37                   +{ (fetch => 2) x VMG_UVAR }, 'slice';
38 is_deeply \@b, [ @n{qw<bar qux>} ], 'hash: slice correctly';
39
40 # exists
41
42 watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },'exists in void context';
43
44 for (1 .. 2) {
45  $s = watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },
46                                                 "exists in scalar context ($_)";
47  ok $s, "hash: exists correctly ($_)";
48 }
49
50 # delete
51
52 watch { delete $h{bar} } +{
53  ((delete => 1) x !VMG_COMPAT_HASH_DELETE_NOUVAR_VOID, copy => 1) x VMG_UVAR
54 }, 'delete in void context';
55
56 for (1 .. 2) {
57  $s = watch { delete $h{baz} } +{ (delete => 1, copy => 1) x VMG_UVAR },
58                                                 "delete in scalar context ($_)";
59  my $exp = $_ == 1 ? $n{baz} : undef;
60  is $s, $exp, "hash: delete correctly ($_)";
61 }
62
63 # clear
64
65 watch { %h = () } { clear => 1 }, 'empty in list context';
66
67 watch { $h{a} = -1; %h = (b => $h{a}) }
68            +{ (fetch => 1, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
69            'empty and set in void context';
70
71 watch { %h = (a => 1, d => 3) }
72                +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
73                'assign from list in void context';
74
75 @b = watch { %h = (a => 1, d => 3) }
76                +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
77                'assign from list in void context';
78
79 watch { %h = map { $_ => 1 } qw<a b d>; }
80                +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 },
81                'assign from map in void context';
82
83 watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR },
84                     'assign old element';
85
86 watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR },
87                     'assign new element';
88
89 $s = watch { %h } { }, 'buckets';
90
91 @b = watch { keys %h } { }, 'keys';
92 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
93
94 @b = watch { values %h } { }, 'values';
95 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
96
97 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
98
99 watch {
100  my %b = %n;
101  watch { cast %b, $wiz } { }, 'cast 2';
102 } { free => 1 }, 'scope end';
103
104 watch { undef %h } { clear => 1 }, 'undef';
105
106 watch { dispell %h, $wiz } { }, 'dispell';
107
108 SKIP: {
109  my $SKIP;
110
111  if (!VMG_UVAR) {
112   $SKIP = 'uvar magic';
113  } else {
114   local $@;
115   unless (eval { require B; require B::Deparse; 1 }) {
116    $SKIP = 'B and B::Deparse';
117   }
118  }
119  if ($SKIP) {
120   $SKIP .= ' required to test uvar/clear interaction fix';
121   skip $SKIP => 2 * ( 2 * 5 + 5);
122  }
123
124  my $bd = B::Deparse->new;
125
126  my %h1 = (a => 13, b => 15);
127  my %h2 = (a => 17, b => 19);
128
129  my @tests = (
130   [ \%h1 => 'first hash'  => (14, 16) ],
131   [ \%h2 => 'second hash' => (18, 20) ],
132  );
133
134  for my $test (@tests) {
135   my ($h, $desc, @exp) = @$test;
136
137   watch { &cast($h, $wiz) } { }, "cast clear/uvar on $desc";
138
139   my $code   = sub { my $x = $h->{$_[0]}; ++$x; $x };
140   my $before = $bd->coderef2text($code);
141   my $res;
142
143   watch { $res = $code->('a') } { fetch => 1 }, "fetch constant 'a' from $desc";
144   is $res, $exp[0], "uvar: fetch constant 'a' from $desc was correct";
145
146   my $after = $bd->coderef2text($code);
147   is $before, $after,
148                 "uvar: code deparses correctly after constant fetch from $desc";
149
150   my $key = 'b';
151   watch { $res = $code->($key) } { fetch => 1 },"fetch variable 'b' from $desc";
152   is $res, $exp[1], "uvar: fetch variable 'b' from $desc was correct";
153
154   $after = $bd->coderef2text($code);
155   is $before, $after,
156                 "uvar: code deparses correctly after variable fetch from $desc";
157
158   watch { %$h = () } { clear => 1 }, "fixed clear for $desc";
159
160   watch { &dispell($h, $wiz) } { }, "dispell clear/uvar from $desc";
161
162   ok(!(B::svref_2object($h)->FLAGS & B::SVs_RMG()),
163                                "$desc no longer has the RMG flag set");
164  }
165 }