Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 162 lines (133 sloc) 5.521 kb
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
ad1b86b [t] Test suite refactoring.
cosimo authored
2 use Test;
fe21439 @jnthn Test for round issue on MoarVM (issue 157).
jnthn authored
3 plan 130;
ad1b86b [t] Test suite refactoring.
cosimo authored
4
b8d99dd [t/spec] fix more smartlinks
moritz authored
5 # L<S32::Numeric/Real/"=item round">
6 # L<S32::Numeric/Real/"=item floor">
7 # L<S32::Numeric/Real/"=item truncate">
8 # L<S32::Numeric/Real/"=item ceiling">
ad1b86b [t] Test suite refactoring.
cosimo authored
9
626a59c [rakudo] add directives to S29-num/ and convert pod to pod6
particle authored
10 =begin pod
ad1b86b [t] Test suite refactoring.
cosimo authored
11
76f901d Tests for rounding NaN and Inf
leto authored
12 Basic tests for the round(), floor(), truncate() and ceiling() built-ins
ad1b86b [t] Test suite refactoring.
cosimo authored
13
626a59c [rakudo] add directives to S29-num/ and convert pod to pod6
particle authored
14 =end pod
ad1b86b [t] Test suite refactoring.
cosimo authored
15
76f901d Tests for rounding NaN and Inf
leto authored
16 is( floor(NaN), NaN, 'floor(NaN) is NaN');
17 is( round(NaN), NaN, 'round(NaN) is NaN');
18 is( ceiling(NaN), NaN, 'ceiling(NaN) is NaN');
19 is( truncate(NaN), NaN, 'truncate(NaN) is NaN');
20
21 is( floor(Inf), Inf, 'floor(Inf) is Inf');
22 is( round(Inf), Inf, 'round(Inf) is Inf');
23 is( ceiling(Inf), Inf, 'ceiling(Inf) is Inf');
24 is( truncate(Inf), Inf, 'truncate(Inf) is Inf');
25
f4a4f4f [t/spec] Add tests for method forms.
colomon authored
26 is( floor(-Inf), -Inf, 'floor(-Inf) is -Inf');
27 is( round(-Inf), -Inf, 'round(-Inf) is -Inf');
28 is( ceiling(-Inf), -Inf, 'ceiling(-Inf) is -Inf');
29 is( truncate(-Inf), -Inf, 'truncate(-Inf) is -Inf');
30
31 is( NaN.floor, NaN, 'NaN.floor is NaN');
32 is( NaN.round, NaN, 'NaN.round is NaN');
33 is( NaN.ceiling, NaN, 'NaN.ceiling is NaN');
34 is( NaN.truncate, NaN, 'NaN.truncate is NaN');
35
36 is( Inf.floor, Inf, 'Inf.floor is Inf');
37 is( Inf.round, Inf, 'Inf.round is Inf');
38 is( Inf.ceiling, Inf, 'Inf.ceiling is Inf');
39 is( Inf.truncate, Inf, 'Inf.truncate is Inf');
40
41 is( (-Inf).floor, -Inf, '(-Inf).floor is -Inf');
42 is( (-Inf).round, -Inf, '(-Inf).round is -Inf');
43 is( (-Inf).ceiling, -Inf, '(-Inf).ceiling is -Inf');
44 is( (-Inf).truncate, -Inf, '(-Inf).truncate is -Inf');
45
ad1b86b [t] Test suite refactoring.
cosimo authored
46 my %tests =
47 ( ceiling => [ [ 1.5, 2 ], [ 2, 2 ], [ 1.4999, 2 ],
48 [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -5 ],
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
49 [ -0.5, 0 ], [ "-0.499.Num", 0 ], [ "-5.499.Num", -5 ],
61ad9ad [t/spec] Cut back on a few more or less duplicate tests.
colomon authored
50 [ "2.Num", 2 ] ],
ad1b86b [t] Test suite refactoring.
cosimo authored
51 floor => [ [ 1.5, 1 ], [ 2, 2 ], [ 1.4999, 1 ],
52 [ -0.1, -1 ], [ -1, -1 ], [ -5.9, -6 ],
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
53 [ -0.5, -1 ], [ "-0.499.Num", -1 ], [ "-5.499.Num", -6 ],
61ad9ad [t/spec] Cut back on a few more or less duplicate tests.
colomon authored
54 [ "2.Num", 2 ] ],
ad1b86b [t] Test suite refactoring.
cosimo authored
55 round => [ [ 1.5, 2 ], [ 2, 2 ], [ 1.4999, 1 ],
56 [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -6 ],
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
57 [ -0.5, 0 ], [ "-0.499.Num", 0 ], [ "-5.499.Num", -5 ],
58 [ "2.Num", 2 ] ],
ad1b86b [t] Test suite refactoring.
cosimo authored
59 truncate => [ [ 1.5, 1 ], [ 2, 2 ], [ 1.4999, 1 ],
60 [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -5 ],
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
61 [ -0.5, 0 ], [ "-0.499.Num", 0 ], [ "-5.499.Num", -5 ],
61ad9ad [t/spec] Cut back on a few more or less duplicate tests.
colomon authored
62 [ "2.Num", 2 ] ],
ad1b86b [t] Test suite refactoring.
cosimo authored
63 );
64
65 for %tests.keys.sort -> $type {
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
66 my @subtests = @(%tests{$type}); # XXX .[] doesn't work yet!
ad1b86b [t] Test suite refactoring.
cosimo authored
67 for @subtests -> $test {
62910e6 [t/spec] clean up of S29-num/rounders.t
moritz authored
68 my $code = "{$type}({$test[0]})";
5bab36f @FROGGS eval => EVAL in S14 to S32
FROGGS authored
69 my $res = EVAL($code);
ee7b7eb [t/spec] corrected rounders.t; fudged for rakudo (ugly mess)
moritz authored
70
ad1b86b [t] Test suite refactoring.
cosimo authored
71 if ($!) {
72 flunk("failed to parse $code ($!)");
73 } else {
62910e6 [t/spec] clean up of S29-num/rounders.t
moritz authored
74 ok($res == $test[1], "$code == {$test[1]}");
ad1b86b [t] Test suite refactoring.
cosimo authored
75 }
76 }
77 }
78
f4a4f4f [t/spec] Add tests for method forms.
colomon authored
79 for %tests.keys.sort -> $type {
80 my @subtests = @(%tests{$type}); # XXX .[] doesn't work yet!
81 for @subtests -> $test {
82 my $code = "({$test[0]}).{$type}";
5bab36f @FROGGS eval => EVAL in S14 to S32
FROGGS authored
83 my $res = EVAL($code);
f4a4f4f [t/spec] Add tests for method forms.
colomon authored
84
85 if ($!) {
86 flunk("failed to parse $code ($!)");
87 } else {
88 ok($res == $test[1], "$code == {$test[1]}");
89 }
576906b [t] added named arg tests to trig and num functions
frew authored
90 }
91 }
92
6e20293 [t/spec] check that the rounder functions return Int values (RT #61526)
moritz authored
93 for %tests.keys.sort -> $t {
5bab36f @FROGGS eval => EVAL in S14 to S32
FROGGS authored
94 isa_ok EVAL("{$t}(1.1)"), Int, "rounder $t returns an Int";
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
95 }
6e20293 [t/spec] check that the rounder functions return Int values (RT #61526)
moritz authored
96
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
97 # MoarVM Issue #157
98 # separate test since rakudo.jvm rounds this very large number
99 # more precise than rakudo.moar and rakudo.parrot
100 {
101 my $number = 5e+33;
102 my $result_1 = 4999999999999999727876154935214080; # result on Moar and Parrot
103 my $result_2 = 5000000000000000000000000000000000; # result on JVM
104
105 ok round($number) ~~ any($result_1,$result_2),
106 'large positive numbers rounded do not give negative numbers (1)';
107 ok $number.round ~~ any($result_1,$result_2),
108 'large positive numbers rounded do not give negative numbers (2)';
6e20293 [t/spec] check that the rounder functions return Int values (RT #61526)
moritz authored
109 }
110
fa26638 @labster testing fix for RT #118545, round with scale argument
labster authored
111 # RT #118545 Round with arguments
11a3b4e @usev6 Factor out test for MoarVM Issue 157
usev6 authored
112 {
fa26638 @labster testing fix for RT #118545, round with scale argument
labster authored
113 my $integer = 987654321;
114 is $integer.round(1), 987654321, "round integer with argument";
115 is $integer.round(5), 987654320, "($integer).round(5) == 987654320";
116 is $integer.round(1e5), 987700000, "($integer).round(1e5) == 987700000";
117 is 2.round(3/20), 1.95, "2.round(3/20) == 1.95";
118 }
119
120 {
121 my $num = 123.456789;
122 is $num.round(1), 123, "round with argument";
123 is $num.round(5), 125, "($num).round(5) == 125";
124 is $num.round(1/100), 123.46, "($num).round(1/100) == 123.46";
125 #?niecza todo "rounding with Num makes more rounding errors"
a3be3ac @coke Rakudo (jvm/parrot) un/fudge
coke authored
126 #?rakudo.jvm todo "nigh"
fa26638 @labster testing fix for RT #118545, round with scale argument
labster authored
127 is $num.round(1e-5), 123.45679, "($num).round(1e-5) == 123.45679";
128 }
129
130
131
132
e993f8d @colomon Add tests to make sure the rounding functions are not cheating.
colomon authored
133 {
134 my $big-int = 1234567890123456789012345678903;
135 is $big-int.floor, $big-int, "floor passes bigints unchanged";
136 is $big-int.ceiling, $big-int, "ceiling passes bigints unchanged";
137 is $big-int.round, $big-int, "round passes bigints unchanged";
138 is $big-int.truncate, $big-int, "truncate passes bigints unchanged";
139 }
140
141 {
142 my $big-rat = 1234567890123456789012345678903 / 2;
143 my $big-int = 1234567890123456789012345678903 div 2;
144 is $big-rat.floor, $big-int, "floor handles Rats properly";
145 is $big-rat.ceiling, $big-int + 1, "ceiling handles Rats properly";
146 is $big-rat.round, $big-int + 1, "round handles Rats properly";
147 is $big-rat.truncate, $big-int, "truncate handles Rats properly";
148 }
149
150 {
151 my $big-rat = FatRat.new(1234567890123456789012345678903, 2);
152 my $big-int = 1234567890123456789012345678903 div 2;
153 is $big-rat.floor, $big-int, "floor handles FatRats properly";
154 is $big-rat.ceiling, $big-int + 1, "ceiling handles FatRats properly";
155 is $big-rat.round, $big-int + 1, "round handles FatRats properly";
156 is $big-rat.truncate, $big-int, "truncate handles FatRats properly";
157 }
158
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
159 done;
752d480 [t/spec] Unfudge a tad.
colomon authored
160
6e20293 [t/spec] check that the rounder functions return Int values (RT #61526)
moritz authored
161 # vim: ft=perl6
Something went wrong with that request. Please try again.