-
Notifications
You must be signed in to change notification settings - Fork 134
/
race.t
98 lines (78 loc) 路 2.72 KB
/
race.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
use v6;
use Test;
plan 12;
{
my @result = <a b c d e f g>.race.map({ $_.uc });
is @result.sort, <A B C D E F G>, "can racemap a simple code block";
@result = (^50).list.race.map({ $_ * 2 });
is @result.sort, (^50).map({$_*2}), "race over a big-ish list of Ints";
}
{
my @result = (^50).list.race.map({ $_ * 2 }).map({ $_ div 2 });
is @result.sort, (^50).list, "two-stage map over big-ish Int list";
@result = <a b c d e f g>.list.race.map({ $_.uc }).map({ $_ x 2 });
is @result.sort, <AA BB CC DD EE FF GG>, "two-stage map over some strings";
}
{
my @result = (50..100).list.race.grep({ $_ %% 10 });
is @result.sort, (50, 60, 70, 80, 90, 100), "race + grep";
}
{
my @result = (^100).list.race.grep({ $_.is-prime }).map({ $_ * $_ });
is @result.sort, (4, 9, 25, 49, 121, 169, 289, 361, 529, 841, 961, 1369, 1681, 1849, 2209, 2809, 3481, 3721, 4489, 5041, 5329, 6241, 6889, 7921, 9409), "race + grep + map";
}
#?rakudo.jvm skip 'hangs'
{
# race + map done in reverse
my @promises;
for 0..4 {
@promises.push(Promise.new);
}
my $last = Promise.new;
@promises.push($last);
$last.keep;
# 0 1 2 3 4 5
# [ Planned Planned Planned Planned Planned Kept ]
# V==V V=V
my @result = (1..5).list.race( degree => 5, batch => 1 ).map({
# wait our turn
await @promises[$_];
# allow the next lower one to proceed
@promises[$_ - 1].keep;
$_; # <==
});
# @result should nearly be in reversed order, so sort it for the test
is @result.sort(), (1, 2, 3, 4, 5), "test that race + map in reverse returns correct values";
}
{
# race + grep done in reverse
my @promises;
for 0..4 {
@promises.push(Promise.new);
}
my $last = Promise.new;
@promises.push($last);
$last.keep;
# 0 1 2 3 4 5
# [ Planned Planned Planned Planned Planned Kept ]
# V==V V==V
my @result = (1..5).list.race( degree => 5, batch => 1 ).grep({
# wait our turn
await @promises[$_];
# allow the next lower one to proceed
@promises[$_ - 1].keep;
True; # <==
});
# @result should nearly be in reversed order, so sort it for the test
is @result.sort(), (1, 2, 3, 4, 5), "test that race + grep in reverse returns correct values";
}
# RT #127191
{
for <batch degree> -> $name {
for (-1,0) -> $value {
throws-like { ^10 .race(|($name => $value)) }, X::Invalid::Value,
:method<race>, :$name, :$value,
"cannot have a $name of $value for hyper";
}
}
}