2 # Copyright (C) 2001-2009, 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()
30 test_setting_first_elem()
31 test_setting_second_elem()
34 test_set_pmc_keys_access_ints()
35 test_set_ints_access_pmc_keys()
37 test_get_uninitialized()
52 fpa = new ['FixedPMCArray']
55 nok($I0,'FixedPMCArray element existence')
58 ok($I0,'FixedPMCArray element existence')
64 ok($I0,'FixedPMCArray element existence')
68 .local pmc compares, cmp_fun
69 # TT #1317 doesnt work wit prederef of JIT
71 compares = new ['Integer']
73 set_global "compares", compares
74 cmp_fun = get_global "cmp_fun"
79 # this is used by test_sort
81 .param pmc cmp_fun :optional
83 compares = get_global "compares"
86 array = new ['FixedPMCArray']
94 ok(1,'call sort on FixedPMCArray')
97 test1 = new ['FixedPMCArray']
105 is_deeply( array, test1 )
109 # this is used by test_sort
115 compares = get_global "compares"
124 .local pmc test1, test2, test3
125 one = new ['Integer']
129 fpa = new ['FixedPMCArray']
132 splice fpa, one, 0, 5
133 test1 = new ['FixedPMCArray']
141 is_deeply(fpa, test1 )
144 two = new ['Integer']
147 splice fpa, two, 1, 3
148 test2 = new ['FixedPMCArray']
155 is_deeply(fpa, test2 )
158 three = new ['Integer']
161 splice fpa, three, 2, 3
162 test3 = new ['FixedPMCArray']
169 is_deeply(fpa, test3 )
173 .local pmc matrix, row
174 .local pmc elem_in_pmc
175 .local pmc elem_out_pmc
176 .local int elem_out_int
177 .local num elem_out_num
178 .local string elem_out_string
180 matrix = new ['FixedPMCArray']
182 row = new ['FixedPMCArray']
183 row = 4 # set the size by assigning an integer, number or pmc
186 matrix[0;1] = 128.128
187 elem_in_pmc = new ['Integer']
189 matrix[0;2] = elem_in_pmc
192 elem_out_int = matrix[0;0]
195 elem_out_pmc = matrix[0;0]
198 elem_out_num = matrix[0;0]
201 elem_out_string = matrix[0;0]
202 is(elem_out_string,128)
204 elem_out_pmc = matrix[0;1]
205 is(elem_out_pmc,"128.128")
207 elem_out_num = matrix[0;1]
208 is(elem_out_num,"128.128")
210 elem_out_string = matrix[0;1]
211 is(elem_out_string,"128.128")
213 elem_out_int = matrix[0;2]
216 elem_out_pmc = matrix[0;2]
219 elem_out_num = matrix[0;2]
222 elem_out_string = matrix[0;2]
223 is(elem_out_string,256)
225 elem_out_int = matrix[0;0]
228 elem_out_pmc = matrix[0;0]
231 elem_out_num = matrix[0;0]
234 elem_out_string = matrix[0;0]
235 is(elem_out_string,128)
240 .local pmc fpa1, fpa2, p1, p2
242 fpa1 = new ['FixedPMCArray']
243 fpa2 = new ['FixedPMCArray']
281 arr1 = new ['FixedPMCArray']
283 elems_i = elements arr1
292 arr1 = new ['FixedPMCArray']
294 elems_i = elements arr1
306 s = get_repr_fpa_n(0)
307 aux = get_repr_fpa_n(1)
309 aux = get_repr_fpa_n(2)
311 aux = get_repr_fpa_n(3)
313 substring(s,'()(0)(0, 1)(0, 1, 2)','get_repr')
321 fpa = new ['FixedPMCArray']
336 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, offset 0')
339 fpa = new ['FixedPMCArray']
345 splice fpa, nil, 0, 6
348 throws_substring(<<'CODE','FixedPMCArray: index out of bounds','splice oob, big offset')
351 fpa = new ['FixedPMCArray']
357 splice fpa, nil, 6, 0
362 .sub test_definedness
364 arr1 = new ['FixedPMCArray']
366 .local int defined_elem_1956
367 defined_elem_1956 = defined arr1[1956]
368 is(defined_elem_1956,0,'definedness')
370 defined_elem_1956 = defined arr1[1956]
371 is(defined_elem_1956,1,'definedness')
375 defined_elem_1956 = defined arr1[1956]
376 is(defined_elem_1956,0,'definedness')
379 .sub test_get_null_elem
383 arr1 = new ['FixedPMCArray']
387 is(i,0,'null int is 0')
389 is(s,"",'null string is empty string')
392 .sub test_get_uninitialized
393 throws_substring(<<'CODE','Null PMC access in name','get uninitialized')
396 arr1 = new ['FixedPMCArray']
399 elem_1956 = arr1[1956]
400 .local string type_1956
401 type_1956 = typeof elem_1956
409 pmc1 = new ['FixedPMCArray']
411 does bool1, pmc1, "scalar"
412 nok(bool1,'FixedPMCArray does not scalar')
413 does bool1, pmc1, "array"
414 ok(bool1,'FixedPMCArray does array')
415 does bool1, pmc1, "no_interface"
416 nok(bool1,'no interface')
418 .sub test_set_ints_access_pmc_keys
419 new $P0, ['FixedPMCArray']
432 is($I0, 125,'got int with pmc key')
436 is($N0,10.2,'got float with pmc key',0.00001)
440 is($S0, "cow", 'got string with pmc key')
445 is($I1, 123456, 'got another int with pmc key')
448 .sub test_set_pmc_keys_access_ints
449 new $P0, ['FixedPMCArray']
460 set $P0[$P1], "bleep"
463 is($I0, 25,'got integer with int lookup')
465 is($N0,2.5,'got float with int lookup',0.00001)
468 is($S0, "bleep",'got string with int lookup')
472 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
474 new $P0, ['FixedPMCArray']
479 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set out-of-bounds index')
481 new $P0, ['FixedPMCArray']
489 .sub test_negative_index
490 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','set negative index')
492 new $P0, ['FixedPMCArray']
497 throws_substring(<<'CODE','FixedPMCArray: index out of bounds!','get negative index')
499 new $P0, ['FixedPMCArray']
507 .sub test_setting_second_elem
508 new $P0, ['FixedPMCArray']
513 is($I0,-7,'set second elem to int')
517 is($N0,3.7,'set second elem to float')
519 set $P0[1],"muwhahaha"
521 is($S0,"muwhahaha",'set second elem to string')
524 .sub test_setting_first_elem
525 new $P0, ['FixedPMCArray']
530 is($I0,-7,'set first elem to int')
534 is($N0,3.7,'set first elem to float')
536 set $P0[0],"muwhahaha"
538 is($S0,"muwhahaha",'set first elem to string')
542 new $P0, ['FixedPMCArray']
544 nok($P0,'length 0 FixedPMCArray is falsey')
546 ok($P0, 'length 1 FixedPMCArray is truthy')
550 throws_substring(<<'CODE','FixedPMCArray: Cannot set array size to a negative number','cannot create a negative length array')
552 new $P0, ['FixedPMCArray']
558 .sub test_resize_exception
559 throws_substring(<<'CODE',"FixedPMCArray: Can't resize",'cannot resize FixedPMCArray')
561 new $P0, ['FixedPMCArray']
568 throws_substring(<<'CODE',"set_number_native() not implemented in class 'FixedPMCArray'", 'cannot use float as length to FixedPMCArray')
570 new $P0, ['FixedPMCArray']
575 throws_substring(<<'CODE',"set_string_native() not implemented in class 'FixedPMCArray'", 'cannot use string as length to FixedPMCArray')
577 new $P0, ['FixedPMCArray']
583 .sub test_assign_non_array
584 throws_substring(<<'CODE', "Can't set self from this type",'assign from non-array')
586 .local pmc arr, other
588 arr = new ['FixedPMCArray']
589 other = new ['Integer']
595 .sub test_assign_self
597 arr = new ['FixedPMCArray']
599 ok(1, 'Can assign FixedPMCArray to itself')
602 .sub test_assign_from_another
603 .local pmc arr1, arr2
605 arr1 = new ['FixedPMCArray']
607 arr2 = new ['FixedPMCArray']
611 is(n,15,'assigning to FixedPMCArray from another FixedPMCArray')
614 .sub test_setting_array_size
615 new $P0, ['FixedPMCArray']
618 is($I0,0,'size of new FixedPMCArray is 0')
623 is($I0,1,'size of FixedPMCArray is 1')
628 # cperl-indent-level: 4
631 # vim: expandtab shiftwidth=4: