2 # Copyright (C) 2001-2008, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
10 use Parrot::Test tests => 21;
14 t/pmc/iterator.t - Test the Iterator PMC
18 % prove t/pmc/iterator.t
22 Tests the C<Iterator> PMC.
26 # TT #1478: Split this test into aggregate specific one.
28 pasm_output_is( <<'CODE', <<'OUTPUT', "new iter" );
29 new P2, ['ResizablePMCArray']
37 pasm_output_is( <<'CODE', <<'OUTPUT', "int test" );
38 .include "iterator.pasm"
39 new P0, ['ResizablePMCArray'] # empty array
40 new P2, ['ResizablePMCArray'] # array with 2 elements
47 eq I0, I1, ok2 # iter.length() == array.length()
51 set P1, .ITERATE_FROM_START
53 unless P1, ok4 # if(iter) == false on empty
57 set P1, .ITERATE_FROM_START
58 if P1, ok5 # if(iter) == true on non empty
62 # while (P1) { element = shift(P1) }
73 unless P1, ok8 # if(iter) == false after last
77 # now iterate from end
78 set P1, .ITERATE_FROM_END
79 if P1, ok9 # if(iter) == true on non empty
82 # while (P1) { element = pop(P1) }
93 unless P1, ok12 # if(iter) == false after last
113 pasm_output_is( <<'CODE', <<'OUTPUT', "Hash iter 1" );
114 .include "iterator.pasm"
115 new P0, ['Hash'] # empty Hash
116 new P2, ['Hash'] # Hash with 2 elements
118 set P2["xy"], "value"
123 eq I0, I1, ok2 # iter.length() == hash.length()
127 set P1, .ITERATE_FROM_START
129 unless P1, ok4 # if(iter) == false on empty
133 set P1, .ITERATE_FROM_START
134 if P1, ok5 # if(iter) == true on non empty
137 # now iterate over P2
138 # while (P1) { key = shift(P1) }
140 shift S3, P1 # get hash.key
151 unless P1, ok8 # if(iter) == false after last
167 pasm_output_is( <<'CODE', <<'OUTPUT', "Hash iter 1" );
168 .include "iterator.pasm"
169 new P0, ['Hash'] # empty Hash
170 new P2, ['Hash'] # Hash with 2 elements
172 set P2["xy"], "value"
177 eq I0, I1, ok2 # iter.length() == hash.length()
181 set P1, .ITERATE_FROM_START
183 unless P1, ok4 # if(iter) == false on empty
187 set P1, .ITERATE_FROM_START
188 if P1, ok5 # if(iter) == true on non empty
191 # now iterate over P2
192 # while (P1) { key = shift(P1) }
194 shift S3, P1 # get hash.key
205 unless P1, ok8 # if(iter) == false after last
221 pasm_output_is( <<'CODE', <<'OUTPUT', "Hash iter 2" );
222 .include "iterator.pasm"
223 new P0, ['Hash'] # Hash for iteration
224 new P2, ['Hash'] # for test
242 set P1, .ITERATE_FROM_START
245 shift S3, P1 # get hash.key
246 set I0, P0[S3] # and value
261 pasm_output_is( <<'CODE', <<OUTPUT, "string iteration forward" );
262 .include "iterator.pasm"
266 set P1, .ITERATE_FROM_START
268 unless P1, iter_end # while (entries) ...
282 pasm_output_is( <<'CODE', <<OUTPUT, "string iteration backward" );
283 .include "iterator.pasm"
287 set P1, .ITERATE_FROM_END
289 unless P1, iter_end # while (entries) ...
303 pasm_output_is( <<'CODE', <<OUTPUT, "string iteration forward get ord" );
304 .include "iterator.pasm"
308 set P1, .ITERATE_FROM_START
310 unless P1, iter_end # while (entries) ...
324 pasm_output_is( <<'CODE', <<OUTPUT, "string iteration backward get ord" );
325 .include "iterator.pasm"
329 set P1, .ITERATE_FROM_END
331 unless P1, iter_end # while (entries) ...
345 pir_output_is( << 'CODE', << 'OUTPUT', "String iterator in PIR" );
347 .include "iterator.pasm"
350 string_1 = new ['String']
351 string_1 = "abcd\x65\x66\x67"
356 iter_1 = iter string_1
357 iter_1 = .ITERATE_FROM_START
359 .local int code_point_1
361 unless iter_1 goto ITER_END
362 shift code_point_1, iter_1
367 print "reached end\n"
382 pir_output_is( <<'CODE', <<'OUTPUT', "Index access for Iterator on String" );
384 .include "iterator.pasm"
387 string_1 = new ['String']
388 string_1 = "abcd\x65\x66\x67"
394 iter_1 = iter string_1
395 iter_1 = .ITERATE_FROM_START
397 .local int code_point_1
398 .local num code_point_2
399 .local string code_point_3
400 .local pmc code_point_4
402 print 'Iterator shift_integer: '
403 shift code_point_1, iter_1
407 print 'Iterator get_integer_keyed_int 2: '
408 code_point_1 = iter_1[2]
412 print 'Iterator get_integer_keyed_int 0: '
413 code_point_1 = iter_1[0]
417 print 'Iterator get_integer_keyed_int -1: '
418 code_point_1 = iter_1[-1]
426 Iterator shift_integer: 97
427 Iterator get_integer_keyed_int 2: 100
428 Iterator get_integer_keyed_int 0: 98
429 Iterator get_integer_keyed_int -1: 97
432 pir_output_is( << 'CODE', << 'OUTPUT', "Index access for Iterator on ResizablePMCArray" );
434 .include "iterator.pasm"
437 array_1 = new ['ResizablePMCArray']
447 print 'ResizablePMCArray get_string: '
452 iter_1 = iter array_1
453 iter_1 = .ITERATE_FROM_START
457 print 'Iterator shift_string: '
462 print 'Iterator get_string_keyed_int 2: '
467 print 'Iterator get_string_keyed_int -1: '
472 print 'Iterator get_string_keyed_int 0: '
477 print 'Iterator get_pmc_keyed_int 3: '
485 print 'Iterator exists_keyed_int 3: '
486 flag = exists iter_1[3]
490 print 'Iterator exists_keyed_int 28: '
491 flag = exists iter_1[28]
496 print 'Iterator defined_keyed_int 3: '
497 flag = defined iter_1[3]
501 print 'Iterator defined_keyed_int -1278: '
502 flag = defined iter_1[-1278]
507 iter_2 = iter array_1
508 iter_2 = .ITERATE_FROM_END
510 print 'Iterator shift_float: '
516 print 'Iterator get_integer: '
525 ResizablePMCArray get_string: 8
526 Iterator shift_string: a
527 Iterator get_string_keyed_int 2: d
528 Iterator get_string_keyed_int -1: a
529 Iterator get_string_keyed_int 0: b
530 Iterator get_pmc_keyed_int 3: e
531 Iterator exists_keyed_int 3: 1
532 Iterator exists_keyed_int 28: 0
533 Iterator defined_keyed_int 3: 1
534 Iterator defined_keyed_int -1278: 0
535 Iterator shift_float: -8.8
536 Iterator get_integer: 7
540 pasm_output_is( <<'CODE', <<'OUTPUT', "shift + index access", todo => "N/Y: length of rest of array ");
541 .include "iterator.pasm"
543 new P2, ['ResizablePMCArray'] # array with 4 elements
549 set P1, .ITERATE_FROM_START
551 set I0, P1 # arr.length
557 shift I0, P1 # get one
562 shift I0, P1 # get one
567 set I0, P1 # arr.length of rest
586 pasm_output_is( <<'CODE', <<'OUTPUT', "iter vtable" );
587 .include "iterator.pasm"
588 new P0, ['ResizablePMCArray']
621 pasm_output_is( <<'CODE', <<OUTPUT, "string iteration with get_iter" );
622 .include "iterator.pasm"
627 unless P1, iter_end # while (entries) ...
642 local $TODO = "adding keys during iteration";
644 pir_output_is( << 'CODE', << 'OUTPUT', "adding keys during iteration" );
660 ar = new ['ResizablePMCArray']
667 if i == 29 goto no_add
685 # or some such output
687 29_28_27_26_25_24_23_22_21_20_9_8_7_6_5_4_3_2_1_0_
691 pir_output_is( << 'CODE', << 'OUTPUT', "by default, iterate from start" );
694 ar= new ['ResizablePMCArray']
713 pir_output_is( << 'CODE', << 'OUTPUT', "iterator can be cloned" );
715 .local pmc ar, i1, i2
716 ar = new ['ResizableIntegerArray']
724 pir_output_is( << 'CODE', << 'OUTPUT', "cloned iterator independent of original" );
726 .local pmc ar, i1, i2
727 ar = new ['ResizableIntegerArray']
737 unless temp == 17 goto fail
740 unless temp == 42 goto fail
743 unless temp == 17 goto fail
746 unless temp == 42 goto fail
757 pir_output_is( <<'CODE', <<'OUTPUT', "clone of partly-advanced iterator" );
759 .local pmc ar, i1, i2
761 ar = new ['ResizableIntegerArray']
767 unless temp == 1 goto fail
771 unless temp == 2 goto fail
774 unless temp == 2 goto fail
786 local $TODO = "cloned iterator doesn't copy the array to which it 'points'";
788 <<'CODE', <<'OUTPUT', "cloned iterator doesn't copy the array to which it 'points'" );
790 .local pmc ar, i1, i2
792 temp = new ['Integer']
793 ar = new ['ResizableIntegerArray']
797 # i1 and i2 now "point" to the same element of the same array.
800 # Modify the array ...
804 # Now read back the modified value ...
807 unless temp == 17 goto fail
821 # cperl-indent-level: 4
824 # vim: expandtab shiftwidth=4: