-
Notifications
You must be signed in to change notification settings - Fork 1
/
filehandle.t
597 lines (481 loc) · 12.9 KB
/
filehandle.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!perl
# Copyright (C) 2006-2008, Parrot Foundation.
# $Id$
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 17;
use Parrot::Test::Util 'create_tempfile';
use Parrot::Test::Util 'create_tempfile';
=head1 NAME
t/pmc/filehandle.t - test the FileHandle PMC
=head1 SYNOPSIS
% prove t/pmc/filehandle.t
=head1 DESCRIPTION
Tests the FileHandle PMC.
=cut
# L<PDD22/I\/O PMC API/=item new>
pir_output_is( <<'CODE', <<'OUT', 'new' );
.sub 'test' :main
$P0 = new ['FileHandle']
say "ok 1 - $P0 = new ['FileHandle']"
.end
CODE
ok 1 - $P0 = new ['FileHandle']
OUT
my (undef, $temp_file) = create_tempfile( UNLINK => 1 );
# L<PDD22/I\/O PMC API/=item open.*=item close>
pir_output_is( <<"CODE", <<'OUT', 'open and close - synchronous' );
.sub 'test' :main
\$P1 = new ['FileHandle']
\$P1.'open'('README')
say 'ok 1 - \$P1.open(\$S1)'
\$P1.'close'()
say 'ok 2 - \$P1.close()'
\$P3 = new ['FileHandle']
\$P3.'open'('$temp_file', 'rw')
say 'ok 3 - \$P3.open(\$S1, \$S2) # rw mode'
\$P3.'close'()
\$P3.'open'()
say 'ok 4 - \$P3.open() # reopening'
\$P3.'close'()
test_5:
\$P5 = new ['FileHandle']
push_eh eh_bad_file_1
\$P5.'open'('bad.file')
pop_eh
test_6:
\$P6 = new ['FileHandle']
push_eh eh_bad_file_2
\$P6.'open'('bad.file', 'r')
pop_eh
test_7:
\$P7 = new ['FileHandle']
\$P7.'open'('$temp_file', 'w')
say 'ok 7 - \$P7.open(\$S1, \$S2) # new file, write mode succeeds'
goto end
eh_bad_file_1:
say 'ok 5 - \$P5.open(\$S1) # with bad file'
goto test_6
eh_bad_file_2:
say "ok 6 - \$P6.open(\$S1, \$S2) # with bad file"
goto test_7
end:
.end
CODE
ok 1 - $P1.open($S1)
ok 2 - $P1.close()
ok 3 - $P3.open($S1, $S2) # rw mode
ok 4 - $P3.open() # reopening
ok 5 - $P5.open($S1) # with bad file
ok 6 - $P6.open($S1, $S2) # with bad file
ok 7 - $P7.open($S1, $S2) # new file, write mode succeeds
OUT
# RT #46827 test open file, close file, delete file, reopen previously opened stream
SKIP: {
skip 'no asynch calls yet' => 1;
pir_output_is( <<'CODE', <<'OUT', 'open and close - asynchronous' );
.sub 'test' :main
$P1 = # RT #46831 create a callback here
$P0 = new ['FileHandle']
$P0.'open'('README')
say 'ok 1 - $P0.open($S1)'
$P0.'close'()
say 'ok 2 - $P0.close($P1)'
$P0.'open'('README', 'rw')
say 'ok 3 - $P0.open($S1, $S2)'
$P0.'close'()
$P0.'open'()
say 'ok 4 - $P0.open()'
cleanup:
$P0.'close'()
.end
CODE
ok 1 - $P0.open($S1)
ok 2 - $P0.close()
ok 3 - $P0.open($S1, $S2)
ok 4 - $P0.open()
OUT
}
# L<PDD22/I\/O PMC API/=item read>
pir_output_is(
<<'CODE', <<'OUT', 'read - synchronous' );
.sub 'test' :main
$P0 = new ['FileHandle']
$P0.'open'('README')
$S0 = $P0.'read'(14) # bytes
if $S0 == 'This is Parrot' goto ok_1
print 'not '
ok_1:
say 'ok 1 - $S0 = $P1.read($I2)'
$S0 = $P0.'read'(9) # bytes
if $S0 == ', version' goto ok_2
print 'not '
ok_2:
say 'ok 2 - $S0 = $P1.read($I2) # again on same stream'
.end
CODE
ok 1 - $S0 = $P1.read($I2)
ok 2 - $S0 = $P1.read($I2) # again on same stream
OUT
# L<PDD22/I\/O PMC API/=item print>
pir_output_is( <<"CODE", <<'OUT', 'print - synchronous' );
.sub 'test' :main
\$P0 = new ['FileHandle']
\$P0.'open'('$temp_file', 'w')
\$P0.'print'(123)
say 'ok 1 - \$P0.print(\$I1)'
\$P0.'print'(456.789)
say 'ok 2 - \$P0.print(\$N1)'
\$P0.'print'("squawk\\n")
say 'ok 3 - \$P0.print(\$S1)'
\$P1 = new ['Integer']
\$P1 = 42
\$P0.'print'(\$P1)
say 'ok 4 - \$P0.print(\$P1)'
\$P0.'close'()
\$P1 = new ['FileHandle']
\$P1.'open'('$temp_file', 'r')
\$S0 = \$P1.'read'(3) # bytes
if \$S0 == "123" goto ok_5
print 'not '
ok_5:
say 'ok 5 - read integer back from file'
\$S0 = \$P1.'read'(16) # bytes
if \$S0 == "456.789squawk\\n42" goto ok_6
say \$S0
print 'not '
ok_6:
say 'ok 6 - read string back from file'
\$P1.'close'()
.end
CODE
ok 1 - $P0.print($I1)
ok 2 - $P0.print($N1)
ok 3 - $P0.print($S1)
ok 4 - $P0.print($P1)
ok 5 - read integer back from file
ok 6 - read string back from file
OUT
(undef, $temp_file) = create_tempfile( UNLINK => 1 );
# L<PDD22/I\/O PMC API/=item print.*=item readline>
pir_output_is( <<"CODE", <<'OUT', 'readline - synchronous' );
.sub 'test' :main
load_bytecode 'String/Utils.pbc'
.local pmc chomp
chomp = get_global ['String';'Utils'], 'chomp'
\$P0 = new ['FileHandle']
\$P0.'open'('$temp_file', 'w')
\$P0.'print'("foobarbaz\\n42")
\$P0.'close'()
\$P1 = new ['FileHandle']
\$P1.'open'('$temp_file')
\$S0 = \$P1.'readline'()
\$S0 = chomp( \$S0 )
if \$S0 == 'foobarbaz' goto ok_1
print 'not '
ok_1:
say 'ok 1 - \$S0 = \$P1.readline()'
\$S0 = \$P1.'readline'()
\$S0 = chomp( \$S0 )
if \$S0 == '42' goto ok_2
print 'not '
ok_2:
say 'ok 2 - \$S0 = \$P1.readline() # again on same stream'
\$P1.'close'()
.end
CODE
ok 1 - $S0 = $P1.readline()
ok 2 - $S0 = $P1.readline() # again on same stream
OUT
my $LINES;
($LINES, $temp_file) = create_tempfile( UNLINK => 1 );
for my $counter (1 .. 10000) {
print $LINES $counter, "\n";
}
close $LINES;
pir_output_is( <<"CODE", <<'OUT', 'readline 10,000 lines' );
.sub 'test' :main
load_bytecode 'String/Utils.pbc'
.local pmc chomp
chomp = get_global ['String';'Utils'], 'chomp'
.local string test_line
.local pmc filehandle
.local int counter
filehandle = new ['FileHandle']
filehandle.'open'('$temp_file')
counter = 0
read_loop:
inc counter
# read in the file one line at a time...
\$I0 = filehandle.'eof'()
if \$I0 goto end_read_loop
test_line = readline filehandle
if test_line == "" goto end_read_loop
test_line = chomp( test_line )
\$I1 = test_line
if \$I1 == counter goto read_loop
print "not "
## the following lines provide more extensive debugging
## output on a readline failure
# print counter
# print " = "
# print \$I1
# print "\\n"
# counter = \$I1
# goto read_loop
end_read_loop:
if counter > 1 goto read_something
print "not "
read_something:
say 'ok 1 - read 10,000 lines'
filehandle.'close'()
.end
CODE
ok 1 - read 10,000 lines
OUT
# RT #46833 test reading/writing code points once supported
# RT #46835 test reading long chunks, eof, and across newlines
# RT #46837 pir_output_is( <<'CODE', <<'OUT', 'print, read, and readline - asynchronous', todo => 'not yet implemented' );
# L<PDD22/I\/O PMC API/=item record_separator>
pir_output_is( <<'CODE', <<'OUT', 'record_separator', todo => 'not yet implemented' );
.sub 'test' :main
$P0 = new ['FileHandle']
$S0 = $P0.'record_separator'()
if $S0 == "\n" goto ok_1
print 'not '
ok_1:
say 'ok 1 - $S0 = $P1.record_separator() # default'
$S99 = 'abc'
$P0.'record_separator'($S99)
$S0 = $P0.'record_separator'()
if $S0 == $S99 goto ok_2
print 'not '
ok_2:
say 'ok 2 - $P0.record_separator($S1)'
$P0.'print'(123)
$S0 = $P0.'record_separator'()
$P0.'print'($S0)
$P0.'print'(456)
$S0 = $P0.'readline'()
if $S0 == '123abc' goto ok_3
print 'not '
ok_3:
say 'ok 3 - $P0.record_separator() # .readline works as expected'
.end
CODE
ok 1 - $S0 = $P1.record_separator() # default
ok 2 - $P0.record_separator($S1)
ok 3 - $P0.record_separator() # .readline works as expected
OUT
# L<PDD22/I\/O PMC API/=item buffer_type>
pir_output_is( <<'CODE', <<'OUT', 'buffer_type' );
.sub 'test' :main
$P0 = new ['FileHandle']
$P0.'buffer_type'('unbuffered')
$S0 = $P0.'buffer_type'()
if $S0 == 'unbuffered' goto ok_1
print 'not '
ok_1:
say 'ok 1 - $S0 = $P1.buffer_type() # unbuffered'
$P0.'buffer_type'('line-buffered')
$S0 = $P0.'buffer_type'()
if $S0 == 'line-buffered' goto ok_2
print 'not '
ok_2:
say 'ok 2 - $S0 = $P1.buffer_type() # line-buffered'
$P0.'buffer_type'('full-buffered')
$S0 = $P0.'buffer_type'()
if $S0 == 'full-buffered' goto ok_3
print 'not '
ok_3:
say 'ok 3 - $S0 = $P1.buffer_type() # full-buffered'
.end
CODE
ok 1 - $S0 = $P1.buffer_type() # unbuffered
ok 2 - $S0 = $P1.buffer_type() # line-buffered
ok 3 - $S0 = $P1.buffer_type() # full-buffered
OUT
# RT #46839 test effects of buffer_type, not just set/get
# RT #46841
# L<PDD22/I\/O PMC API/=item buffer_size>
# NOTES: try setting positive, zero, negative int
# perform print and read ops
# change buffer size while it contains data
# try with all 'buffer_type' modes
(undef, $temp_file) = create_tempfile( UNLINK => 1 );
pir_output_is( <<"CODE", <<'OUT', 'buffer_size' );
.sub 'test' :main
\$P0 = new ['FileHandle']
\$P0.'buffer_type'('full-buffered')
\$P0.'buffer_size'(42)
say 'ok 1 - \$P0.buffer_size(42) # set buffer size'
\$I0 = \$P0.'buffer_size'()
# The set buffer size is a minimum, the I/O subsystem may scale it upward
# to a round block, so test that the buffer size is equal or greater than
# the set size.
if \$I0 >= 42 goto ok_2
print 'not '
ok_2:
say 'ok 2 - \$I0 = \$P0.buffer_size() # get buffer size'
\$P0.'open'('$temp_file', 'w')
\$P0.'print'(1234567890)
\$P0.'close'()
\$P1 = new ['FileHandle']
\$P1.'open'('$temp_file')
\$S0 = \$P1.'readline'()
if \$S0 == '1234567890' goto ok_3
print 'not '
ok_3:
say 'ok 3 - \$S0 = \$P0.readline() # buffer flushed'
\$P1.'close'()
.end
CODE
ok 1 - $P0.buffer_size(42) # set buffer size
ok 2 - $I0 = $P0.buffer_size() # get buffer size
ok 3 - $S0 = $P0.readline() # buffer flushed
OUT
# L<PDD22/I\/O PMC API/=item encoding>
pir_output_is( <<'CODE', <<'OUT', 'encoding' );
.sub 'test' :main
$P0 = new ['FileHandle']
$P0.'encoding'('utf8')
$S0 = $P0.'encoding'()
if $S0 == 'utf8' goto ok_1
print 'not '
ok_1:
say 'ok 1 - $S0 = $P1.encoding() # utf8'
.end
CODE
ok 1 - $S0 = $P1.encoding() # utf8
OUT
(undef, $temp_file) = create_tempfile( UNLINK => 1 );
pir_output_is( <<"CODE", <<'OUT', 'encoding - read/write' );
.sub 'test' :main
\$P0 = new ['FileHandle']
\$P0.'encoding'('utf8')
\$P0.'open'('$temp_file', 'w')
\$P0.'print'(1234567890)
\$P0.'print'("\\n")
\$S0 = iso-8859-1:"TÖTSCH"
\$P0.'print'(\$S0)
\$P0.'close'()
\$P1 = new ['FileHandle']
\$P1.'encoding'('utf8')
\$P1.'open'('$temp_file')
\$S1 = \$P1.'readline'()
if \$S1 == "1234567890\\n" goto ok_1
print \$S1
print 'not '
ok_1:
say 'ok 1 - \$S1 = \$P1.readline() # read with utf8 encoding on'
\$S2 = \$P1.'readline'()
if \$S2 == \$S0 goto ok_2
print \$S2
print 'not '
ok_2:
say 'ok 2 - \$S2 = \$P1.readline() # read iso-8859-1 string'
\$P1.'close'()
.end
CODE
ok 1 - $S1 = $P1.readline() # read with utf8 encoding on
ok 2 - $S2 = $P1.readline() # read iso-8859-1 string
OUT
(undef, $temp_file) = create_tempfile( UNLINK => 1 );
# L<PDD22/I\/O PMC API/=item mode>
pir_output_is( <<'CODE', <<'OUT', 'mode' );
.sub 'test' :main
$P0 = new ['FileHandle']
$P0.'open'('README')
$S0 = $P0.'mode'()
if $S0 == 'r' goto ok_1
print 'not '
ok_1:
say 'ok 1 - $S0 = $P0.mode() # get read mode'
$P0.'close'()
.end
CODE
ok 1 - $S0 = $P0.mode() # get read mode
OUT
pir_output_is( <<"CODE", <<"OUTPUT", "readall - closed filehandle" );
.sub main :main
\$S0 = <<"EOS"
line 1
line 2
line 3
EOS
.local pmc pio, pio2
pio = new ['FileHandle']
pio.'open'("$temp_file", "w")
pio.'print'(\$S0)
pio.'close'()
pio2 = new ['FileHandle']
\$S1 = pio2.'readall'('$temp_file')
if \$S0 == \$S1 goto ok
print "not "
ok:
say "ok"
.end
CODE
ok
OUTPUT
pir_output_is( <<"CODE", <<"OUTPUT", "readall() - opened filehandle" );
.sub main :main
\$S0 = <<"EOS"
line 1
line 2
line 3
EOS
.local pmc pio, pio2
pio = new ['FileHandle']
pio.'open'("$temp_file", "w")
pio.'print'(\$S0)
pio.'close'()
pio2 = new ['FileHandle']
pio2.'open'("$temp_file", "r")
\$S1 = pio2.'readall'()
if \$S0 == \$S1 goto ok
print "not "
ok:
say "ok"
.end
CODE
ok
OUTPUT
pir_output_is( <<"CODE", <<"OUTPUT", "readall() - utf8 on closed filehandle" );
.sub 'main'
.local pmc ifh
ifh = new ['FileHandle']
ifh.'encoding'('utf8')
\$S0 = ifh.'readall'('$temp_file')
\$I0 = encoding \$S0
\$S1 = encodingname \$I0
say \$S1
.end
CODE
utf8
OUTPUT
pir_output_is( <<"CODE", <<"OUTPUT", "readall() - utf8 on opened filehandle" );
.sub 'main'
.local pmc ifh
ifh = new ['FileHandle']
ifh.'encoding'('utf8')
ifh.'open'('$temp_file')
\$S0 = ifh.'readall'()
\$I0 = encoding \$S0
\$S1 = encodingname \$I0
say \$S1
.end
CODE
utf8
OUTPUT
# RT #46843
# L<PDD22/I\/O PMC API/=item get_fd>
# NOTES: this is going to be platform dependent
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: