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()
54 fpa = new ['FixedPMCArray']
57 nok($I0,'FixedPMCArray element existence')
60 ok($I0,'FixedPMCArray element existence')
66 ok($I0,'FixedPMCArray element existence')
70 .local pmc compares, cmp_fun
71 # TT #1317 doesnt work wit prederef of JIT
73 compares = new ['Integer']
75 set_global "compares", compares
76 cmp_fun = get_global "cmp_fun"
81 # this is used by test_sort
83 .param pmc cmp_fun :optional
85 compares = get_global "compares"
88 array = new ['FixedPMCArray']
96 ok(1,'call sort on FixedPMCArray')
99 test1 = new ['FixedPMCArray']
107 is_deeply( array, test1 )
111 # this is used by test_sort
117 compares = get_global "compares"
126 .local pmc test1, test2, test3
127 one = new ['Integer']
131 fpa = new ['FixedPMCArray']
134 splice fpa, one, 0, 5
135 test1 = new ['FixedPMCArray']
143 is_deeply(fpa, test1 )
146 two = new ['Integer']
149 splice fpa, two, 1, 3
150 test2 = new ['FixedPMCArray']
157 is_deeply(fpa, test2 )
160 three = new ['Integer']
163 splice fpa, three, 2, 3
164 test3 = new ['FixedPMCArray']
171 is_deeply(fpa, test3 )
175 .local pmc matrix, row
176 .local pmc elem_in_pmc
177 .local pmc elem_out_pmc
178 .local int elem_out_int
179 .local num elem_out_num
180 .local string elem_out_string
182 matrix = new ['FixedPMCArray']
184 row = new ['FixedPMCArray']
185 row = 4 # set the size by assigning an integer, number or pmc
188 matrix[0;1] = 128.128
189 elem_in_pmc = new ['Integer']
191 matrix[0;2] = elem_in_pmc
194 elem_out_int = matrix[0;0]
197 elem_out_pmc = matrix[0;0]
200 elem_out_num = matrix[0;0]
203 elem_out_string = matrix[0;0]
204 is(elem_out_string,128)
206 elem_out_pmc = matrix[0;1]
207 is(elem_out_pmc,"128.128")
209 elem_out_num = matrix[0;1]
210 is(elem_out_num,"128.128")
212 elem_out_string = matrix[0;1]
213 is(elem_out_string,"128.128")
215 elem_out_int = matrix[0;2]
218 elem_out_pmc = matrix[0;2]
221 elem_out_num = matrix[0;2]
224 elem_out_string = matrix[0;2]
225 is(elem_out_string,256)
227 elem_out_int = matrix[0;0]
230 elem_out_pmc = matrix[0;0]
233 elem_out_num = matrix[0;0]
236 elem_out_string = matrix[0;0]
237 is(elem_out_string,128)
242 .local pmc fpa1, fpa2, p1, p2
244 fpa1 = new ['FixedPMCArray']
245 fpa2 = new ['FixedPMCArray']
283 arr1 = new ['FixedPMCArray']
285 elems_i = elements arr1
294 arr1 = new ['FixedPMCArray']
296 elems_i = elements arr1
308 s = get_repr_fpa_n(0)
309 aux = get_repr_fpa_n(1)
311 aux = get_repr_fpa_n(2)
313 aux = get_repr_fpa_n(3)
315 substring(s,'()(0)(0, 1)(0, 1, 2)','get_repr')
323 fpa = new ['FixedPMCArray']
338 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, offset 0')
341 fpa = new ['FixedPMCArray']
347 splice fpa, nil, 0, 6
350 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, big offset')
353 fpa = new ['FixedPMCArray']
359 splice fpa, nil, 6, 0
364 .sub test_definedness
366 arr1 = new ['FixedPMCArray']
368 .local int defined_elem_1956
369 defined_elem_1956 = defined arr1[1956]
370 is(defined_elem_1956,0,'definedness')
372 defined_elem_1956 = defined arr1[1956]
373 is(defined_elem_1956,1,'definedness')
377 defined_elem_1956 = defined arr1[1956]
378 is(defined_elem_1956,0,'definedness')
381 .sub test_get_null_elem
385 arr1 = new ['FixedPMCArray']
389 is(i,0,'null int is 0')
391 is(s,"",'null string is empty string')
394 .sub test_get_uninitialized
395 throws_substring(<<'CODE','Null PMC access in name','get uninitialized')
398 arr1 = new ['FixedPMCArray']
401 elem_1956 = arr1[1956]
402 .local string type_1956
403 type_1956 = typeof elem_1956
411 pmc1 = new ['FixedPMCArray']
413 does bool1, pmc1, "scalar"
414 nok(bool1,'FixedPMCArray does not scalar')
415 does bool1, pmc1, "array"
416 ok(bool1,'FixedPMCArray does array')
417 does bool1, pmc1, "no_interface"
418 nok(bool1,'no interface')
420 .sub test_set_ints_access_pmc_keys
421 new $P0, ['FixedPMCArray']
434 is($I0, 125,'got int with pmc key')
438 is($N0,10.2,'got float with pmc key',0.00001)
442 is($S0, "cow", 'got string with pmc key')
447 is($I1, 123456, 'got another int with pmc key')
450 .sub test_set_pmc_keys_access_ints
451 new $P0, ['FixedPMCArray']
462 set $P0[$P1], "bleep"
465 is($I0, 25,'got integer with int lookup')
467 is($N0,2.5,'got float with int lookup',0.00001)
470 is($S0, "bleep",'got string with int lookup')
474 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
476 new $P0, ['FixedPMCArray']
481 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
483 new $P0, ['FixedPMCArray']
491 .sub test_negative_index
492 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set negative index')
494 new $P0, ['FixedPMCArray']
499 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','get negative index')
501 new $P0, ['FixedPMCArray']
509 .sub test_setting_second_elem
510 new $P0, ['FixedPMCArray']
515 is($I0,-7,'set second elem to int')
519 is($N0,3.7,'set second elem to float')
521 set $P0[1],"muwhahaha"
523 is($S0,"muwhahaha",'set second elem to string')
526 .sub test_setting_first_elem
527 new $P0, ['FixedPMCArray']
532 is($I0,-7,'set first elem to int')
536 is($N0,3.7,'set first elem to float')
538 set $P0[0],"muwhahaha"
540 is($S0,"muwhahaha",'set first elem to string')
544 new $P0, ['FixedPMCArray']
546 nok($P0,'length 0 FixedPMCArray is falsey')
548 ok($P0, 'length 1 FixedPMCArray is truthy')
552 throws_substring(<<'CODE','FixedPMCArray: Cannot set array size to a negative number','cannot create a negative length array')
554 new $P0, ['FixedPMCArray']
562 arr = new 'FixedPMCArray'
569 .local pmc sorted_arr
570 sorted_arr = new 'FixedPMCArray'
572 sorted_arr[0] = 'another'
573 sorted_arr[1] = 'hacker'
574 sorted_arr[2] = 'just'
575 sorted_arr[3] = 'perl'
577 $P0 = get_global 'cmpfn1'
580 is_deeply($P1, sorted_arr, 'fpa.sort called with normal Sub')
582 $P0 = get_global 'cmpfn2'
585 is_deeply($P1, sorted_arr, 'fpa.sort called with MultiSub')
595 .sub 'cmpfn2' :multi(_, _)
602 .sub test_resize_exception
603 throws_substring(<<'CODE',"FixedPMCArray: Can't resize",'cannot resize FixedPMCArray')
605 new $P0, ['FixedPMCArray']
612 throws_substring(<<'CODE',"set_number_native() not implemented in class 'FixedPMCArray'", 'cannot use float as length to FixedPMCArray')
614 new $P0, ['FixedPMCArray']
619 throws_substring(<<'CODE',"set_string_native() not implemented in class 'FixedPMCArray'", 'cannot use string as length to FixedPMCArray')
621 new $P0, ['FixedPMCArray']
627 .sub test_assign_non_array
628 throws_substring(<<'CODE', "Can't set self from this type",'assign from non-array')
630 .local pmc arr, other
632 arr = new ['FixedPMCArray']
633 other = new ['Integer']
639 .sub test_assign_self
641 arr = new ['FixedPMCArray']
643 ok(1, 'Can assign FixedPMCArray to itself')
646 .sub test_assign_from_another
647 .local pmc arr1, arr2
649 arr1 = new ['FixedPMCArray']
651 arr2 = new ['FixedPMCArray']
655 is(n,15,'assigning to FixedPMCArray from another FixedPMCArray')
658 .sub test_setting_array_size
659 new $P0, ['FixedPMCArray']
662 is($I0,0,'size of new FixedPMCArray is 0')
667 is($I0,1,'size of FixedPMCArray is 1')
670 .sub 'test_new_style_init'
671 $P0 = new 'FixedPMCArray', 10
674 is($I0, 10, "New style init creates the correct # of elements")
676 $P0 = new ['FixedPMCArray'], 10
679 is($I0, 10, "New style init creates the correct # of elements for a key constant")
686 # vim: expandtab shiftwidth=4 ft=pir: