tagged release 0.6.4
[parrot.git] / t / pmc / sarray.t
bloba7a664d5c8037742f2d4e5cd20cc5d1025862a6b
1 #! perl
2 # Copyright (C) 2001-2007, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( . lib ../lib ../../lib );
8 use Test::More;
9 use Parrot::Test tests => 30;
11 =head1 NAME
13 t/pmc/sarray.t - Simple Array
15 =head1 SYNOPSIS
17     % prove t/pmc/sarray.t
19 =head1 DESCRIPTION
21 Tests the C<SArray> PMC, which is used for parameter-passing.
23 =cut
25 my $fp_equality_macro = pasm_fp_equality_macro();
27 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting sarray size" );
28     new P0, 'SArray'
29     set I0, P0
30     eq I0, 0, OK_1
31     print "not "
32 OK_1:    print "ok 1\n"
34     set P0, 1
35     set P0[0], 100
36     set I0, P0
37     eq I0, 1, OK_2
38     print "not "
39 OK_2:    print "ok 2\n"
40     end
41 CODE
42 ok 1
43 ok 2
44 OUTPUT
46 pasm_error_output_like( <<'CODE', <<'OUTPUT', "attempt resize" );
47     new P0, 'SArray'
48     set P0, 1
49     set P0[0], 100
50     set I0, P0
51     eq I0, 1, OK_1
52     print "not "
53 OK_1:    print "ok 1\n"
55     set P0, 2
56     end
57 CODE
58 /ok 1
59 SArray: Can't resize!
60 current instr\.:/
61 OUTPUT
63 # '
65 pasm_output_is( <<'CODE', <<'OUTPUT', "indexed access" );
66     new P0, 'SArray'
67     set P0, 3
68     set P0[0], 100
69     set P0[1], 200
70     set P0[2], 300
71     set I0, P0[0]
72     eq I0, 100, ok1
73     print "not "
74 ok1:    print "ok 1\n"
75     set I0, P0[1]
76     eq I0, 200, ok2
77     print "not "
78 ok2:    print "ok 2\n"
79     set I0, P0[2]
80     eq I0, 300, ok3
81     print "not "
82 ok3:    print "ok 3\n"
83     set I0, P0[-1]
84     eq I0, 300, ok4
85     print "not "
86 ok4:    print "ok 4\n"
87     set I0, P0[-2]
88     eq I0, 200, ok5
89     print "not "
90 ok5:    print "ok 5\n"
91     set I0, P0[-3]
92     eq I0, 100, ok6
93     print "not "
94 ok6:    print "ok 6\n"
95     end
96 CODE
97 ok 1
98 ok 2
99 ok 3
100 ok 4
101 ok 5
102 ok 6
103 OUTPUT
105 pasm_output_is( <<'CODE', <<'OUTPUT', "push" );
106     new P0, 'SArray'
107     set P0, 3
108     push P0, 100
109     push P0, 200
110     push P0, 300
111     set I0, P0[0]
112     eq I0, 100, ok1
113     print "not "
114 ok1:    print "ok 1\n"
115     set I0, P0[1]
116     eq I0, 200, ok2
117     print "not "
118 ok2:    print "ok 2\n"
119     set I0, P0[2]
120     eq I0, 300, ok3
121     print "not "
122 ok3:    print "ok 3\n"
123     set I0, P0[-1]
124     eq I0, 300, ok4
125     print "not "
126 ok4:    print "ok 4\n"
127     set I0, P0[-2]
128     eq I0, 200, ok5
129     print "not "
130 ok5:    print "ok 5\n"
131     set I0, P0[-3]
132     eq I0, 100, ok6
133     print "not "
134 ok6:    print "ok 6\n"
135     end
136 CODE
137 ok 1
138 ok 2
139 ok 3
140 ok 4
141 ok 5
142 ok 6
143 OUTPUT
145 pasm_output_is( <<'CODE', <<'OUTPUT', "push / indexed" );
146     new P0, 'SArray'
147     set P0, 3
148     push P0, 100
149     set P0[1], 200
150     push P0, 300
151     set I0, P0[0]
152     eq I0, 100, ok1
153     print "not "
154 ok1:    print "ok 1\n"
155     set I0, P0[1]
156     eq I0, 200, ok2
157     print "not "
158 ok2:    print "ok 2\n"
159     set I0, P0[2]
160     eq I0, 300, ok3
161     print "not "
162 ok3:    print "ok 3\n"
163     set I0, P0[-1]
164     eq I0, 300, ok4
165     print "not "
166 ok4:    print "ok 4\n"
167     set I0, P0[-2]
168     eq I0, 200, ok5
169     print "not "
170 ok5:    print "ok 5\n"
171     set I0, P0[-3]
172     eq I0, 100, ok6
173     print "not "
174 ok6:    print "ok 6\n"
175     end
176 CODE
177 ok 1
178 ok 2
179 ok 3
180 ok 4
181 ok 5
182 ok 6
183 OUTPUT
185 pasm_output_is( <<'CODE', <<'OUTPUT', "mixed indexed" );
186     new P0, 'SArray'
187     set P0, 4
188     set P0[0], 1000
189     set N0, 222.22
190     set P0[1], N0
191     set S0, "string\n"
192     set P0[2], S0
193     new P1, 'Undef'
194     set P1, 42
195     set P0[3], P1
197     set I0, P0[0]
198     eq I0, 1000, ok1
199     print "not "
200 ok1:    print "ok 1\n"
201     set N1, P0[1]
202     eq N0, N1, ok2
203     print "not "
204 ok2:    print "ok 2\n"
205     set S1, P0[2]
206     print S1
207     set P3, P0[3]
208     print P3
209     print "\n"
210     end
211 CODE
212 ok 1
213 ok 2
214 string
216 OUTPUT
218 pasm_output_is( <<'CODE', <<'OUTPUT', "mixed push" );
219     new P0, 'SArray'
220     set P0, 4
221     push P0, 1000
222     set N0, 222.22
223     push P0, N0
224     set S0, "string\n"
225     push P0, S0
226     new P1, 'Undef'
227     set P1, 42
228     push P0, P1
230     set I0, P0[0]
231     eq I0, 1000, ok1
232     print "not "
233 ok1:    print "ok 1\n"
234     set N1, P0[1]
235     eq N0, N1, ok2
236     print "not "
237 ok2:    print "ok 2\n"
238     set S1, P0[2]
239     print S1
240     set P3, P0[3]
241     print P3
242     print "\n"
243     end
244 CODE
245 ok 1
246 ok 2
247 string
249 OUTPUT
251 pasm_output_is( <<'CODE', <<'OUTPUT', "mixed push - clone" );
252     new P2, 'SArray'
253     set P2, 4
254     push P2, 1000
255     set N0, 222.22
256     push P2, N0
257     set S0, "string\n"
258     push P2, S0
259     new P1, 'Undef'
260     set P1, 42
261     push P2, P1
263     clone P0, P2
265     set I0, P0[0]
266     eq I0, 1000, ok1
267     print "not "
268 ok1:    print "ok 1\n"
269     set N1, P0[1]
270     eq N0, N1, ok2
271     print "not "
272 ok2:    print "ok 2\n"
273     set S1, P0[2]
274     print S1
275     set P3, P0[3]
276     print P3
277     print "\n"
278     end
279 CODE
280 ok 1
281 ok 2
282 string
284 OUTPUT
286 pasm_output_is( <<'CODE', <<'OUTPUT', "shift_integer" );
287     new P0, 'SArray'
288     set P0, 3
289     set P0[0], 100
290     set P0[1], 200
291     set P0[2], 300
293     shift I0, P0
294     eq I0, 100, ok1
295     print "not "
296 ok1:    print "ok 1\n"
297     set I0, P0
298     eq I0, 2, ok2
299 ok2:    print "ok 2\n"
301     shift I0, P0
302     eq I0, 200, ok3
303     print "not "
304 ok3:    print "ok 3\n"
305     set I0, P0
306     eq I0, 1, ok4
307 ok4:    print "ok 4\n"
309     shift I0, P0
310     eq I0, 300, ok5
311     print "not "
312 ok5:    print "ok 5\n"
313     set I0, P0
314     eq I0, 0, ok6
315 ok6:    print "ok 6\n"
316     end
317 CODE
318 ok 1
319 ok 2
320 ok 3
321 ok 4
322 ok 5
323 ok 6
324 OUTPUT
326 pasm_output_is( <<'CODE', <<'OUTPUT', "mixed shift" );
327     new P0, 'SArray'
328     set P0, 4
329     push P0, 1000
330     set N0, 222.22
331     push P0, N0
332     set S0, "string\n"
333     push P0, S0
334     new P1, 'Undef'
335     set P1, 42
336     push P0, P1
338     shift I0, P0
339     eq I0, 1000, ok1
340     print "not "
341 ok1:    print "ok 1\n"
342     shift N1, P0
343     eq N0, N1, ok2
344     print "not "
345 ok2:    print "ok 2\n"
346     shift S1, P0
347     print S1
348     shift P3, P0
349     print P3
350     print "\n"
351     end
352 CODE
353 ok 1
354 ok 2
355 string
357 OUTPUT
359 pasm_output_is( <<'CODE', <<'OUTPUT', "iterator" );
360     .include "iterator.pasm"
361     new P0, 'SArray'       # empty array
362     new P2, 'SArray'       # array with 2 elements
363     set P2, 2
364     push P2, 10
365     push P2, 20
366     set I0, P2
367     new P1, 'Iterator', P2
368     print "ok 1\n"
369     set I1, P1
370     eq I0, I1, ok2        # iter.length() == array.length()
371     print "not "
372 ok2:    print "ok 2\n"
373     new P1, 'Iterator', P0
374     set P1, .ITERATE_FROM_START
375     print "ok 3\n"
376     unless P1, ok4        # if(iter) == false on empty
377     print "not "
378 ok4:    print "ok 4\n"
379     new P1, 'Iterator', P2
380     set P1, .ITERATE_FROM_START
381     if P1, ok5        # if(iter) == true on non empty
382     print "not "
383 ok5:    print "ok 5\n"
384     # now iterate over P2
385     # while (P1) { element = shift(P1) }
386     unless P1, nok6
387         shift I3, P1
388     eq I3, 10, ok6
389 nok6:    print "not "
390 ok6:    print "ok 6\n"
391     unless P1, nok7
392         shift I3, P1
393     eq I3, 20, ok7
394 nok7:    print "not "
395 ok7:    print "ok 7\n"
396     unless P1, ok8        # if(iter) == false after last
397     print "not "
398 ok8:    print "ok 8\n"
400     # now iterate from end
401     set P1, .ITERATE_FROM_END
402     if P1, ok9        # if(iter) == true on non empty
403     print "not "
404 ok9:    print "ok 9\n"
405     # while (P1) { element = pop(P1) }
406     unless P1, nok10
407         pop I3, P1
408     eq I3, 20, ok10
409 nok10:    print "not "
410 ok10:    print "ok 10\n"
411     unless P1, nok11
412         pop I3, P1
413     eq I3, 10, ok11
414 nok11:    print "not "
415 ok11:    print "ok 11\n"
416     unless P1, ok12        # if(iter) == false after last
417     print "not "
418 ok12:    print "ok 12\n"
419     end
421 CODE
422 ok 1
423 ok 2
424 ok 3
425 ok 4
426 ok 5
427 ok 6
428 ok 7
429 ok 8
430 ok 9
431 ok 10
432 ok 11
433 ok 12
434 OUTPUT
436 pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" );
438 .sub _main
439     .local pmc pmc1
440     pmc1 = new 'SArray'
441     .local int bool1
442     does bool1, pmc1, "scalar"
443     print bool1
444     print "\n"
445     does bool1, pmc1, "array"
446     print bool1
447     print "\n"
448     does bool1, pmc1, "no_interface"
449     print bool1
450     print "\n"
451     end
452 .end
453 CODE
457 OUTPUT
459 pasm_output_is( << "CODE", << 'OUTPUT', "Access via Key PMC" );
460 @{[ $fp_equality_macro ]}
461     new P0, 'SArray'
462     set P0, 4
463     set P0[0], 100
464     set P0[1], 12.298
465     set P0[2], "yarrAS"
466     new P31, 'Hash'
467     set P31["Test"], "ok"
468     set P0[3], P31
469     new P1, 'Key'
470     set P1, 0
471     set I0, P0[P1]
472     eq I0, 100, ok1
473     print "not "
474 ok1:    print "ok 1\\n"
475     new P2, 'Key'
476     set P2, 1
477     set N0, P0[P2]
478     .fp_eq(N0, 12.298, ok2)
479     print "not "
480 ok2:    print "ok 2\\n"
481     new P3, 'Key'
482     set P3, 2
483     set S0, P0[P3]
484     eq S0, "yarrAS", ok3
485     print "not "
486 ok3:    print "ok 3\\n"
487     new P4, 'Key'
488     set P4, 3
489     set P5, P0[P4]
490     set S1, P5["Test"]
491     eq S1, "ok", ok4
492     print "not "
493 ok4:    print "ok 4\\n"
494     end
495 CODE
496 ok 1
497 ok 2
498 ok 3
499 ok 4
500 OUTPUT
502 pasm_output_is( << 'CODE', << 'OUTPUT', "Store PMC, get int" );
503     new P0, 'SArray'
504     set P0, 2
505     new P1, 'Integer'
506     set P1, 11
507     new P2, 'Float'
508     set P2, 1.1
509     set P0[0], P1
510     set P0[1], P2
511     set I0, P0[0]
512     eq I0, 11, ok1
513     print "not "
514 ok1:    print "ok 1\n"
515     set I0, P0[1]
516     eq I0, 1, ok2
517     print "not "
518 ok2:    print "ok 2\n"
519         end
520 CODE
521 ok 1
522 ok 2
523 OUTPUT
525 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store num, get int" );
526     new P0, 'SArray'
527     set P0, 1
528         set P0[0], 4.2
529         set I0, P0[0]
530         print I0
531         end
532 CODE
533 /SArray: Entry not an integer!/
534 OUTPUT
536 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store string, get int" );
537     new P0, 'SArray'
538     set P0, 1
539         set P0[0], "Non-numeric string"
540         set I0, P0[0]
541         print I0
542         end
543 CODE
544 /SArray: Entry not an integer!/
545 OUTPUT
547 pasm_output_is( << "CODE", << 'OUTPUT', "Store PMC, get num" );
548 @{[ $fp_equality_macro ]}
549     new P0, 'SArray'
550     set P0, 2
551     new P1, 'Integer'
552     set P1, 11
553     new P2, 'Float'
554     set P2, 1.1
555     set P0[0], P1
556     set P0[1], P2
557     set N0, P0[0]
558     .fp_eq(N0, 11.0, ok1)
559     print "not "
560 ok1:    print "ok 1\\n"
561     set N0, P0[1]
562     .fp_eq(N0, 1.1, ok2)
563     print "not "
564 ok2:    print "ok 2\\n"
565         end
566 CODE
567 ok 1
568 ok 2
569 OUTPUT
571 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store int, get num" );
572     new P0, 'SArray'
573     set P0, 1
574         set P0[0], 12
575         set N0, P0[0]
576         print N0
577         end
578 CODE
579 /SArray: Entry not a number!/
580 OUTPUT
582 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store string, get num" );
583     new P0, 'SArray'
584     set P0, 1
585         set P0[0], "Non-numeric string"
586         set N0, P0[0]
587         print N0
588         end
589 CODE
590 /SArray: Entry not a number!/
591 OUTPUT
593 pasm_output_is( << 'CODE', << 'OUTPUT', "Store PMC, get string" );
594     new P0, 'SArray'
595     set P0, 2
596     new P1, 'String'
597     set P1, "Hello"
598     new P2, 'Integer'
599     set P2, 1010
600     set P0[0], P1
601     set P0[1], P2
602     set S0, P0[0]
603     eq S0, "Hello", ok1
604     print "not "
605 ok1:    print "ok 1\n"
606     set S0, P0[1]
607     eq S0, "1010", ok2
608     print "not "
609 ok2:    print "ok 2\n"
610         end
611 CODE
612 ok 1
613 ok 2
614 OUTPUT
616 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store int, get string" );
617     new P0, 'SArray'
618     set P0, 1
619         set P0[0], 12
620         set S0, P0[0]
621         print S0
622         end
623 CODE
624 /SArray: Entry not a string!/
625 OUTPUT
627 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Store num, get string" );
628     new P0, 'SArray'
629     set P0, 1
630         set P0[0], 12.5
631         set S0, P0[0]
632         print S0
633         end
634 CODE
635 /SArray: Entry not a string!/
636 OUTPUT
638 pasm_output_is( << "CODE", << 'OUTPUT', "Store num, get PMC" );
639 @{[ $fp_equality_macro ]}
640     new P0, 'SArray'
641     set P0, 2
642     set P0[0], 12.239
643     set P0[1], -1.9742
644     new P1, 'Float'
645     set P1, P0[0]
646     set N0, P1
647     .fp_eq(N0, 12.239, ok1)
648     print "not "
649 ok1:    print "ok 1\\n"
650     new P2, 'Integer'
651     set P2, P0[1]
652     set N0, P2
653     .fp_eq(N0, -1.9742, ok2)
654     print "not "
655 ok2:    print "ok 2\\n"
656         end
657 CODE
658 ok 1
659 ok 2
660 OUTPUT
662 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: int" );
663     new P0, 'SArray'
664     set P0, 1
665         set P0[5], 12
666         set I0, P0[5]
667         print I0
668         end
669 CODE
670 /SArray index out of bounds/
671 OUTPUT
673 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: num" );
674     new P0, 'SArray'
675     set P0, 1
676         set P0[5], 12.5
677         set N0, P0[5]
678         print N0
679         end
680 CODE
681 /SArray index out of bounds/
682 OUTPUT
684 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: string" );
685     new P0, 'SArray'
686     set P0, 1
687         set P0[5], "asdf"
688         set S0, P0[5]
689         print S0
690         end
691 CODE
692 /SArray index out of bounds/
693 OUTPUT
695 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push int" );
696     new P0, 'SArray'
697         push P0, 12
698         set I0, P0[0]
699         print I0
700         end
701 CODE
702 /SArray index out of bounds/
703 OUTPUT
705 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push num" );
706     new P0, 'SArray'
707         push P0, 12.09
708         set N0, P0[0]
709         print N0
710         end
711 CODE
712 /SArray index out of bounds/
713 OUTPUT
715 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push string" );
716     new P0, 'SArray'
717         push P0, "Ygnve"
718         set S0, P0[0]
719         print S0
720         end
721 CODE
722 /SArray index out of bounds/
723 OUTPUT
725 pasm_error_output_like( << 'CODE', << 'OUTPUT', "Out-of-bounds access: push pmc" );
726     new P0, 'SArray'
727     new P1, 'Integer'
728     set P1, 1234
729     push P0, P1
730     set I0, P0[0]
731     print I0
732     end
733 CODE
734 /SArray index out of bounds/
735 OUTPUT
737 # Local Variables:
738 #   mode: cperl
739 #   cperl-indent-level: 4
740 #   fill-column: 100
741 # End:
742 # vim: expandtab shiftwidth=4: