]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/32-hash.t
Reenable copy tests in t/32-hash.t as they are not the issue
[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 * 19 + 5 + 1;
7
8 use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/;
9
10 use lib 't/lib';
11 use Variable::Magic::TestWatcher;
12
13 my $wiz = init
14         [ qw/get set len free copy dup local fetch store exists delete/ ], # clear
15         'hash';
16
17 my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/;
18 my %h = %n;
19
20 check { cast %h, $wiz } { }, 'cast';
21
22 my $s;
23 check { $s = $h{foo} } +{ (fetch => 1) x VMG_UVAR },
24                        # (copy => 1) x MGf_COPY # if clear magic
25                        'assign element to';
26 is $s, $n{foo}, 'hash: assign element to correctly';
27
28 my %b;
29 check { %b = %h } { }, 'assign to';
30 is_deeply \%b, \%n, 'hash: assign to correctly';
31
32 check { $s = \%h } { }, 'reference';
33
34 my @b;
35 check { @b = @h{qw/bar qux/} }
36                   +{ (fetch => 2) x VMG_UVAR }, 'slice';
37                   # (copy => 2) x MGf_COPY # if clear magic
38 is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly';
39
40 check { %h = () } { }, 'empty in list context'; # clear => 1
41
42 check { %h = (a => 1, d => 3); () }
43                +{ (store => 2, copy => 2) x VMG_UVAR }, # clear => 1
44                'assign from list in void context';
45
46 check { %h = map { $_ => 1 } qw/a b d/; }
47                +{ (exists => 3, store => 3, copy => 3) x VMG_UVAR }, # clear =>1
48                'assign from map in list context';
49
50 check { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR },
51                     'assign old element';
52
53 check { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR },
54                     'assign new element';
55
56 check { $s = %h } { }, 'buckets';
57
58 check { @b = keys %h } { }, 'keys';
59 is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly';
60
61 check { @b = values %h } { }, 'values';
62 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
63
64 check { while (my ($k, $v) = each %h) { } } { }, 'each';
65
66 check {
67  my %b = %n;
68  check { cast %b, $wiz } { }, 'cast 2';
69 } { free => 1 }, 'scope end';
70
71 check { undef %h } { }, 'undef'; # clear => 1
72
73 check { dispell %h, $wiz } { }, 'dispell';