2 # Copyright (C) 2001-2008, The Perl Foundation.
7 t/pmc/n_arithmetics.t - n_* Arithmetic Ops
11 % prove t/pmc/n_arithmetics.t
15 Tests basic arithmetic ops that construct a new return value on
16 various combinations of Parrot integer and number types.
22 .include 'include/test_more.pir'
26 take_the_negative_of_an_integer()
27 take_the_absolute_value_of_an_integer()
28 add_integer_to_integer()
29 subtract_integer_from_integer()
30 multiply_integer_by_integer()
31 divide_integer_by_integer()
33 take_the_absolute_value_of_a_float()
34 add_integer_to_float()
35 subtract_integer_from_float()
36 multiply_float_by_integer()
37 divide_float_by_integer()
39 add_sub_mul_div_of_float_with_constants()
40 subtract_float_from_float()
41 multiply_float_by_float()
42 divide_float_by_float()
47 ### Operations on a single INTVAL
49 .sub take_the_negative_of_an_integer
54 is( P1, 0, 'negavite of zero is zero' )
56 ## negate a positive number.
59 is( P1, -1234567890, 'negavite of positive' )
60 ## check that we are not reusing P1.
61 ne_addr P30, P1, not_broken
62 ok( 0, 'not reusing P1' )
65 ok( 1, 'not reusing P1' )
67 ## negate a negative number.
70 is( P1, 1234567890, 'negative of negative' )
73 .sub take_the_absolute_value_of_an_integer
75 ## find absolute zero (so to speak).
78 is( P1, 0, 'abs of zero' )
80 ## find the absolute value of a positive Integer.
83 is( P1, 1234567890, 'abs of positive' )
84 ## check that we are not reusing P1.
85 ne_addr P30, P1, not_broken
86 ok( 0, 'not reusing P1' )
89 ok( 1, 'not reusing P1' )
91 ## find the absolute value of a negative number.
94 is( P1, 1234567890, 'abs of negative' )
98 ### first arg is Integer, second arg is Integer
100 .sub add_integer_to_integer
108 is( P2, 3877, 'add integer to integer' )
111 is( P2, 3877, 'add integer to integer in assignment' )
112 ## check that we are not reusing P2.
113 ne_addr P30, P2, not_broken
114 ok( 0, 'not reusing P2' )
115 goto not_broken__done
117 ok( 1, 'not reusing P2' )
119 ## check adding constants.
121 is( P2, 4011, 'adding integer and constant' )
123 is( P0, 4011, 'adding integer and constant and assign to Int arg' )
126 .sub subtract_integer_from_integer
134 is( P2, 4123, 'subtract Integer from Integer' )
137 is( P2, 4123, 'subtract Integer from Integer in assignment' )
138 ## check that we are not reusing P2.
139 ne_addr P30, P2, not_broken
140 ok( 0, 'not reusing P2' )
141 goto not_broken__done
143 ok( 1, 'not reusing P2' )
145 ## check subtracting constants.
147 is( P2, 3989, 'subtract constant from Integer' )
149 is( P2, 3989, 'subtract constant from Integer and assign to Int arg' )
152 .sub multiply_integer_by_integer
160 is( P2, -492000, 'multiply Integer by Integer' )
162 is( P3, -492000, 'multiply Integer by Integer in assginment' )
163 ## check multiplying constants.
165 is( P2, 44000, 'multiply Integer by constant' )
167 is( P0, 44000, 'multiply Integer by constant and assign to Int arg' )
170 .sub divide_integer_by_integer
178 is( P2, -32.5203, 'divide Integer by Integer' )
180 is( P3, -32.5203, 'divide Integer by Integer in assignment' )
181 ## check dividing by constants.
183 is( P2, 363.636, 'divide Integer by constant' )
185 is( P0, 363.636, 'divide Integer by constant and assign to Int arg' )
189 ### Operations on a single NUMVAL
195 is( P1, 0, 'neg of Float 0 is 0' )
198 is( P1, 0, 'neg of Float -0.0 is 0' )
201 is( P1, -123.4567890, 'neg of positive Float is negative' )
204 is( P1, 123.4567890, 'neg of negavite Float is positive' )
207 .sub take_the_absolute_value_of_a_float
211 is( P1, 0, 'abs value of float 0 is zero' )
214 is( P1, 0, 'abs value of float -0.0 is zero' )
217 is( P1, 123.45678901, 'abs value of positive float is positive' )
218 set P0, -123.45678901
220 is( P1, 123.45678901, 'abs value of negative float is positive' )
224 ### FLOATVAL and INTVAL tests
226 .sub add_integer_to_float
232 is( P1, 3876.877, 'add Float and Int' )
235 is( P1, 3876.877, 'add Float and Int in assignment' )
236 ## check that we are not reusing P1.
237 ne_addr P30, P1, not_broken
238 ok( 0, 'not reusing P1' )
239 goto not_broken__done
241 ok( 1, 'not reusing P1' )
245 is( P2, 3876.877, 'add Int and Float' )
247 is( P1, 7876.877, 'add Float and Int and assign to Float arg' )
250 .sub subtract_integer_from_float
256 is( P1, -4123.123, 'subtract Int from Float' )
259 is( P1, -4123.123, 'subtract Int from Float in assignment' )
260 ## check that we are not reusing P1.
261 ne_addr P30, P1, not_broken
262 ok( 0, 'not reusing P1' )
263 goto not_broken__done
265 ok( 1, 'not reusing P1' )
269 is( P2, 4123.123, 'subtract Float from Int in assignment' )
271 is( P1, -8123.123, 'subtract Float from Int and assign to Float arg' )
274 .sub multiply_float_by_integer
280 is( P1, -492492, 'multiply Float by Int' )
283 is( P1, -492492, 'multiply Float by Int in assignment' )
284 ## check that we are not reusing P1.
285 ne_addr P30, P1, not_broken
286 ok( 0, 'not reusing P1' )
287 goto not_broken__done
289 ok( 1, 'not reusing P1' )
293 is( P1, -492492, 'multiply Int by Float in assignment' )
295 is( P1, 984984, 'multiply Float by const int and assign to Float arg' )
298 .sub divide_float_by_integer
304 is( P1, -0.0307808, 'divide Float by Int' )
307 is( P1, -0.0307808, 'divide Float by Int in assignment' )
308 ## check that we are not reusing P1.
309 ne_addr P30, P1, not_broken
310 ok( 0, 'not reusing P1' )
311 goto not_broken__done
313 ok( 1, 'not reusing P1' )
317 is( P1, -123.123, 'divide Float by constant Int 1' )
320 is( P1, 1, 'divide Float by constant Int' )
322 is( P1, 100, 'divide Float by constant Float' )
326 ### FLOATVAL and FLOATVAL tests
328 .sub add_float_to_float
336 is( P2, 3877.123, 'add Float to Float' )
339 is( P2, 3877.123, 'add Float to Float in assignment' )
340 ## check that we are not reusing P2.
341 ne_addr P30, P2, not_broken
342 ok( 0, 'not reusing P2' )
343 goto not_broken__done
345 ok( 1, 'not reusing P2' )
350 ## This tests n_infix_ic_p_p_nc for n_add, n_sub, n_mul, and n_div. Note that
351 ## there is no n_infix_ic_p_nc_p op; the PMC argument always comes first.
352 .sub add_sub_mul_div_of_float_with_constants
358 is( P2, -116.343, 'add neg Float to constant Float' )
360 is( P2, 4007.03, 'add pos Float to constant Float' )
362 is( P2, -834.774, 'multily Float by constant Float' )
364 is( P2, 590.007, 'divide Float by constant Float' )
367 .sub subtract_float_from_float
375 is( P2, 4123.369, 'subtract Float from Float' )
378 is( P2, 4123.369, 'subtract Float from Float in assignment' )
379 ## check that we are not reusing P2.
380 ne_addr P30, P2, not_broken
381 ok( 0, 'not reusing P2' )
382 goto not_broken__done
384 ok( 1, 'not reusing P2' )
389 .sub multiply_float_by_float
397 is( P2, -49252.229, 'multiply Float from Float' )
400 is( P2, -49252.229, 'muliply Float from Float in assignment' )
401 ## check that we are not reusing P2.
402 ne_addr P30, P2, not_broken
403 ok( 0, 'not reusing P2' )
404 goto not_broken__done
406 ok( 1, 'not reusing P2' )
411 .sub divide_float_by_float
419 is( P2, -0.0307789, 'divide neg Float by pos Float' )
422 is( P2, -32.4898, 'divide pos Float by neg Float in assignment' )
423 ## check that we are not reusing P2.
424 ne_addr P30, P2, not_broken
425 ok( 0, 'not reusing P2' )
426 goto not_broken__done
428 ok( 1, 'not reusing P2' )
437 is( P0, 1, 'add constant to new (unassigned) PMC' )
439 ok( 1, 'variables have different addresses' )
442 ok( 0, 'variables have different addresses' )
445 ### Tests of ".pragma n_operators 1" moved to n_operators.t
451 # vim: expandtab shiftwidth=4 ft=pir: