2 # Copyright (C) 2001-2010, Parrot Foundation.
7 t/pmc/fixedpmcarray.t - FixedPMCArray PMC
11 % prove t/pmc/fixedpmcarray.t
15 Tests C<FixedPMCArray> PMC. Checks size, sets various elements, including
16 out-of-bounds test. Checks INT and PMC keys.
21 .include 'test_more.pir'
23 test_setting_array_size()
24 test_assign_from_another()
26 test_assign_non_array()
27 test_resize_exception()
31 test_setting_first_elem()
32 test_setting_second_elem()
35 test_set_pmc_keys_access_ints()
36 test_set_ints_access_pmc_keys()
38 test_get_uninitialized()
50 test_invalid_init_tt1509()
55 fpa = new ['FixedPMCArray']
58 nok($I0,'FixedPMCArray element existence')
61 ok($I0,'FixedPMCArray element existence')
67 ok($I0,'FixedPMCArray element existence')
71 .local pmc compares, cmp_fun
72 # TT #1317 doesnt work wit prederef of JIT
74 compares = new ['Integer']
76 set_global "compares", compares
77 cmp_fun = get_global "cmp_fun"
82 # this is used by test_sort
84 .param pmc cmp_fun :optional
86 compares = get_global "compares"
89 array = new ['FixedPMCArray']
97 ok(1,'call sort on FixedPMCArray')
100 test1 = new ['FixedPMCArray']
108 is_deeply( array, test1 )
112 # this is used by test_sort
118 compares = get_global "compares"
127 .local pmc test1, test2, test3
128 one = new ['Integer']
132 fpa = new ['FixedPMCArray']
135 splice fpa, one, 0, 5
136 test1 = new ['FixedPMCArray']
144 is_deeply(fpa, test1 )
147 two = new ['Integer']
150 splice fpa, two, 1, 3
151 test2 = new ['FixedPMCArray']
158 is_deeply(fpa, test2 )
161 three = new ['Integer']
164 splice fpa, three, 2, 3
165 test3 = new ['FixedPMCArray']
172 is_deeply(fpa, test3 )
176 .local pmc matrix, row
177 .local pmc elem_in_pmc
178 .local pmc elem_out_pmc
179 .local int elem_out_int
180 .local num elem_out_num
181 .local string elem_out_string
183 matrix = new ['FixedPMCArray']
185 row = new ['FixedPMCArray']
186 row = 4 # set the size by assigning an integer, number or pmc
189 matrix[0;1] = 128.128
190 elem_in_pmc = new ['Integer']
192 matrix[0;2] = elem_in_pmc
195 elem_out_int = matrix[0;0]
198 elem_out_pmc = matrix[0;0]
201 elem_out_num = matrix[0;0]
204 elem_out_string = matrix[0;0]
205 is(elem_out_string,128)
207 elem_out_pmc = matrix[0;1]
208 is(elem_out_pmc,"128.128")
210 elem_out_num = matrix[0;1]
211 is(elem_out_num,"128.128")
213 elem_out_string = matrix[0;1]
214 is(elem_out_string,"128.128")
216 elem_out_int = matrix[0;2]
219 elem_out_pmc = matrix[0;2]
222 elem_out_num = matrix[0;2]
225 elem_out_string = matrix[0;2]
226 is(elem_out_string,256)
228 elem_out_int = matrix[0;0]
231 elem_out_pmc = matrix[0;0]
234 elem_out_num = matrix[0;0]
237 elem_out_string = matrix[0;0]
238 is(elem_out_string,128)
243 .local pmc fpa1, fpa2, p1, p2
245 fpa1 = new ['FixedPMCArray']
246 fpa2 = new ['FixedPMCArray']
284 arr1 = new ['FixedPMCArray']
286 elems_i = elements arr1
295 arr1 = new ['FixedPMCArray']
297 elems_i = elements arr1
309 s = get_repr_fpa_n(0)
310 aux = get_repr_fpa_n(1)
312 aux = get_repr_fpa_n(2)
314 aux = get_repr_fpa_n(3)
316 substring(s,'()(0)(0, 1)(0, 1, 2)','get_repr')
324 fpa = new ['FixedPMCArray']
339 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, offset 0')
342 fpa = new ['FixedPMCArray']
348 splice fpa, nil, 0, 6
351 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, big offset')
354 fpa = new ['FixedPMCArray']
360 splice fpa, nil, 6, 0
365 .sub test_definedness
367 arr1 = new ['FixedPMCArray']
369 .local int defined_elem_1956
370 defined_elem_1956 = defined arr1[1956]
371 is(defined_elem_1956,0,'definedness')
373 defined_elem_1956 = defined arr1[1956]
374 is(defined_elem_1956,1,'definedness')
378 defined_elem_1956 = defined arr1[1956]
379 is(defined_elem_1956,0,'definedness')
382 .sub test_get_null_elem
386 arr1 = new ['FixedPMCArray']
390 is(i,0,'null int is 0')
392 is(s,"",'null string is empty string')
395 .sub test_get_uninitialized
396 throws_substring(<<'CODE','Null PMC access in name','get uninitialized')
399 arr1 = new ['FixedPMCArray']
402 elem_1956 = arr1[1956]
403 .local string type_1956
404 type_1956 = typeof elem_1956
412 pmc1 = new ['FixedPMCArray']
414 does bool1, pmc1, "scalar"
415 nok(bool1,'FixedPMCArray does not scalar')
416 does bool1, pmc1, "array"
417 ok(bool1,'FixedPMCArray does array')
418 does bool1, pmc1, "no_interface"
419 nok(bool1,'no interface')
421 .sub test_set_ints_access_pmc_keys
422 new $P0, ['FixedPMCArray']
435 is($I0, 125,'got int with pmc key')
439 is($N0,10.2,'got float with pmc key',0.00001)
443 is($S0, "cow", 'got string with pmc key')
448 is($I1, 123456, 'got another int with pmc key')
451 .sub test_set_pmc_keys_access_ints
452 new $P0, ['FixedPMCArray']
463 set $P0[$P1], "bleep"
466 is($I0, 25,'got integer with int lookup')
468 is($N0,2.5,'got float with int lookup',0.00001)
471 is($S0, "bleep",'got string with int lookup')
475 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
477 new $P0, ['FixedPMCArray']
482 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
484 new $P0, ['FixedPMCArray']
492 .sub test_negative_index
493 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set negative index')
495 new $P0, ['FixedPMCArray']
500 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','get negative index')
502 new $P0, ['FixedPMCArray']
510 .sub test_setting_second_elem
511 new $P0, ['FixedPMCArray']
516 is($I0,-7,'set second elem to int')
520 is($N0,3.7,'set second elem to float')
522 set $P0[1],"muwhahaha"
524 is($S0,"muwhahaha",'set second elem to string')
527 .sub test_setting_first_elem
528 new $P0, ['FixedPMCArray']
533 is($I0,-7,'set first elem to int')
537 is($N0,3.7,'set first elem to float')
539 set $P0[0],"muwhahaha"
541 is($S0,"muwhahaha",'set first elem to string')
545 new $P0, ['FixedPMCArray']
547 nok($P0,'length 0 FixedPMCArray is falsey')
549 ok($P0, 'length 1 FixedPMCArray is truthy')
553 throws_substring(<<'CODE','FixedPMCArray: Cannot set array size to a negative number','cannot create a negative length array')
555 new $P0, ['FixedPMCArray']
563 arr = new 'FixedPMCArray'
570 .local pmc sorted_arr
571 sorted_arr = new 'FixedPMCArray'
573 sorted_arr[0] = 'another'
574 sorted_arr[1] = 'hacker'
575 sorted_arr[2] = 'just'
576 sorted_arr[3] = 'perl'
578 $P0 = get_global 'cmpfn1'
581 is_deeply($P1, sorted_arr, 'fpa.sort called with normal Sub')
583 $P0 = get_global 'cmpfn2'
586 is_deeply($P1, sorted_arr, 'fpa.sort called with MultiSub')
596 .sub 'cmpfn2' :multi(_, _)
603 .sub test_resize_exception
604 throws_substring(<<'CODE',"FixedPMCArray: Can't resize",'cannot resize FixedPMCArray')
606 new $P0, ['FixedPMCArray']
613 throws_substring(<<'CODE',"set_number_native() not implemented in class 'FixedPMCArray'", 'cannot use float as length to FixedPMCArray')
615 new $P0, ['FixedPMCArray']
620 throws_substring(<<'CODE',"set_string_native() not implemented in class 'FixedPMCArray'", 'cannot use string as length to FixedPMCArray')
622 new $P0, ['FixedPMCArray']
628 .sub test_assign_non_array
629 throws_substring(<<'CODE', "Can't set self from this type",'assign from non-array')
631 .local pmc arr, other
633 arr = new ['FixedPMCArray']
634 other = new ['Integer']
640 .sub test_assign_self
642 arr = new ['FixedPMCArray']
644 ok(1, 'Can assign FixedPMCArray to itself')
647 .sub test_assign_from_another
648 .local pmc arr1, arr2
650 arr1 = new ['FixedPMCArray']
652 arr2 = new ['FixedPMCArray']
656 is(n,15,'assigning to FixedPMCArray from another FixedPMCArray')
659 .sub test_setting_array_size
660 new $P0, ['FixedPMCArray']
663 is($I0,0,'size of new FixedPMCArray is 0')
668 is($I0,1,'size of FixedPMCArray is 1')
671 .sub 'test_new_style_init'
672 $P0 = new 'FixedPMCArray', 10
675 is($I0, 10, "New style init creates the correct # of elements")
677 $P0 = new ['FixedPMCArray'], 10
680 is($I0, 10, "New style init creates the correct # of elements for a key constant")
685 is($P2, $P1, 'New style init creates the array')
688 .sub test_invalid_init_tt1509
689 throws_substring(<<'CODE', 'Cannot set array size to a negative number (-10)', 'New style init does not dump core for negative array lengths')
691 $P0 = new ['FixedPMCArray'], -10
695 throws_substring(<<'CODE', 'Cannot set array size to a negative number (-10)', 'New style init (key constant) does not dump core for negative array lengths')
697 $P0 = new 'FixedPMCArray', -10
706 # vim: expandtab shiftwidth=4 ft=pir: