2 # Copyright (C) 2001-2007, Parrot Foundation.
7 t/pmc/resizablebooleanarray.t - testing the ResizableBooleanArray PMC
11 % prove t/pmc/resizablebooleanarray.t
15 Tests C<ResizableBooleanArray> PMC. Checks size, sets various elements, including
16 out-of-bounds test. Checks INT and PMC keys.
20 .include 'except_types.pasm'
21 .include 'fp_equality.pasm'
25 .include 'test_more.pir'
30 setting_first_element()
31 setting_second_element()
32 setting_negatively_indexed_element()
33 getting_negatively_indexed_element()
36 set_via_pmc_keys_access_via_ints()
37 set_via_int_access_via_key_pmc()
47 check_for_zeroedness()
51 alternate_clone_tests()
57 .sub setting_array_size
59 new $P0, ['ResizableBooleanArray']
61 is($P0, 0, "new ResizableBooleanArray is empty")
64 is($P0, 1, "int assignment to RBA works")
67 is($P0, 5, "another int assignment to RBA works")
70 is($P0, 50, "yet another int assignment to RBA works")
73 is($P0, 7, "shrinking via int assignment to RBA works")
75 new $P1, ['ExceptionHandler']
77 $P1.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
80 ok(0, "no exception caught for setting negative size")
83 ok(1, "caught exception on setting negative size")
87 .sub setting_first_element
89 new $P0, ['ResizableBooleanArray']
94 is($I0, 1, "negative int -> boolean conversion is ok")
98 is($N0, 1.0, "float -> boolean conversion is ok")
102 is($I0, 1, "positive int -> boolean conversion is ok")
106 .sub setting_second_element
108 new $P0, ['ResizableBooleanArray']
113 is($I0, 1, "negative int -> boolean conversion is ok")
117 is($N0, 1.0, "float -> boolean conversion is ok")
121 is($I0, 1, "positive int -> boolean conversion is ok")
126 .sub setting_negatively_indexed_element
127 new $P0, ['ResizableBooleanArray']
128 new $P1, ['ExceptionHandler']
131 $P1.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
137 ok(0, "no exception caught for negative index access")
141 ok(1, "caught exception on negative index access")
146 .sub getting_negatively_indexed_element
147 new $P0, ['ResizableBooleanArray']
151 is($I0, 0, "negative index retrieval is 0")
153 new $P1, ['ExceptionHandler']
155 $P1.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
158 ok(0, "no exception caught for negative index out of range access")
162 ok(1, "caught exception on negative index out of range access")
166 .sub setting_oob_element
167 new $P0, ['ResizableBooleanArray']
171 is($I0, 1, "negative oob assignment is fine")
175 is($N0, 1.0, "float -> boolean conversion w/ oob assignment is ok")
179 is($I0, 1, "positive int -> boolean conversion w/ oob assignment is ok")
184 .sub getting_oob_element
185 new $P0, ['ResizableBooleanArray']
189 ok(1, "setting/getting an oob element worked")
193 .sub set_via_pmc_keys_access_via_ints
194 new $P0, ['ResizableBooleanArray']
207 is($I0, 1, "key set, int get worked")
210 is($N0, 1.0, "key set, num get worked")
213 is($S0, "1", "key set, string get worked")
216 .sub set_via_int_access_via_key_pmc
217 new $P0, ['ResizableBooleanArray']
230 is($I0, 1, "int set, key get worked")
234 is($N0, 1.0, "int set, key get worked")
238 is($S0, "1", "int set key get worked")
242 is($P3, 1, "int set, key get worked")
247 pmc1 = new ['ResizableBooleanArray']
249 does bool1, pmc1, "scalar"
250 is(bool1, 0, "RBA doesn't do 'scalar'")
251 does bool1, pmc1, "array"
252 is(bool1, 1, "RBA does 'array'")
253 does bool1, pmc1, "no_interface"
254 is(bool1, 0, "RBA doesn't do 'no_interface'")
262 pmc1 = new ['ResizableBooleanArray']
266 is(elements, 10001, "element count looks good")
269 is(last, 1, "last element has the right value")
278 pmc_arr = new ['ResizableBooleanArray']
280 is(pmc_arr, 0, "new RBA doesn't have any elements")
283 is(pmc_arr, 1, "RBA with 1 element says it has 1 element")
286 is(pmc_arr, 2, "RBA with 2 elements says it has 2 elements")
289 is(i_elem, 0, "pop popped the right value")
290 is(pmc_arr, 1, "RBA has 1 element, as expected")
293 is(i_elem, 1, "pop popped the right value again")
294 is(pmc_arr, 0, "RBA is now empty, expectedly")
304 is(i_elem, 1, "pop popped the right thing again")
305 is(pmc_arr, 63, "RBA has expected size")
309 .sub pop_bounds_check
310 $P0 = new ['ResizableBooleanArray']
311 $P1 = new ['ExceptionHandler']
314 $P1.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
318 ok(0, "failed to catch an oob exception")
321 ok(1, "caught an oob exception")
326 .sub shift_and_unshift
332 pmc_arr = new ['ResizableBooleanArray']
334 # No elements are set
335 is(pmc_arr, "", "stringification looks ok")
337 # Set two of the first three elements
340 is(pmc_arr, "101", "stringification w/ 3 elems is good")
342 # Unshift a "1" element on
344 is(pmc_arr, "1101", "still ok")
346 # Unshift a "0" element on
348 is(pmc_arr, "01101", "still ok")
350 # Shift an element off
351 i_elem = shift pmc_arr
352 is(i_elem, 0, "shift shifted a 0")
353 is(pmc_arr, "1101", "stringification ok")
355 # Shift an element off
356 i_elem = shift pmc_arr
357 is(i_elem, 1, "shift shifted a 1")
358 is(pmc_arr, "101", "stringification ok")
362 is(pmc_arr, "1010000000000000000000000", "long stringification is correct")
364 # Unshift 4 elements on
369 is(pmc_arr, "10111010000000000000000000000", "longer stringification is ok")
371 # Shift 3 elements off
372 i_elem = shift pmc_arr
373 i_elem = shift pmc_arr
374 i_elem = shift pmc_arr
375 is(i_elem, 1, "shift shifted the right thing")
376 is(pmc_arr, "11010000000000000000000000", "stringification is still ok")
378 # Set same size array is currently
380 is(pmc_arr, "11010000000000000000000000", "noop size change did nothing")
384 is(pmc_arr, 101, "setting pmc_arr[100] changed size to 101")
386 # Shift off 99 elements
390 i_elem = shift pmc_arr
392 if counter > 0 goto shift_loop
394 is(i_elem, 0, "all peachy")
395 is(pmc_arr, "001", "all's well that ends well")
399 .sub shift_bounds_check
400 $P0 = new ['ResizableBooleanArray']
401 $P1 = new ['ExceptionHandler']
404 $P1.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
408 ok(0, "no OOB exception thrown")
412 ok(1, "OOB exception thrown and caught")
419 jmpstack = new 'ResizableIntegerArray'
420 new $P0, ['ResizableBooleanArray']
426 ge $I0, $I10, postBuildUp
430 add $I1, 1 # Push $P0, mod $I1++, 2
433 add $I1, 1 # Push $P0, mod $I1++, 2
436 add $I1, 1 # Push $P0, mod $I1++, 2
442 ne $I2, $I3, errFirstPop # fail if pop != mod $I0 * 3 + 2, 2
448 ne $I2, $I3, errSecondPop # fail if pop != mod $I0 * 3 + 1, 2
452 ne $I2, $I3, errBuildLen # fail if length != $I0 + 1
460 ge $I0, $I10, postCheckBuildUpLeft
464 ne $I2, $I3, errLeftGet
466 branch checkBuildUpLeft
467 postCheckBuildUpLeft:
471 ge $I0, 0, postCheckBuildUpRight
476 ne $I2, $I3, errRightGet
478 branch checkBuildUpRight
479 postCheckBuildUpRight:
483 le $I0, 0, postTearDown
487 ne $I2, $I3, errTearDown
493 ok(1, "aerobics completed successfully")
496 print "FAILED: first pop\n"
497 local_branch jmpstack, info
500 print "FAILED: second pop\n"
501 local_branch jmpstack, info
504 print "FAILED: buildup length\n"
505 local_branch jmpstack, info
508 print "FAILED: left get\n"
509 local_branch jmpstack, info
512 print "FAILED: right get\n"
513 local_branch jmpstack, info
516 print "FAILED: tear down cap\n"
517 local_branch jmpstack, info
520 ok(0, "aerobics goof:")
526 local_return jmpstack
531 new $P0, ['ResizableBooleanArray']
553 # and pushed value at $I0+1
562 ok(1, "direct access check passed")
587 new $P0, ['ResizableBooleanArray']
609 # and pushed value at $I0+1
618 ok(1, "sparse access tests ok")
620 # now repeat and fill some holes
640 ok(1, "sparse access tests still ok")
658 .sub check_for_zeroedness
659 new $P0, ['ResizableBooleanArray']
673 ok(1, "zeroedness tests passed")
676 ok(0, "zeroedness tests failed")
677 print "#Found non-zero value "
685 new $P0, ['ResizableBooleanArray']
688 # push some values at start
703 # push some values after hole
730 ok(1, "pop into sparse tests passed")
737 ok(0, "pop into sparse tests failed")
755 .local pmc rba1, rba2
757 rba1 = new ['ResizableBooleanArray']
760 is(i, 0, "clone empty passed")
764 .local pmc rba1, rba2
767 rba1 = new ['ResizableBooleanArray']
785 if i == 5000 goto ok_2
795 if i == 5000 goto ok_4
799 i = rba2[5000] #should be undefined, i.e. 0
804 i = pop rba2 #same as previous
811 is(failed, 0, "all clone tests passed")
814 .sub alternate_clone_tests
815 .local pmc rba1, rba2
818 rba1 = new ['ResizableBooleanArray']
841 if i == 5000 goto ok_2
851 if i == 5000 goto ok_4
855 i = rba2[5000] #should be undefined, i.e. 0
860 i = pop rba2 #same as previous
865 is(failed, 0, "all alternate clone tests passed")
869 $P0 = new ['ResizableBooleanArray']
876 unless $P1 goto loop_end
878 $S0 = concat $S0, $S2
881 is($S0, "101", "get_iter works")
890 # vim: expandtab shiftwidth=4 ft=pir: