2 # Copyright (C) 2006-2008, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
10 use Parrot::Test tests => 19;
14 t/pmc/stringhandle.t - test the StringHandle PMC
18 % prove t/pmc/stringhandle.t
22 Tests the StringHandle PMC.
26 # L<PDD22/I\/O PMC API/=item new>
27 pir_output_is( <<'CODE', <<'OUT', 'new' );
29 new $P0, ['StringHandle']
30 say "ok 1 - $P0 = new ['StringHandle']"
33 ok 1 - $P0 = new ['StringHandle']
36 # L<PDD22/I\/O PMC API/=item open.*=item close>
37 pir_output_is( <<"CODE", <<'OUT', 'open and close - synchronous' );
39 \$P1 = new ['StringHandle']
41 say 'ok 1 - \$P1.open(\$S1)'
44 say 'ok 2 - \$P1.close()'
46 \$P3 = new ['StringHandle']
47 \$P3.'open'('temp_file', 'rw')
48 say 'ok 3 - \$P3.open(\$S1, \$S2) # rw mode'
52 say 'ok 4 - \$P3.open() # reopening'
56 \$P7 = new ['StringHandle']
57 \$P7.'open'('temp_file', 'w')
58 say 'ok 7 - \$P7.open(\$S1, \$S2) # new file, write mode succeeds'
67 ok 3 - $P3.open($S1, $S2) # rw mode
68 ok 4 - $P3.open() # reopening
69 ok 7 - $P7.open($S1, $S2) # new file, write mode succeeds
73 skip 'no asynch calls yet' => 1;
75 pir_output_is( <<'CODE', <<'OUT', 'open and close - asynchronous' );
77 $P1 = # TT #1204 create a callback here
78 $P0 = new ['StringHandle']
81 say 'ok 1 - $P0.open($S1)'
84 say 'ok 2 - $P0.close($P1)'
86 $P0.'open'('README', 'rw')
87 say 'ok 3 - $P0.open($S1, $S2)'
91 say 'ok 4 - $P0.open()'
99 ok 3 - $P0.open($S1, $S2)
104 # L<PDD22/I\/O PMC API/=item read>
106 <<'CODE', <<'OUT', 'read - synchronous' );
108 $P0 = new ['StringHandle']
109 $P0.'open'('README', 'w')
111 $P0.'print'("This is Parrot, version")
117 $S0 = $P0.'read'(14) # bytes
118 if $S0 == 'This is Parrot' goto ok_1
121 say 'ok 1 - $S0 = $P1.read($I2)'
123 $S0 = $P0.'read'(9) # bytes
124 if $S0 == ', version' goto ok_2
127 say 'ok 2 - $S0 = $P1.read($I2) # again on same stream'
130 ok 1 - $S0 = $P1.read($I2)
131 ok 2 - $S0 = $P1.read($I2) # again on same stream
135 <<'CODE', <<'OUT', 'read opcode' );
137 $P0 = new ['StringHandle']
138 $P0.'open'('README', 'w')
140 print $P0, "This is Parrot, version"
145 $S0 = read $P0, 14 # bytes
146 if $S0 == 'This is Parrot' goto ok_1
149 say 'ok 1 - $S0 = read $P1, $I2'
151 $S0 = read $P0, 9 # bytes
152 if $S0 == ', version' goto ok_2
155 say 'ok 2 - $S0 = read $P1, $I2 # again on same stream'
158 ok 1 - $S0 = read $P1, $I2
159 ok 2 - $S0 = read $P1, $I2 # again on same stream
162 # L<PDD22/I\/O PMC API/=item print>
163 pir_output_is( <<"CODE", <<'OUT', 'print - synchronous' );
166 \$P0 = new ['StringHandle']
167 \$P0.'open'('temp_file', 'w')
170 say 'ok 1 - \$P0.print(\$I1)'
171 \$P0.'print'(456.789)
172 say 'ok 2 - \$P0.print(\$N1)'
173 \$P0.'print'("squawk\\n")
174 say 'ok 3 - \$P0.print(\$S1)'
175 \$P1 = new ['Integer']
178 say 'ok 4 - \$P0.print(\$P1)'
182 \$P0.'open'('temp_file', 'r')
184 \$S0 = \$P0.'read'(3) # bytes
185 if \$S0 == "123" goto ok_5
188 say 'ok 5 - read integer back from file'
190 \$S0 = \$P0.'read'(16) # bytes
191 if \$S0 == "456.789squawk\\n42" goto ok_6
196 say 'ok 6 - read string back from file'
201 ok 1 - $P0.print($I1)
202 ok 2 - $P0.print($N1)
203 ok 3 - $P0.print($S1)
204 ok 4 - $P0.print($P1)
205 ok 5 - read integer back from file
206 ok 6 - read string back from file
209 # L<PDD22/I\/O PMC API/=item print.*=item readline>
210 pir_output_is( <<"CODE", <<'OUT', 'readline - synchronous' );
212 load_bytecode 'String/Utils.pbc'
214 chomp = get_global ['String';'Utils'], 'chomp'
216 \$P0 = new ['StringHandle']
217 \$P0.'open'('temp_file', 'w')
218 \$P0.'print'("foobarbaz\\n42")
221 \$P0.'open'('temp_file')
223 \$S0 = \$P0.'readline'()
225 if \$S0 == 'foobarbaz' goto ok_1
228 say 'ok 1 - \$S0 = \$P0.readline()'
230 \$S0 = \$P0.'readline'()
232 if \$S0 == '42' goto ok_2
235 say 'ok 2 - \$S0 = \$P0.readline() # again on same stream'
240 ok 1 - $S0 = $P0.readline()
241 ok 2 - $S0 = $P0.readline() # again on same stream
244 pir_output_is( <<'CODE', <<'OUT', 'readline 10,000 lines' );
246 load_bytecode 'String/Utils.pbc'
248 chomp = get_global ['String';'Utils'], 'chomp'
249 .local string test_line
250 .local pmc stringhandle
252 stringhandle = new ['StringHandle']
254 stringhandle.'open'('temp_file', 'w')
259 if counter > 10000 goto end_write_loop
261 stringhandle.'print'(counter)
262 stringhandle.'print'("\n")
266 stringhandle.'close'()
267 stringhandle.'open'('temp_file')
272 # read in the file one line at a time...
273 $I0 = stringhandle.'eof'()
274 if $I0 goto end_read_loop
276 test_line = readline stringhandle
277 if test_line == "" goto end_read_loop
278 test_line = chomp( test_line )
280 if $I1 == counter goto read_loop
282 ## the following lines provide more extensive debugging
283 ## output on a readline failure
292 if counter > 9000 goto read_something
295 say 'ok 1 - read 10,000 lines'
296 stringhandle.'close'()
299 ok 1 - read 10,000 lines
303 # TT #1204 test reading long chunks, eof, and across newlines
305 # TT #1204 pir_output_is( <<'CODE', <<'OUT', 'print, read, and readline - asynchronous', todo => 'not yet implemented' );
307 # L<PDD22/I\/O PMC API/=item record_separator>
308 pir_output_is( <<'CODE', <<'OUT', 'record_separator', todo => 'not yet implemented' );
310 $P0 = new ['StringHandle']
312 $S0 = $P0.'record_separator'()
313 if $S0 == "\n" goto ok_1
316 say 'ok 1 - $S0 = $P1.record_separator() # default'
319 $P0.'record_separator'($S99)
320 $S0 = $P0.'record_separator'()
321 if $S0 == $S99 goto ok_2
324 say 'ok 2 - $P0.record_separator($S1)'
327 $S0 = $P0.'record_separator'()
331 $S0 = $P0.'readline'()
332 if $S0 == '123abc' goto ok_3
335 say 'ok 3 - $P0.record_separator() # .readline works as expected'
338 ok 1 - $S0 = $P1.record_separator() # default
339 ok 2 - $P0.record_separator($S1)
340 ok 3 - $P0.record_separator() # .readline works as expected
343 # L<PDD22/I\/O PMC API/=item buffer_type>
344 pir_output_is( <<'CODE', <<'OUT', 'buffer_type' );
346 $P0 = new ['StringHandle']
348 $P0.'buffer_type'('unbuffered')
349 $S0 = $P0.'buffer_type'()
350 if $S0 == 'unbuffered' goto ok_1
353 say 'ok 1 - $S0 = $P1.buffer_type() # unbuffered'
355 $P0.'buffer_type'('line-buffered')
356 $S0 = $P0.'buffer_type'()
357 if $S0 == 'line-buffered' goto ok_2
360 say 'ok 2 - $S0 = $P1.buffer_type() # line-buffered'
362 $P0.'buffer_type'('full-buffered')
363 $S0 = $P0.'buffer_type'()
364 if $S0 == 'full-buffered' goto ok_3
367 say 'ok 3 - $S0 = $P1.buffer_type() # full-buffered'
371 ok 1 - $S0 = $P1.buffer_type() # unbuffered
372 ok 2 - $S0 = $P1.buffer_type() # line-buffered
373 ok 3 - $S0 = $P1.buffer_type() # full-buffered
376 # TT #1204 test effects of buffer_type, not just set/get
379 # L<PDD22/I\/O PMC API/=item buffer_size>
380 # NOTES: try setting positive, zero, negative int
381 # perform print and read ops
382 # change buffer size while it contains data
383 # try with all 'buffer_type' modes
385 pir_output_is( <<"CODE", <<'OUT', 'buffer_size' );
387 \$P0 = new ['StringHandle']
389 \$P0.'buffer_type'('full-buffered')
390 \$P0.'buffer_size'(42)
391 say 'ok 1 - \$P0.buffer_size(42) # set buffer size'
393 \$P0.'open'('temp_file', 'w')
394 \$P0.'print'(1234567890)
396 \$I0 = \$P0.'buffer_size'()
398 # The set buffer size is a minimum, the I/O subsystem may scale it upward
399 # to a round block, so test that the buffer size is equal or greater than
401 if \$I0 == 10 goto ok_2
404 say 'ok 2 - \$I0 = \$P0.buffer_size() # get buffer size'
408 \$P0.'open'('temp_file')
409 \$S0 = \$P0.'readline'()
411 if \$S0 == '1234567890' goto ok_3
414 say 'ok 3 - \$S0 = \$P0.readline() # buffer flushed'
420 ok 1 - $P0.buffer_size(42) # set buffer size
421 ok 2 - $I0 = $P0.buffer_size() # get buffer size
422 ok 3 - $S0 = $P0.readline() # buffer flushed
425 # L<PDD22/I\/O PMC API/=item encoding>
426 pir_output_is( <<'CODE', <<'OUT', 'encoding' );
428 $P0 = new ['StringHandle']
430 $P0.'encoding'('utf8')
431 $S0 = $P0.'encoding'()
432 if $S0 == 'utf8' goto ok_1
435 say 'ok 1 - $S0 = $P1.encoding() # utf8'
439 ok 1 - $S0 = $P1.encoding() # utf8
442 pir_output_is( <<"CODE", <<'OUT', 'encoding - read/write' );
444 \$P0 = new ['StringHandle']
445 \$P0.'encoding'('utf8')
447 \$P0.'open'('temp_file', 'w')
449 \$P0.'print'(1234567890)
451 \$S0 = iso-8859-1:"TÖTSCH"
455 \$P0.'open'('temp_file')
457 \$S1 = \$P0.'readline'()
458 if \$S1 == "1234567890\\n" goto ok_1
462 say 'ok 1 - \$S1 = \$P0.readline() # read with utf8 encoding on'
464 \$S2 = \$P0.'readline'()
465 if \$S2 == \$S0 goto ok_2
469 say 'ok 2 - \$S2 = \$P0.readline() # read iso-8859-1 string'
475 ok 1 - $S1 = $P0.readline() # read with utf8 encoding on
476 ok 2 - $S2 = $P0.readline() # read iso-8859-1 string
480 # L<PDD22/I\/O PMC API/=item mode>
481 pir_output_is( <<'CODE', <<'OUT', 'mode' );
483 $P0 = new ['StringHandle']
488 if $S0 == 'r' goto ok_1
491 say 'ok 1 - $S0 = $P0.mode() # get read mode'
497 ok 1 - $S0 = $P0.mode() # get read mode
500 pir_output_is( <<"CODE", <<"OUTPUT", "readall - closed stringhandle" );
508 pio = new ['StringHandle']
509 pio.'open'("temp_file", "w")
512 pio.'open'("temp_file")
513 \$S1 = pio.'readall'('temp_file')
514 if \$S0 == \$S1 goto ok
523 pir_output_is( <<"CODE", <<"OUTPUT", "readall() - opened stringhandle" );
531 pio = new ['StringHandle']
532 pio.'open'("temp_file", "w")
536 pio.'open'("temp_file", "r")
537 \$S1 = pio.'readall'()
538 if \$S0 == \$S1 goto ok
547 pir_output_is( <<"CODE", <<"OUTPUT", "readall() - utf8 on closed stringhandle" );
550 ifh = new ['StringHandle']
551 ifh.'encoding'('utf8')
553 \$S0 = ifh.'readall'('temp_file')
556 \$S1 = encodingname \$I0
564 pir_output_is( <<"CODE", <<"OUTPUT", "readall() - utf8 on opened stringhandle" );
567 ifh = new ['StringHandle']
568 ifh.'encoding'('utf8')
569 ifh.'open'('temp_file')
571 \$S0 = ifh.'readall'()
574 \$S1 = encodingname \$I0
582 pir_output_is( <<'CODE', <<'OUTPUT', "clone an uninitialized stringhandle" );
584 $P0 = new ['StringHandle']
593 # L<PDD22/I\/O PMC API/=item get_fd>
594 # NOTES: this is going to be platform dependent
598 # cperl-indent-level: 4
601 # vim: expandtab shiftwidth=4: