]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
Test 'delete' uvar magic and introduce VMG_COMPAT_HASH_DELETE_NOUVAR_VOID
[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 * 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::Deparse; 1 }) {
116    $SKIP = 'B::Deparse';
117   }
118  }
119  if ($SKIP) {
120   $SKIP .= ' required to test uvar/clear interaction fix';
121   skip $SKIP => 2 * 5 + 5;
122  }
123
124  my $bd = B::Deparse->new;
125
126  my %h = (a => 13, b => 15);
127  watch { cast %h, $wiz } { }, 'cast clear/uvar';
128
129  my $code   = sub { my $x = $h{$_[0]}; ++$x; $x };
130  my $before = $bd->coderef2text($code);
131  my $res;
132
133  watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
134  is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
135
136  my $after = $bd->coderef2text($code);
137  is $before, $after, 'uvar: fixed fetch deparse correctly';
138
139  watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
140  is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
141
142  $after = $bd->coderef2text($code);
143  is $before, $after, 'uvar: fixed fetch deparse correctly';
144
145  watch { %h = () } { clear => 1 }, 'fixed clear';
146
147  watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
148
149  require B;
150  ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');
151 }