/
mixed_multi_dimensional.t
213 lines (168 loc) · 5.94 KB
/
mixed_multi_dimensional.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use v6;
use Test;
plan 80;
=begin description
This tests some mixed multi-dimensional structures.
NOTE:
These tests don't go any more than two levels deep
(AoH, AoP) in most cases because I know these won't
work yet in Pugs. When we have this support, then
this test should be added too more.
Some deeper tests were already added.
=end description
# UNSPECCED
#?niecza skip 'Cannot use hash access on an object of type Pair'
{ # Array of Pairs
my @array;
isa_ok(@array, Array);
my $pair = ('key' => 'value');
isa_ok($pair, Pair);
@array[0] = $pair; # assign a variable
is(+@array, 1, 'the array has one value in it');
isa_ok(@array[0], Pair);
is(@array[0]<key>, 'value', 'got the right pair value');
@array[1] = ('key1' => 'value1'); # assign it inline
is(+@array, 2, 'the array has two values in it');
isa_ok(@array[1], Pair);
is(@array[1]<key1>, 'value1', 'got the right pair value');
}
# UNSPECCED
{ # Array of Hashes
my @array;
isa_ok(@array, Array);
my %hash = ('key', 'value', 'key1', 'value1');
isa_ok(%hash, Hash);
is(+%hash.keys, 2, 'our hash has two keys');
@array[0] = %hash;
is(+@array, 1, 'the array has one value in it');
isa_ok(@array[0], Hash);
is(@array[0]{"key"}, 'value', 'got the right value for key');
is(@array[0]<key1>, 'value1', 'got the right value1 for key1');
}
{ # Array of Arrays
# L<S09/Multidimensional arrays>
my @array = (1, [2, 3], [4, 5], 6);
isa_ok(@array, Array);
is(+@array, 4, 'got 4 elements in the Array of Arrays');
is(@array[0], 1, 'got the right first element');
isa_ok(@array[1], Array);
is(@array[1][0], 2, 'got the right second/first element');
is(@array[1][1], 3, 'got the right second/second element');
isa_ok(@array[2], Array);
is(@array[2][0], 4, 'got the right third/first element');
is(@array[2][1], 5, 'got the right third/second element');
is(@array[3], 6, 'got the right fourth element');
}
# UNSPECCED
{ # Array of Subs
my @array;
isa_ok(@array, Array);
@array[0] = sub { 1 };
@array[1] = { 2 };
@array[2] = -> { 3 };
is(+@array, 3, 'got three elements in the Array');
isa_ok(@array[0], Sub);
isa_ok(@array[1], Block);
isa_ok(@array[2], Block);
is(@array[0](), 1, 'the first element (when executed) is 1');
is(@array[1](), 2, 'the second element (when executed) is 2');
is(@array[2](), 3, 'the third element (when executed) is 3');
}
# UNSPECCED
{ # Hash of Arrays
my %hash;
isa_ok(%hash, Hash);
%hash<key> = [ 1, 2, 3 ];
isa_ok(%hash<key>, Array);
is(+%hash<key>, 3, 'it should have 3 values in it');
is(%hash<key>[0], 1, 'got the right value');
is(%hash<key>[1], 2, 'got the right value');
is(%hash<key>[2], 3, 'got the right value');
{
my $array = %hash<key>;
is(+$array, 3, 'it should have 3 values in it');
is($array[0], 1, 'got the right value (when I pull the array out)');
is($array[1], 2, 'got the right value (when I pull the array out)');
is($array[2], 3, 'got the right value (when I pull the array out)');
}
{
%hash<key>.push(4);
is(+%hash<key>, 4, 'it should now have 4 values in it');
is(%hash<key>[3], 4, 'got the right value (which we just pushed onto the list)');
}
}
{ # Hash of Array-refs
# UNSPECCED
my %hash;
isa_ok(%hash, Hash);
my @array = ( 1, 2, 3 );
isa_ok(@array, Array);
%hash<key> = @array;
isa_ok(%hash<key>, Array);
is(+%hash<key>, 3, 'it should have 3 values in it');
is(%hash<key>[0], 1, 'got the right value');
is(%hash<key>[1], 2, 'got the right value');
is(%hash<key>[2], 3, 'got the right value');
{
my @array = @( %hash<key> );
is(+@array, 3, 'it should have 3 values in it');
is(@array[0], 1, 'got the right value (when I pull the array out)');
is(@array[1], 2, 'got the right value (when I pull the array out)');
is(@array[2], 3, 'got the right value (when I pull the array out)');
}
{
%hash<key>.push(4);
is(+%hash<key>, 4, 'it should now have 4 values in it');
is(%hash<key>[3], 4, 'got the right value (which we just pushed onto the array)');
}
}
{ # Hashref survive addition to an array.
my %h = <a 5 b 6>;
my $hr = \%h;
my $a0 = [ \%h ,'extra' ];
my $a1 = [ \%h ];
my $a2 = [ $hr ];
is($a0.elems,2,'hash references should not get decomposed');
is($a1.elems,1,'hash references should not get decomposed');
is($a2.elems,1,'hash references should not get decomposed');
}
{ # nested, declared in one statement
my $h = { a => [ 1,2,3 ] };
isa_ok($h<a>.WHAT, Array, "array nested in hashref in one declaration");
}
#?rakudo 18 skip "nom regression: 'Cannot assign to a non-container'"
{ # structures deeper than 2 levels
my @array;
@array[0][0][0][0][0] = 5;
isa_ok(@array, Array);
isa_ok(@array[0], Array);
isa_ok(@array[0][0], Array);
isa_ok(@array[0][0][0], Array);
isa_ok(@array[0][0][0][0], Array);
is(@array[0][0][0][0][0], 5, "5 level deep arrays only structure");
@array[1]<two>[0]<four>[0]<six> = 6;
isa_ok(@array, Array);
#?rakudo todo 'isa hash'
isa_ok(@array[1], Hash);
isa_ok(@array[1]<two>, Array);
#?rakudo todo 'isa hash'
isa_ok(@array[1]<two>[0], Hash);
is(+@array[1]<two>[0], 1, "one key at level 4");
isa_ok(@array[1]<two>[0]<four>, Array);
#?rakudo todo 'isa hash'
isa_ok(@array[1]<two>[0]<four>[0], Hash);
is(@array[1]<two>[0]<four>[0]<six>, 6, "6 level deep mixed structure");
@array[2]<two>[0]<f><other> = 5;
#?rakudo todo 'isa hash'
isa_ok(@array[1]<two>[0], Hash);
#?pugs 3 todo 'bug'
#?rakudo todo 'isa hash'
#?niecza todo
isa_ok(@array[1]<two>[0]<f>, Hash);
#?rakudo 2 todo 'unknown'
#?niecza todo
is(+@array[1]<two>[0], 2, "two keys at level 4");
#?niecza todo 'Use of uninitialized value in string context'
is(@array[1]<two>[0]<f><other>, 5, "more keys at level 4");
}
# vim: ft=perl6