]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
54c23d8dc406841a989da2713c095854c826668f
[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 => 17;
7
8 use Variable::Magic qw/wizard cast dispell MGf_COPY VMG_UVAR/;
9
10 my @c = (0) x 12;
11 my @x = (0) x 12;
12
13 sub check {
14  for (0 .. 11) { return 0 unless $c[$_] == $x[$_]; }
15  return 1;
16 }
17
18 my $wiz = wizard get   => sub { ++$c[0] },
19                  set   => sub { ++$c[1] },
20                  len   => sub { ++$c[2]; $_[2] },
21                  clear => sub { ++$c[3] },
22                  free  => sub { ++$c[4] },
23                  copy  => sub { ++$c[5] },
24                  dup   => sub { ++$c[6] },
25                  local => sub { ++$c[7] },
26                  fetch => sub { ++$c[8] },
27                  store => sub { ++$c[9] },
28                  'exists' => sub { ++$c[10] },
29                  'delete' => sub { ++$c[11] };
30 ok(check(), 'hash : create wizard');
31
32 my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/;
33 my %a = %n;
34
35 cast %a, $wiz;
36 ok(check(), 'hash : cast');
37
38 my $b = $a{foo};
39 ++$x[5] if MGf_COPY;
40 ++$x[8] if VMG_UVAR;
41 ok(check(), 'hash : assign element to');
42
43 my %b = %a;
44 ok(check(), 'hash : assign to');
45
46 $b = "X%{a}Y";
47 ok(check(), 'hash : interpolate');
48
49 $b = \%a;
50 ok(check(), 'hash : reference');
51
52 my @b = @a{qw/bar qux/};
53 $x[5] += 2 if MGf_COPY;
54 $x[8] += 2 if VMG_UVAR;
55 ok(check(), 'hash : slice');
56
57 %a = map { $_ => 1 } qw/a b d/;
58 ++$x[3];
59 $x[5] += 3 if MGf_COPY && $^V && $^V gt 5.9.3;
60 $x[9] += 3 if VMG_UVAR;
61 ok(check(), 'hash : assign');
62
63 $a{d} = 2;
64 ++$x[5] if MGf_COPY;
65 ++$x[9] if VMG_UVAR;
66 ok(check(), 'hash : assign old element');
67
68 $a{c} = 3;
69 ++$x[5] if MGf_COPY;
70 ++$x[9] if VMG_UVAR;
71 ok(check(), 'hash : assign new element');
72
73 $b = %a;
74 ok(check(), 'hash : buckets');
75
76 @b = keys %a;
77 ok(check(), 'hash : keys');
78
79 @b = values %a;
80 ok(check(), 'hash : values');
81
82 while (my ($k, $v) = each %a) { }
83 ok(check(), 'hash : each');
84
85 {
86  my %b = %n;
87  cast %b, $wiz;
88 }
89 ++$x[4];
90 ok(check(), 'hash : scope end');
91
92 undef %a;
93 ++$x[3];
94 ok(check(), 'hash : undef');
95
96 dispell %a, $wiz;
97 ok(check(), 'hash : dispel');