Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 303 lines (261 sloc) 10.213 kB
57e7ad8 [t] move some junction tests
moritz authored
1 use v6;
2 use Test;
3
000d727 [t/spec] tests for RT #75368 and new spec wrt autothreading over nega…
moritz authored
4 plan 83;
57e7ad8 [t] move some junction tests
moritz authored
5
6 {
ffa7511 [t/spec/] "Junction" -> "junction", ".eigenstates" -> "!eigenstates" …
Kodi authored
7 # Solves the equation A + B = A * C for integers
57e7ad8 [t] move some junction tests
moritz authored
8 # by autothreading over all interesting values
9
10 my $n = 0;
11 sub is_it($a, $b, $c) {
12 $n++;
13 if ($a != $b && $b != $c && $a != $c &&
14 $a * 10 + $c == $a + $b ) {
ffa7511 [t/spec/] "Junction" -> "junction", ".eigenstates" -> "!eigenstates" …
Kodi authored
15 return "$a + $b = $a$c";
57e7ad8 [t] move some junction tests
moritz authored
16 } else {
ffa7511 [t/spec/] "Junction" -> "junction", ".eigenstates" -> "!eigenstates" …
Kodi authored
17 return ();
57e7ad8 [t] move some junction tests
moritz authored
18 }
19 }
20
21 # note that since the junction is not evaluated in boolean context,
22 # it's not collapsed, and the auto-threading may not abort prematurely
23 # when a result is found.
24 my $answer = is_it(any(1..2), any(7..9), any(0..6));
25 is($n, 42, "called lots of times :-)");
d11fdec [t/spec] avoid autothreading through ok() in three test files
moritz authored
26 ok( ?($answer eq "1 + 9 = 10"), "found right answer");
57e7ad8 [t] move some junction tests
moritz authored
27 }
28
5c2c4cd [t/spec] Few more tests for auto-threading, these ones covering auto-…
jnthn authored
29 {
30 # Checks auto-threading works on method calls too, and that we get the
31 # right result.
32 class Foo {
33 has $.count = 0;
34 method test($x) { $!count++; return $x }
35 }
36
37 my ($x, $r, $ok);
38 $x = Foo.new;
39 $r = $x.test(1|2);
40 is($x.count, 2, 'method called right number of times');
41 $ok = $r.perl.subst(/\D/, '', :g) eq '12' | '21';
42 ok(?$ok, 'right values passed to method');
43
44 $x = Foo.new;
45 $r = $x.test(1 & 2 | 3);
46 is($x.count, 3, 'method called right number of times');
47 $ok = $r.perl.subst(/\D/, '', :g) eq '123' | '213' | '312' | '321'; # e.g. & values together
48 ok(?$ok, 'junction structure maintained');
49 }
50
de5b1fb [t/spec] A bunch of tests for junctional dispatch with multi-subs (wi…
jnthn authored
51 {
52 # Check auto-threding works right on multi-subs.
53 my $calls_a = 0;
54 my $calls_b = 0;
55 my $calls_c = 0;
56 my ($r, $ok);
57 multi mstest(Int $x) { $calls_a++; return $x }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored
58 multi mstest(Str $x, Str $y) { $calls_b++ } #OK not used
59 multi mstest(Str $x) { $calls_c++ } #OK not used
de5b1fb [t/spec] A bunch of tests for junctional dispatch with multi-subs (wi…
jnthn authored
60 $r = mstest(1&2 | 3);
61 is($calls_a, 3, 'correct multi-sub called right number of times');
62 is($calls_b, 0, 'incorrect multi-sub not called');
63 is($calls_c, 0, 'incorrect multi-sub not called');
64 $ok = $r.perl.subst(/\D/, '', :g) eq '123' | '213' | '312' | '321'; # e.g. & values together
65 ok(?$ok, 'junction structure maintained');
66
67 $calls_a = 0;
68 $calls_b = 0;
69 $calls_c = 0;
70 mstest("a" | "b", "c" & "d");
71 is($calls_b, 4, 'correct multi-sub called right number of times');
72 is($calls_a, 0, 'incorrect multi-sub not called');
73 is($calls_c, 0, 'incorrect multi-sub not called');
74
75 $calls_a = 0;
76 $calls_b = 0;
77 $calls_c = 0;
78 mstest('a' | 1 & 'b');
79 is($calls_a, 1, 'correct multi-sub called right number of times (junction of many types)');
80 is($calls_c, 2, 'correct multi-sub called right number of times (junction of many types)');
81 is($calls_b, 0, 'incorrect multi-sub not called');
82
83 # Extra sanity, in case some multi-dispatch caching issues existed.
84 $calls_a = 0;
85 $calls_b = 0;
86 $calls_c = 0;
87 mstest('a' | 1 & 'b');
88 is($calls_a, 1, 'correct multi-sub called again right number of times (junction of many types)');
89 is($calls_c, 2, 'correct multi-sub called again right number of times (junction of many types)');
90 is($calls_b, 0, 'incorrect multi-sub again not called');
91
92 $calls_a = 0;
93 $calls_b = 0;
94 $calls_c = 0;
95 mstest('a');
96 is($calls_a, 0, 'non-junctional dispatch still works');
97 is($calls_b, 0, 'non-junctional dispatch still works');
98 is($calls_c, 1, 'non-junctional dispatch still works');
99 }
5c2c4cd [t/spec] Few more tests for auto-threading, these ones covering auto-…
jnthn authored
100
de5b1fb [t/spec] A bunch of tests for junctional dispatch with multi-subs (wi…
jnthn authored
101 {
102 # Check auto-threading with multi-methods. Basically a re-hash of the
103 # above, but in a class.
104 class MMTest {
105 has $.calls_a = 0;
106 has $.calls_b = 0;
107 has $.calls_c = 0;
108 multi method mmtest(Int $x) { $!calls_a++; return $x }
91600de [t/spec] more warning suppressions
lwall authored
109 multi method mmtest(Str $x, Str $y) { $!calls_b++ } #OK not used
110 multi method mmtest(Str $x) { $!calls_c++ } #OK not used
de5b1fb [t/spec] A bunch of tests for junctional dispatch with multi-subs (wi…
jnthn authored
111 }
112 my ($obj, $r, $ok);
113 $obj = MMTest.new();
114 $r = $obj.mmtest(1&2 | 3);
115 is($obj.calls_a, 3, 'correct multi-method called right number of times');
116 is($obj.calls_b, 0, 'incorrect multi-method not called');
117 is($obj.calls_c, 0, 'incorrect multi-method not called');
118 $ok = $r.perl.subst(/\D/, '', :g) eq '123' | '213' | '312' | '321'; # e.g. & values together
119 ok(?$ok, 'junction structure maintained');
120
121 $obj = MMTest.new();
122 $obj.mmtest("a" | "b", "c" & "d");
123 is($obj.calls_b, 4, 'correct multi-method called right number of times');
124 is($obj.calls_a, 0, 'incorrect multi-method not called');
125 is($obj.calls_c, 0, 'incorrect multi-method not called');
126
127 $obj = MMTest.new();
128 $obj.mmtest('a' | 1 & 'b');
129 is($obj.calls_a, 1, 'correct multi-method called right number of times (junction of many types)');
130 is($obj.calls_c, 2, 'correct multi-method called right number of times (junction of many types)');
131 is($obj.calls_b, 0, 'incorrect multi-method not called');
132 }
62c7abe [t/spec] Tests for junction auto-threading and named parameters - fud…
jnthn authored
133
134 {
135 # Ensure named params in single dispatch auto-thread.
136 my $count = 0;
137 my @got;
138 sub nptest($a, :$b, :$c) { $count++; @got.push($a ~ $b ~ $c) }
139 my $r = nptest(1, c => 4|5, b => 2|3);
140 is($count, 4, 'auto-threaded over named parameters to call sub enough times');
141 @got .= sort;
142 is(@got.elems, 4, 'got array of right size to check what was called');
143 is(@got[0], '124', 'called with correct parameters');
144 is(@got[1], '125', 'called with correct parameters');
145 is(@got[2], '134', 'called with correct parameters');
146 is(@got[3], '135', 'called with correct parameters');
147 }
148
149 {
150 # Ensure named params in multi dispatch auto-thread.
151 my $count_a = 0;
152 my $count_b = 0;
153 my @got;
154 multi npmstest(Int $a, :$b, :$c) { $count_a++; @got.push($a ~ $b ~ $c) }
155 multi npmstest(Str $a, :$b, :$c) { $count_b++; @got.push($a ~ $b ~ $c) }
156 my $r = npmstest(1&'a', c => 2|3, b => 1);
157 is($count_a, 2, 'auto-threaded over named parameters to call multi-sub variant enough times');
158 is($count_b, 2, 'auto-threaded over named parameters to call multi-sub variant enough times');
159 @got .= sort;
160 is(@got.elems, 4, 'got array of right size to check what was called');
161 is(@got[0], '112', 'called with correct parameters');
162 is(@got[1], '113', 'called with correct parameters');
163 is(@got[2], 'a12', 'called with correct parameters');
164 is(@got[3], 'a13', 'called with correct parameters');
165 }
f914433 [t/spec] Various, though certainly not exhaustive, tests for auto-thr…
jnthn authored
166
6e4df83 [t/spec] Fudge some of the auto-threading tests that rely on class at…
jnthn authored
167 #?rakudo skip 'class attributes NYI'
f914433 [t/spec] Various, though certainly not exhaustive, tests for auto-thr…
jnthn authored
168 {
169 # Auto-threading over an invocant.
170 class JuncInvTest1 {
171 my $.cnt is rw = 0;
172 method a { $.cnt++; }
173 has $.n;
174 method d { 2 * $.n }
175 }
176 class JuncInvTest2 {
177 my $.cnt is rw = 0;
178 method a { $.cnt++; }
91600de [t/spec] more warning suppressions
lwall authored
179 method b($x) { $.cnt++ } #OK not used
f914433 [t/spec] Various, though certainly not exhaustive, tests for auto-thr…
jnthn authored
180 }
181
182 my $x = JuncInvTest1.new | JuncInvTest1.new | JuncInvTest2.new;
183 $x.a;
184 is JuncInvTest1.cnt, 2, 'basic auto-threading over invocant works';
185 is JuncInvTest2.cnt, 1, 'basic auto-threading over invocant works';
186
187 JuncInvTest1.cnt = 0;
188 JuncInvTest2.cnt = 0;
189 $x = JuncInvTest1.new | JuncInvTest2.new & JuncInvTest2.new;
190 $x.a;
191 is JuncInvTest1.cnt, 1, 'auto-threading over invocant of nested junctions works';
192 is JuncInvTest2.cnt, 2, 'auto-threading over invocant of nested junctions works';
193
194 $x = JuncInvTest1.new(n => 1) | JuncInvTest1.new(n => 2) & JuncInvTest1.new(n => 4);
195 my $r = $x.d;
8ed5f61 [t/spec] many small improvements:
moritz authored
196 my $ok = ?($r.perl.subst(/\D/, '', :g) eq '248' | '284' | '482' | '842');
f914433 [t/spec] Various, though certainly not exhaustive, tests for auto-thr…
jnthn authored
197 ok($ok, 'auto-threading over invocant produced correct junctional result');
198
199 JuncInvTest2.cnt = 0;
200 $x = JuncInvTest2.new | JuncInvTest2.new;
201 $x.b('a' | 'b' | 'c');
202 is JuncInvTest2.cnt, 6, 'auto-threading over invocant and parameters works';
203 }
147ede3 [t/spec] more autothreading tests (for .values and prefix:<+>)
moritz authored
204
205 # test that various things autothread
206
207 {
46b247a [t/spec] clean up some junction testing mess
moritz authored
208 my $j = [1, 2] | 5;
147ede3 [t/spec] more autothreading tests (for .values and prefix:<+>)
moritz authored
209
210 ok ?( +$j == 5 ), 'prefix:<+> autothreads (1)';
211 ok ?( +$j == 2 ), 'prefix:<+> autothreads (2)';
212 ok !( +$j == 3 ), 'prefix:<+> autothreads (3)';
213 }
9a049c7 [t/spec] autothreading prime test
moritz authored
214
215 # this is nothing new, but it's such a cool example for
216 # autothreading that I want it to be in the test suite nonetheless ;-)
217 {
218 sub primetest(Int $n) {
219 ?(none(2..$n) * any(2..$n) == $n);
220 };
221
222 # 2 3 4 5 6 7 8 9 10 11 12 13 14 15
223 my @is_prime = (1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0);
224
225 for @is_prime.kv -> $idx, $ref {
226 is +primetest($idx + 2), $ref, "primality test for { $idx + 2 } works";
227 }
228 }
5b43411 [t] merge array_deref.t into autothreading.t
moritz authored
229
230
231 #?pugs skip 'autothreading over array indexing'
232 {
233 my $junc = 0|1|2;
234 my @a = (0,1,2);
235 my $bool = Bool::False;
236 ok ?(@a[$junc] == $junc), 'can autothread over array indexes';
237 }
d505e37 [t] merge junction/s09eg.t into autothreading.t
moritz authored
238
239 # Tests former autothreading junction example from Synopsis 09
240 {
241 my $c = 0;
242
243 is(substr("camel", 0, 2), "ca", "substr()");
244
245 $c = 0;
246 sub my_substr ($str, $i, $j) {
247 $c++;
9c2b9a8 [t/spec] revert r26201 (except the part that touches TASKS)
moritz authored
248 my @c = split "", $str;
d505e37 [t] merge junction/s09eg.t into autothreading.t
moritz authored
249 join("", @c[$i..($i+$j-1)]);
250 }
251
252 my $j = my_substr("camel", 0|1, 2&3);
253
254 is($c, 4, "substr() called 4 times");
255 }
8176a9b [t/spec] tests for RT #65096, autothreading over Array parameters
moritz authored
256
257 # test autothreading while passing arrays:
258 {
259 sub my_elems(@a) {
260 @a.elems;
261 }
262 ok !(my_elems([2, 3]|[4, 5, 6]) == 1),
263 'autothreading over array parameters (0)';
264 ok ?(my_elems([2, 3]|[4, 5, 6]) == 2),
265 'autothreading over array parameters (1)';
266 ok ?(my_elems([2, 3]|[4, 5, 6]) == 3),
267 'autothreading over array parameters (2)';
268 ok !(my_elems([2, 3]|[4, 5, 6]) == 4),
269 'autothreading over array parameters (3)';
270 }
271
82332a3 [t/spec] fix some smartlinks, and a broken test in autothreading.t
moritz authored
272 # L<S02/Undefined types/"default block parameter type">
0e3afce [t/TASKS] update; [t/spec] default block parameter type is Object
moritz authored
273
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
274 # block parameters default to Mu, so test that they don't autothread:
0e3afce [t/TASKS] update; [t/spec] default block parameter type is Object
moritz authored
275 {
276 my $c = 0;
277 for 1|2, 3|4, 5|6 -> $x {
278 $c++;
279 }
280 is $c, 3, 'do not autothread over blocks by default';
281 $c = 0;
282 for 1|2, 3|4, 5|6 -> Any $x {
283 $c++;
284 }
82332a3 [t/spec] fix some smartlinks, and a broken test in autothreading.t
moritz authored
285 is $c, 6, 'do autothread over blocks with explicit Any';
0e3afce [t/TASKS] update; [t/spec] default block parameter type is Object
moritz authored
286 }
287
000d727 [t/spec] tests for RT #75368 and new spec wrt autothreading over nega…
moritz authored
288 # used to be RT #75368
289 # L<S03/Junctive operators/Use of negative operators with junctions>
290 {
291 my Mu $x = 'a' ne ('a'|'b'|'c');
292 ok $x ~~ Bool, 'infix:<ne> collapses the junction (1)';
293 ok $x !~~ Junction, 'infix:<ne> collapses the junction (2)';
294 nok $x, '... and the result is False';
295
296 my Mu $y = 'a' !eq ('a'|'b'|'c');
297 ok $y ~~ Bool, 'infix:<!eq> collapses the junction (1)';
298 ok $y !~~ Junction, 'infix:<!eq> collapses the junction (2)';
299 nok $y, '... and the result is False';
300 }
301
8176a9b [t/spec] tests for RT #65096, autothreading over Array parameters
moritz authored
302 # vim: ft=perl6
Something went wrong with that request. Please try again.