summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/t/op/avhv.t
blob: 0937293129fa6a3ab341a2ffc95c9de6371dce57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}

use warnings;
no warnings 'deprecated';
use strict;
use vars qw(@fake %fake);

require Tie::Array;

package Tie::BasicArray;
@Tie::BasicArray::ISA = 'Tie::Array';
sub TIEARRAY  { bless [], $_[0] }
sub STORE     { $_[0]->[$_[1]] = $_[2] }
sub FETCH     { $_[0]->[$_[1]] }
sub FETCHSIZE { scalar(@{$_[0]})} 
sub STORESIZE { $#{$_[0]} = $_[1]+1 } 

package main;

plan tests => 36;

my $sch = {
    'abc' => 1,
    'def' => 2,
    'jkl' => 3,
};

# basic normal array
$a = [];
$a->[0] = $sch;

$a->{'abc'} = 'ABC';
$a->{'def'} = 'DEF';
$a->{'jkl'} = 'JKL';

my @keys = keys %$a;
my @values = values %$a;

is ($#keys, 2);
is ($#values, 2);

my $i = 0;	# stop -w complaints

while (my ($key,$value) = each %$a) {
    if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
	$key =~ y/a-z/A-Z/;
	$i++ if $key eq $value;
    }
}

is ($i, 3);

# quick check with tied array
tie @fake, 'Tie::StdArray';
$a = \@fake;
$a->[0] = $sch;

$a->{'abc'} = 'ABC';
is ($a->{'abc'}, 'ABC');

# quick check with tied array
tie @fake, 'Tie::BasicArray';
$a = \@fake;
$a->[0] = $sch;

$a->{'abc'} = 'ABC';
is ($a->{'abc'}, 'ABC');

# quick check with tied array & tied hash
require Tie::Hash;
tie %fake, 'Tie::StdHash';
%fake = %$sch;
$a->[0] = \%fake;

$a->{'abc'} = 'ABC';
is ($a->{'abc'}, 'ABC');

# hash slice
{
  no warnings 'uninitialized';
  my $slice = join('', 'x',@$a{'abc','def'},'x');
  is ($slice, 'xABCx');
}

# evaluation in scalar context
my $avhv = [{}];
ok (!%$avhv);

push @$avhv, "a";
ok (!%$avhv);

$avhv = [];
eval { $a = %$avhv };
like ($@, qr/^Can't coerce array into hash/);

$avhv = [{foo=>1, bar=>2}];
like (%$avhv, qr,^\d+/\d+,);

# check if defelem magic works
sub f {
    is ($_[0], 'a');
    $_[0] = 'b';
}
$a = [{key => 1}, 'a'];
f($a->{key});
is ($a->[1], 'b');

# check if exists() is behaving properly
$avhv = [{foo=>1,bar=>2,pants=>3}];
ok (!exists $avhv->{bar});

$avhv->{pants} = undef;
ok (exists $avhv->{pants});
ok (!exists $avhv->{bar});

$avhv->{bar} = 10;
ok (exists $avhv->{bar});
is ($avhv->{bar}, 10);

my $v = delete $avhv->{bar};
is ($v, 10);

ok (!exists $avhv->{bar});

$avhv->{foo} = 'xxx';
$avhv->{bar} = 'yyy';
$avhv->{pants} = 'zzz';
my @x = delete @{$avhv}{'foo','pants'};
is ("@x", "xxx zzz");

is ("$avhv->{bar}", "yyy");

# hash assignment
%$avhv = ();
is (ref($avhv->[0]), 'HASH');

my %hv = %$avhv;
ok (!grep defined, values %hv);
ok (!grep ref, keys %hv);

%$avhv = (foo => 29, pants => 2, bar => 0);
is ("@$avhv[1..3]", '29 0 2');

my $extra;
my @extra;
($extra, %$avhv) = ("moo", foo => 42, pants => 53, bar => "HIKE!");
is ("@$avhv[1..3]", '42 HIKE! 53');
is ($extra, 'moo');

%$avhv = ();
(%$avhv, $extra) = (foo => 42, pants => 53, bar => "HIKE!");
is ("@$avhv[1..3]", '42 HIKE! 53');
ok (!defined $extra);

@extra = qw(whatever and stuff);
%$avhv = ();
(%$avhv, @extra) = (foo => 42, pants => 53, bar => "HIKE!");
is ("@$avhv[1..3]", '42 HIKE! 53');
is (@extra, 0);

%$avhv = ();
(@extra, %$avhv) = (foo => 42, pants => 53, bar => "HIKE!");
is (ref $avhv->[0], 'HASH');
is (@extra, 6);

# Check hash slices (BUG ID 20010423.002)
$avhv = [{foo=>1, bar=>2}];
@$avhv{"foo", "bar"} = (42, 53);
is ($avhv->{foo}, 42);
is ($avhv->{bar}, 53);