2 # Copyright (C) 2001-2009, Parrot Foundation.
7 t/pmc/resizablepmcarray.t - testing the ResizablePMCArray PMC
11 % prove t/pmc/resizablepmcarray.t
15 Tests C<ResizablePMCArray> PMC. Checks size, sets various elements, including
16 out-of-bounds test. Checks INT and PMC keys.
21 .include 'fp_equality.pasm'
22 .include 'test_more.pir'
30 set_keyed_get_keyed_tests()
32 inherited_sort_method()
48 iterate_subclass_of_rpa()
49 method_forms_of_unshift_etc()
50 sort_with_broken_cmp()
59 p = new ['ResizablePMCArray']
63 ok(is_ok, "resize test (0)")
68 ok(is_ok, "resize test (1)")
73 ok(is_ok, "resize test (5)")
78 ok(is_ok, "resize test (9)")
83 ok(is_ok, "resize test (7)")
87 .sub negative_array_size
90 p = new ['ResizablePMCArray']
94 ok(0, "exception not caught")
97 ok(1, "exception caught")
108 p = new ['ResizablePMCArray']
114 ok(is_ok, "INTVAL assignment to first element")
119 ok(is_ok, "FLOATVAL assignment to first element")
123 is_ok = s == "muwhahaha"
124 ok(is_ok, "STRING assignment to first element")
129 ok(is_ok, "INTVAL assignment to second element")
134 ok(is_ok, "FLOATVAL assignment to second element")
138 is_ok = s == "muwhahaha"
139 ok(is_ok, "STRING assignment to second element")
144 ok(is_ok, "INTVAL assignment to last element")
149 ok(is_ok, "FLOATVAL assignment to last element")
153 is_ok = s == "muwhahaha"
154 ok(is_ok, "STRING assignment to last element")
161 rpa = new ['ResizablePMCArray']
171 ok(0, "unwanted ex thrown for out-of-bounds index")
174 ok(1, "no ex thrown for out-of-bounds index")
183 ok(1, "ex thrown for negative index")
186 ok(0, "no ex thrown for negative index")
195 ok(0, "unwanted ex thrown for out-of-bounds index")
198 ok(1, "no ex thrown for out-of-bounds index")
207 ok(1, "ex thrown for negative index")
210 ok(0, "no ex thrown for negative index")
215 .sub set_keyed_get_keyed_tests
217 new $P0, ['ResizablePMCArray']
227 set $P0[$P1], "bleep"
235 is($I0, 25, "set int via Key PMC, get int via int")
238 .fp_eq($N0, 2.5, OK1)
239 ok(0, "set num via Key PMC, get num via int fails")
242 ok(1, "set num via Key PMC, get num via int fails")
246 is($S0, "bleep", "set string via Key PMC, get string via int")
251 is($S0, "Bloop", "set PMC via Key PMC, get PMC via PMC")
254 new $P0, ['ResizablePMCArray']
267 is($I0, 125, "set int via int, get int via Key PMC")
271 .fp_eq($N0, 10.2, OK2)
272 ok(0, "set num via int, get num via Key PMC")
275 ok(1, "set num via int, get num via Key PMC")
280 is($S0, "cow", "set string via int, get string via Key PMC")
285 is($I1, 123456, "set int via int, get int via Key PMC")
292 p = new ['ResizablePMCArray']
295 is(b, 0 ,"ResizablePMCArray doesn't do scalar")
297 is(b, 1, "ResizablePMCArray does array")
298 does b, p, "no_interface"
299 is(b, 0, "ResizablePMCArray doesn't do no_interface")
303 .sub inherited_sort_method
305 ar = new ['ResizablePMCArray']
329 is(sorted, "1 2 5 9 10 ", "inherited sort method works")
334 .local pmc subrpa, arr
335 subrpa = subclass ['ResizablePMCArray'], 'ssRPA'
340 # Use a comparator that gives a reverse alphabetical order
341 # to make sure sort is using it, and not some default from
343 .local pmc comparator
344 comparator = get_global 'compare_reverse'
345 arr.'sort'(comparator)
351 is(s, 'ssRPA:z-p-a', "sort works in a pir subclass, TT #218")
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']
369 pmc_arr[9999] = pmc_9999
370 push pmc_arr, pmc_10000
373 is(elements, 10001, "element count is correct")
375 last = pmc_arr[10000]
376 is(last, 123.123, "last element has correct value")
381 .local pmc pmc_arr, pmc_9999
383 pmc_arr = new ['ResizablePMCArray']
384 pmc_9999 = new ['Float']
385 pmc_9999 = 10000.10000
387 pmc_arr[9999] = pmc_9999
388 push pmc_arr, int_10000
391 is(elements, 10001, "element count is correct")
393 last = pmc_arr[10000]
394 is(last, 123, "last element has correct value")
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
409 is(elements, 10001, "element count is correct")
411 last = pmc_arr[10000]
412 is(last, "123asdf", "last element has correct value")
417 .local pmc pmc_arr, elem
418 pmc_arr = new ['ResizablePMCArray']
428 is(elements, 5, "element count is correct")
431 is(elem, 4, "correct element unshifted")
433 is(elements, 4, "correct element count after unshifing")
436 is(elem, 3, "correct element unshifted")
438 is(elements, 3, "correct element count after unshifing")
441 is(elem, 2, "correct element unshifted")
443 is(elements, 2, "correct element count after unshifing")
446 is(elem, 1, "correct element unshifted")
448 is(elements, 1, "correct element count after unshifing")
451 is(elem, 0, "correct element unshifted")
453 is(elements, 0, "correct element count after unshifing")
458 new $P0, ['ResizablePMCArray']
469 is($I0, 3, "element count is correct")
471 is($P3, 3, "element 0 has correct value")
473 is($P3, 2, "element 1 has correct value")
475 is($P3, 1, "element 2 has correct value")
480 new $P0, ['ResizablePMCArray']
481 $P1 = inspect $P0, 'mro'
482 ok(1, "get_mro didn't explode")
494 is($S1, "ResizablePMCArray,FixedPMCArray,", "ResizablePMCArrays have the right MRO")
500 .local int i, i_elem, elements
501 .local pmc p, p_elem, pmc_arr
502 .local string s, s_elem
510 pmc_arr = new ['ResizablePMCArray']
513 is(elements, 0, "element count of empty ResizablePMCArray is 0")
521 is(elements, 4, "element count after several push operations is correct")
524 is(f_elem, 123.123000, "shifted float is correct")
527 is(i_elem, 123, "shifted int is correct")
530 is(p_elem, 456.456, "shifted PMC is correct")
533 is(s_elem, "abc", "shifted string is correct")
535 is(elements, 0, "element count after several shift operations is correct")
540 .sub unshift_and_shift
542 .local int i, i_elem, elements
543 .local pmc p, p_elem, pmc_arr
544 .local string s, s_elem
552 pmc_arr = new ['ResizablePMCArray']
555 is(elements, 0, "empty RPA has 0 elements")
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")
577 is(elements, 0, "expectedly empty RPA has 0 elements")
580 ## an Integer Matrix, as used by befunge as a playing field
582 .local pmc matrix, row_in, row_out
583 matrix = new ['ResizablePMCArray']
584 row_in = new ['ResizableIntegerArray']
590 is(elem, 42, "int in nested ResizableIntegerArray is 42")
594 is(elem, 43, "int in nested ResizableIntegerArray is 43")
598 .sub exists_and_defined
600 array = new ['ResizablePMCArray']
612 .local int flag, index, ex, def
614 ## bounds checking: lower (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)
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)
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")
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)
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")
666 $P1 = new ['ResizablePMCArray']
671 $P2 = new ['FixedPMCArray']
678 $P3 = new ['ResizablePMCArray']
683 $P4 = new ['ResizablePMCArray']
685 $P5 = new ['MultiSub'] # extends ResizablePMCArray
693 is( $I1, 0, 'still size 0' )
699 is( $I1, $I2, 'append empty ResizablePMCArray' )
702 is( $S1, 'c', 'indexing elements' )
705 is( $P10, 5, 'append FixedPMCArray' )
708 is( $S1, 'c', 'indexing elements' )
711 is( $S1, 'e', 'indexing elements' )
714 is( $P3, 8, 'append ResizablePMCArray' )
717 is( $S1, '-8.8', 'indexing elements' )
720 is( $S1, 'b', 'indexing elements' )
723 is( $P3, 9, 'append subclass' )
726 is( $S1, '-8.8', 'indexing elements' )
729 $I99 = isa $P99, 'Sub'
730 ok( $I99, 'indexing elements' )
734 .sub get_array_string
739 unless $P3 goto loop_end
751 ar1 = new ['ResizablePMCArray']
758 ar2 = new ['ResizablePMCArray']
767 splice $P1, $P2, 0, 5
768 $S0 = get_array_string($P1)
769 is($S0, "ABCDE", "splice with complete replace")
773 splice $P1, $P2, 5, 0
774 $S0 = get_array_string($P1)
775 is($S0, "12345ABCDE", "splice, append")
779 splice $P1, $P2, 4, 0
780 $S0 = get_array_string($P1)
781 is($S0, "1234ABCDE5", "splice, insert before last element")
785 splice $P1, $P2, 3, 0
786 $S0 = get_array_string($P1)
787 is($S0, "123ABCDE45", "splice, append-in-middle")
791 splice $P1, $P2, 0, 2
792 $S0 = get_array_string($P1)
793 is($S0, "ABCDE345", "splice, replace at beginning")
797 splice $P1, $P2, 2, 2
798 $S0 = get_array_string($P1)
799 is($S0, "12ABCDE5", "splice, replace in middle")
803 splice $P1, $P2, 3, 2
804 $S0 = get_array_string($P1)
805 is($S0, "123ABCDE", "splice, replace at end")
815 splice $P1, $P2, 3, 2
816 $S0 = get_array_string($P1)
817 is($S0, "123ABCDE", "splice, replace with another type")
820 $P2 = new ['ResizablePMCArray']
821 splice $P1, $P2, 2, 2
822 $S0 = get_array_string($P1)
823 is($S0, "125", "splice with empty replacement")
826 $P2 = new ['ResizablePMCArray']
828 splice $P1, $P2, 2, 1
829 $S0 = get_array_string($P1)
830 is($S0, "12A45", "splice with empty replacement")
836 $P1 = new ['ResizablePMCArray']
841 $P2 = new ['ResizablePMCArray']
844 splice $P1, $P2, 1, 2
846 is($S0, "1A", "replacement via splice works")
851 $P1 = new ['ResizablePMCArray']
856 $P2 = new ['ResizablePMCArray']
859 splice $P1, $P2, 0, 2
861 is($S0, "A3", "replacement via splice works")
865 #RT #40958 - can't iterate subclass of ResizablePMCArray
866 .sub iterate_subclass_of_rpa
868 $P0 = subclass 'ResizablePMCArray', 'MyArray'
870 arr = new ['MyArray']
875 is($I0, 3, "RPA subclass has correct element count")
887 is($S1, "11,13,15,", "iterator works on RPA subclass")
891 .sub method_forms_of_unshift_etc
892 $P0 = new ['ResizablePMCArray']
896 is($I0, 2, "method forms of unshift and push add elements to an RPA")
898 is($P1, 1, "method form of shift works")
900 is($P1, "two", "method form of pop works")
904 #RT #56636 - segfault from sort if comparison is always 1
905 .sub sort_with_broken_cmp
907 array = new ['ResizablePMCArray']
915 $S0 = join ' ', array
916 is($S0, "4 5 3 2 5 1", "RPA has expected values")
918 $P0 = get_global 'cmp_func'
920 ok(1, "sort returns without crashing")
931 $P0 = new 'ResizablePMCArray'
933 $P1 = new 'ResizablePMCArray'
937 ok($I2, 'ResizablePMCArray address is not zero')
939 ok($I2, 'Two empty RPAs do not have same address')
943 is($I0, $I1, 'Adding element to RPA keeps same addr')
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!"
956 $P0 = box "Hello Parrot!"
963 array3[0] = "Goodbye Parrot!"
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')
974 # don't forget to change the test plan
980 # vim: expandtab shiftwidth=4 ft=pir: