[t] Convert an exception test to PIR
[parrot.git] / t / pmc / resizablepmcarray.t
blobd6067621b79af61b7da5fecc7e46280dc665dea8
1 #! parrot
2 # Copyright (C) 2001-2009, Parrot Foundation.
3 # $Id$
5 =head1 NAME
7 t/pmc/resizablepmcarray.t - testing the ResizablePMCArray PMC
9 =head1 SYNOPSIS
11     % prove t/pmc/resizablepmcarray.t
13 =head1 DESCRIPTION
15 Tests C<ResizablePMCArray> PMC. Checks size, sets various elements, including
16 out-of-bounds test. Checks INT and PMC keys.
18 =cut
20 .sub main :main
21     .include 'fp_equality.pasm'
22     .include 'test_more.pir'
24     plan(125)
26     resize_tests()
27     negative_array_size()
28     set_tests()
29     exception_tests()
30     set_keyed_get_keyed_tests()
31     interface_check()
32     inherited_sort_method()
33     sort_subclass()
34     push_pmc()
35     push_int()
36     push_string()
37     shift_int()
38     unshift_pmc()
39     get_mro_tests()
40     push_and_pop()
41     unshift_and_shift()
42     multikey_access()
43     exists_and_defined()
44     append_tests()
45     splice_tests()
46     splice_replace1()
47     splice_replace2()
48     iterate_subclass_of_rpa()
49     method_forms_of_unshift_etc()
50     sort_with_broken_cmp()
51     addr_tests()
52     equality_tests()
53 .end
56 .sub resize_tests
57     .local pmc p
58     .local int is_ok, i
59     p = new ['ResizablePMCArray']
61     i = p
62     is_ok = i == 0
63     ok(is_ok, "resize test (0)")
65     p = 1
66     i = p
67     is_ok = i == 1
68     ok(is_ok, "resize test (1)")
70     p = 5
71     i = p
72     is_ok = i == 5
73     ok(is_ok, "resize test (5)")
75     p = 9
76     i = p
77     is_ok = i == 9
78     ok(is_ok, "resize test (9)")
80     p = 7
81     i = p
82     is_ok = i == 7
83     ok(is_ok, "resize test (7)")
84 .end
87 .sub negative_array_size
88     .local pmc p
89     .local int is_ok, i
90     p = new ['ResizablePMCArray']
91     push_eh eh
92     p = -1
93     pop_eh
94     ok(0, "exception not caught")
95     goto end
96 eh:
97     ok(1, "exception caught")
98 end:
99 .end
102 .sub set_tests
103     .local pmc p
104     .local int is_ok, i
105     .local num n
106     .local string s
108     p = new ['ResizablePMCArray']
109     p = 1
111     p[0] = -7
112     i = p[0]
113     is_ok = i == -7
114     ok(is_ok, "INTVAL assignment to first element")
116     p[0] = 3.7
117     n = p[0]
118     is_ok = n == 3.7
119     ok(is_ok, "FLOATVAL assignment to first element")
121     p[0] = "muwhahaha"
122     s = p[0]
123     is_ok = s == "muwhahaha"
124     ok(is_ok, "STRING assignment to first element")
126     p[1] = -7
127     i = p[1]
128     is_ok = i == -7
129     ok(is_ok, "INTVAL assignment to second element")
131     p[1] = 3.7
132     n = p[1]
133     is_ok = n == 3.7
134     ok(is_ok, "FLOATVAL assignment to second element")
136     p[1] = "muwhahaha"
137     s = p[1]
138     is_ok = s == "muwhahaha"
139     ok(is_ok, "STRING assignment to second element")
141     p[10] = -7
142     i = p[10]
143     is_ok = i == -7
144     ok(is_ok, "INTVAL assignment to last element")
146     p[10] = 3.7
147     n = p[10]
148     is_ok = n == 3.7
149     ok(is_ok, "FLOATVAL assignment to last element")
151     p[10] = "muwhahaha"
152     s = p[10]
153     is_ok = s == "muwhahaha"
154     ok(is_ok, "STRING assignment to last element")
155 .end
158 .sub exception_tests
159     .local pmc rpa, i
161     rpa = new ['ResizablePMCArray']
162     rpa = 1
163     i = new ['Integer']
164     i = 12345
166     push_eh eh1
167     rpa[10] = i
168     pop_eh
169     goto no_eh1
170 eh1:
171     ok(0, "unwanted ex thrown for out-of-bounds index")
172     goto test2
173 no_eh1:
174     ok(1, "no ex thrown for out-of-bounds index")
176 test2:
177     rpa = 1
178     push_eh eh2
179     rpa[-10] = i
180     pop_eh
181     goto no_eh2
182 eh2:
183     ok(1, "ex thrown for negative index")
184     goto test3
185 no_eh2:
186     ok(0, "no ex thrown for negative index")
188 test3:
189     rpa = 1
190     push_eh eh3
191     i = rpa[10]
192     pop_eh
193     goto no_eh3
194 eh3:
195     ok(0, "unwanted ex thrown for out-of-bounds index")
196     goto test4
197 no_eh3:
198     ok(1, "no ex thrown for out-of-bounds index")
200 test4:
201     rpa = 1
202     push_eh eh4
203     i = rpa[-10]
204     pop_eh
205     goto no_eh4
206 eh4:
207     ok(1, "ex thrown for negative index")
208     goto end
209 no_eh4:
210     ok(0, "no ex thrown for negative index")
211 end:
212 .end
215 .sub set_keyed_get_keyed_tests
217     new $P0, ['ResizablePMCArray']
218     new $P1, ['Key']
220     set $P1, 0
221     set $P0[$P1], 25
223     set $P1, 1
224     set $P0[$P1], 2.5
226     set $P1, 2
227     set $P0[$P1], "bleep"
229     new $P2, ['String']
230     set $P2, "Bloop"
231     set $P1, 3
232     set $P0[$P1], $P2
234     set $I0, $P0[0]
235     is($I0, 25, "set int via Key PMC, get int via int")
237     set $N0, $P0[1]
238     .fp_eq($N0, 2.5, OK1)
239     ok(0, "set num via Key PMC, get num via int fails")
240     goto NOK1
241 OK1:
242     ok(1, "set num via Key PMC, get num via int fails")
243 NOK1:
245     set $S0, $P0[2]
246     is($S0, "bleep", "set string via Key PMC, get string via int")
248     new $P3, ['Undef']
249     set $P3, $P0[3]
250     set $S0, $P3
251     is($S0, "Bloop", "set PMC via Key PMC, get PMC via PMC")
254     new $P0, ['ResizablePMCArray']
255     set $P0, 1
257     set $P0[25], 125
258     set $P0[128], 10.2
259     set $P0[513], "cow"
260     new $P1, ['Integer']
261     set $P1, 123456
262     set $P0[1023], $P1
264     new $P2, ['Key']
265     set $P2, 25
266     set $I0, $P0[$P2]
267     is($I0, 125, "set int via int, get int via Key PMC")
269     set $P2, 128
270     set $N0, $P0[$P2]
271     .fp_eq($N0, 10.2, OK2)
272     ok(0, "set num via int, get num via Key PMC")
273     goto NOK2
274 OK2:
275     ok(1, "set num via int, get num via Key PMC")
276 NOK2:
278     set $P2, 513
279     set $S0, $P0[$P2]
280     is($S0, "cow", "set string via int, get string via Key PMC")
282     set $P2, 1023
283     set $P3, $P0[$P2]
284     set $I1, $P3
285     is($I1, 123456, "set int via int, get int via Key PMC")
287 .end
290 .sub interface_check
291     .local pmc p
292     p = new ['ResizablePMCArray']
293     .local int b
294     does b, p, "scalar"
295     is(b, 0 ,"ResizablePMCArray doesn't do scalar")
296     does b, p, "array"
297     is(b, 1, "ResizablePMCArray does array")
298     does b, p, "no_interface"
299     is(b, 0, "ResizablePMCArray doesn't do no_interface")
300 .end
303 .sub inherited_sort_method
304     .local pmc ar
305     ar = new ['ResizablePMCArray']
307     ar[0] = 10
308     ar[1] = 2
309     ar[2] = 5
310     ar[3] = 9
311     ar[4] = 1
313     .local pmc cmp_fun
314     null cmp_fun
315     ar."sort"(cmp_fun)
317     .local string sorted
318     sorted = ''
319     .local pmc it
320     iter it, ar
322     unless it goto done
323     $P0 = shift it
324     $S0 = $P0
325     concat sorted, $S0
326     concat sorted, " "
327     goto lp
328 done:
329     is(sorted, "1 2 5 9 10 ", "inherited sort method works")
330 .end
333 .sub sort_subclass
334     .local pmc subrpa, arr
335     subrpa = subclass ['ResizablePMCArray'], 'ssRPA'
336     arr = new subrpa
337     arr[0] = 'p'
338     arr[1] = 'a'
339     arr[2] = 'z'
340     # Use a comparator that gives a reverse alphabetical order
341     # to make sure sort is using it, and not some default from
342     # elsewhere.
343     .local pmc comparator
344     comparator = get_global 'compare_reverse'
345     arr.'sort'(comparator)
346     .local string s, aux
347     s = typeof arr
348     concat s, ':'
349     aux = join '-', arr
350     concat s, aux
351     is(s, 'ssRPA:z-p-a', "sort works in a pir subclass, TT #218")
352 .end
354 .sub compare_reverse
355     .param string a
356     .param string b
357     $I0 = cmp_str b, a
358     .return($I0)
359 .end
362 .sub push_pmc
363     .local pmc pmc_arr, pmc_9999, pmc_10000
364     pmc_arr = new ['ResizablePMCArray']
365     pmc_9999  = new ['Float']
366     pmc_9999  = 10000.10000
367     pmc_10000 = new ['Float']
368     pmc_10000 = 123.123
369     pmc_arr[9999] = pmc_9999
370     push pmc_arr, pmc_10000
371     .local int elements
372     elements = pmc_arr
373     is(elements, 10001, "element count is correct")
374     .local pmc last
375     last = pmc_arr[10000]
376     is(last, 123.123, "last element has correct value")
377 .end
380 .sub push_int
381     .local pmc pmc_arr, pmc_9999
382     .local int int_10000
383     pmc_arr = new ['ResizablePMCArray']
384     pmc_9999  = new ['Float']
385     pmc_9999  = 10000.10000
386     int_10000 = 123
387     pmc_arr[9999] = pmc_9999
388     push pmc_arr, int_10000
389     .local int elements
390     elements = pmc_arr
391     is(elements, 10001, "element count is correct")
392     .local pmc last
393     last = pmc_arr[10000]
394     is(last, 123, "last element has correct value")
395 .end
398 .sub push_string
399     .local pmc pmc_arr, pmc_9999
400     .local string string_10000
401     pmc_arr = new ['ResizablePMCArray']
402     pmc_9999  = new ['Float']
403     pmc_9999  = 10000.10000
404     string_10000 = '123asdf'
405     pmc_arr[9999] = pmc_9999
406     push pmc_arr, string_10000
407     .local int elements
408     elements = pmc_arr
409     is(elements, 10001, "element count is correct")
410     .local pmc last
411     last = pmc_arr[10000]
412     is(last, "123asdf", "last element has correct value")
413 .end
416 .sub shift_int
417     .local pmc pmc_arr, elem
418     pmc_arr = new ['ResizablePMCArray']
419     push pmc_arr, 4
420     push pmc_arr, 3
421     push pmc_arr, 2
422     push pmc_arr, 1
423     push pmc_arr, 0
425     .local int elements
427     elements = pmc_arr
428     is(elements, 5, "element count is correct")
430     elem = shift pmc_arr
431     is(elem, 4, "correct element unshifted")
432     elements = pmc_arr
433     is(elements, 4, "correct element count after unshifing")
435     elem = shift pmc_arr
436     is(elem, 3, "correct element unshifted")
437     elements = pmc_arr
438     is(elements, 3, "correct element count after unshifing")
440     elem = shift pmc_arr
441     is(elem, 2, "correct element unshifted")
442     elements = pmc_arr
443     is(elements, 2, "correct element count after unshifing")
445     elem = shift pmc_arr
446     is(elem, 1, "correct element unshifted")
447     elements = pmc_arr
448     is(elements, 1, "correct element count after unshifing")
450     elem = shift pmc_arr
451     is(elem, 0, "correct element unshifted")
452     elements = pmc_arr
453     is(elements, 0, "correct element count after unshifing")
455 .end
457 .sub unshift_pmc
458     new $P0, ['ResizablePMCArray']
459     new $P1, ['Integer']
460     set $P1, 1
461     new $P2, ['Integer']
462     set $P2, 2
463     new $P3, ['Integer']
464     set $P3, 3
465     unshift $P0, $P1
466     unshift $P0, $P2
467     unshift $P0, $P3
468     elements $I0, $P0
469     is($I0, 3, "element count is correct")
470     set $P3, $P0[0]
471     is($P3, 3, "element 0 has correct value")
472     set $P3, $P0[1]
473     is($P3, 2, "element 1 has correct value")
474     set $P3, $P0[2]
475     is($P3, 1, "element 2 has correct value")
476 .end
479 .sub get_mro_tests
480     new $P0, ['ResizablePMCArray']
481     $P1 = inspect $P0, 'mro'
482     ok(1, "get_mro didn't explode")
483     elements $I1, $P1
484     null $I0
485     $S1 = ''
486 loop:
487     set $P2, $P1[$I0]
488     typeof $S0, $P2
489     concat $S1, $S0
490     concat $S1, ","
491     inc $I0
492     lt $I0, $I1, loop
494     is($S1, "ResizablePMCArray,FixedPMCArray,", "ResizablePMCArrays have the right MRO")
495 .end
498 .sub push_and_pop
499     .local num f, f_elem
500     .local int i, i_elem, elements
501     .local pmc p, p_elem, pmc_arr
502     .local string s, s_elem
504     f = 123.123
505     i = 123
506     p = new ['Float']
507     p = 456.456
508     s = "abc"
510     pmc_arr = new ['ResizablePMCArray']
512     elements = pmc_arr
513     is(elements, 0, "element count of empty ResizablePMCArray is 0")
515     push pmc_arr, s
516     push pmc_arr, p
517     push pmc_arr, i
518     push pmc_arr, f
520     elements = pmc_arr
521     is(elements, 4, "element count after several push operations is correct")
523     f_elem = pop pmc_arr
524     is(f_elem, 123.123000, "shifted float is correct")
526     i_elem = pop pmc_arr
527     is(i_elem, 123, "shifted int is correct")
529     p_elem = pop pmc_arr
530     is(p_elem, 456.456, "shifted PMC is correct")
532     s_elem = pop pmc_arr
533     is(s_elem, "abc", "shifted string is correct")
534     elements = pmc_arr
535     is(elements, 0, "element count after several shift operations is correct")
537 .end
540 .sub unshift_and_shift
541     .local num f, f_elem
542     .local int i, i_elem, elements
543     .local pmc p, p_elem, pmc_arr
544     .local string s, s_elem
546     f = 123.123
547     i = 123
548     p = new ['Float']
549     p = 456.456
550     s = "abc"
552     pmc_arr = new ['ResizablePMCArray']
554     elements = pmc_arr
555     is(elements, 0, "empty RPA has 0 elements")
557     unshift pmc_arr, f
558     unshift pmc_arr, i
559     unshift pmc_arr, p
560     unshift pmc_arr, s
562     elements = pmc_arr
563     is(elements, 4, "RPA has 4 elements after 4 unshifts")
565     s_elem = shift pmc_arr
566     is(s_elem, "abc", "shifted string has correct value")
568     p_elem = shift pmc_arr
569     is(p_elem, 456.456, "shifted pmc has correct value")
571     i_elem = shift pmc_arr
572     is(i_elem, 123, "shifted int has correct value")
574     f_elem = shift pmc_arr
575     is(f_elem, 123.123000, "shifted num has correct value")
576     elements = pmc_arr
577     is(elements, 0, "expectedly empty RPA has 0 elements")
578 .end
580 ## an Integer Matrix, as used by befunge as a playing field
581 .sub multikey_access
582     .local pmc matrix, row_in, row_out
583     matrix = new ['ResizablePMCArray']
584     row_in = new ['ResizableIntegerArray']
585     push row_in, 42
586     push matrix, row_in
588     .local int elem
589     elem = matrix[0;0]
590     is(elem, 42, "int in nested ResizableIntegerArray is 42")
592     matrix[0;1] = 43
593     elem = matrix[0;1]
594     is(elem, 43, "int in nested ResizableIntegerArray is 43")
595 .end
598 .sub exists_and_defined
599     .local pmc array
600     array = new ['ResizablePMCArray']
601     push array, 'a'
602     push array, 'b'
603     push array, 'c'
604     $P0 = new ['Null']
605     push array, $P0
606     push array, 'e'
607     $P0 = new ['Undef']
608     push array, $P0
609     push array, '7'
610     push array, '-8.8'
612     .local int flag, index, ex, def
614     ## bounds checking: lower (0)
615     ex = exists array[0]
616     is(ex, 1, "element at idx 0 exists")
617     def = defined array[0]
618     is(def, 1, "element at idx 0 is defined")
620     ## bounds checking: upper (7)
621     ex = exists array[7]
622     is(ex, 1, "element at idx 7 exists")
623     def = defined array[7]
624     is(def, 1, "element at idx 7 is defined")
626     ## bounds checking: negative lower (-1)
627     ex = exists array[-1]
628     is(ex, 1, "element at idx -1 exists")
629     def = defined array[-1]
630     is(def, 1, "element at idx -1 is defined")
632     ## bounds checking: negative upper (-8)
633     ex = exists array[-8]
634     is(ex, 1, "element at idx -8 exists")
635     def = defined array[-8]
636     is(def, 1, "element at idx -8 is defined")
638     ## bounds checking: out-of-bounds (8)
639     ex = exists array[8]
640     is(ex, 0, "element at idx 8 does not exist")
641     def = defined array[8]
642     is(def, 0, "element at idx 8 is not defined")
644     ## bounds checking: negative out-of-bounds (-9)
645     ex = exists array[-9]
646     is(ex, 0, "element at idx -9 does not exist")
647     def = defined array[-9]
648     is(def, 0, "element at idx -9 is not defined")
650     ## null value (3)
651     ex = exists array[3]
652     is(ex, 0, "element at idx 3 does not exist")
653     def = defined array[3]
654     is(def, 0, "element at idx 3 is not defined")
656     ## undefined value (5)
657     ex = exists array[5]
658     is(ex, 1, "element at idx 5 does not exist")
659     def = defined array[5]
660     is(def, 0, "element at idx 5 is not defined")
661 .end
664 .sub append_tests
666     $P1 = new ['ResizablePMCArray']
667     push $P1, 'a'
668     push $P1, 'b'
669     push $P1, 'c'
671     $P2 = new ['FixedPMCArray']
672     $P2 = 2
673     $P0 = new ['Null']
674     $P2[0] = $P0
675     $P2[1] = 'e'
676     $P0 = new ['Undef']
678     $P3 = new ['ResizablePMCArray']
679     push $P3, $P0
680     push $P3, '7'
681     push $P3, '-8.8'
683     $P4 = new ['ResizablePMCArray']
685     $P5 = new ['MultiSub']    # extends ResizablePMCArray
686     $P99 = new ['Sub']
687     push $P5, $P99
689     $P4.'append'( $P4 )
690     ok( 1, 'parsing' )
692     $I1 = $P4
693     is( $I1, 0, 'still size 0' )
695     $P10 = $P1
696     $I1 = $P10
697     $P10.'append'( $P4 )
698     $I2 = $P10
699     is( $I1, $I2, 'append empty ResizablePMCArray' )
701     $S1 = $P10[2]
702     is( $S1, 'c', 'indexing elements' )
704     $P10.'append'( $P2 )
705     is( $P10, 5, 'append FixedPMCArray' )
707     $S1 = $P10[2]
708     is( $S1, 'c', 'indexing elements' )
710     $S1 = $P10[4]
711     is( $S1, 'e', 'indexing elements' )
713     $P3.'append'( $P10 )
714     is( $P3, 8, 'append ResizablePMCArray' )
716     $S1 = $P3[2]
717     is( $S1, '-8.8', 'indexing elements' )
719     $S1 = $P3[4]
720     is( $S1, 'b', 'indexing elements' )
722     $P3.'append'( $P5 )
723     is( $P3, 9, 'append subclass' )
725     $S1 = $P3[2]
726     is( $S1, '-8.8', 'indexing elements' )
728     $P99 = $P3[8]
729     $I99 = isa $P99, 'Sub'
730     ok( $I99, 'indexing elements' )
731 .end
734 .sub get_array_string
735     .param pmc p
736     $S0 = ''
737     $P3 = iter p
738 loop:
739     unless $P3 goto loop_end
740     $P4 = shift $P3
741     $S1 = $P4
742     concat $S0, $S1
743     goto loop
744 loop_end:
745     .return($S0)
746 .end
749 .sub splice_tests
750     .local pmc ar1, ar2
751     ar1 = new ['ResizablePMCArray']
752     ar1[0] = 1
753     ar1[1] = 2
754     ar1[2] = 3
755     ar1[3] = 4
756     ar1[4] = 5
758     ar2 = new ['ResizablePMCArray']
759     ar2[0] = 'A'
760     ar2[1] = 'B'
761     ar2[2] = 'C'
762     ar2[3] = 'D'
763     ar2[4] = 'E'
765     $P1 = clone ar1
766     $P2 = clone ar2
767     splice $P1, $P2, 0, 5
768     $S0 = get_array_string($P1)
769     is($S0, "ABCDE", "splice with complete replace")
771     $P1 = clone ar1
772     $P2 = clone ar2
773     splice $P1, $P2, 5, 0
774     $S0 = get_array_string($P1)
775     is($S0, "12345ABCDE", "splice, append")
777     $P1 = clone ar1
778     $P2 = clone ar2
779     splice $P1, $P2, 4, 0
780     $S0 = get_array_string($P1)
781     is($S0, "1234ABCDE5", "splice, insert before last element")
783     $P1 = clone ar1
784     $P2 = clone ar2
785     splice $P1, $P2, 3, 0
786     $S0 = get_array_string($P1)
787     is($S0, "123ABCDE45", "splice, append-in-middle")
789     $P1 = clone ar1
790     $P2 = clone ar2
791     splice $P1, $P2, 0, 2
792     $S0 = get_array_string($P1)
793     is($S0, "ABCDE345", "splice, replace at beginning")
795     $P1 = clone ar1
796     $P2 = clone ar2
797     splice $P1, $P2, 2, 2
798     $S0 = get_array_string($P1)
799     is($S0, "12ABCDE5", "splice, replace in middle")
801     $P1 = clone ar1
802     $P2 = clone ar2
803     splice $P1, $P2, 3, 2
804     $S0 = get_array_string($P1)
805     is($S0, "123ABCDE", "splice, replace at end")
807     $P1 = clone ar1
808     $P2 = new ['Array']
809     $P2 = 5
810     $P2[0] = 'A'
811     $P2[1] = 'B'
812     $P2[2] = 'C'
813     $P2[3] = 'D'
814     $P2[4] = 'E'
815     splice $P1, $P2, 3, 2
816     $S0 = get_array_string($P1)
817     is($S0, "123ABCDE", "splice, replace with another type")
819     $P1 = clone ar1
820     $P2 = new ['ResizablePMCArray']
821     splice $P1, $P2, 2, 2
822     $S0 = get_array_string($P1)
823     is($S0, "125", "splice with empty replacement")
825     $P1 = clone ar1
826     $P2 = new ['ResizablePMCArray']
827     $P2[0] = 'A'
828     splice $P1, $P2, 2, 1
829     $S0 = get_array_string($P1)
830     is($S0, "12A45", "splice with empty replacement")
832 .end
835 .sub splice_replace1
836     $P1 = new ['ResizablePMCArray']
837     $P1 = 3
838     $P1[0] = '1'
839     $P1[1] = '2'
840     $P1[2] = '3'
841     $P2 = new ['ResizablePMCArray']
842     $P2 = 1
843     $P2[0] = 'A'
844     splice $P1, $P2, 1, 2
845     $S0 = join "", $P1
846     is($S0, "1A", "replacement via splice works")
847 .end
850 .sub splice_replace2
851     $P1 = new ['ResizablePMCArray']
852     $P1 = 3
853     $P1[0] = '1'
854     $P1[1] = '2'
855     $P1[2] = '3'
856     $P2 = new ['ResizablePMCArray']
857     $P2 = 1
858     $P2[0] = 'A'
859     splice $P1, $P2, 0, 2
860     $S0 = join "", $P1
861     is($S0, "A3", "replacement via splice works")
862 .end
865 #RT #40958 - can't iterate subclass of ResizablePMCArray
866 .sub iterate_subclass_of_rpa
867     .local pmc arr, it
868     $P0 = subclass 'ResizablePMCArray', 'MyArray'
870     arr = new ['MyArray']
871     push arr, 11
872     push arr, 13
873     push arr, 15
874     $I0 = elements arr
875     is($I0, 3, "RPA subclass has correct element count")
877     $S1 = ''
878     it = iter arr
879 loop:
880     unless it goto end
881     $P2 = shift it
882     $S0 = $P2
883     concat $S1, $S0
884     concat $S1, ","
885     goto loop
886 end:
887     is($S1, "11,13,15,", "iterator works on RPA subclass")
888 .end
891 .sub method_forms_of_unshift_etc
892     $P0 = new ['ResizablePMCArray']
893     $P0.'unshift'(1)
894     $P0.'push'('two')
895     $I0 = $P0
896     is($I0, 2, "method forms of unshift and push add elements to an RPA")
897     $P1 = $P0.'shift'()
898     is($P1, 1, "method form of shift works")
899     $P1 = $P0.'pop'()
900     is($P1, "two", "method form of pop works")
901 .end
904 #RT #56636 - segfault from sort if comparison is always 1
905 .sub sort_with_broken_cmp
906     .local pmc array
907     array = new ['ResizablePMCArray']
908     push array, 4
909     push array, 5
910     push array, 3
911     push array, 2
912     push array, 5
913     push array, 1
915     $S0 = join ' ', array
916     is($S0, "4 5 3 2 5 1", "RPA has expected values")
918     $P0 = get_global 'cmp_func'
919     array.'sort'($P0)
920     ok(1, "sort returns without crashing")
921 .end
923 .sub 'cmp_func'
924     .param pmc a
925     .param pmc b
926     $I0 = 1
927     .return ($I0)
928 .end
930 .sub 'addr_tests'
931     $P0 = new 'ResizablePMCArray'
932     $I0 = get_addr $P0
933     $P1 = new 'ResizablePMCArray'
934     $I1 = get_addr $P1
936     $I2 = $I0 != 0
937     ok($I2, 'ResizablePMCArray address is not zero')
938     $I2 = $I0 != $I1
939     ok($I2, 'Two empty RPAs do not have same address')
941     push $P0, 3
942     $I1 = get_addr $P0
943     is($I0, $I1, 'Adding element to RPA keeps same addr')
944 .end
946 .sub 'equality_tests'
947     .local pmc array1, array2, array3, array4
948     array1 = new ['ResizablePMCArray']
949     array2 = new ['ResizablePMCArray']
950     array3 = new ['ResizablePMCArray']
952     array1[0] = "Hello Parrot!"
953     array1[1] = 1664
954     array1[2] = 2.718
956     $P0 = box "Hello Parrot!"
957     array2[0] = $P0
958     $P0 = box 1664
959     array2[1] = $P0
960     $P0 = box 2.718
961     array2[2] = $P0
963     array3[0] = "Goodbye Parrot!"
964     array3[1] = 1664
965     array3[2] = 2.718
967     array4 = clone array1
969     is(array1, array2, 'Physically disjoint, but equal arrays')
970     is(array1, array4, 'Clones are equal')
971     isnt(array1, array3, 'Different arrays')
972 .end
974 # don't forget to change the test plan
976 # Local Variables:
977 #   mode: pir
978 #   fill-column: 100
979 # End:
980 # vim: expandtab shiftwidth=4 ft=pir: