Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 501 lines (419 sloc) 18.452 kB
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
1 use v6;
59b5e94 @colomon Remove TrigTestSupport.pm.
colomon authored
2
3 sub degrees-to-radians($x) {
4 $x * (312689/99532) / 180;
5 }
6
7 my @sines = (
8 degrees-to-radians(-360) => 0,
9 degrees-to-radians(135 - 360) => 1/2*sqrt(2),
10 degrees-to-radians(330 - 360) => -0.5,
11 degrees-to-radians(0) => 0,
12 degrees-to-radians(30) => 0.5,
13 degrees-to-radians(45) => 1/2*sqrt(2),
14 degrees-to-radians(90) => 1,
15 degrees-to-radians(135) => 1/2*sqrt(2),
16 degrees-to-radians(180) => 0,
17 degrees-to-radians(225) => -1/2*sqrt(2),
18 degrees-to-radians(270) => -1,
19 degrees-to-radians(315) => -1/2*sqrt(2),
20 degrees-to-radians(360) => 0,
21 degrees-to-radians(30 + 360) => 0.5,
22 degrees-to-radians(225 + 360) => -1/2*sqrt(2),
23 degrees-to-radians(720) => 0
24 );
25
26 my @cosines = @sines.map({; $_.key - degrees-to-radians(90) => $_.value });
27
28 my @sinhes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key =>
29 (exp($_.key) - exp(-$_.key)) / 2.0 });
30
31 my @coshes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key =>
32 (exp($_.key) + exp(-$_.key)) / 2.0 });
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
33
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
34 my $functions_file = "trig_functions";
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
35
36 sub Substitute($str, *%rules) {
37 my $result = $str;
38 for %rules.keys.sort(*.chars).reverse -> $key {
39 $result.=subst: '$' ~ $key, %rules{$key}, :g;
40 }
41 return $result;
42 }
43
44 sub Type($num, $type) {
45 my $typed-num = "($num).$type";
46 given $type {
47 when "Rat" {
48 $typed-num = "({$num}).{$type}(1e-9)";
49 }
50 when "NotComplex" {
51 $typed-num = "NotComplex.new($num)";
52 }
53 when "DifferentReal" {
54 $typed-num = "DifferentReal.new($num)";
55 }
56 }
57 $typed-num;
58 }
59
60 sub ForwardTest($str, $angle, $fun, $type, $desired-result-rule, $base = "Radians") {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
61 my $input_angle = $angle.key();
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
62 my $desired-result = eval($desired-result-rule);
63 given $type {
64 when "Complex" | "NotComplex" {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
65 $input_angle = $angle.key + 2i;
66 $desired-result = ($angle.key() + 2i)."$fun"();
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
67 }
68 }
69
70 my $typed-angle = Type($input_angle, $type);
71 my $typed-result = Type($desired-result, $type);
72
73 Substitute($str, :$fun, :$type, :angle($input_angle), :$typed-angle, :$base,
74 :$desired-result, :$typed-result);
75 }
76
77 sub InverseTest($str, $angle, $fun, $type, $desired-result-rule, $base = "Radians") {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
78 my $input_angle = $angle.key();
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
79 my $desired-result = eval($desired-result-rule);
80 given $type {
81 when "Complex" | "NotComplex" {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
82 $input_angle = ($angle.key() + 2i)."$fun"($base.eval);
83 $desired-result = ($angle.key() + 2i);
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
84 }
85 }
86
87 my $typed-angle = Type($input_angle, $type);
88 my $typed-result = Type($desired-result, $type);
89
90 Substitute($str, :$fun, :$type, :angle($input_angle), :$typed-angle, :$base,
91 :$desired-result, :$typed-result);
92 }
93
94 multi sub Atan2Test($str, Real $value, $type1, $base = "Radians") {
95 my $desired-result = $value.atan2(1, $base.eval);
96 my $type1-value = Type($value, $type1);
97 Substitute($str, :$type1, :$base, :$desired-result, :$type1-value);
98 }
99
100 multi sub Atan2Test($str, Real $value1, Real $value2, $type1, $type2, $base = "Radians") {
101 my $desired-result = $value1.atan2($value2, $base.eval);
102 my $type1-value = Type($value1, $type1);
103 my $type2-value = Type($value2, $type2);
104 Substitute($str, :$type1, :$type2, :$base, :$desired-result, :$type1-value, :$type2-value);
105 }
106
107 sub grep-and-repeat(@a, $skip-rule) {
108 gather loop {
109 for @a -> $a {
110 if $skip-rule {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
111 take $a unless $skip-rule.subst('$angle', $a.key()).eval;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
112 } else {
113 take $a;
114 }
115 }
116 }
117 }
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
118
119 class TrigFunction
120 {
121 has $.function_name;
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
122 has $.inverted_function_name;
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
123 has $.angle_and_results_name;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
124 has $.rational_inverse_tests;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
125 has $.skip;
126 has $.desired-result-code;
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
127 has $.complex_check;
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
128 has $.plus_inf;
129 has $.minus_inf;
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
130
131 multi method new(Str $function_name is copy,
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
132 Str $inverted_function_name is copy;
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
133 Str $angle_and_results_name is copy,
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
134 Str $rational_inverse_tests is copy;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
135 Str $skip is copy,
136 Str $desired-result-code is copy,
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
137 Str $complex_check is copy,
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
138 Str $plus_inf is copy,
139 Str $minus_inf is copy) {
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
140 self.bless(*,
141 :$function_name,
142 :$inverted_function_name,
143 :$angle_and_results_name,
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
144 :$rational_inverse_tests,
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
145 :$skip,
146 :$desired-result-code,
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
147 :$complex_check,
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
148 :$plus_inf,
149 :$minus_inf);
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
150 }
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
151
152 my sub notgrep(@a, Mu $condition) {
153 gather for @a -> $a {
154 take $a if $a ~~ $condition;
155 }
156 }
157
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
158 method dump_forward_tests($file) {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
159 my $setup_block = $skip ?? "next if " ~ $.skip.subst('$angle', '$angle.key()') ~ ";" !! "";
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
160
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
161 my $code = q[
162 # $.function_name tests
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
163
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
164 my $iter_count = 0;
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
165 for $.angle_and_results_name -> $angle
166 {
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
167 $.setup_block
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
168 my $desired-result = $.desired-result-code;
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
169
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
170 # Num.$.function_name tests -- very thorough
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
171 is_approx($angle.key().$.function_name, $desired-result,
172 "Num.$.function_name - {$angle.key()}");
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
173
174 # Complex.$.function_name tests -- also very thorough
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
175 my Complex $zp0 = $angle.key + 0.0i;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
176 my Complex $sz0 = $desired-result + 0i;
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
177 my Complex $zp1 = $angle.key + 1.0i;
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
178 my Complex $sz1 = $.complex_check($zp1);
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
179 my Complex $zp2 = $angle.key + 2.0i;
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
180 my Complex $sz2 = $.complex_check($zp2);
181
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
182 is_approx($zp0.$.function_name, $sz0, "Complex.$.function_name - $zp0");
183 is_approx($zp1.$.function_name, $sz1, "Complex.$.function_name - $zp1");
184 is_approx($zp2.$.function_name, $sz2, "Complex.$.function_name - $zp2");
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
185 }
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
186
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
187 is($.function_name(Inf), $.plus_inf, "$.function_name(Inf) -");
188 is($.function_name(-Inf), $.minus_inf, "$.function_name(-Inf) -");
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
189 ];
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
190 $code.=subst: '$.function_name', $.function_name, :g;
191 $code.=subst: '$.inverted_function_name', $.inverted_function_name, :g;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
192 $code.=subst: '$.setup_block', $setup_block, :g;
193 $code.=subst: '$.desired-result-code', $.desired-result-code, :g;
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
194 $code.=subst: '$.complex_check', $.complex_check, :g;
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
195 $code.=subst: '$.angle_and_results_name', $.angle_and_results_name, :g;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
196 $code.=subst: '$.rational_inverse_tests', $.rational_inverse_tests, :g;
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
197 $code.=subst: '$.plus_inf', $.plus_inf, :g;
198 $code.=subst: '$.minus_inf', $.minus_inf, :g;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
199 $code.=subst: / ^^ ' ' ** 12 /, '', :g;
dd46ca7 [t/spec] make generate-tests.pl emit less ugly indented code.
moritz authored
200
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
201 $file.say: $code;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
202
203 # next block is bordering on evil, and hopefully can be cleaned up in the near future
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
204 my $base_list = (<Radians> xx *).flat;
59b5e94 @colomon Remove TrigTestSupport.pm.
colomon authored
205 my $angle_list = grep-and-repeat(eval($.angle_and_results_name), $.skip);
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
206 my $fun = $.function_name;
207 for <Num Rat Complex Str NotComplex DifferentReal> -> $type {
d05d08a @colomon Move classes NotComplex and DifferentReal from TrigTestSupport.pm to …
colomon authored
208 my $indent = "";
209 if $type eq "NotComplex" || $type eq "DifferentReal" {
210 $file.say: '{';
211 $indent = " ";
212 }
213
214 $file.say: "$indent\# $type tests";
215
216 if $type eq "NotComplex" {
217 $file.say: q[
218 class NotComplex is Cool {
219 has $.value;
220
221 multi method new(Complex $value is copy) {
222 self.bless(*, :$value);
223 }
224
225 multi method Numeric() {
226 self.value;
227 }
228 }
229 ];
230 }
231
232 if $type eq "DifferentReal" {
233 $file.say: q[
234 class DifferentReal is Real {
235 has $.value;
236
237 multi method new($value is copy) {
238 self.bless(*, :$value);
239 }
240
241 multi method Bridge() {
242 self.value;
243 }
244 }
245 ];
246 }
247
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
248 unless $type eq "Num" || $type eq "Complex" {
d05d08a @colomon Move classes NotComplex and DifferentReal from TrigTestSupport.pm to …
colomon authored
249 $file.say: ForwardTest($indent ~ 'is_approx($typed-angle.$fun, $desired-result, "$type.$fun - $angle");',
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
250 $angle_list.shift, $fun, $type, $.desired-result-code);
251 }
d05d08a @colomon Move classes NotComplex and DifferentReal from TrigTestSupport.pm to …
colomon authored
252 $file.say: ForwardTest($indent ~ 'is_approx($fun($typed-angle), $desired-result, "$fun($type) - $angle");',
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
253 $angle_list.shift, $fun, $type, $.desired-result-code);
d05d08a @colomon Move classes NotComplex and DifferentReal from TrigTestSupport.pm to …
colomon authored
254 $file.say: ForwardTest($indent ~ 'is_approx($fun(:x($typed-angle)), $desired-result, "$fun(:x($type)) - $angle");',
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
255 $angle_list.shift, $fun, $type, $.desired-result-code);
256
d05d08a @colomon Move classes NotComplex and DifferentReal from TrigTestSupport.pm to …
colomon authored
257 if $type eq "NotComplex" || $type eq "DifferentReal" {
258 $file.say: '}';
259 }
260
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
261 $file.say: "";
262 }
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
263 }
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
264
265 method dump_inverse_tests($file) {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
266 my $setup_block = $skip ?? "next if " ~ $.skip.subst('$angle', '$angle.key()') ~ ";" !! "";
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
267
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
268 my $code = q[
269 # $.inverted_function_name tests
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
270
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
271 for $.angle_and_results_name -> $angle
272 {
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
273 $.setup_block
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
274 my $desired-result = $.desired-result-code;
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
275
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
276 # Num.$.inverted_function_name tests -- thorough
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
277 is_approx($desired-result.Num.$.inverted_function_name.$.function_name, $desired-result,
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
278 "Num.$.inverted_function_name - {$angle.key()}");
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
279
280 # Num.$.inverted_function_name(Complex) tests -- thorough
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
281 for ($desired-result + 0i, $desired-result + .5i, $desired-result + 2i) -> $z {
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
282 is_approx($.function_name($.inverted_function_name($z)), $z,
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
283 "$.inverted_function_name(Complex) - {$angle.key()}");
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
284 is_approx($z.$.inverted_function_name.$.function_name, $z,
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
285 "Complex.$.inverted_function_name - {$angle.key()}");
de3da1b [t/spec] Partial update to trig test generating code to be smarter.
colomon authored
286 }
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
287 }
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
288 ];
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
289 $code.=subst: '$.function_name', $.function_name, :g;
290 $code.=subst: '$.inverted_function_name', $.inverted_function_name, :g;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
291 $code.=subst: '$.setup_block', $setup_block, :g;
292 $code.=subst: '$.desired-result-code', $.desired-result-code, :g;
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
293 $code.=subst: '$.angle_and_results_name', $.angle_and_results_name, :g;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
294 $code.=subst: '$.rational_inverse_tests', $.rational_inverse_tests, :g;
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
295 $code.=subst: '$.plus_inf', $.plus_inf, :g;
296 $code.=subst: '$.minus_inf', $.minus_inf, :g;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
297 $code.=subst: / ^^ ' ' ** 12 /, '', :g;
63273a5 [t/spec] Replace the ugly code as qq strings (which required all sort…
colomon authored
298
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
299 $file.say: $code;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
300
301 # next block is bordering on evil, and hopefully can be cleaned up in the near future
302 my $base_list = (<Radians Degrees Gradians Circles> xx *).flat;
59b5e94 @colomon Remove TrigTestSupport.pm.
colomon authored
303 my $angle_list = grep-and-repeat(notgrep(eval($.angle_and_results_name),
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
304 {0 < $_.key() < pi / 2}), $.skip);
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
305 my $fun = $.function_name;
306 my $inv = $.inverted_function_name;
307 for <Num Rat Complex Str NotComplex DifferentReal> -> $type {
308 $file.say: "# $type tests";
309 unless $type eq "Num" || $type eq "Complex" {
310 $file.say: InverseTest('is_approx(($typed-result).$fun, $angle, "$type.$fun - $angle");',
311 $angle_list.shift, $inv, $type, $.desired-result-code);
312 }
313
314 $file.say: InverseTest('is_approx($fun($typed-result), $angle, "$fun($type) - $angle");',
315 $angle_list.shift, $inv, $type, $.desired-result-code);
316
317 $file.say: InverseTest('is_approx($fun(:x($typed-result)), $angle, "$fun(:x($type)) - $angle");',
318 $angle_list.shift, $inv, $type, $.desired-result-code);
319
320 $file.say: "";
321 }
9f94378 [t/spec] Start sketching in inverse trig functions as well.
colomon authored
322 }
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
323 }
324
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
325 sub OpenAndStartOutputFile($output_file)
326 {
327 my $file = open $output_file, :w or die "Unable to open $output_file $!\n";
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
328
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
329 $file.say: '# WARNING:
dd46ca7 [t/spec] make generate-tests.pl emit less ugly indented code.
moritz authored
330 # This is a generated file and should not be edited directly.
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
331 # look into generate-tests.pl instead
dd46ca7 [t/spec] make generate-tests.pl emit less ugly indented code.
moritz authored
332
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
333 use v6;
334 use Test;
59b5e94 @colomon Remove TrigTestSupport.pm.
colomon authored
335
336 sub degrees-to-radians($x) {
337 $x * (312689/99532) / 180;
338 }
339
340 my @sines = (
341 degrees-to-radians(-360) => 0,
342 degrees-to-radians(135 - 360) => 1/2*sqrt(2),
343 degrees-to-radians(330 - 360) => -0.5,
344 degrees-to-radians(0) => 0,
345 degrees-to-radians(30) => 0.5,
346 degrees-to-radians(45) => 1/2*sqrt(2),
347 degrees-to-radians(90) => 1,
348 degrees-to-radians(135) => 1/2*sqrt(2),
349 degrees-to-radians(180) => 0,
350 degrees-to-radians(225) => -1/2*sqrt(2),
351 degrees-to-radians(270) => -1,
352 degrees-to-radians(315) => -1/2*sqrt(2),
353 degrees-to-radians(360) => 0,
354 degrees-to-radians(30 + 360) => 0.5,
355 degrees-to-radians(225 + 360) => -1/2*sqrt(2),
356 degrees-to-radians(720) => 0
357 );
358
359 my @cosines = @sines.map({; $_.key - degrees-to-radians(90) => $_.value });
360
361 my @sinhes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key =>
362 (exp($_.key) - exp(-$_.key)) / 2.0 });
363
364 my @coshes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key =>
365 (exp($_.key) + exp(-$_.key)) / 2.0 });
366
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
367 ';
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
368
369 return $file;
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
370 }
371
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
372 sub CloseOutputFile($file)
373 {
374 # the {} afer 'vim' just generate an empty string.
375 # this is to avoid the string constant being interpreted as a modeline
376 # here in generate-tests.pl
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
377 $file.say: "done;";
2859574 [t/spec] Get the trig test generation script working under current Ra…
colomon authored
378 $file.say: "";
379 $file.say: '# vim: ft=perl6 nomodifiable';
380 $file.close;
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
381 }
382
383 my $file;
384
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
385 my $functions = open $functions_file, :r or die "Unable to open $functions_file: $!\n";
109f242 [t/spec] Expand trig tests to handle Inf and -Inf tests properly.
colomon authored
386
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
387 my Str $function_name;
388 my Str $inverted_function_name;
389 my Str $angle_and_results_name;
390 my Str $rational_inverse_tests;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
391 my Str $desired-result-code;
392 my Str $skip;
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
393 my Str $complex_check;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
394 my Str $plus_inf;
395 my Str $minus_inf;
396 for $functions.lines {
7defbc8 [generate-tests.pl] suppress STD error on \# in regex
lwall authored
397 when /^'#'/ { } # skip comment lines
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
398 when /Function\:\s+(.*)/ {
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
399 $function_name = ~$0;
400 $inverted_function_name = "a$0";
401 $angle_and_results_name = "";
402 $rational_inverse_tests = "(-2/2, -1/2, 1/2, 2/2)";
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
403 $skip = "";
404 $desired-result-code = "";
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
405 $complex_check = "";
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
406 $plus_inf = "NaN";
407 $minus_inf = "NaN";
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
408
409 $file = OpenAndStartOutputFile($function_name ~ ".t");
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
410 }
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
411 when /skip\:\s+(.*)/ { $skip = ~$0; }
412 when /desired_result\:\s+(.*)/ { $desired-result-code = ~$0; }
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
413 when /loop_over\:\s+(.*)/ { $angle_and_results_name = ~$0; }
414 when /inverted_function\:\s+(.*)/ { $inverted_function_name = ~$0; }
415 when /rational_inverse_tests\:\s+(.*)/ { $rational_inverse_tests = ~$0; }
0676f52 [t/spec] Bring the complex trig tests into the new (still not active)…
colomon authored
416 when /complex_check\:\s+(.*)/ { $complex_check = ~$0; }
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
417 when /plus_inf\:\s+(.*)/ { $plus_inf = ~$0; }
418 when /minus_inf\:\s+(.*)/ { $minus_inf = ~$0; }
419 when /End/ {
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
420 say :$function_name.perl;
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
421 my $tf = TrigFunction.new($function_name, $inverted_function_name, $angle_and_results_name,
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
422 $rational_inverse_tests, $skip, $desired-result-code,
423 $complex_check, $plus_inf, $minus_inf);
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
424 $tf.dump_forward_tests($file);
425 $tf.dump_inverse_tests($file);
162816b [t/spec] Change generate-tests.pl to generate a test file for each fo…
colomon authored
426 CloseOutputFile($file);
dbe2b53 [t/spec] Split the trig function by trig function info off into a sep…
colomon authored
427 }
428 }
792a84b [t/spec] Check in very early draft version of the code to auto-genera…
colomon authored
429
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
430 # output the atan2 file, a special case
431
432 $file = OpenAndStartOutputFile("atan2.t");
433 $file.say: q[
434 # atan2 tests
435
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
436 # First, test atan2 with x = 1
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
437
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
438 for TrigTest::sines() -> $angle
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
439 {
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
440 next if abs(cos($angle.key())) < 1e-6;
441 my $desired-result = sin($angle.key()) / cos($angle.key());
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
442
443 # Num.atan2 tests
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
444 is_approx($desired-result.Num.atan2.tan, $desired-result,
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
445 "Num.atan2() - {$angle.key()}");
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
446 is_approx($desired-result.Num.atan2(1.Num).tan, $desired-result,
aea78b8 @colomon Class AngleAndResult gone completely now.
colomon authored
447 "Num.atan2(1.Num) - {$angle.key()}");
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
448 }
449
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
450 # check that the proper quadrant is returned
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
451
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
452 is_approx(atan2(4, 4), pi / 4, "atan2(4, 4) is pi / 4");
453 is_approx(atan2(-4, 4), -pi / 4, "atan2(-4, 4) is -pi / 4");
454 is_approx(atan2(4, -4), 3 * pi / 4, "atan2(4, -4) is 3pi / 4");
455 is_approx(atan2(-4, -4), -3 * pi / 4, "atan2(-4, -4) is -3pi / 4");
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
456 ];
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
457
079975f @colomon Remove the trigbase tests from the trig spectests.
colomon authored
458 my $base_list = (<Radians> xx *).flat;
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
459 my @values = (-100, -10, -1, -.1, .1, 1, 10, 100);
460
461 sub filter-type(@values is copy, $type) {
462 given $type {
463 when "Int" {
464 @values.=grep({ $_ == $_.Int });
465 }
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
466 }
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
467 @values;
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
468 }
469
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
470 for <Num Rat Int Str DifferentReal> -> $type1 {
471 $file.say: "# $type1 tests";
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
472
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
473 unless $type1 eq "Num" {
474 $file.say: Atan2Test('is_approx($type1-value.atan2, $desired-result, "$type1.atan2");',
475 filter-type(@values, $type1).pick, $type1);
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
476 }
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
477
478 $file.say: Atan2Test('is_approx(atan2($type1-value), $desired-result, "atan2($type1)");',
479 filter-type(@values, $type1).pick, $type1);
480
481 $file.say: Atan2Test('is_approx(atan2(:y($type1-value)), $desired-result, "atan2(:y($type1))");',
482 filter-type(@values, $type1).pick, $type1);
483 $file.say: "";
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
484
217fb45 [t/spec] Overhaul of the trig tests. generate-tests.pl still despera…
colomon authored
485 for <Num Rat Int Str DifferentReal> -> $type2 {
486 $file.say: "# $type1 vs $type2 tests";
487
488 $file.say: Atan2Test('is_approx($type1-value.atan2($type2-value), $desired-result, "$type1.atan2($type2)");',
489 filter-type(@values, $type1).pick, filter-type(@values, $type2).pick, $type1, $type2);
490 $file.say: Atan2Test('is_approx(atan2($type1-value, $type2-value), $desired-result, "atan2($type1, $type2)");',
491 filter-type(@values, $type1).pick, filter-type(@values, $type2).pick, $type1, $type2);
492 $file.say: Atan2Test('is_approx(atan2(:y($type1-value), :x($type2-value)), $desired-result, "atan2(:y($type1), :x($type2))");',
493 filter-type(@values, $type1).pick, filter-type(@values, $type2).pick, $type1, $type2);
494 $file.say: "";
495 }
4277cf5 [t/spec] Full version of the new trig tests. (Still a bit of work to…
colomon authored
496 }
497
498 CloseOutputFile($file);
499
dd46ca7 [t/spec] make generate-tests.pl emit less ugly indented code.
moritz authored
500 # vim: ft=perl6
Something went wrong with that request. Please try again.