Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 214 lines (179 sloc) 6.974 kb
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
1 use v6;
2 use Test;
e122112 [t/spec] fix reduce-metaop.t, moritz--
moritz authored
3
09a80e6 [t/spec] Add two tests for lazy triangle reduce.
colomon authored
4 plan 83;
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
5
6 =begin pod
7
8 =head1 DESCRIPTION
9
10 This test tests the C<[...]> reduce metaoperator.
11
12 Reference:
13 L<"http://groups.google.de/group/perl.perl6.language/msg/bd9eb275d5da2eda">
14
15 =end pod
16
17 # L<S03/"Reduction operators">
18
19 # [...] reduce metaoperator
20 {
21 my @array = <5 -3 7 0 1 -9>;
22 my $sum = 5 + -3 + 7 + 0 + 1 + -9; # laziness :)
23
24 is(([+] @array), $sum, "[+] works");
25 is(([*] 1,2,3), (1*2*3), "[*] works");
26 is(([-] 1,2,3), (1-2-3), "[-] works");
27 is(([/] 12,4,3), (12/4/3), "[/] works");
28 is(([**] 2,2,3), (2**2**3), "[**] works");
5d1999e [t/spec] a test for [%] (meta reduce modulo)
moritz authored
29 is(([%] 13,7,4), (13%7%4), "[%] works");
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
30
31 is((~ [\+] @array), "5 2 9 9 10 1", "[\\+] works");
32 is((~ [\-] 1, 2, 3), "1 -1 -4", "[\\-] works");
33 }
34
35 {
36 is ([~] <a b c d>), "abcd", "[~] works";
37 is (~ [\~] <a b c d>), "a ab abc abcd", "[\\~] works";
38 }
39
40 {
41 ok ( [<] 1, 2, 3, 4), "[<] works (1)";
42 ok (not [<] 1, 3, 2, 4), "[<] works (2)";
43 ok ( [>] 4, 3, 2, 1), "[>] works (1)";
44 ok (not [>] 4, 2, 3, 1), "[>] works (2)";
45 ok ( [==] 4, 4, 4), "[==] works (1)";
46 ok (not [==] 4, 5, 4), "[==] works (2)";
47 ok ( [!=] 4, 5, 6), "[!=] works (1)";
48 ok (not [!=] 4, 4, 4), "[!=] works (2)";
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
49 }
d1ccd8b [t/spec] tests for === and =:= and their negated forms inside meta ops
moritz authored
50
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
51 {
e75736f [t/spec] tests for [eq], [ne]
moritz authored
52 ok (! [eq] <a a b a>), '[eq] basic sanity (positive)';
53 ok ( [eq] <a a a a>), '[eq] basic sanity (negative)';
54 ok ( [ne] <a b c a>), '[ne] basic sanity (positive)';
55 ok (! [ne] <a a b c>), '[ne] basic sanity (negative)';
56 ok ( [lt] <a b c e>), '[lt] basic sanity (positive)';
57 ok (! [lt] <a a c e>), '[lt] basic sanity (negative)';
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
58 }
e75736f [t/spec] tests for [eq], [ne]
moritz authored
59
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
60 #?rakudo skip "=:= NYI"
61 {
d1ccd8b [t/spec] tests for === and =:= and their negated forms inside meta ops
moritz authored
62 my ($x, $y);
63 ok ( [=:=] $x, $x, $x), '[=:=] basic sanity 1';
64 ok (not [=:=] $x, $y, $x), '[=:=] basic sanity 2';
65 ok ( [!=:=] $x, $y, $x), '[!=:=] basic sanity (positive)';
66 ok (not [!=:=] $y, $y, $x), '[!=:=] basic sanity (negative)';
67 $y := $x;
68 ok ( [=:=] $y, $x, $y), '[=:=] after binding';
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
69 }
d1ccd8b [t/spec] tests for === and =:= and their negated forms inside meta ops
moritz authored
70
e7dbb23 [t/spec] Refudge S03-metaops/reduce.t a bit so that it works again.
colomon authored
71 {
d1ccd8b [t/spec] tests for === and =:= and their negated forms inside meta ops
moritz authored
72 my $a = [1, 2];
73 my $b = [1, 2];
74
75 ok ( [===] 1, 1, 1, 1), '[===] with literals';
76 ok ( [===] $a, $a, $a), '[===] with vars (positive)';
77 ok (not [===] $a, $a, [1, 2]), '[===] with vars (negative)';
78 ok ( [!===] $a, $b, $a), '[!===] basic sanity (positive)';
79 ok (not [!===] $a, $b, $b), '[!===] basic sanity (negative)';
434eb4e [t/spec] Unfudge tests for reduction meta-operator with chaining comparr...
jnthn authored
80 }
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
81
434eb4e [t/spec] Unfudge tests for reduction meta-operator with chaining comparr...
jnthn authored
82 {
5cb1e9f [t/spec] Fix triangle form of reduce metaop tests.
bacek authored
83 is (~ [\<] 1, 2, 3, 4), "1 1 1 1", "[\\<] works (1)";
2126a8d [t/spec] Revert previous commit for reduce metaops.
bacek authored
84 is (~ [\<] 1, 3, 2, 4), "1 1 0 0", "[\\<] works (2)";
5cb1e9f [t/spec] Fix triangle form of reduce metaop tests.
bacek authored
85 is (~ [\>] 4, 3, 2, 1), "1 1 1 1", "[\\>] works (1)";
6ac9615 [t/spec] Update reduce-metaop to current spec.
bacek authored
86 is (~ [\>] 4, 2, 3, 1), "1 1 0 0", "[\\>] works (2)";
5cb1e9f [t/spec] Fix triangle form of reduce metaop tests.
bacek authored
87 is (~ [\==] 4, 4, 4), "1 1 1", "[\\==] works (1)";
88 is (~ [\==] 4, 5, 4), "1 0 0", "[\\==] works (2)";
6ac9615 [t/spec] Update reduce-metaop to current spec.
bacek authored
89 is (~ [\!=] 4, 5, 6), "1 1 1", "[\\!=] works (1)";
90 is (~ [\!=] 4, 5, 5), "1 1 0", "[\\!=] works (2)";
5cb1e9f [t/spec] Fix triangle form of reduce metaop tests.
bacek authored
91 is (~ [\**] 1, 2, 3), "3 8 1", "[\\**] (right assoc) works (1)";
92 is (~ [\**] 3, 2, 0), "0 1 3", "[\\**] (right assoc) works (2)";
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
93 }
94
95 {
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
96 my @array = (Mu, Mu, 3, Mu, 5);
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
97 is ([//] @array), 3, "[//] works";
4acefb9 [t/spec] unfudge [//] and [||] tests for rakudo
moritz authored
98 #?rakudo skip '[orelse]'
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
99 is ([orelse] @array), 3, "[orelse] works";
100 }
101
102 {
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
103 my @array = (Mu, Mu, 0, 3, Mu, 5);
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
104 is ([||] @array), 3, "[||] works";
105 is ([or] @array), 3, "[or] works";
106
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
107 # Mu as well as [//] should work too, but testing it like
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
108 # this would presumably emit warnings when we have them.
109 is (~ [\||] 0, 0, 3, 4, 5), "0 0 3 3 3", "[\\||] works";
110 }
111
bc058f2 [STD] improve parsing of reduceops
lwall authored
112 # not currently legal without an infix subscript operator
113 # {
114 # my $hash = {a => {b => {c => {d => 42, e => 23}}}};
115 # is try { [.{}] $hash, <a b c d> }, 42, '[.{}] works';
116 # }
117 #
118 # {
119 # my $hash = {a => {b => 42}};
120 # is ([.{}] $hash, <a b>), 42, '[.{}] works two levels deep';
121 # }
122 #
123 # {
124 # my $arr = [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]];
125 # is ([.[]] $arr, 1, 0, 2), 9, '[.[]] works';
126 # }
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
127
128 {
129 # 18:45 < autrijus> hm, I found a way to easily do linked list consing in Perl6
130 # 18:45 < autrijus> [=>] 1..10;
131 my $list = [=>] 1,2,3;
ed68273 [t/spec] remove usage of try as a function
moritz authored
132 is $list.key, 1, "[=>] works (1)";
133 is (try {$list.value.key}), 2, "[=>] works (2)";
134 is (try {$list.value.value}), 3, "[=>] works (3)";
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
135 }
136
137 {
21768a7 [t/spec] clean up and fudge reduce-metaop.t for rakudo.
moritz authored
138 my @array = <5 -3 7 0 1 -9>;
139 # according to http://irclog.perlgeek.de/perl6/2008-09-10#i_560910
140 # [,] returns a scalar (holding an Array)
141 my $count = 0;
142 $count++ for [,] @array;
143 is $count, 1, '[,] returns a single Array';
a246cb9 [t/spec] correct a test for [,], and unfudge a few for rakudo
moritz authored
144 ok ([,] @array) ~~ Positional, '[,] returns something Positional';
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
145 }
146
147 # Following two tests taken verbatim from former t/operators/reduce.t
148 lives_ok({my @foo = [1..3] >>+<< [1..3] >>+<< [1..3]},'Sanity Check');
149 lives_ok({my @foo = [>>+<<] ([1..3],[1..3],[1..3])},'Parse [>>+<<]');
150
151 # Check that user defined infix ops work with [...], too.
21768a7 [t/spec] clean up and fudge reduce-metaop.t for rakudo.
moritz authored
152 #?pugs todo 'bug'
8de8af6 [t/spec] partially fudge reduce.t for rakudo
moritz authored
153 #?rakudo skip 'reduce of user defined op'
21768a7 [t/spec] clean up and fudge reduce-metaop.t for rakudo.
moritz authored
154 {
155 sub infix:<more_than_plus>(Int $a, Int $b) { $a + $b + 1 }
d1ccd8b [t/spec] tests for === and =:= and their negated forms inside meta ops
moritz authored
156 is (try { [more_than_plus] 1, 2, 3 }), 8, "[...] reduce metaop works on user defined ops";
21768a7 [t/spec] clean up and fudge reduce-metaop.t for rakudo.
moritz authored
157 }
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
158
f148336 [STD vs t] user-defined prefix, infix, and postfix ops now derive new la...
lwall authored
159 # {
160 # my $arr = [ 42, [ 23 ] ];
161 # $arr[1][1] = $arr;
162 #
163 # is try { [.[]] $arr, 1, 1, 1, 1, 1, 0 }, 23, '[.[]] works with infinite data structures';
164 # }
165 #
166 # {
167 # my $hash = {a => {b => 42}};
168 # $hash<a><c> = $hash;
169 #
170 # is try { [.{}] $hash, <a c a c a b> }, 42, '[.{}] works with infinite data structures';
171 # }
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
172
173 # L<S03/"Reduction operators"/"Among the builtin operators, [+]() returns 0 and [*]() returns 1">
174
21768a7 [t/spec] clean up and fudge reduce-metaop.t for rakudo.
moritz authored
175 is( ([*]()), 1, "[*]() returns 1");
176 is( ([+]()), 0, "[+]() returns 0");
932a1bc [gsoc_spectest] reorganization of reduce tests
Auzon authored
177
2d7d862 [t/spec] Add a few tests for the one-argument case.
colomon authored
178 is( ([*] 41), 41, "[*] 41 returns 41");
179 is( ([*] 42), 42, "[*] 42 returns 42");
0e61dff [t/spec] Add a few more tests for the one-argument case.
colomon authored
180 is( ~([\*] 42), "42", "[\*] 42 returns (42)");
2d7d862 [t/spec] Add a few tests for the one-argument case.
colomon authored
181 is( ([~] 'towel'), 'towel', "[~] 'towel' returns 'towel'");
182 is( ([~] 'washcloth'), 'washcloth', "[~] 'washcloth' returns 'washcloth'");
0e61dff [t/spec] Add a few more tests for the one-argument case.
colomon authored
183 is( ([\~] 'towel'), 'towel', "[\~] 'towel' returns 'towel'");
f9ed5f4 [reduce.t] fix parsefail
lwall authored
184 ok( ([\~] 'towel') ~~ Iterable, "[\~] 'towel' returns something Iterable");
2d7d862 [t/spec] Add a few tests for the one-argument case.
colomon authored
185 is( ([<] 42), Bool::True, "[<] 42 returns true");
0e61dff [t/spec] Add a few more tests for the one-argument case.
colomon authored
186 is( ~([\<] 42), "1", "[\<] 42 returns '1'");
187 ok( ([\<] 42) ~~ Iterable, "[\<] 42 returns something Iterable");
2d7d862 [t/spec] Add a few tests for the one-argument case.
colomon authored
188
09a80e6 [t/spec] Add two tests for lazy triangle reduce.
colomon authored
189 is( ([\*] 1..*).batch(10).join(', '), '1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800',
190 'triangle reduce is lazy');
191 is( ([\R~] 'a'..*).batch(8).join(', '), 'a, ba, cba, dcba, edcba, fedcba, gfedcba, hgfedcba',
192 'triangle reduce is lazy');
193
f80ef63 [t/spec] Tests for RT #65164
kyle authored
194 # RT #65164 (TODO: implement [^^])
195 #?rakudo skip 'implement [^^]'
196 {
197 is [^^](0, 42), 42, '[^^] works (one of two true)';
198 is [^^](42, 0), 42, '[^^] works (one of two true)';
199 ok ! [^^](1, 42), '[^^] works (two true)';
200 ok ! [^^](0, 0), '[^^] works (two false)';
201
202 ok ! [^^](0, 0, 0), '[^^] works (three false)';
203 ok ! [^^](5, 9, 17), '[^^] works (three true)';
204
205 is [^^](5, 9, 0), (5 ^^ 9 ^^ 0), '[^^] mix 1';
206 is [^^](5, 0, 17), (5 ^^ 0 ^^ 17), '[^^] mix 2';
207 is [^^](0, 9, 17), (0 ^^ 9 ^^ 17), '[^^] mix 3';
208 is [^^](5, 0, 0), (5 ^^ 0 ^^ 0), '[^^] mix 4';
209 is [^^](0, 9, 0), (0 ^^ 9 ^^ 0), '[^^] mix 5';
210 is [^^](0, 0, 17), (0 ^^ 0 ^^ 17), '[^^] mix 6';
211 }
212
4acefb9 [t/spec] unfudge [//] and [||] tests for rakudo
moritz authored
213 # vim: ft=perl6
Something went wrong with that request. Please try again.