2 # Copyright (C) 2001-2009, Parrot Foundation.
7 t/pmc/resizableintegerarray.t - Tests for the ResizableIntegerArray PMC
11 % prove t/pmc/resizableintegerarray.t
15 This tests the C<ResizableIntegerArray> PMC. It checks size, sets various
16 elements, including out-of-bounds test as well as INT and PMC keys.
27 * Type of value (int, num, string, pmc)
28 * Type of index (int, pmc)
29 * index negative/in-range/beyond-end
30 * Set doesn't clobber other elements
33 * Push/Unshift, Pop/Shift
39 * Doesn't change array size
40 * Multiple concurrent iterators don't interfere
45 .include 'test_more.pir'
48 test_does_interfaces()
52 test_distinct_storage()
54 test_cant_set_negative()
55 test_cant_get_negative()
61 test_conversion_overflow()
74 test_cant_shift_empty()
80 .sub test_does_interfaces
81 $P0 = new ['ResizableIntegerArray']
82 ok( 1, 'Instantiated ResizableIntegerArray PMC' )
83 $I0 = does $P0, 'array'
84 ok( $I0, 'Interface does "array"' )
85 $I0 = does $P0, 'scalar'
86 is( $I0, 0, 'Interface does not do "scalar"' )
87 $I0 = does $P0, 'no_interface'
88 is( $I0, 0, 'Interface does not do "no_interface"' )
92 $P0 = new ['ResizableIntegerArray']
94 is( $I0, 0, 'Initial array size is 0' )
96 is( $I0, $I1, '... and "elements" opcode agrees' )
100 $P0 = new ['ResizableIntegerArray']
134 is( $I1, 6, 'Setting array size (four different values, including 0)' )
137 is( $I0, $I2, '... and "elements" opcode still agrees' )
145 ok( $I1, 'Setting negative size should throw an exception' )
148 .sub test_distinct_storage
149 # Walk the array in pseudo-random order
150 # Pick a sample size $I4 and another number $I2, such that
151 ############################################################
152 ##### Plase rewrite this with ascii chars, it got unreadable
153 ##### by editing with mixed charsets.
154 # n: n > 0 $I2 % $I4 = 1 n % $I4 = 0
155 ############################################################
158 # Create and fill array in random order
159 $P0 = new ['ResizableIntegerArray']
161 # say '\n ... checking that pseudo-random sequence is exhaustive ...'
171 # Read back array and check values match
179 is( $I0, $I4, 'All array elements stored separately' )
182 .sub test_cant_set_negative
183 $P0 = new ['ResizableIntegerArray']
191 ok( $I0, 'Setting with negative index should throw an exception' )
194 .sub test_cant_get_negative
195 $P0 = new ['ResizableIntegerArray']
203 ok( $I0, 'Getting with negative index should throw an exception' )
206 .sub test_set_beyond_end
207 $P0 = new ['ResizableIntegerArray']
215 ok( $I0, 'Setting with too-big index should not throw an exception' )
218 is( $I0, 2, '... and should extend array' )
221 .sub test_get_beyond_end
222 $P0 = new ['ResizableIntegerArray']
230 ok( $I0, 'Getting with too-big index should not throw an exception' )
231 is( $I1, 0, '... and result should be 0' )
234 is( $I0, 1, '... and should not extend array' )
238 $P0 = new ['ResizableIntegerArray'], 3
244 is( $I0, 2, 'delete one element dec size')
246 is( $I0, 7, 'deleted move back the remaining part')
250 ok(0, 'delete ouf of bound should throw')
254 ok(1, 'delete ouf of bound throws')
259 $P0 = new ['ResizableIntegerArray']
264 $P1 = new ['Integer']
274 is( $I0, -7, 'Setting element to integer' )
276 is( $N0, 3.0, 'Setting element to float (gets truncated)' )
278 is( $S0, '17', 'Setting element to string (gets converted to int and back)' )
280 is( $I0, 123456, 'Setting element to boxed integer' )
282 is( $N0, 7.0, 'Setting element to boxed float (gets truncated)' )
284 is( $S0, '987654321', 'Setting element to boxed string (gets converted to int and back)' )
287 .sub test_conversion_overflow
288 $P0 = new ['ResizableIntegerArray']
291 $S0 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
300 ok( $I1, 'Throw exception when setting element to too-large digit-string' )
304 .sub test_set_pmc_index
305 $P0 = new ['ResizableIntegerArray']
328 is( $I1, 3, 'Setting via PMC key (3 different types)' )
331 .sub test_get_pmc_index
332 $P0 = new ['ResizableIntegerArray']
363 is( $I1, 4, 'Getting via PMC key (4 different types)' )
367 $P0 = new ['ResizableIntegerArray']
371 is( $I0, 10001, 'Push increases number of elements by one' )
373 is( $I0, 12345, '... and stores correct value' )
377 $P0 = new ['ResizableIntegerArray']
383 is( $I0, 16, 'Pop retrieves correct value' )
385 is( $I0, 2, '... and reduces number of elements by one' )
389 $P0 = new ['ResizableIntegerArray']
401 is( $I0, $I1, 'Pop many times retrieves correct values' )
403 is( $I0, 0, '... and leaves array empty' )
407 $P0 = new ['ResizableIntegerArray']
414 is( $I1, 100000, 'Push many values fills array to correct size' )
421 is( $I0, $I1, '... and stores correct values')
425 $P0 = new ['ResizableIntegerArray']
468 is( $I1, 9, 'Push-then-Pop retrieves values in reverse order' )
471 .sub test_cant_pop_empty
472 $P0 = new ['ResizableIntegerArray']
479 ok( $I0, 'Pop from empty array should throw an exception' )
482 # .sub test_cant_pop_empty
483 # # test_pass( 'pop from empty array should throw exception' )
484 # throws_like( <<'CODE', 'Can\'t pop from an empty array!', 'pop from empty array should throw exception' )
486 # $P0 = new ['ResizableIntegerArray']
490 # # test_test( 'pop from empty array should throw exception' )
494 $P0 = new ['ResizableIntegerArray']
517 is( $I1, 4, 'Shift returns values in correct order' )
520 is( $I0, 0, '... and removes correct number of elements' )
524 $P0 = new ['ResizableIntegerArray']
529 is( $I0, 2, 'Unshift adds correct number of elements' )
541 is( $I1, 2, '... and stores values in correct order' )
544 .sub test_cant_shift_empty
545 $P0 = new ['ResizableIntegerArray']
552 ok( $I0, 'Shift from empty array should throw an exception' )
556 $P0 = new ['ResizableIntegerArray']
589 is( $I0, 4, 'get_iter: iterator returns all values in correct sequence' )
593 $P0 = new ['ResizableIntegerArray']
597 is( $I0, 1, 'cloned is equal to original')
603 ria = new ['ResizableIntegerArray']
608 is( ria, th, 'freeze/thaw copy is equal to original' )
615 # vim: expandtab shiftwidth=4 ft=pir: