/
array-shapes.t
150 lines (132 loc) · 4.1 KB
/
array-shapes.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
use v6;
use Test;
plan 37;
# L<S09/Fixed-size arrays>
{
my @arr[*];
@arr[42] = "foo";
is(+@arr, 43, 'my @arr[*] autoextends like my @arr');
}
{
my @arr[7] = <a b c d e f g>;
is(@arr, [<a b c d e f g>], 'my @arr[num] can hold num things');
throws-like q[push @arr, 'h'],
X::IllegalOnFixedDimensionArray,
operation => 'push',
'adding past num items in my @arr[num] dies';
throws-like '@arr[7]',
Exception,
'accessing past num items in my @arr[num] dies';
}
{
lives-ok { my @arr\ [7] },
'array with fixed size with unspace';
#?rakudo 2 todo 'code does not die, array shapes RT #124502'
throws-like 'my @arr.[8]',
Exception, # XXX fix when this block is no longer skipped
'array with dot form dies';
throws-like 'my @arr\ .[8]',
Exception, # XXX fix when this block is no longer skipped
'array with dot form and unspace dies';
}
# L<S09/Typed arrays>
{
my @arr of Int = 1, 2, 3, 4, 5;
is(@arr, <1 2 3 4 5>, 'my @arr of Type works');
throws-like q[push @arr, 's'],
X::TypeCheck,
'type constraints on my @arr of Type works (1)';
throws-like 'push @arr, 4.2',
X::TypeCheck,
'type constraints on my @arr of Type works (2)';
}
{
my Int @arr = 1, 2, 3, 4, 5;
is(@arr, <1 2 3 4 5>, 'my Type @arr works');
throws-like q[push @arr, 's'],
X::TypeCheck,
'type constraints on my Type @arr works (1)';
throws-like 'push @arr, 4.2',
X::TypeCheck,
'type constraints on my Type @arr works (2)';
}
{
my @arr[5] of Int = <1 2 3 4 5>;
is(@arr, <1 2 3 4 5>, 'my @arr[Int] of Type works');
throws-like 'push @arr, 123',
Exception,
'boundary constraints on my @arr[Int] of Type works';
}
{
my int @arr = 1, 2, 3, 4, 5;
is(@arr, <1 2 3 4 5>, 'my type @arr works');
is push(@arr, 6), [1,2,3,4,5,6], 'push on native @arr works';
# RT #125123'
throws-like 'push @arr, "s"',
X::TypeCheck,
'type constraints on my type @arr works (1)';
throws-like 'push @arr, 4.2',
X::TypeCheck,
'type constraints on my type @arr works (2)';
}
{
my int @arr[5] = <1 2 3 4 5>;
is(@arr, <1 2 3 4 5>, 'my Type @arr[num] works');
throws-like 'push @arr, 123',
X::IllegalOnFixedDimensionArray,
operation => 'push';
throws-like 'pop @arr',
X::IllegalOnFixedDimensionArray,
operation => 'pop';
throws-like q[push @arr, 's'],
X::IllegalOnFixedDimensionArray,
operation => 'push';
throws-like 'push @arr, 4.2',
X::IllegalOnFixedDimensionArray,
operation => 'push';
}
# RT 126800
{
throws-like 'my @a[0]',
X::IllegalDimensionInShape;
throws-like 'my @a[-9999999999999999]',
X::IllegalDimensionInShape;
throws-like 'my @a[-9223372036854775808,-2]',
X::IllegalDimensionInShape;
}
# RT #130513
subtest '.List on uninited shaped array' => {
plan 2;
my @a[2;2];
my @result;
lives-ok { @result = @a.List }, 'does not die';
is-deeply @result, [Any xx 4], 'gives correct results';
}
# RT #130510
eval-lives-ok 「my @c[2;2] .= new(:shape(2, 2), <a b>, <c d>)」,
'@c[some shape] accepts a .new: :shape(same shape)...';
# RT #130440
{
my @a[1;1]; for @a.pairs -> $p { $p.value = 42 };
is-deeply @a[0;0], 42,
'@shaped-array.pairs provides with writable container in .value';
my @b[1]; for @b.values <-> $a { $a = 42 };
is-deeply @b[0], 42,
'@shaped-array.values provides with writable containers';
}
# RT #130451
{
my @array[8];
is-deeply ($_ for @array), (Any for 1..8), "For statement across simple uninitialized shaped array";
is-deeply (for @array { $_ }), (Any for 1..8), "For loop across simple uninitialized shaped array";
is-deeply (@array.map: {$_}), (Any for 1..8), ".map of simple uninitialized shaped array";
}
# https://irclog.perlgeek.de/perl6/2017-03-20#i_14297219
#?rakudo.jvm skip 'Cannot modify an immutable Int'
{
# Z= for shape filling
my int @a[2;3] Z= 0..5;
is +@a, 2, 'Z= shape filling';
is @a[0;1], 1, 'Z= shape filling';
is @a[1;2], 5, 'Z= shape filling';
}