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
32 * Push/Unshift, Pop/Shift
38 * Doesn't change array size
39 * Multiple concurrent iterators don't interfere
44 .include 'test_more.pir'
47 test_does_interfaces()
51 test_distinct_storage()
53 test_cant_set_negative()
54 test_cant_get_negative()
59 test_conversion_overflow()
75 .sub test_does_interfaces
76 $P0 = new ['ResizableIntegerArray']
77 ok( 1, 'Instantiated ResizableIntegerArray PMC' )
78 $I0 = does $P0, 'array'
79 ok( $I0, 'Interface does "array"' )
80 $I0 = does $P0, 'scalar'
81 is( $I0, 0, 'Interface does not do "scalar"' )
82 $I0 = does $P0, 'no_interface'
83 is( $I0, 0, 'Interface does not do "no_interface"' )
87 $P0 = new ['ResizableIntegerArray']
89 is( $I0, 0, 'Initial array size is 0' )
91 is( $I0, $I1, '... and "elements" opcode agrees' )
95 $P0 = new ['ResizableIntegerArray']
129 is( $I1, 6, 'Setting array size (four different values, including 0)' )
132 is( $I0, $I2, '... and "elements" opcode still agrees' )
140 ok( $I1, 'Setting negative size should throw an exception' )
143 .sub test_distinct_storage
144 # Walk the array in pseudo-random order
145 # Pick a sample size $I4 and another number $I2, such that
146 # ∀n: n > 0 ∧ $I2 ⁿ % $I4 = 1 ⇒ n % $I4 = 0
149 # Create and fill array in random order
150 $P0 = new ['ResizableIntegerArray']
152 # say '\n ... checking that pseudo-random sequence is exhaustive ...'
162 # Read back array and check values match
170 is( $I0, $I4, 'All array elements stored separately' )
173 .sub test_cant_set_negative
174 $P0 = new ['ResizableIntegerArray']
182 ok( $I0, 'Setting with negative index should throw an exception' )
185 .sub test_cant_get_negative
186 $P0 = new ['ResizableIntegerArray']
194 ok( $I0, 'Getting with negative index should throw an exception' )
197 .sub test_set_beyond_end
198 $P0 = new ['ResizableIntegerArray']
206 ok( $I0, 'Setting with too-big index should not throw an exception' )
209 is( $I0, 2, '... and should extend array' )
212 .sub test_get_beyond_end
213 $P0 = new ['ResizableIntegerArray']
221 ok( $I0, 'Getting with too-big index should not throw an exception' )
222 is( $I1, 0, '... and result should be 0' )
225 is( $I0, 1, '... and should not extend array' )
229 $P0 = new ['ResizableIntegerArray']
234 $P1 = new ['Integer']
244 is( $I0, -7, 'Setting element to integer' )
246 is( $N0, 3.0, 'Setting element to float (gets truncated)' )
248 is( $S0, '17', 'Setting element to string (gets converted to int and back)' )
250 is( $I0, 123456, 'Setting element to boxed integer' )
252 is( $N0, 7.0, 'Setting element to boxed float (gets truncated)' )
254 is( $S0, '987654321', 'Setting element to boxed string (gets converted to int and back)' )
257 .sub test_conversion_overflow
258 $P0 = new ['ResizableIntegerArray']
261 $S0 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
270 ok( $I1, 'Throw exception when setting element to too-large digit-string' )
274 .sub test_set_pmc_index
275 $P0 = new ['ResizableIntegerArray']
298 is( $I1, 3, 'Setting via PMC key (3 different types)' )
301 .sub test_get_pmc_index
302 $P0 = new ['ResizableIntegerArray']
333 is( $I1, 4, 'Getting via PMC key (4 different types)' )
337 $P0 = new ['ResizableIntegerArray']
341 is( $I0, 10001, 'Push increases number of elements by one' )
343 is( $I0, 12345, '... and stores correct value' )
347 $P0 = new ['ResizableIntegerArray']
353 is( $I0, 16, 'Pop retrieves correct value' )
355 is( $I0, 2, '... and reduces number of elements by one' )
359 $P0 = new ['ResizableIntegerArray']
371 is( $I0, $I1, 'Pop many times retrieves correct values' )
373 is( $I0, 0, '... and leaves array empty' )
377 $P0 = new ['ResizableIntegerArray']
384 is( $I1, 100000, 'Push many values fills array to correct size' )
391 is( $I0, $I1, '... and stores correct values')
395 $P0 = new ['ResizableIntegerArray']
438 is( $I1, 9, 'Push-then-Pop retrieves values in reverse order' )
441 .sub test_cant_pop_empty
442 $P0 = new ['ResizableIntegerArray']
449 ok( $I0, 'Pop from empty array should throw an exception' )
452 # .sub test_cant_pop_empty
453 # # test_pass( 'pop from empty array should throw exception' )
454 # throws_like( <<'CODE', 'Can\'t pop from an empty array!', 'pop from empty array should throw exception' )
456 # $P0 = new ['ResizableIntegerArray']
460 # # test_test( 'pop from empty array should throw exception' )
464 $P0 = new ['ResizableIntegerArray']
487 is( $I1, 4, 'Shift returns values in correct order' )
490 is( $I0, 0, '... and removes correct number of elements' )
494 $P0 = new ['ResizableIntegerArray']
499 is( $I0, 2, 'Unshift adds correct number of elements' )
511 is( $I1, 2, '... and stores values in correct order' )
515 $P0 = new ['ResizableIntegerArray']
548 is( $I0, 4, 'get_iter: iterator returns all values in correct sequence' )
555 # vim: expandtab shiftwidth=4 ft=pir: