-
Notifications
You must be signed in to change notification settings - Fork 138
/
sarray.t
742 lines (688 loc) · 13 KB
/
sarray.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#! perl
# Copyright (C) 2001-2007, The Perl Foundation.
# $Id$
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 30;
=head1 NAME
t/pmc/sarray.t - Simple Array
=head1 SYNOPSIS
% prove t/pmc/sarray.t
=head1 DESCRIPTION
Tests the C<SArray> PMC, which is used for parameter-passing.
=cut
my $fp_equality_macro = pasm_fp_equality_macro();
pasm_output_is( <<'CODE', <<'OUTPUT', "Setting sarray size" );
new P0, 'SArray'
set I0, P0
eq I0, 0, OK_1
print "not "
OK_1: print "ok 1\n"
set P0, 1
set P0[0], 100
set I0, P0
eq I0, 1, OK_2
print "not "
OK_2: print "ok 2\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_error_output_like( <<'CODE', <<'OUTPUT', "attempt resize" );
new P0, 'SArray'
set P0, 1
set P0[0], 100
set I0, P0
eq I0, 1, OK_1
print "not "
OK_1: print "ok 1\n"
set P0, 2
end
CODE
/ok 1
SArray: Can't resize!
current instr\.:/
OUTPUT
# '
pasm_output_is( <<'CODE', <<'OUTPUT', "indexed access" );
new P0, 'SArray'
set P0, 3
set P0[0], 100
set P0[1], 200
set P0[2], 300
set I0, P0[0]
eq I0, 100, ok1
print "not "
ok1: print "ok 1\n"
set I0, P0[1]
eq I0, 200, ok2
print "not "
ok2: print "ok 2\n"
set I0, P0[2]
eq I0, 300, ok3
print "not "
ok3: print "ok 3\n"
set I0, P0[-1]
eq I0, 300, ok4
print "not "
ok4: print "ok 4\n"
set I0, P0[-2]
eq I0, 200, ok5
print "not "
ok5: print "ok 5\n"
set I0, P0[-3]
eq I0, 100, ok6
print "not "
ok6: print "ok 6\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push" );
new P0, 'SArray'
set P0, 3
push P0, 100
push P0, 200
push P0, 300
set I0, P0[0]
eq I0, 100, ok1
print "not "
ok1: print "ok 1\n"
set I0, P0[1]
eq I0, 200, ok2
print "not "
ok2: print "ok 2\n"
set I0, P0[2]
eq I0, 300, ok3
print "not "
ok3: print "ok 3\n"
set I0, P0[-1]
eq I0, 300, ok4
print "not "
ok4: print "ok 4\n"
set I0, P0[-2]
eq I0, 200, ok5
print "not "
ok5: print "ok 5\n"
set I0, P0[-3]
eq I0, 100, ok6
print "not "
ok6: print "ok 6\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "push / indexed" );
new P0, 'SArray'
set P0, 3
push P0, 100
set P0[1], 200
push P0, 300
set I0, P0[0]
eq I0, 100, ok1
print "not "
ok1: print "ok 1\n"
set I0, P0[1]
eq I0, 200, ok2
print "not "
ok2: print "ok 2\n"
set I0, P0[2]
eq I0, 300, ok3
print "not "
ok3: print "ok 3\n"
set I0, P0[-1]
eq I0, 300, ok4
print "not "
ok4: print "ok 4\n"
set I0, P0[-2]
eq I0, 200, ok5
print "not "
ok5: print "ok 5\n"
set I0, P0[-3]
eq I0, 100, ok6
print "not "
ok6: print "ok 6\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "mixed indexed" );
new P0, 'SArray'
set P0, 4
set P0[0], 1000
set N0, 222.22
set P0[1], N0
set S0, "string\n"
set P0[2], S0
new P1, 'Undef'
set P1, 42
set P0[3], P1
set I0, P0[0]
eq I0, 1000, ok1
print "not "
ok1: print "ok 1\n"
set N1, P0[1]
eq N0, N1, ok2
print "not "
ok2: print "ok 2\n"
set S1, P0[2]
print S1
set P3, P0[3]
print P3
print "\n"
end
CODE
ok 1
ok 2
string
42
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "mixed push" );
new P0, 'SArray'
set P0, 4
push P0, 1000
set N0, 222.22
push P0, N0
set S0, "string\n"
push P0, S0
new P1, 'Undef'
set P1, 42
push P0, P1
set I0, P0[0]
eq I0, 1000, ok1
print "not "
ok1: print "ok 1\n"
set N1, P0[1]
eq N0, N1, ok2
print "not "
ok2: print "ok 2\n"
set S1, P0[2]
print S1
set P3, P0[3]
print P3
print "\n"
end
CODE
ok 1
ok 2
string
42
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "mixed push - clone" );
new P2, 'SArray'
set P2, 4
push P2, 1000
set N0, 222.22
push P2, N0
set S0, "string\n"
push P2, S0
new P1, 'Undef'
set P1, 42
push P2, P1
clone P0, P2
set I0, P0[0]
eq I0, 1000, ok1
print "not "
ok1: print "ok 1\n"
set N1, P0[1]
eq N0, N1, ok2
print "not "
ok2: print "ok 2\n"
set S1, P0[2]
print S1
set P3, P0[3]
print P3
print "\n"
end
CODE
ok 1
ok 2
string
42
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "shift_integer" );
new P0, 'SArray'
set P0, 3
set P0[0], 100
set P0[1], 200
set P0[2], 300
shift I0, P0
eq I0, 100, ok1
print "not "
ok1: print "ok 1\n"
set I0, P0
eq I0, 2, ok2
ok2: print "ok 2\n"
shift I0, P0
eq I0, 200, ok3
print "not "
ok3: print "ok 3\n"
set I0, P0
eq I0, 1, ok4
ok4: print "ok 4\n"
shift I0, P0
eq I0, 300, ok5
print "not "
ok5: print "ok 5\n"
set I0, P0
eq I0, 0, ok6
ok6: print "ok 6\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "mixed shift" );
new P0, 'SArray'
set P0, 4
push P0, 1000
set N0, 222.22
push P0, N0
set S0, "string\n"
push P0, S0
new P1, 'Undef'
set P1, 42
push P0, P1
shift I0, P0
eq I0, 1000, ok1
print "not "
ok1: print "ok 1\n"
shift N1, P0
eq N0, N1, ok2
print "not "
ok2: print "ok 2\n"
shift S1, P0
print S1
shift P3, P0
print P3
print "\n"
end
CODE
ok 1
ok 2
string
42
OUTPUT
pasm_output_is( <<'CODE', <<'OUTPUT', "iterator" );
.include "iterator.pasm"
new P0, 'SArray' # empty array
new P2, 'SArray' # array with 2 elements
set P2, 2
push P2, 10
push P2, 20
set I0, P2
new P1, 'Iterator', P2
print "ok 1\n"
set I1, P1
eq I0, I1, ok2 # iter.length() == array.length()
print "not "
ok2: print "ok 2\n"
new P1, 'Iterator', P0
set P1, .ITERATE_FROM_START
print "ok 3\n"
unless P1, ok4 # if(iter) == false on empty
print "not "
ok4: print "ok 4\n"
new P1, 'Iterator', P2
set P1, .ITERATE_FROM_START
if P1, ok5 # if(iter) == true on non empty
print "not "
ok5: print "ok 5\n"
# now iterate over P2
# while (P1) { element = shift(P1) }
unless P1, nok6
shift I3, P1
eq I3, 10, ok6
nok6: print "not "
ok6: print "ok 6\n"
unless P1, nok7
shift I3, P1
eq I3, 20, ok7
nok7: print "not "
ok7: print "ok 7\n"
unless P1, ok8 # if(iter) == false after last
print "not "
ok8: print "ok 8\n"
# now iterate from end
set P1, .ITERATE_FROM_END
if P1, ok9 # if(iter) == true on non empty
print "not "
ok9: print "ok 9\n"
# while (P1) { element = pop(P1) }
unless P1, nok10
pop I3, P1
eq I3, 20, ok10
nok10: print "not "
ok10: print "ok 10\n"
unless P1, nok11
pop I3, P1
eq I3, 10, ok11
nok11: print "not "
ok11: print "ok 11\n"
unless P1, ok12 # if(iter) == false after last
print "not "
ok12: print "ok 12\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
ok 7
ok 8
ok 9
ok 10
ok 11
ok 12
OUTPUT
pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" );
.sub _main
.local pmc pmc1
pmc1 = new 'SArray'
.local int bool1
does bool1, pmc1, "scalar"
print bool1
print "\n"
does bool1, pmc1, "array"
print bool1
print "\n"
does bool1, pmc1, "no_interface"
print bool1
print "\n"
end
.end
CODE
0
1
0
OUTPUT
pasm_output_is( << "CODE", << 'OUTPUT', "Access via Key PMC" );
@{[ $fp_equality_macro ]}
new P0, 'SArray'
set P0, 4
set P0[0], 100
set P0[1], 12.298
set P0[2], "yarrAS"
new P31, 'Hash'
set P31["Test"], "ok"
set P0[3], P31
new P1, 'Key'
set P1, 0
set I0, P0[P1]
eq I0, 100, ok1
print "not "
ok1: print "ok 1\\n"
new P2, 'Key'
set P2, 1
set N0, P0[P2]
.fp_eq(N0, 12.298, ok2)
print "not "
ok2: print "ok 2\\n"
new P3, 'Key'
set P3, 2
set S0, P0[P3]
eq S0, "yarrAS", ok3
print "not "
ok3: print "ok 3\\n"
new P4, 'Key'
set P4, 3
set P5, P0[P4]
set S1, P5["Test"]
eq S1, "ok", ok4
print "not "
ok4: print "ok 4\\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
OUTPUT
pasm_output_is( << 'CODE', << 'OUTPUT', "Store PMC, get int" );
new P0, 'SArray'
set P0, 2
new P1, 'Integer'
set P1, 11
new P2, 'Float'
set P2, 1.1
set P0[0], P1
set P0[1], P2
set I0, P0[0]
eq I0, 11, ok1
print "not "
ok1: print "ok 1\n"
set I0, P0[1]
eq I0, 1, ok2
print "not "
ok2: print "ok 2\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store num, get int" );
new P0, 'SArray'
set P0, 1
set P0[0], 4.2
set I0, P0[0]
print I0
end
CODE
/SArray: Entry not an integer!/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store string, get int" );
new P0, 'SArray'
set P0, 1
set P0[0], "Non-numeric string"
set I0, P0[0]
print I0
end
CODE
/SArray: Entry not an integer!/
OUTPUT
pasm_output_is( << "CODE", << 'OUTPUT', "Store PMC, get num" );
@{[ $fp_equality_macro ]}
new P0, 'SArray'
set P0, 2
new P1, 'Integer'
set P1, 11
new P2, 'Float'
set P2, 1.1
set P0[0], P1
set P0[1], P2
set N0, P0[0]
.fp_eq(N0, 11.0, ok1)
print "not "
ok1: print "ok 1\\n"
set N0, P0[1]
.fp_eq(N0, 1.1, ok2)
print "not "
ok2: print "ok 2\\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store int, get num" );
new P0, 'SArray'
set P0, 1
set P0[0], 12
set N0, P0[0]
print N0
end
CODE
/SArray: Entry not a number!/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store string, get num" );
new P0, 'SArray'
set P0, 1
set P0[0], "Non-numeric string"
set N0, P0[0]
print N0
end
CODE
/SArray: Entry not a number!/
OUTPUT
pasm_output_is( << 'CODE', << 'OUTPUT', "Store PMC, get string" );
new P0, 'SArray'
set P0, 2
new P1, 'String'
set P1, "Hello"
new P2, 'Integer'
set P2, 1010
set P0[0], P1
set P0[1], P2
set S0, P0[0]
eq S0, "Hello", ok1
print "not "
ok1: print "ok 1\n"
set S0, P0[1]
eq S0, "1010", ok2
print "not "
ok2: print "ok 2\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store int, get string" );
new P0, 'SArray'
set P0, 1
set P0[0], 12
set S0, P0[0]
print S0
end
CODE
/SArray: Entry not a string!/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store num, get string" );
new P0, 'SArray'
set P0, 1
set P0[0], 12.5
set S0, P0[0]
print S0
end
CODE
/SArray: Entry not a string!/
OUTPUT
pasm_output_is( << "CODE", << 'OUTPUT', "Store num, get PMC" );
@{[ $fp_equality_macro ]}
new P0, 'SArray'
set P0, 2
set P0[0], 12.239
set P0[1], -1.9742
new P1, 'Float'
set P1, P0[0]
set N0, P1
.fp_eq(N0, 12.239, ok1)
print "not "
ok1: print "ok 1\\n"
new P2, 'Integer'
set P2, P0[1]
set N0, P2
.fp_eq(N0, -1.9742, ok2)
print "not "
ok2: print "ok 2\\n"
end
CODE
ok 1
ok 2
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: int" );
new P0, 'SArray'
set P0, 1
set P0[5], 12
set I0, P0[5]
print I0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: num" );
new P0, 'SArray'
set P0, 1
set P0[5], 12.5
set N0, P0[5]
print N0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: string" );
new P0, 'SArray'
set P0, 1
set P0[5], "asdf"
set S0, P0[5]
print S0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push int" );
new P0, 'SArray'
push P0, 12
set I0, P0[0]
print I0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push num" );
new P0, 'SArray'
push P0, 12.09
set N0, P0[0]
print N0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push string" );
new P0, 'SArray'
push P0, "Ygnve"
set S0, P0[0]
print S0
end
CODE
/SArray index out of bounds/
OUTPUT
pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push pmc" );
new P0, 'SArray'
new P1, 'Integer'
set P1, 1234
push P0, P1
set I0, P0[0]
print I0
end
CODE
/SArray index out of bounds/
OUTPUT
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: