-
Notifications
You must be signed in to change notification settings - Fork 138
/
n_arithmetics.t
589 lines (559 loc) · 8.97 KB
/
n_arithmetics.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#! perl
# Copyright: 2001-2005 The Perl Foundation. All Rights Reserved.
# $Id$
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 20;
=head1 NAME
t/pmc/n_arithmetics.t - n_* Arithmetic Ops
=head1 SYNOPSIS
% prove t/pmc/n_arithmetics.t
=head1 DESCRIPTION
Tests basic arithmetic ops that construct a new return value on
various combinations of Parrot integer and number types.
=cut
###
### Operations on a single INTVAL
###
pir_output_is(<<'CODE', <<OUTPUT, "take the negative of an Integer");
.sub _main :main
P0 = new Integer
## negate zero.
set P0, 0
n_neg P1, P0
print P1
print "\n"
P30 = P1
## negate a positive number.
set P0, 1234567890
n_neg P1, P0
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
## negate a negative number.
set P0, -1234567890
P1 = n_neg P0
print P1
print "\n"
.end
CODE
0
-1234567890
1234567890
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "take the absolute value of an Integer");
.sub _main :main
P0 = new Integer
## find absolute zero (so to speak).
set P0, 0
P1 = n_abs P0
print P1
print "\n"
P30 = P1
## find the absolute value of a positive Integer.
set P0, 1234567890
n_abs P1, P0
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
## find the absolute value of a negative number.
set P0, -1234567890
n_abs P1, P0
print P1
print "\n"
.end
CODE
0
1234567890
1234567890
OUTPUT
###
### first arg is Integer, second arg is Integer
###
pir_output_is(<<'CODE', <<OUTPUT, "add Integer to Integer");
.sub _main :main
P0 = new Integer
set P0, 4000
P1 = new Integer
set P1, -123
P2 = new Integer
set P2, 666
n_add P2, P0, P1
print P2
print "\n"
P30 = P2
P2 = n_add P0, P1
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
## check adding constants.
P2 = n_add P0, 11
print P2
print "\n"
P0 = n_add P0, 11
print P0
print "\n"
.end
CODE
3877
3877
4011
4011
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "subtract Integer from Integer");
.sub _main :main
P0 = new Integer
set P0, 4000
P1 = new Integer
set P1, -123
P2 = new Integer
set P2, 666
n_sub P2, P0, P1
print P2
print "\n"
P30 = P2
P2 = n_sub P0, P1
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
## check subtracting constants.
P2 = n_sub P0, 11
print P2
print "\n"
P0 = n_sub P0, 11
print P0
print "\n"
.end
CODE
4123
4123
3989
3989
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "multiply Integer with Integer");
.sub _main :main
P0 = new Integer
set P0, 4000
P1 = new Integer
set P1, -123
P2 = new Integer
set P2, 666
n_mul P2, P0, P1
print P2
print "\n"
P3 = n_mul P0, P1
print P3
print "\n"
## check multiplying constants.
P2 = n_mul P0, 11
print P2
print "\n"
P0 = n_mul P0, 11
print P0
print "\n"
.end
CODE
-492000
-492000
44000
44000
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "divide Integer by Integer");
.sub _main :main
P0 = new Integer
set P0, 4000
P1 = new Integer
set P1, -123
P2 = new Integer
set P2, 666
n_div P2, P0, P1
print P2
print "\n"
P3 = n_div P0, P1
print P3
print "\n"
## check dividing by constants.
P2 = n_div P0, 11
print P2
print "\n"
P0 = n_div P0, 11
print P0
print "\n"
.end
CODE
-32.5203
-32.5203
363.636
363.636
OUTPUT
###
### Operations on a single NUMVAL
###
pir_output_is(<<'CODE', <<OUTPUT, "negate a Float");
.sub _main :main
P0 = new Float
set P0, 0
P1 = n_neg P0
print P1
print "\n"
set P0, -0.0
n_neg P1, P0
print P1
print "\n"
set P0, 123.4567890
P1 = n_neg P0
print P1
print "\n"
set P0, -123.4567890
n_neg P1, P0
print P1
print "\n"
.end
CODE
0
0
-123.457
123.457
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "take the absolute value of a Float");
.sub _main :main
P0 = new Integer
set P0, 0
P1 = n_abs P0
print P1
print "\n"
set P0, -0.0
n_abs P1, P0
print P1
print "\n"
set P0, 123.45678901
n_abs P1, P0
print P1
print "\n"
set P0, -123.45678901
P1 = n_abs P0
print P1
print "\n"
.end
CODE
0
0
123.457
123.457
OUTPUT
###
### FLOATVAL and INTVAL tests
###
pir_output_is(<<'CODE', <<OUTPUT, "add Integer to Float");
.sub _main :main
P10 = new Integer
set P10, 4000
P0 = new Float
set P0, -123.123
n_add P1, P0, P10
print P1
print "\n"
P30 = P1
P1 = n_add P0, P10
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
P2 = n_add P10, P0
print P2
print "\n"
P1 = n_add P1, P10
print P1
print "\n"
.end
CODE
3876.88
3876.88
3876.88
7876.88
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "subtract Integer from Float");
.sub _main :main
P10 = new Integer
set P10, 4000
P0 = new Float
set P0, -123.123
n_sub P1, P0, P10
print P1
print "\n"
P30 = P1
P1 = n_sub P0, P10
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
P2 = n_sub P10, P0
print P2
print "\n"
P1 = n_sub P1, P10
print P1
print "\n"
.end
CODE
-4123.12
-4123.12
4123.12
-8123.12
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "multiply Float with Integer");
.sub _main :main
P10 = new Integer
set P10, 4000
P0 = new Float
set P0, -123.123
n_mul P1, P0, P10
print P1
print "\n"
P30 = P1
P1 = n_mul P0, P10
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
P1 = n_mul P10, P0
print P1
print "\n"
P1 = n_mul P1, -2
print P1
print "\n"
.end
CODE
-492492
-492492
-492492
984984
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "divide Float by Integer");
.sub _main :main
P10 = new Integer
set P10, 4000
P0 = new Float
set P0, -123.123
n_div P1, P0, P10
print P1
print "\n"
P30 = P1
P1 = n_div P0, P10
print P1
print "\n"
## check that we are not reusing P1.
ne_addr P30, P1, not_broken
print "Broken!\n"
not_broken:
P1 = n_div P0, 1
print P1
print "\n"
set P0, 100.000
P1 = n_div P0, 100
print P1
print "\n"
P1 = n_div P1, 0.01
print P1
print "\n"
.end
CODE
-0.0307808
-0.0307808
-123.123
1
100
OUTPUT
###
### FLOATVAL and FLOATVAL tests
###
pir_output_is(<<'CODE', <<OUTPUT, "add Float to Float");
.sub _main :main
P0 = new Float
set P0, 4000.246
P1 = new Float
set P1, -123.123
P2 = new Float
set P2, 6.66
n_add P2, P0, P1
print P2
print "\n"
P30 = P2
P2 = n_add P0, P1
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
.end
CODE
3877.12
3877.12
OUTPUT
## This tests n_infix_ic_p_p_nc for n_add, n_sub, n_mul, and n_div. Note that
## there is no n_infix_ic_p_nc_p op; the PMC argument always comes first.
pir_output_is(<<'CODE', <<OUTPUT, "add/sub/mul/div of Float with constants");
.sub _main :main
P0 = new Float
set P0, 4000.246
P1 = new Float
set P1, -123.123
P2 = n_add P1, 6.78
print P2
print "\n"
P2 = n_add P0, 6.78
print P2
print "\n"
P2 = n_mul P1, 6.78
print P2
print "\n"
P2 = n_div P0, 6.78
print P2
print "\n"
.end
CODE
-116.343
4007.03
-834.774
590.007
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "subtract Float from Float");
.sub _main :main
P0 = new Float
set P0, 4000.246
P1 = new Float
set P1, -123.123
P2 = new Float
set P2, 6.66
n_sub P2, P0, P1
print P2
print "\n"
P30 = P2
P2 = n_sub P1, P0
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
.end
CODE
4123.37
-4123.37
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "multiply Float with Float");
.sub _main :main
P0 = new Float
set P0, 400.0246
P1 = new Float
set P1, -123.123
P2 = new Float
set P2, 6.66
n_mul P2, P0, P1
print P2
print "\n"
P30 = P2
P2 = n_mul P0, P1
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
.end
CODE
-49252.2
-49252.2
OUTPUT
pir_output_is(<<'CODE', <<OUTPUT, "divide Float by Float");
.sub _main :main
P0 = new Float
set P0, 4000.246
P1 = new Float
set P1, -123.123
P2 = new Float
set P2, 6.66
n_div P2, P1, P0
print P2
print "\n"
P30 = P2
P2 = n_div P0, P1
print P2
print "\n"
## check that we are not reusing P2.
ne_addr P30, P2, not_broken
print "Broken!\n"
not_broken:
.end
CODE
-0.0307789
-32.4898
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', "verify new PMC");
.sub main :main
P0 = new Integer
P1 = P0
P0 = n_add P0, 1
print P0
print "\n"
eq_addr P0, P1, nok
print "ok\n"
end
nok:
print "not ok\n"
.end
CODE
1
ok
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', ".pragma n_operators");
.pragma n_operators 1
.sub main :main
P0 = new Integer
P1 = P0
P0 = P0 + 1
print P0
print "\n"
eq_addr P0, P1, nok
print "ok\n"
end
nok:
print "not ok\n"
.end
CODE
1
ok
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', ".pragma n_operators - inplace");
.pragma n_operators 1
.sub main :main
.local pmc p
p = new .Integer
p = 10
p += 4
print p
print "\n"
.end
CODE
14
OUTPUT