-
Notifications
You must be signed in to change notification settings - Fork 135
/
hashes.t
186 lines (147 loc) · 5.75 KB
/
hashes.t
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
177
178
179
180
181
182
183
184
185
186
use v6;
use Test;
# L<S03/Item assignment precedence>
plan 39;
# Binding of hash elements.
# See thread "Binding of array elements" on p6l started by Ingo Blechschmidt:
# L<"http://www.nntp.perl.org/group/perl.perl6.language/22915">
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
try { %hash<b> := $var };
is %hash<b>, "d", "basic binding of a hash element (1)";
unless %hash<b> eq "d" {
skip-rest "Skipping binding of hash elements tests (not yet implemented in the normal runcore)";
exit;
}
$var = "e";
is %hash<b>, "e", "basic binding of a hash element (2)";
%hash<b> = "f";
is $var, "f", "basic binding of a hash element (3)";
}
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
$var = "e";
is %hash<b>, "e", "binding of hash elements works with delete (1)";
%hash<b>:delete;
# $var unchanged, but assigning to $var doesn't modify @hash any
# longer; similarily, changing @hash[1] doesn't modify $var now
is $var, "e", "binding of hash elements works with delete (2)";
is ~%hash.values.sort, "x z", "binding of hash elements works with delete (3)";
$var = "f";
%hash<b> = "g";
is $var, "f", "binding of hash elements works with delete (4)";
is %hash<b>, "g", "binding of hash elements works with delete (5)";
}
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
$var = "e";
is %hash<b>, "e", "binding of hash elements works with resetting the hash (1)";
%hash = ();
# $var unchanged, but assigning to $var doesn't modify @hash any
# longer; similarily, changing @hash[1] doesn't modify $var now
is $var, "e", "binding of hash elements works with resetting the hash (2)";
is ~%hash, "", "binding of hash elements works with resetting the hash (3)";
$var = "f";
%hash<b> = "g";
is $var, "f", "binding of hash elements works with resetting the hash (4)";
is %hash<b>, "g", "binding of hash elements works with resetting the hash (5)";
}
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
$var = "e";
is %hash<b>, "e", "binding of hash elements works with rebinding the hash (1)";
my %other_hash = (:p<q>, :r<s>, :t<u>);
%hash := %other_hash;
# $var unchanged, but assigning to $var doesn't modify @hash any
# longer; similarily, changing @hash[1] doesn't modify $var now
is $var, "e", "binding of hash elements works with rebinding the hash (2)";
is ~%hash.values.sort, "q s u",
"binding of hash elements works with rebinding the hash (3)";
$var = "f";
%hash<b> = "g";
is $var, "f", "binding of hash elements works with rebinding the hash (4)";
is %hash<b>, "g", "binding of hash elements works with rebinding the hash (5)";
}
{
my sub foo (%h) { %h<b> = "new_value" }
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
foo %hash;
is $var, "new_value", "passing a hash to a sub expecting a hash behaves correctly (1)";
is ~%hash.values.sort, "new_value x z",
"passing a hash to a sub expecting a hash behaves correctly (2)";
}
{
my sub foo (Hash $h) { $h<b> = "new_value" }
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
foo %hash;
is $var, "new_value",
"passing a hash to a sub expecting a hashref behaves correctly (1)";
is ~%hash.values.sort, "new_value x z",
"passing a hash to a sub expecting a hashref behaves correctly (2)";
}
# Binding of not yet existing elements should autovivify
{
my %hash;
my $var = "d";
lives-ok { %hash<b> := $var },
"binding of not yet existing elements should autovivify (1)";
is %hash<b>, "d", "binding of not yet existing elements should autovivify (2)";
$var = "e";
is %hash<b>, "e", "binding of not yet existing elements should autovivify (3)";
is $var, "e", "binding of not yet existing elements should autovivify (4)";
}
# Assignment (not binding) creates new containers
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
$var = "e";
is %hash<b>, "e", "hash assignment creates new containers (1)";
my %new_hash = %hash;
$var = "f";
# %hash<b> and $var are now "f", but %new_hash is unchanged.
is $var, "f", "hash assignment creates new containers (2)";
is ~%hash\ .values.sort, "f x z", "hash assignment creates new containers (3)";
is ~%new_hash.values.sort, "e x z", "hash assignment creates new containers (4)";
}
# Binding does not create new containers
{
my %hash = (:a<x>, :b<y>, :c<z>);
my $var = "d";
%hash<b> := $var;
$var = "e";
is %hash<b>, "e", "hash binding does not create new containers (1)";
my %new_hash := %hash;
$var = "f";
# %hash<b> and $var are now "f", but %new_hash is unchanged.
is $var, "f", "hash binding does not create new containers (2)";
is ~%hash\ .values.sort, "f x z", "hash binding does not create new containers (3)";
is ~%new_hash.values.sort, "f x z", "hash binding does not create new containers (4)";
}
# Binding %hash := $hashref.
# See http://colabti.org/irclogger/irclogger_log/perl6?date=2005-11-06,Sun&sel=388#l564
# and consider the magic behind parameter binding (which is really normal
# binding).
{
my $hashref = { a => "a", b => "b" };
my %hash := $hashref;
is +%hash, 2, 'binding %hash := $hashref works (1)';
%hash<b> = "c";
is ~$hashref.values.sort, "a c", 'binding %hash := $hashref works (2)';
is ~%hash\ .values.sort, "a c", 'binding %hash := $hashref works (3)';
}
throws-like 'my %h = a => 1, b => 2; %h<a b> := (4, 5)', X::Bind::Slice,
'Cannot bind to hash slices';
is 1,1, 'dummy';
# vim: ft=perl6