Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 240 lines (214 sloc) 9.104 kB
5ebd7de [t/spec] add tests for typed arrays
moritz authored
1 use v6;
2 use Test;
3
8dd0ebb @usev6 Add test for RT #81682
usev6 authored
4 plan 76;
5ebd7de [t/spec] add tests for typed arrays
moritz authored
5
6 # L<S09/Typed arrays/>
7
8 {
9 my Int @x;
7613807 @lizmat Some more .of -> .VAR.of I missed
lizmat authored
10 ok @x.VAR.of === Int, '@x.VAR.of of typed array (my Int @x)';
77b4e58 @moritz RT #77748, type object gistification of typed array
moritz authored
11 # RT #77748
12 ok @x.WHAT.gist ~~ /Array/, '.WHAT.gist of the type object makes sense';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
13 lives-ok { @x = 1, 2, 3 }, 'can assign values of the right type';
14 lives-ok { @x = 1..3 }, 'can assign range of the right type';
15 lives-ok { @x.push: 3, 4}, 'can push values of the right type';
16 lives-ok { @x.unshift: 3}, 'can unshift values of the right type';
17 lives-ok { @x[0, 2] = 2, 3}, 'can assign values to a slice';
5ebd7de [t/spec] add tests for typed arrays
moritz authored
18 @x = 2, 3, 4;
19 is @x.pop, 4, 'can pop from typed array';
7f074a5 [t/spec] Fudged and fixed S09-typed-arrays/arrays.t
arnsholt authored
20 is @x.unshift(2), [2, 2, 3], 'can unshift from typed array';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
21 } #9
5ebd7de [t/spec] add tests for typed arrays
moritz authored
22
23 {
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
24 my Int @x;
25 ok @x.VAR.of === Int, '@x.VAR.of of typed array (my Int @x)';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
26 lives-ok { @x = 1, 2, 3 }, 'can assign values of the right type (Int @x)';
27 lives-ok { @x = 1..3 }, 'can assign range of the right type (Int @x)';
28 lives-ok { @x.push: 3, 4}, 'can push values of the right type (Int @x)';
29 lives-ok { @x.unshift: 3}, 'can unshift values of the right type (Int @x)';
30 lives-ok { @x[0, 2] = 2, 3}, 'can assign values to a slice (Int @x)';
5ebd7de [t/spec] add tests for typed arrays
moritz authored
31 @x = 2, 3, 4;
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
32 is @x.pop, 4, 'can pop from typed array (Int @x)';
33 is @x.unshift(1), [1, 2, 3], 'can unshift from typed array (Int @x)';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
34 } #8
5ebd7de [t/spec] add tests for typed arrays
moritz authored
35
26af8f8 @usev6 Add test for RT #114968
usev6 authored
36 # initialization
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
37 {
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
38 lives-ok { my Int @x = 1, 2, 3 }, 'initialization of typed array';
39 lives-ok { my Int @x = 1 .. 3 }, 'initialization of typed array from range';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
40 } #2
5ebd7de [t/spec] add tests for typed arrays
moritz authored
41
42 {
43 my @x of Int;
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
44 ok @x.VAR.of === Int, '@x.VAR.of of typed array (my @x of Int)';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
45 lives-ok { @x = 1, 2, 3 }, 'can assign values of the right type (@x of Int)';
46 lives-ok { @x = 1..3 }, 'can assign range of the right type (@x of Int)';
47 lives-ok { @x.push: 3, 4}, 'can push values of the right type (@x of Int)';
48 lives-ok { @x.unshift: 3}, 'can unshift values of the right type (@x of Int)';
49 lives-ok { @x[0, 2] = 2, 3}, 'can assign values to a slice (@x of Int)';
5ebd7de [t/spec] add tests for typed arrays
moritz authored
50 @x = 2, 3, 4;
51 is @x.pop, 4, 'can pop from typed array (@x of Int)';
26af8f8 @usev6 Add test for RT #114968
usev6 authored
52
44a60c0 [t/spec] push and unshift return the array, not .elems
moritz authored
53 ok @x.unshift, 'can unshift from typed array (@x of Int)';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
54 } #8
5ebd7de [t/spec] add tests for typed arrays
moritz authored
55
56 {
57 my Array @x;
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
58 ok @x.VAR.of === Array, '@x.VAR.of of typed array (my Array @x)';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
59 dies-ok { @x = 1, 2, 3 }, 'can not assign values of the wrong type';
60 dies-ok { @x = 1..3 }, 'can not assign range of the wrong type';
61 dies-ok { @x.push: 3, 4}, 'can not push values of the wrong type';
62 dies-ok { @x.unshift: 3}, 'can not unshift values of the wrong type';
63 dies-ok { @x[0, 2] = 2, 3},
5ebd7de [t/spec] add tests for typed arrays
moritz authored
64 'can not assign values of wrong type to a slice';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
65 lives-ok { @x = [1, 2], [3, 4] },
5ebd7de [t/spec] add tests for typed arrays
moritz authored
66 '... but assigning values of the right type is OK';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
67 } #7
75ede61 [t/spec] Tests for of trait verb on variables (some unfudges, some new).
jnthn authored
68
69 {
70 my @x of Array;
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
71 ok @x.VAR.of === Array, '@x.VAR.of of typed array (my @x of Array)';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
72 dies-ok { @x = 1, 2, 3 }, 'can not assign values of the wrong type';
73 dies-ok { @x = 1..3 }, 'can not assign range of the wrong type';
74 dies-ok { @x.push: 3, 4}, 'can not push values of the wrong type';
75 dies-ok { @x.unshift: 3}, 'can not unshift values of the wrong type';
76 dies-ok { @x[0, 2] = 2, 3},
75ede61 [t/spec] Tests for of trait verb on variables (some unfudges, some new).
jnthn authored
77 'can not assign values of wrong type to a slice';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
78 lives-ok { @x = [1, 2], [3, 4] },
75ede61 [t/spec] Tests for of trait verb on variables (some unfudges, some new).
jnthn authored
79 '... but assigning values of the right type is OK';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
80 } #7
5ebd7de [t/spec] add tests for typed arrays
moritz authored
81
7d0ccbd [t/spec] tests for 'my Int @x; @x.of'; tests for typed nested arrays
moritz authored
82 {
83 my Array of Int @x;
7613807 @lizmat Some more .of -> .VAR.of I missed
lizmat authored
84 ok @x.VAR.of === Array[Int], 'my Array of Int @x declares a nested array';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
85 #?rakudo skip "nested typechecks are borked"
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
86 lives-ok { @x = [2, 3], [5, 6] }, 'assignment works';
7b2783d @lizmat Unfudge / fix "of Type" related tests
lizmat authored
87 #?rakudo todo "nested typechecks are borked"
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
88 lives-ok { @x.push: [8, 9] }, 'pushing works';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
89 dies-ok { @x.push: 8 }, 'type constraint is enforced';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
90 lives-ok { @x[0].push: 3 }, 'pushing to the inner array is OK';
c2ace96 @lizmat Fudge wonky test that got borked by fixing #125428
lizmat authored
91 #?rakudo todo "nested typechecks are borked"
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
92 dies-ok { @x[0].push: 'foo' }, 'inner array enforces the type constraint';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
93 } #6
ba40048 [t/spec] methods on typed arrays should return typed arrays
moritz authored
94
95 # test that lists/arrays returned from array methods are typed as well
96 {
97 my Int @a = 1, 2, 3;
98 my Int @b;
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
99 lives-ok { @b = @a }, 'can assign typed array to typed array';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
100 #?rakudo todo 'need parameterized Lists'
7613807 @lizmat Some more .of -> .VAR.of I missed
lizmat authored
101 ok @a.values.VAR.of.WHICH eqv Int.WHICH, '@a.values is typed (1)';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
102 lives-ok { @b = @a.values }, '@a.values is typed (2)';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
103 } #3
ba40048 [t/spec] methods on typed arrays should return typed arrays
moritz authored
104
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
105 #?rakudo todo 'initialization RT #124676'
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
106 {
ba40048 [t/spec] methods on typed arrays should return typed arrays
moritz authored
107 my Str @c = <foo bar baz>;
7613807 @lizmat Some more .of -> .VAR.of I missed
lizmat authored
108 ok @c.keys.VAR.of.WHICH eqv Str.WHICH, '@array.keys is typed with Str';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
109 } #1
ba40048 [t/spec] methods on typed arrays should return typed arrays
moritz authored
110
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
111 # test that we can have parametric array return types
112 {
113 sub ret_pos_1 returns Positional of Int { my Int @a = 1,2,3; return @a; }
114 sub ret_pos_2 returns Positional of Int { my Int @a = 1,2,3; @a }
115 sub ret_pos_3 returns Positional of Int { my @a = 1,2,3; return @a; }
116 sub ret_pos_4 returns Positional of Int { my @a = 1,2,3; @a }
117 sub ret_pos_5 returns Positional of Int { my Num @a = 1,2,3; return @a; }
118 sub ret_pos_6 returns Positional of Int { my Num @a = 1,2,3; @a }
801ec1e @jnthn Correct a couple of tests; Int !~~ Num.
jnthn authored
119 sub ret_pos_7 returns Positional of Numeric { my Int @a = 1,2,3; return @a; }
120 sub ret_pos_8 returns Positional of Numeric { my Int @a = 1,2,3; @a }
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
121 lives-ok { ret_pos_1() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
122 'type check Positional of Int allows correctly typed array to be returned explicitly';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
123 lives-ok { ret_pos_2() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
124 'type check Positional of Int allows correctly typed array to be returned implicitly';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
125 dies-ok { ret_pos_3() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
126 'type check Positional of Int prevents untyped array to be returned explicitly';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
127 dies-ok { ret_pos_4() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
128 'type check Positional of Int prevents untyped array to be returned implicitly';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
129 dies-ok { ret_pos_5() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
130 'type check Positional of Int prevents incorrectly typed array to be returned explicitly';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
131 dies-ok { ret_pos_6() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
132 'type check Positional of Int prevents incorrectly typed array to be returned implicitly';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
133 lives-ok { ret_pos_7() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
134 'type check Positional of Num allows subtyped Int array to be returned explicitly';
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
135 lives-ok { ret_pos_8() },
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
136 'type check Positional of Num allows subtyped Int array to be returned implicitly';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
137 } #8
5f186a9 [t/spec] Tests for returning an array on a sub with a constraint like…
jnthn authored
138
ccb6769 [t/spec] test for RT #69482: type constraints on arrays leaked to sub…
moritz authored
139 # RT #69482
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
140 #?rakudo skip 'type on our-variables RT #124677'
ccb6769 [t/spec] test for RT #69482: type constraints on arrays leaked to sub…
moritz authored
141 {
142 our Int @a1;
143 our @a2;
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
144 lives-ok { @a2[0] = 'string' },
ccb6769 [t/spec] test for RT #69482: type constraints on arrays leaked to sub…
moritz authored
145 'Can assign to untyped package array in presence of typed array';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
146 } #1
ccb6769 [t/spec] test for RT #69482: type constraints on arrays leaked to sub…
moritz authored
147
26af8f8 @usev6 Add test for RT #114968
usev6 authored
148 # RT #71958
2db7c0f [t/spec] Test for RT 71958: array type constraint leaks
kyle authored
149 {
150 class RT71958 {
151 has @.rt71958 is rw;
152 }
153 my Int @typed_array;
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
154 lives-ok { RT71958.new().rt71958[0] = RT71958.new() },
2db7c0f [t/spec] Test for RT 71958: array type constraint leaks
kyle authored
155 'can assign to untyped array in presence of typed array';
a30383c @lizmat Unfudge/correct now passing tests
lizmat authored
156 } #1
2db7c0f [t/spec] Test for RT 71958: array type constraint leaks
kyle authored
157
26af8f8 @usev6 Add test for RT #114968
usev6 authored
158 # RT #114968
159 {
08b5de5 @lizmat s/throws_like/throws-like/g
lizmat authored
160 throws-like 'my Int @a = "ab", "cd"', X::TypeCheck::Assignment,
26af8f8 @usev6 Add test for RT #114968
usev6 authored
161 'typed arrays do check type during list assignment';
162 }
163
ece6378 @usev6 Add test for RT #119061
usev6 authored
164 # RT #119061
165 {
166 my Int @a;
08b5de5 @lizmat s/throws_like/throws-like/g
lizmat authored
167 throws-like { @a[@a.elems] = "a"; }, X::TypeCheck::Assignment,
ece6378 @usev6 Add test for RT #119061
usev6 authored
168 'assignment checks for types';
169 }
170
e9694a7 @usev6 Add test for RT #125428
usev6 authored
171 # RT #125428
172 {
173 subset Y of Int where 1..10;
174 my Y @x;
175 @x.push: 10;
176 throws-like '@x[0]++', X::TypeCheck,
177 'pushed value to typed array (using "subset") is type checked';
178 }
179
9e509a2 @lizmat Add tests for roundtripping typed arrays
lizmat authored
180 {
181 my Int @a; @a[2] = 42;
182 my @b := @a.perl.EVAL;
183 is @b.of, Int, 'does the roundtrip preserve typedness';
184 is @a, @b, 'do typed arrays with empty elements roundtrip';
185 }
2db7c0f [t/spec] Test for RT 71958: array type constraint leaks
kyle authored
186
b421638 @jnthn Test for RT#66892.
jnthn authored
187 # RT #66892
188 {
189 sub foo(--> Array of Str) {
190 my Str @a = <foo bar baz>;
191 @a
192 };
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
193 lives-ok { foo() }, 'Array of Str works as return constraint';
b421638 @jnthn Test for RT#66892.
jnthn authored
194 ok foo().of === Str, 'Get back the typed array correctly (1)';
195 is foo(), Array[Str].new('foo', 'bar', 'baz'),
196 'Get back the typed array correctly (2)';
197 }
198
5f5f5fb @Mouq Add tests for #120506
Mouq authored
199 # RT #120506
200 {
201 my @RT120506-bind := Array[Array[Bool]].new($(Array[Bool].new(True, False, True)), $(Array[Bool].new(True)));
427f1dc @xwang62 RT120506, nypm hackathon,skip and document failed array type, review …
xwang62 authored
202 #?rakudo todo 'unexpectedly getting array Array[Bool].new'
a87fde4 @lizmat s/is_deeply/is-deeply/g
lizmat authored
203 is-deeply @RT120506-bind[0, 1]».Parcel, ((True, False, True), (True,)),
5f5f5fb @Mouq Add tests for #120506
Mouq authored
204 "Can feed Arrays of Type to .new of Array[Array[Type]] (binding)";
205 is @RT120506-bind[0].WHAT, Array[Bool], "Type is maintained (binding)";
206
207 my Array of Bool @RT120506-assign .= new($(Array[Bool].new(True, False, True)), $(Array[Bool].new(True)));
427f1dc @xwang62 RT120506, nypm hackathon,skip and document failed array type, review …
xwang62 authored
208 #?rakudo todo 'unexpectedly getting array Array[Bool].new'
a87fde4 @lizmat s/is_deeply/is-deeply/g
lizmat authored
209 is-deeply @RT120506-assign[0, 1]».Parcel, ((True, False, True), (True,)),
5f5f5fb @Mouq Add tests for #120506
Mouq authored
210 "Can feed Arrays of Type to .new of Array[Array[Type]] (assignment)";
211 is @RT120506-assign[0].WHAT, Array[Bool], "Type is maintained (assignment)";
212 }
213
df6e66b @Mouq Add test for RT #121804
Mouq authored
214 # RT #121804
215 {
216 sub RT121804 returns Array of Hash {
217 my %a1 = :x<y>, :y<z>, :w<c>;
218 my %a2 =:x<y>, :y<t>, :w<c>;
219 my %a3 = :x<y>, :y<z>, :w<h>;
220 my Hash @array1 = $%a1, $%a2, $%a3;
221 }
a87fde4 @lizmat s/is_deeply/is-deeply/g
lizmat authored
222 is-deeply RT121804, Array[Hash].new({:x<y>, :y<z>, :w<c>}, {:x<y>, :y<t>, :w<c>}, {:x<y>, :y<z>, :w<h>}),
df6e66b @Mouq Add test for RT #121804
Mouq authored
223 "Can assign to and return Array[Hash] from type-constrained sub";
224 }
225
8dd0ebb @usev6 Add test for RT #81682
usev6 authored
226 # RT #81682
227 {
228 my Int @rt81682 = ^3;
229 my Int $x = 5;
230 @rt81682[0] := $x;
231 is ~@rt81682, '5 1 2',
232 'can bind element of typed array to scalar container of same type';
233 my $y = 6;
234 @rt81682[2] := $y;
235 is ~@rt81682, '5 1 6',
806ed2b @coke fix typo
coke authored
236 'can bind element of typed array to scalar container of same type (but not explicitly typed)';
8dd0ebb @usev6 Add test for RT #81682
usev6 authored
237 }
238
ba40048 [t/spec] methods on typed arrays should return typed arrays
moritz authored
239 # vim: ft=perl6
Something went wrong with that request. Please try again.