2 # Copyright (C) 2001-2008, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
10 use Parrot::Test tests => 32;
11 use Parrot::Test::Util 'create_tempfile';
23 Tests the Parrot IO operations.
28 my ( $file, $content, $name ) = @_;
33 open my $FOO, '<', $file;
35 local $Test::Builder::Level = $Test::Builder::Level + 1;
36 is( <$FOO>, $content, $name );
41 my (undef, $temp_file) = create_tempfile( UNLINK => 1 );
43 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "timely destruction" );
44 .const string temp_file = '%s'
46 interpinfo $I0, 2 # GC mark runs
47 $P0 = new ['FileHandle']
48 $P0.'open'(temp_file, 'w')
52 sweep 0 # a lazy GC has to close the PIO
53 $P0 = new ['FileHandle']
54 $P0.'open'(temp_file, 'r')
62 pir_output_is( <<'CODE', <<'OUTPUT', "read on invalid fh should throw exception" );
64 new $P0, ['FileHandle']
66 push_eh _readline_handler
67 $S0 = $P0.'readline'()
82 push_eh _print_handler
83 print $P0, "kill me now\n"
98 skip( "clone not finished yet", 1 );
99 pasm_output_is( <<"CODE", <<'OUTPUT', "clone" );
100 open P0, "$temp_file", 'r'
110 (my $FOO, $temp_file) = create_tempfile( UNLINK => 1 );
112 # It would be very embarrassing if these didnt work...
116 pir_output_is( sprintf( <<'CODE', $temp_file ), <<'OUTPUT', "open and readline" );
117 .const string temp_file = '%s'
119 $P0 = new ['FileHandle']
120 $P0.'open'(temp_file)
125 $S0 = $P0.'readline'()
126 $S1 = $P0.'readline'()
136 ($FOO, $temp_file) = create_tempfile( UNLINK => 1 );
140 pir_output_is( sprintf( <<'CODE', $temp_file ), <<'OUTPUT', "open and readline, no final newline" );
141 .const string temp_file = '%s'
143 $P0 = new ['FileHandle']
144 $P0.'open'(temp_file)
149 $S0 = $P0.'readline'()
150 $S1 = $P0.'readline'()
159 ($FOO, $temp_file) = create_tempfile( UNLINK => 1 );
162 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "open & print" );
163 .const string temp_file = '%s'
171 $P1 = new ['FileHandle']
172 $P1.'open'(temp_file, 'w')
179 $P2 = new ['FileHandle']
180 $P2.'open'(temp_file)
181 $S1 = $P2.'readline'()
190 ($FOO, $temp_file) = create_tempfile( UNLINK => 1 );
193 # write to file opened for reading
194 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "3-arg open" );
195 .const string temp_file = '%s'
197 $P1 = new ['FileHandle']
198 $P1.'open'(temp_file, 'w')
199 $P1.'print'("Foobar\n")
202 push_eh _print_to_read_only
204 $P2 = new ['FileHandle']
205 $P2.'open'(temp_file, 'r')
210 say "caught writing to file opened for reading"
215 $P3 = new ['FileHandle']
216 $P3.'open'(temp_file, 'r')
217 $S1 = $P3.'readline'()
222 caught writing to file opened for reading
226 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', 'open and close' );
227 .const string temp_file = '%s'
229 $P1 = new ['FileHandle']
230 $P1.'open'(temp_file, "w")
231 $P1.'print'("Hello, World!\n")
239 file_content_is( $temp_file, <<'OUTPUT', 'file contents' );
243 pir_output_is( sprintf(<<'CODE', $temp_file), '', 'append' );
244 .const string temp_file = '%s'
246 $P1 = new ['FileHandle']
247 $P1.'open'(temp_file, 'wa')
248 $P1.'print'("Parrot flies\n")
253 file_content_is( $temp_file, <<'OUTPUT', 'append file contents' );
258 pir_output_is( sprintf(<<'CODE', $temp_file), '', 'write to file' );
259 .const string temp_file = '%s'
261 $P1 = new ['FileHandle']
262 $P1.'open'(temp_file, 'w')
263 $P1.'print'("Parrot overwrites\n")
268 file_content_is( $temp_file, <<'OUTPUT', 'file contents' );
272 pir_output_is( <<"CODE", '', "Parrot_io_flush on buffer full" );
277 new \$P0, ['FileHandle']
278 \$P0.'open'("$temp_file", 'w')
282 print \$P0, "words\\n"
291 file_content_is( $temp_file, <<'OUTPUT' x 10000, 'buffered file contents' );
295 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "turn off buffering" );
296 .const string temp_file = '%s'
299 $P0 = new ['FileHandle']
300 $P0.'open'(temp_file, 'w')
303 $P0.'buffer_type'('unbuffered')
306 $S0 = $P0.'buffer_type'()
310 print $P0, "Howdy World\n"
319 file_content_is( $temp_file, <<'OUTPUT', 'unbuffered file contents' );
323 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', 'I/O buffering' );
324 .const string temp_file = '%s'
327 .local string filename
329 $P1 = new ['FileHandle']
330 $P1.'open'(filename, 'w')
331 .local int count, max, nltest
335 if count > max goto DONE
337 $S1 = concat $S1, " "
340 nltest = mod count, 20
349 $P1 = new ['FileHandle']
353 $S1 = $P1.'readline'()
354 unless $S1 goto SUCCESS
359 if $I1 <= 1 goto LINE
362 $S3 = substr $S1, 0, 1
363 $S1 = replace $S1, 0, 1, ""
364 if $S3 == " " goto GOT_NR
365 $S2 = concat $S2, $S3
369 if $I0 != $I1 goto FAILED
386 pir_output_is( <<'CODE', <<'OUT', 'standard file descriptors' );
387 .include 'stdio.pasm'
390 $P0 = $P99.'stdhandle'(.PIO_STDIN_FILENO)
392 # I0 is 0 on Unix and non-Null on stdio and win32
395 $P1 = $P99.'stdhandle'(.PIO_STDOUT_FILENO)
401 $P2 = $P99.'stdhandle'(.PIO_STDERR_FILENO)
414 pir_output_is( <<'CODE', <<'OUTPUT', 'puts method' );
415 .include 'stdio.pasm'
418 $P2 = $P0.'stdhandle'(.PIO_STDOUT_FILENO)
423 set_args "0,0", $P2, "ok 2\n"
424 callmethodcc $P2, "puts"
431 pir_output_is( <<'CODE', <<'OUTPUT', 'puts method - PIR' );
432 .include 'stdio.pasm'
438 io = $P0.'stdhandle'(.PIO_STDOUT_FILENO)
451 pasm_output_is( <<'CODE', <<'OUTPUT', 'callmethod puts' );
452 .include 'stdio.pasm'
453 getinterp P0 # invocant
454 set I0, .PIO_STDERR_FILENO # 1st argument
455 set_args "0,0", P0, I0
456 callmethodcc P0, "stdhandle"
457 get_results "0", P2 # STDERR
459 set S0, "puts" # method
460 set S5, "ok 1\n" # 2nd param
461 set_args "0,0", P2, S5
465 set_args "0,0", P2, S5
474 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', 'seek/tell' );
475 .const string temp_file = '%s'
477 $P0 = new ['FileHandle']
479 $P0.'open'(temp_file, 'w')
480 $P0.'print'("Hello ")
482 $P0.'print'("World!")
484 $P0.'print'("Parrot!\n")
488 $P0.'open'(temp_file, 'r')
489 $S0 = $P0.'read'(65635)
497 pir_error_output_like( sprintf(<<'CODE', $temp_file), <<'OUTPUT', '32bit seek: exception' );
498 .const string temp_file = '%s'
500 $P0 = new ['FileHandle']
501 $P0.'open'(temp_file, 'w')
509 pir_error_output_like( sprintf(<<'CODE', $temp_file), <<'OUTPUT', '64bit seek: exception' );
510 .const string temp_file = '%s'
512 $P0 = new ['FileHandle']
513 $P0.'open'(temp_file, 'w')
514 $P0.'seek'(-1, -1, 0)
521 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "peek" );
522 .const string temp_file = '%s'
524 $P0 = new ['FileHandle']
525 $P0.'open'(temp_file, 'w')
526 print $P0, "a line\n"
529 $P0 = new ['FileHandle']
530 $P0.'open'(temp_file, 'r')
546 pir_output_is( sprintf(<<'CODE', $temp_file), <<'OUTPUT', "peek on an empty file" );
547 .const string temp_file = '%s'
549 $P0 = new ['FileHandle']
550 $P0.'open'(temp_file, 'w')
553 $P0 = new ['FileHandle']
554 $P0.'open'(temp_file, 'r')
565 pir_output_is( <<"CODE", <<'OUTPUT', "substr after reading from file" );
567 # Write something into a file
569 out = new ['FileHandle']
570 out.'open'("$temp_file", 'w')
571 print out, "0123456789\\n"
574 # read file contents back in
576 in = new ['FileHandle']
577 in.'open'("$temp_file", 'r')
578 .local string from_file
579 from_file = in.'read'(20)
581 # Extract part of the read in file
582 .local string head_from_file
583 head_from_file = substr from_file, 0, 5
592 pir_output_is( <<"CODE", <<'OUTPUT', "multiple substr after reading from file" );
595 # Write something into a file
597 out = new ['FileHandle']
598 out.'open'("$temp_file", 'w')
599 print out, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\\n"
604 in = new ['FileHandle']
605 in.'open'('$temp_file', 'r')
606 line = in.'read'(50000)
615 substr sub_1, line, 0, 3
616 substr sub_2, line, 3, 3
617 line = replace line, 0, 6, ''
627 line: 6789ABCDEFGHIJKLMNOPQRSTUVWXYZ
632 ($FOO, $temp_file) = create_tempfile( UNLINK => 1 );
634 print $FOO "T\xc3\xb6tsch\n";
637 pir_output_is( sprintf(<<'CODE', $temp_file), <<"OUTPUT", "utf8 read enabled, read parts" );
638 .const string temp_file = '%s'
641 pio = new ['FileHandle']
642 pio.'open'(temp_file, 'r')
643 pio.'encoding'("utf8")
645 $S1 = pio.'read'(1024) # read the rest of the file (much shorter than 1K)
650 $S2 = encodingname $I1
653 $I1 = find_encoding 'iso-8859-1'
654 trans_encoding $S1, $S0, $I1
662 pir_output_is( <<"CODE", <<"OUTPUT", "PIO.readall() - classmeth" );
670 pio = new ['FileHandle']
671 pio.'open'("$temp_file", 'w')
674 cl = new ['FileHandle']
675 \$S1 = cl.'readall'('$temp_file')
676 if \$S0 == \$S1 goto ok
685 pir_output_is( <<"CODE", <<"OUTPUT", "PIO.readall() - object" );
693 pio = new ['FileHandle']
694 pio.'open'("$temp_file", 'w')
698 pio2 = new ['FileHandle']
699 pio2.'open'("$temp_file", 'r')
700 \$S1 = pio2.'readall'()
701 if \$S0 == \$S1 goto ok
712 # cperl-indent-level: 4
715 # vim: expandtab shiftwidth=4: