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()
52 iterate_subclass_of_rpa()
53 method_forms_of_unshift_etc()
54 sort_with_broken_cmp()
58 push_to_subclasses_array()
65 p = new ['ResizablePMCArray']
69 ok(is_ok, "resize test (0)")
74 ok(is_ok, "resize test (1)")
79 ok(is_ok, "resize test (5)")
84 ok(is_ok, "resize test (9)")
89 ok(is_ok, "resize test (7)")
93 .sub negative_array_size
96 p = new ['ResizablePMCArray']
100 ok(0, "exception not caught")
103 ok(1, "exception caught")
114 p = new ['ResizablePMCArray']
120 ok(is_ok, "INTVAL assignment to first element")
125 ok(is_ok, "FLOATVAL assignment to first element")
129 is_ok = s == "muwhahaha"
130 ok(is_ok, "STRING assignment to first element")
135 ok(is_ok, "INTVAL assignment to second element")
140 ok(is_ok, "FLOATVAL assignment to second element")
144 is_ok = s == "muwhahaha"
145 ok(is_ok, "STRING assignment to second element")
150 ok(is_ok, "INTVAL assignment to last element")
155 ok(is_ok, "FLOATVAL assignment to last element")
159 is_ok = s == "muwhahaha"
160 ok(is_ok, "STRING assignment to last element")
167 rpa = new ['ResizablePMCArray']
177 ok(0, "unwanted ex thrown for out-of-bounds index")
180 ok(1, "no ex thrown for out-of-bounds index")
189 ok(1, "ex thrown for negative index")
192 ok(0, "no ex thrown for negative index")
201 ok(0, "unwanted ex thrown for out-of-bounds index")
204 ok(1, "no ex thrown for out-of-bounds index")
213 ok(1, "ex thrown for negative index")
216 ok(0, "no ex thrown for negative index")
221 .sub set_keyed_get_keyed_tests
223 new $P0, ['ResizablePMCArray']
233 set $P0[$P1], "bleep"
241 is($I0, 25, "set int via Key PMC, get int via int")
244 .fp_eq($N0, 2.5, OK1)
245 ok(0, "set num via Key PMC, get num via int fails")
248 ok(1, "set num via Key PMC, get num via int fails")
252 is($S0, "bleep", "set string via Key PMC, get string via int")
257 is($S0, "Bloop", "set PMC via Key PMC, get PMC via PMC")
260 new $P0, ['ResizablePMCArray']
273 is($I0, 125, "set int via int, get int via Key PMC")
277 .fp_eq($N0, 10.2, OK2)
278 ok(0, "set num via int, get num via Key PMC")
281 ok(1, "set num via int, get num via Key PMC")
286 is($S0, "cow", "set string via int, get string via Key PMC")
291 is($I1, 123456, "set int via int, get int via Key PMC")
298 p = new ['ResizablePMCArray']
301 is(b, 0 ,"ResizablePMCArray doesn't do scalar")
303 is(b, 1, "ResizablePMCArray does array")
304 does b, p, "no_interface"
305 is(b, 0, "ResizablePMCArray doesn't do no_interface")
309 .sub inherited_sort_method
311 ar = new ['ResizablePMCArray']
335 is(sorted, "1 2 5 9 10 ", "inherited sort method works")
340 .local pmc subrpa, arr
341 subrpa = subclass ['ResizablePMCArray'], 'ssRPA'
346 # Use a comparator that gives a reverse alphabetical order
347 # to make sure sort is using it, and not some default from
349 .local pmc comparator
350 comparator = get_global 'compare_reverse'
351 arr.'sort'(comparator)
357 is(s, 'ssRPA:z-p-a', "sort works in a pir subclass, TT #218")
369 .local pmc pmc_arr, pmc_9999, pmc_10000
370 pmc_arr = new ['ResizablePMCArray']
371 pmc_9999 = new ['Float']
372 pmc_9999 = 10000.10000
373 pmc_10000 = new ['Float']
375 pmc_arr[9999] = pmc_9999
376 push pmc_arr, pmc_10000
379 is(elements, 10001, "element count is correct")
381 last = pmc_arr[10000]
382 is(last, 123.123, "last element has correct value")
387 .local pmc pmc_arr, pmc_9999
389 pmc_arr = new ['ResizablePMCArray']
390 pmc_9999 = new ['Float']
391 pmc_9999 = 10000.10000
393 pmc_arr[9999] = pmc_9999
394 push pmc_arr, int_10000
397 is(elements, 10001, "element count is correct")
399 last = pmc_arr[10000]
400 is(last, 123, "last element has correct value")
405 .local pmc pmc_arr, pmc_9999
406 .local string string_10000
407 pmc_arr = new ['ResizablePMCArray']
408 pmc_9999 = new ['Float']
409 pmc_9999 = 10000.10000
410 string_10000 = '123asdf'
411 pmc_arr[9999] = pmc_9999
412 push pmc_arr, string_10000
415 is(elements, 10001, "element count is correct")
417 last = pmc_arr[10000]
418 is(last, "123asdf", "last element has correct value")
423 .local pmc pmc_arr, elem
424 pmc_arr = new ['ResizablePMCArray']
434 is(elements, 5, "element count is correct")
437 is(elem, 4, "correct element unshifted")
439 is(elements, 4, "correct element count after unshifing")
442 is(elem, 3, "correct element unshifted")
444 is(elements, 3, "correct element count after unshifing")
447 is(elem, 2, "correct element unshifted")
449 is(elements, 2, "correct element count after unshifing")
452 is(elem, 1, "correct element unshifted")
454 is(elements, 1, "correct element count after unshifing")
457 is(elem, 0, "correct element unshifted")
459 is(elements, 0, "correct element count after unshifing")
464 new $P0, ['ResizablePMCArray']
475 is($I0, 3, "element count is correct")
477 is($P3, 3, "element 0 has correct value")
479 is($P3, 2, "element 1 has correct value")
481 is($P3, 1, "element 2 has correct value")
486 new $P0, ['ResizablePMCArray']
487 $P1 = inspect $P0, 'mro'
488 ok(1, "get_mro didn't explode")
500 is($S1, "ResizablePMCArray,FixedPMCArray,", "ResizablePMCArrays have the right MRO")
506 .local int i, i_elem, elements
507 .local pmc p, p_elem, pmc_arr
508 .local string s, s_elem
516 pmc_arr = new ['ResizablePMCArray']
519 is(elements, 0, "element count of empty ResizablePMCArray is 0")
527 is(elements, 4, "element count after several push operations is correct")
530 is(f_elem, 123.123000, "shifted float is correct")
533 is(i_elem, 123, "shifted int is correct")
536 is(p_elem, 456.456, "shifted PMC is correct")
539 is(s_elem, "abc", "shifted string is correct")
541 is(elements, 0, "element count after several shift operations is correct")
546 .sub unshift_and_shift
548 .local int i, i_elem, elements
549 .local pmc p, p_elem, pmc_arr
550 .local string s, s_elem
558 pmc_arr = new ['ResizablePMCArray']
561 is(elements, 0, "empty RPA has 0 elements")
569 is(elements, 4, "RPA has 4 elements after 4 unshifts")
571 s_elem = shift pmc_arr
572 is(s_elem, "abc", "shifted string has correct value")
574 p_elem = shift pmc_arr
575 is(p_elem, 456.456, "shifted pmc has correct value")
577 i_elem = shift pmc_arr
578 is(i_elem, 123, "shifted int has correct value")
580 f_elem = shift pmc_arr
581 is(f_elem, 123.123000, "shifted num has correct value")
583 is(elements, 0, "expectedly empty RPA has 0 elements")
588 pmc_arr = new ['ResizablePMCArray']
595 is($I1, 0, 'shift int from empty RPA throws')
602 is($I1, 0, 'shift num from empty RPA throws')
609 is($I1, 0, 'shift string from empty RPA throws')
616 is($I1, 0, 'shift pmc from empty RPA throws')
622 pmc_arr = new ['ResizablePMCArray']
629 is($I1, 0, 'pop int from empty RPA throws')
636 is($I1, 0, 'pop num from empty RPA throws')
643 is($I1, 0, 'pop string from empty RPA throws')
650 is($I1, 0, 'pop pmc from empty RPA throws')
654 ## an Integer Matrix, as used by befunge as a playing field
656 .local pmc matrix, row_in, row_out
657 matrix = new ['ResizablePMCArray']
658 row_in = new ['ResizableIntegerArray']
664 is(elem, 42, "int in nested ResizableIntegerArray is 42")
668 is(elem, 43, "int in nested ResizableIntegerArray is 43")
672 .sub exists_and_defined
674 array = new ['ResizablePMCArray']
686 .local int flag, index, ex, def
688 ## bounds checking: lower (0)
690 is(ex, 1, "element at idx 0 exists")
691 def = defined array[0]
692 is(def, 1, "element at idx 0 is defined")
693 $P0 = new 'Integer', 0
694 ex = exists array[$P0]
695 is(ex, 1, "element at PMC idx 0 exists")
697 ## bounds checking: upper (7)
699 is(ex, 1, "element at idx 7 exists")
700 def = defined array[7]
701 is(def, 1, "element at idx 7 is defined")
703 ## bounds checking: negative lower (-1)
704 ex = exists array[-1]
705 is(ex, 1, "element at idx -1 exists")
706 def = defined array[-1]
707 is(def, 1, "element at idx -1 is defined")
709 ## bounds checking: negative upper (-8)
710 ex = exists array[-8]
711 is(ex, 1, "element at idx -8 exists")
712 def = defined array[-8]
713 is(def, 1, "element at idx -8 is defined")
715 ## bounds checking: out-of-bounds (8)
717 is(ex, 0, "element at idx 8 does not exist")
718 def = defined array[8]
719 is(def, 0, "element at idx 8 is not defined")
721 ## bounds checking: negative out-of-bounds (-9)
722 ex = exists array[-9]
723 is(ex, 0, "element at idx -9 does not exist")
724 def = defined array[-9]
725 is(def, 0, "element at idx -9 is not defined")
729 is(ex, 0, "element at idx 3 does not exist")
730 def = defined array[3]
731 is(def, 0, "element at idx 3 is not defined")
733 ## undefined value (5)
735 is(ex, 1, "element at idx 5 does not exist")
736 def = defined array[5]
737 is(def, 0, "element at idx 5 is not defined")
742 array = new ['ResizablePMCArray']
746 $P0 = new 'Integer', 1
749 is($S0, 'c', 'delete_keyed with PMC key')
754 array = new ['ResizablePMCArray']
758 is($S0, '[ a, b ]', 'get_repr')
763 $P1 = new ['ResizablePMCArray']
768 $P2 = new ['FixedPMCArray']
775 $P3 = new ['ResizablePMCArray']
780 $P4 = new ['ResizablePMCArray']
782 $P5 = new ['MultiSub'] # extends ResizablePMCArray
790 is( $I1, 0, 'still size 0' )
796 is( $I1, $I2, 'append empty ResizablePMCArray' )
799 is( $S1, 'c', 'indexing elements' )
802 is( $P10, 5, 'append FixedPMCArray' )
805 is( $S1, 'c', 'indexing elements' )
808 is( $S1, 'e', 'indexing elements' )
811 is( $P3, 8, 'append ResizablePMCArray' )
814 is( $S1, '-8.8', 'indexing elements' )
817 is( $S1, 'b', 'indexing elements' )
820 is( $P3, 9, 'append subclass' )
823 is( $S1, '-8.8', 'indexing elements' )
826 $I99 = isa $P99, 'Sub'
827 ok( $I99, 'indexing elements' )
831 .sub get_array_string
836 unless $P3 goto loop_end
848 ar1 = new ['ResizablePMCArray']
855 ar2 = new ['ResizablePMCArray']
864 splice $P1, $P2, 0, 5
865 $S0 = get_array_string($P1)
866 is($S0, "ABCDE", "splice with complete replace")
870 splice $P1, $P2, 5, 0
871 $S0 = get_array_string($P1)
872 is($S0, "12345ABCDE", "splice, append")
876 splice $P1, $P2, 4, 0
877 $S0 = get_array_string($P1)
878 is($S0, "1234ABCDE5", "splice, insert before last element")
882 splice $P1, $P2, 3, 0
883 $S0 = get_array_string($P1)
884 is($S0, "123ABCDE45", "splice, append-in-middle")
888 splice $P1, $P2, 0, 2
889 $S0 = get_array_string($P1)
890 is($S0, "ABCDE345", "splice, replace at beginning")
894 splice $P1, $P2, 2, 2
895 $S0 = get_array_string($P1)
896 is($S0, "12ABCDE5", "splice, replace in middle")
900 splice $P1, $P2, 3, 2
901 $S0 = get_array_string($P1)
902 is($S0, "123ABCDE", "splice, replace at end")
905 $P2 = new ['FixedStringArray']
912 splice $P1, $P2, 3, 2
913 $S0 = get_array_string($P1)
914 is($S0, "123ABCDE", "splice, replace with another type")
917 $P2 = new ['ResizablePMCArray']
918 splice $P1, $P2, 2, 2
919 $S0 = get_array_string($P1)
920 is($S0, "125", "splice with empty replacement")
923 $P2 = new ['ResizablePMCArray']
925 splice $P1, $P2, 2, 1
926 $S0 = get_array_string($P1)
927 is($S0, "12A45", "splice with empty replacement")
931 splice $P1, $P2, -3, 2
932 $S0 = get_array_string($P1)
933 is($S0, "12ABCDE5", "splice with negative offset")
939 splice $P1, $P2, -10, 2
946 ok($I0, "splice with negative offset too low")
951 $P1 = new ['ResizablePMCArray']
956 $P2 = new ['ResizablePMCArray']
959 splice $P1, $P2, 1, 2
961 is($S0, "1A", "replacement via splice works")
966 $P1 = new ['ResizablePMCArray']
971 $P2 = new ['ResizablePMCArray']
974 splice $P1, $P2, 0, 2
976 is($S0, "A3", "replacement via splice works")
980 .sub iterate_subclass_of_rpa
982 $P0 = subclass 'ResizablePMCArray', 'MyArray'
984 arr = new ['MyArray']
989 is($I0, 3, "RPA subclass has correct element count")
1001 is($S1, "11,13,15,", "iterator works on RPA subclass")
1005 .sub method_forms_of_unshift_etc
1006 $P0 = new ['ResizablePMCArray']
1010 is($I0, 2, "method forms of unshift and push add elements to an RPA")
1012 is($P1, 1, "method form of shift works")
1014 is($P1, "two", "method form of pop works")
1018 .sub sort_with_broken_cmp
1020 array = new ['ResizablePMCArray']
1028 $S0 = join ' ', array
1029 is($S0, "4 5 3 2 5 1", "RPA has expected values")
1031 $P0 = get_global 'cmp_func'
1033 ok(1, "sort returns without crashing")
1044 $P0 = new 'ResizablePMCArray'
1046 $P1 = new 'ResizablePMCArray'
1050 ok($I2, 'ResizablePMCArray address is not zero')
1052 ok($I2, 'Two empty RPAs do not have same address')
1056 is($I0, $I1, 'Adding element to RPA keeps same addr')
1059 .sub 'equality_tests'
1060 .local pmc array1, array2, array3, array4
1061 array1 = new ['ResizablePMCArray']
1062 array2 = new ['ResizablePMCArray']
1063 array3 = new ['ResizablePMCArray']
1065 array1[0] = "Hello Parrot!"
1069 $P0 = box "Hello Parrot!"
1076 array3[0] = "Goodbye Parrot!"
1080 array4 = clone array1
1082 is(array1, array2, 'Physically disjoint, but equal arrays')
1083 is(array1, array4, 'Clones are equal')
1084 isnt(array1, array3, 'Different arrays')
1089 array = new 'ResizablePMCArray'
1097 .local string unsorted
1098 unsorted = join ' ', array
1099 is(unsorted,"4 5 3 2 5 1", "unsorted array")
1101 ## sort using a non-tailcall function
1102 .const 'Sub' cmp_normal = 'cmp_normal_tailcall'
1104 $P1.'sort'(cmp_normal)
1105 .local string sorted1
1106 sorted1 = join ' ', $P1
1107 is (sorted1, "1 2 3 4 5 5", "sorted array, no tailcall")
1109 ## sort using a tailcall function
1110 .const 'Sub' cmp_tailcall = 'cmp_tailcall_tailcall'
1112 $P1.'sort'(cmp_tailcall)
1113 .local string sorted2
1114 sorted2 = join ' ', $P1
1115 is(sorted2, "1 2 3 4 5 5", "sorted array, with tailcall")
1118 .sub 'cmp_func_tailcall'
1125 .sub 'cmp_normal_tailcall'
1128 $P0 = 'cmp_func_tailcall'(a, b)
1132 .sub 'cmp_tailcall_tailcall'
1135 .tailcall 'cmp_func_tailcall'(a, b)
1138 # Regression test for TT#835
1139 .sub 'push_to_subclasses_array'
1140 .local pmc cl, array_one
1141 cl = subclass "ResizablePMCArray", "ExampleArray"
1142 array_one = new "ExampleArray"
1146 array_one.'push'($I0)
1150 ok(1, "Push to subclassed array works")
1153 # don't forget to change the test plan
1159 # vim: expandtab shiftwidth=4 ft=pir: