]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
Importing Variable-Magic-0.11.tar.gz
[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 => 18;
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 = (a => 1, d => 3);
58 ++$x[3];
59 $x[5] += 2 if VMG_UVAR;
60 $x[9] += 2 if VMG_UVAR;
61 ok(check(), 'hash : assign from list');
62
63 %a = map { $_ => 1 } qw/a b d/;
64 ++$x[3];
65 $x[5] += 3 if VMG_UVAR;
66 $x[9] += 3 if VMG_UVAR;
67 ok(check(), 'hash : assign from map');
68
69 $a{d} = 2;
70 ++$x[5] if MGf_COPY;
71 ++$x[9] if VMG_UVAR;
72 ok(check(), 'hash : assign old element');
73
74 $a{c} = 3;
75 ++$x[5] if MGf_COPY;
76 ++$x[9] if VMG_UVAR;
77 ok(check(), 'hash : assign new element');
78
79 $b = %a;
80 ok(check(), 'hash : buckets');
81
82 @b = keys %a;
83 ok(check(), 'hash : keys');
84
85 @b = values %a;
86 ok(check(), 'hash : values');
87
88 while (my ($k, $v) = each %a) { }
89 ok(check(), 'hash : each');
90
91 {
92  my %b = %n;
93  cast %b, $wiz;
94 }
95 ++$x[4];
96 ok(check(), 'hash : scope end');
97
98 undef %a;
99 ++$x[3];
100 ok(check(), 'hash : undef');
101
102 dispell %a, $wiz;
103 ok(check(), 'hash : dispel');