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()
60 p = new ['ResizablePMCArray']
64 ok(is_ok, "resize test (0)")
69 ok(is_ok, "resize test (1)")
74 ok(is_ok, "resize test (5)")
79 ok(is_ok, "resize test (9)")
84 ok(is_ok, "resize test (7)")
88 .sub negative_array_size
91 p = new ['ResizablePMCArray']
95 ok(0, "exception not caught")
98 ok(1, "exception caught")
109 p = new ['ResizablePMCArray']
115 ok(is_ok, "INTVAL assignment to first element")
120 ok(is_ok, "FLOATVAL assignment to first element")
124 is_ok = s == "muwhahaha"
125 ok(is_ok, "STRING assignment to first element")
130 ok(is_ok, "INTVAL assignment to second element")
135 ok(is_ok, "FLOATVAL assignment to second element")
139 is_ok = s == "muwhahaha"
140 ok(is_ok, "STRING assignment to second element")
145 ok(is_ok, "INTVAL assignment to last element")
150 ok(is_ok, "FLOATVAL assignment to last element")
154 is_ok = s == "muwhahaha"
155 ok(is_ok, "STRING assignment to last element")
162 rpa = new ['ResizablePMCArray']
172 ok(0, "unwanted ex thrown for out-of-bounds index")
175 ok(1, "no ex thrown for out-of-bounds index")
184 ok(1, "ex thrown for negative index")
187 ok(0, "no ex thrown for negative index")
196 ok(0, "unwanted ex thrown for out-of-bounds index")
199 ok(1, "no ex thrown for out-of-bounds index")
208 ok(1, "ex thrown for negative index")
211 ok(0, "no ex thrown for negative index")
216 .sub set_keyed_get_keyed_tests
218 new $P0, ['ResizablePMCArray']
228 set $P0[$P1], "bleep"
236 is($I0, 25, "set int via Key PMC, get int via int")
239 .fp_eq($N0, 2.5, OK1)
240 ok(0, "set num via Key PMC, get num via int fails")
243 ok(1, "set num via Key PMC, get num via int fails")
247 is($S0, "bleep", "set string via Key PMC, get string via int")
252 is($S0, "Bloop", "set PMC via Key PMC, get PMC via PMC")
255 new $P0, ['ResizablePMCArray']
268 is($I0, 125, "set int via int, get int via Key PMC")
272 .fp_eq($N0, 10.2, OK2)
273 ok(0, "set num via int, get num via Key PMC")
276 ok(1, "set num via int, get num via Key PMC")
281 is($S0, "cow", "set string via int, get string via Key PMC")
286 is($I1, 123456, "set int via int, get int via Key PMC")
293 p = new ['ResizablePMCArray']
296 is(b, 0 ,"ResizablePMCArray doesn't do scalar")
298 is(b, 1, "ResizablePMCArray does array")
299 does b, p, "no_interface"
300 is(b, 0, "ResizablePMCArray doesn't do no_interface")
304 .sub inherited_sort_method
306 ar = new ['ResizablePMCArray']
330 is(sorted, "1 2 5 9 10 ", "inherited sort method works")
335 .local pmc subrpa, arr
336 subrpa = subclass ['ResizablePMCArray'], 'ssRPA'
341 # Use a comparator that gives a reverse alphabetical order
342 # to make sure sort is using it, and not some default from
344 .local pmc comparator
345 comparator = get_global 'compare_reverse'
346 arr.'sort'(comparator)
352 is(s, 'ssRPA:z-p-a', "sort works in a pir subclass, TT #218")
364 .local pmc pmc_arr, pmc_9999, pmc_10000
365 pmc_arr = new ['ResizablePMCArray']
366 pmc_9999 = new ['Float']
367 pmc_9999 = 10000.10000
368 pmc_10000 = new ['Float']
370 pmc_arr[9999] = pmc_9999
371 push pmc_arr, pmc_10000
374 is(elements, 10001, "element count is correct")
376 last = pmc_arr[10000]
377 is(last, 123.123, "last element has correct value")
382 .local pmc pmc_arr, pmc_9999
384 pmc_arr = new ['ResizablePMCArray']
385 pmc_9999 = new ['Float']
386 pmc_9999 = 10000.10000
388 pmc_arr[9999] = pmc_9999
389 push pmc_arr, int_10000
392 is(elements, 10001, "element count is correct")
394 last = pmc_arr[10000]
395 is(last, 123, "last element has correct value")
400 .local pmc pmc_arr, pmc_9999
401 .local string string_10000
402 pmc_arr = new ['ResizablePMCArray']
403 pmc_9999 = new ['Float']
404 pmc_9999 = 10000.10000
405 string_10000 = '123asdf'
406 pmc_arr[9999] = pmc_9999
407 push pmc_arr, string_10000
410 is(elements, 10001, "element count is correct")
412 last = pmc_arr[10000]
413 is(last, "123asdf", "last element has correct value")
418 .local pmc pmc_arr, elem
419 pmc_arr = new ['ResizablePMCArray']
429 is(elements, 5, "element count is correct")
432 is(elem, 4, "correct element unshifted")
434 is(elements, 4, "correct element count after unshifing")
437 is(elem, 3, "correct element unshifted")
439 is(elements, 3, "correct element count after unshifing")
442 is(elem, 2, "correct element unshifted")
444 is(elements, 2, "correct element count after unshifing")
447 is(elem, 1, "correct element unshifted")
449 is(elements, 1, "correct element count after unshifing")
452 is(elem, 0, "correct element unshifted")
454 is(elements, 0, "correct element count after unshifing")
459 new $P0, ['ResizablePMCArray']
470 is($I0, 3, "element count is correct")
472 is($P3, 3, "element 0 has correct value")
474 is($P3, 2, "element 1 has correct value")
476 is($P3, 1, "element 2 has correct value")
481 new $P0, ['ResizablePMCArray']
482 $P1 = inspect $P0, 'mro'
483 ok(1, "get_mro didn't explode")
495 is($S1, "ResizablePMCArray,FixedPMCArray,", "ResizablePMCArrays have the right MRO")
501 .local int i, i_elem, elements
502 .local pmc p, p_elem, pmc_arr
503 .local string s, s_elem
511 pmc_arr = new ['ResizablePMCArray']
514 is(elements, 0, "element count of empty ResizablePMCArray is 0")
522 is(elements, 4, "element count after several push operations is correct")
525 is(f_elem, 123.123000, "shifted float is correct")
528 is(i_elem, 123, "shifted int is correct")
531 is(p_elem, 456.456, "shifted PMC is correct")
534 is(s_elem, "abc", "shifted string is correct")
536 is(elements, 0, "element count after several shift operations is correct")
541 .sub unshift_and_shift
543 .local int i, i_elem, elements
544 .local pmc p, p_elem, pmc_arr
545 .local string s, s_elem
553 pmc_arr = new ['ResizablePMCArray']
556 is(elements, 0, "empty RPA has 0 elements")
564 is(elements, 4, "RPA has 4 elements after 4 unshifts")
566 s_elem = shift pmc_arr
567 is(s_elem, "abc", "shifted string has correct value")
569 p_elem = shift pmc_arr
570 is(p_elem, 456.456, "shifted pmc has correct value")
572 i_elem = shift pmc_arr
573 is(i_elem, 123, "shifted int has correct value")
575 f_elem = shift pmc_arr
576 is(f_elem, 123.123000, "shifted num has correct value")
578 is(elements, 0, "expectedly empty RPA has 0 elements")
581 ## an Integer Matrix, as used by befunge as a playing field
583 .local pmc matrix, row_in, row_out
584 matrix = new ['ResizablePMCArray']
585 row_in = new ['ResizableIntegerArray']
591 is(elem, 42, "int in nested ResizableIntegerArray is 42")
595 is(elem, 43, "int in nested ResizableIntegerArray is 43")
599 .sub exists_and_defined
601 array = new ['ResizablePMCArray']
613 .local int flag, index, ex, def
615 ## bounds checking: lower (0)
617 is(ex, 1, "element at idx 0 exists")
618 def = defined array[0]
619 is(def, 1, "element at idx 0 is defined")
621 ## bounds checking: upper (7)
623 is(ex, 1, "element at idx 7 exists")
624 def = defined array[7]
625 is(def, 1, "element at idx 7 is defined")
627 ## bounds checking: negative lower (-1)
628 ex = exists array[-1]
629 is(ex, 1, "element at idx -1 exists")
630 def = defined array[-1]
631 is(def, 1, "element at idx -1 is defined")
633 ## bounds checking: negative upper (-8)
634 ex = exists array[-8]
635 is(ex, 1, "element at idx -8 exists")
636 def = defined array[-8]
637 is(def, 1, "element at idx -8 is defined")
639 ## bounds checking: out-of-bounds (8)
641 is(ex, 0, "element at idx 8 does not exist")
642 def = defined array[8]
643 is(def, 0, "element at idx 8 is not defined")
645 ## bounds checking: negative out-of-bounds (-9)
646 ex = exists array[-9]
647 is(ex, 0, "element at idx -9 does not exist")
648 def = defined array[-9]
649 is(def, 0, "element at idx -9 is not defined")
653 is(ex, 0, "element at idx 3 does not exist")
654 def = defined array[3]
655 is(def, 0, "element at idx 3 is not defined")
657 ## undefined value (5)
659 is(ex, 1, "element at idx 5 does not exist")
660 def = defined array[5]
661 is(def, 0, "element at idx 5 is not defined")
667 $P1 = new ['ResizablePMCArray']
672 $P2 = new ['FixedPMCArray']
679 $P3 = new ['ResizablePMCArray']
684 $P4 = new ['ResizablePMCArray']
686 $P5 = new ['MultiSub'] # extends ResizablePMCArray
694 is( $I1, 0, 'still size 0' )
700 is( $I1, $I2, 'append empty ResizablePMCArray' )
703 is( $S1, 'c', 'indexing elements' )
706 is( $P10, 5, 'append FixedPMCArray' )
709 is( $S1, 'c', 'indexing elements' )
712 is( $S1, 'e', 'indexing elements' )
715 is( $P3, 8, 'append ResizablePMCArray' )
718 is( $S1, '-8.8', 'indexing elements' )
721 is( $S1, 'b', 'indexing elements' )
724 is( $P3, 9, 'append subclass' )
727 is( $S1, '-8.8', 'indexing elements' )
730 $I99 = isa $P99, 'Sub'
731 ok( $I99, 'indexing elements' )
735 .sub get_array_string
740 unless $P3 goto loop_end
752 ar1 = new ['ResizablePMCArray']
759 ar2 = new ['ResizablePMCArray']
768 splice $P1, $P2, 0, 5
769 $S0 = get_array_string($P1)
770 is($S0, "ABCDE", "splice with complete replace")
774 splice $P1, $P2, 5, 0
775 $S0 = get_array_string($P1)
776 is($S0, "12345ABCDE", "splice, append")
780 splice $P1, $P2, 4, 0
781 $S0 = get_array_string($P1)
782 is($S0, "1234ABCDE5", "splice, insert before last element")
786 splice $P1, $P2, 3, 0
787 $S0 = get_array_string($P1)
788 is($S0, "123ABCDE45", "splice, append-in-middle")
792 splice $P1, $P2, 0, 2
793 $S0 = get_array_string($P1)
794 is($S0, "ABCDE345", "splice, replace at beginning")
798 splice $P1, $P2, 2, 2
799 $S0 = get_array_string($P1)
800 is($S0, "12ABCDE5", "splice, replace in middle")
804 splice $P1, $P2, 3, 2
805 $S0 = get_array_string($P1)
806 is($S0, "123ABCDE", "splice, replace at end")
816 splice $P1, $P2, 3, 2
817 $S0 = get_array_string($P1)
818 is($S0, "123ABCDE", "splice, replace with another type")
821 $P2 = new ['ResizablePMCArray']
822 splice $P1, $P2, 2, 2
823 $S0 = get_array_string($P1)
824 is($S0, "125", "splice with empty replacement")
827 $P2 = new ['ResizablePMCArray']
829 splice $P1, $P2, 2, 1
830 $S0 = get_array_string($P1)
831 is($S0, "12A45", "splice with empty replacement")
837 $P1 = new ['ResizablePMCArray']
842 $P2 = new ['ResizablePMCArray']
845 splice $P1, $P2, 1, 2
847 is($S0, "1A", "replacement via splice works")
852 $P1 = new ['ResizablePMCArray']
857 $P2 = new ['ResizablePMCArray']
860 splice $P1, $P2, 0, 2
862 is($S0, "A3", "replacement via splice works")
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 .sub sort_with_broken_cmp
906 array = new ['ResizablePMCArray']
914 $S0 = join ' ', array
915 is($S0, "4 5 3 2 5 1", "RPA has expected values")
917 $P0 = get_global 'cmp_func'
919 ok(1, "sort returns without crashing")
930 $P0 = new 'ResizablePMCArray'
932 $P1 = new 'ResizablePMCArray'
936 ok($I2, 'ResizablePMCArray address is not zero')
938 ok($I2, 'Two empty RPAs do not have same address')
942 is($I0, $I1, 'Adding element to RPA keeps same addr')
945 .sub 'equality_tests'
946 .local pmc array1, array2, array3, array4
947 array1 = new ['ResizablePMCArray']
948 array2 = new ['ResizablePMCArray']
949 array3 = new ['ResizablePMCArray']
951 array1[0] = "Hello Parrot!"
955 $P0 = box "Hello Parrot!"
962 array3[0] = "Goodbye Parrot!"
966 array4 = clone array1
968 is(array1, array2, 'Physically disjoint, but equal arrays')
969 is(array1, array4, 'Clones are equal')
970 isnt(array1, array3, 'Different arrays')
975 array = new 'ResizablePMCArray'
983 .local string unsorted
984 unsorted = join ' ', array
985 is(unsorted,"4 5 3 2 5 1", "unsorted array")
987 ## sort using a non-tailcall function
988 .const 'Sub' cmp_normal = 'cmp_normal_tailcall'
990 $P1.'sort'(cmp_normal)
991 .local string sorted1
992 sorted1 = join ' ', $P1
993 is (sorted1, "1 2 3 4 5 5", "sorted array, no tailcall")
995 ## sort using a tailcall function
996 .const 'Sub' cmp_tailcall = 'cmp_tailcall_tailcall'
998 $P1.'sort'(cmp_tailcall)
999 .local string sorted2
1000 sorted2 = join ' ', $P1
1001 is(sorted2, "1 2 3 4 5 5", "sorted array, with tailcall")
1004 .sub 'cmp_func_tailcall'
1011 .sub 'cmp_normal_tailcall'
1014 $P0 = 'cmp_func_tailcall'(a, b)
1018 .sub 'cmp_tailcall_tailcall'
1021 .tailcall 'cmp_func_tailcall'(a, b)
1023 # don't forget to change the test plan
1029 # vim: expandtab shiftwidth=4 ft=pir: