]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
5cc5e1f2a6ad4ce9c322344c7cd26646176d01f1
[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 * 22 + 7) + (2 * 5 + 5) + 1;
7
8 use Variable::Magic qw<cast dispell VMG_UVAR>;
9
10 use lib 't/lib';
11 use Variable::Magic::TestWatcher;
12
13 my $wiz = init_watcher
14         [ qw<get set len clear free copy dup local fetch store exists delete> ],
15         'hash';
16
17 my %n = map { $_ => int rand 1000 } qw<foo bar baz qux>;
18 my %h = %n;
19
20 watch { cast %h, $wiz } { }, 'cast';
21
22 my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
23                        'assign element to';
24 is $s, $n{foo}, 'hash: assign element to correctly';
25
26 for (1 .. 2) {
27  $s = watch { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, "exists ($_)";
28  ok $s, "hash: exists correctly ($_)";
29 }
30
31 my %b;
32 watch { %b = %h } { }, 'assign to';
33 is_deeply \%b, \%n, 'hash: assign to correctly';
34
35 $s = watch { \%h } { }, 'reference';
36
37 my @b = watch { @h{qw<bar qux>} }
38                   +{ (fetch => 2) x VMG_UVAR }, 'slice';
39 is_deeply \@b, [ @n{qw<bar qux>} ], 'hash: slice correctly';
40
41 watch { %h = () } { clear => 1 }, 'empty in list context';
42
43 watch { %h = (a => 1, d => 3) }
44                +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
45                'assign from list in void context';
46
47 @b = watch { %h = (a => 1, d => 3) }
48                +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
49                'assign from list in void context';
50
51 watch { %h = map { $_ => 1 } qw<a b d>; }
52                +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 },
53                'assign from map in void context';
54
55 watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR },
56                     'assign old element';
57
58 watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR },
59                     'assign new element';
60
61 $s = watch { %h } { }, 'buckets';
62
63 @b = watch { keys %h } { }, 'keys';
64 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
65
66 @b = watch { values %h } { }, 'values';
67 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
68
69 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
70
71 watch {
72  my %b = %n;
73  watch { cast %b, $wiz } { }, 'cast 2';
74 } { free => 1 }, 'scope end';
75
76 watch { undef %h } { clear => 1 }, 'undef';
77
78 watch { dispell %h, $wiz } { }, 'dispell';
79
80 SKIP: {
81  my $SKIP;
82
83  if (!VMG_UVAR) {
84   $SKIP = 'uvar magic';
85  } else {
86   local $@;
87   unless (eval { require B::Deparse; 1 }) {
88    $SKIP = 'B::Deparse';
89   }
90  }
91  if ($SKIP) {
92   $SKIP .= ' required to test uvar/clear interaction fix';
93   skip $SKIP => 2 * 5 + 5;
94  }
95
96  my $bd = B::Deparse->new;
97
98  my %h = (a => 13, b => 15);
99  watch { cast %h, $wiz } { }, 'cast clear/uvar';
100
101  my $code   = sub { my $x = $h{$_[0]}; ++$x; $x };
102  my $before = $bd->coderef2text($code);
103  my $res;
104
105  watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
106  is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
107
108  my $after = $bd->coderef2text($code);
109  is $before, $after, 'uvar: fixed fetch deparse correctly';
110
111  watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
112  is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
113
114  $after = $bd->coderef2text($code);
115  is $before, $after, 'uvar: fixed fetch deparse correctly';
116
117  watch { %h = () } { clear => 1 }, 'fixed clear';
118
119  watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
120
121  require B;
122  ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');
123 }