/
io.t
807 lines (721 loc) · 14.2 KB
/
io.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#! perl
# Copyright (C) 2001-2005, The Perl Foundation.
# $Id$
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 39;
=head1 NAME
t/pmc/io.t - IO Ops
=head1 SYNOPSIS
% prove t/pmc/io.t
=head1 DESCRIPTION
Tests the Parrot IO operations.
=cut
sub file_content_is {
my ($file, $content, $name) = @_;
local $/=undef; # slurp mode
open my $FOO, '<', "temp.file";
is(<$FOO>, $content, $name);
close $FOO;
}
TODO: {
local $TODO = "IO on some invalid types";
pir_output_is(<<'CODE', <<'OUTPUT', "IO on some invalid types");
.sub main
$P0 = null
test($P0, "Undef")
new $P0, .Integer
test($P0, "null")
new $P0, .Undef
test($P0, "Integer")
new $P0, .String
test($P0, "String")
.end
.sub test
.param pmc io
.param string name
print name
read $S0, io, 1
length $I0, $S0
if $I0 == 0 goto ok1
print " not"
ok1:
print " ok 1\n"
print name
# what should happen here?
close io
print " ok 2\n"
print name
# what should happen here?
print io, "not"
print " ok 3\n"
.end
CODE
Undef ok 1
Undef ok 2
Undef ok 3
null ok 1
null ok 2
null ok 3
Integer ok 1
Integer ok 2
Integer ok 3
String ok 1
String ok 2
String ok 3
OUTPUT
};
pasm_output_is(<<'CODE', <<'OUTPUT', "open/close");
open P0, "temp.file", ">"
print P0, "a line\n"
close P0
open P0, "temp.file", "<"
read S0, P0, 20
print S0
end
CODE
a line
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "timely destruction");
interpinfo I0, 2 # DOD runs
open P0, "temp.file", ">"
needs_destroy P0
print P0, "a line\n"
null P0 # kill it
sweep 0 # a lazy DOD has to close the PIO
open P0, "temp.file", "<"
read S0, P0, 20
print S0
end
CODE
a line
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "getfd/fdopen");
getstdout P0
getfd I0, P0
fdopen P1, I0, ">"
defined I0, P1
unless I0, nok
print P1, "ok\n"
close P1
end
nok:
print "fdopen failed\n"
end
CODE
ok
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "fdopen - no close");
getstdout P0
getfd I0, P0
fdopen P1, I0, ">"
defined I0, P1
unless I0, nok
print P1, "ok\n"
end
nok:
print "fdopen failed\n"
end
CODE
ok
OUTPUT
unlink "no_such_file" if (-e "no_such_file");
pasm_output_is(<<'CODE', <<'OUTPUT', "get_bool");
open P0, "no_such_file", "<"
unless P0, ok1
print "Huh: 'no_such_file' exists? - not "
ok1:
print "ok 1\n"
open P0, "temp.file", "<"
if P0, ok2
print "not "
ok2: print "ok 2\n"
read S0, P0, 1024
read S0, P0, 1024
unless P0, ok3
print "not "
ok3: print "ok 3\n"
defined I0, P0
if I0, ok4
print "not "
ok4: print "ok 4\n"
close P0
defined I0, P0 # closed file is still defined
if I0, ok5
print "not "
ok5: print "ok 5\n"
unless P0, ok6 # but false
print "not "
ok6: print "ok 6\n"
end
CODE
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "read on invalid fh should throw exception");
open P0, "no_such_file", "<"
unless P0, ok1
print "Huh: 'no_such_file' exists? - not "
ok1:
print "ok 1\n"
push_eh _readline_handler
readline S0, P0 # Currently segfaults
print "not "
_readline_handler:
print "ok 2\n"
branch fin
push_eh _read_handler
read S0, P0, 1
print "not "
_read_handler:
print "ok 3\n"
push_eh _print_handler
print P0, "kill me now\n"
print "not "
_print_handler:
fin:
print "ok 4\n"
end
CODE
ok 1
ok 2
ok 4
OUTPUT
SKIP: {
skip ("clone not finished yet", 1);
pasm_output_is(<<'CODE', <<'OUTPUT', "clone");
open P0, "temp.file", "<"
clone P1, P0
read S0, P1, 1024
print S0
end
CODE
a line
OUTPUT
}
# It would be very embarrassing if these didnt work...
open my $FOO, '>', "temp.file" or die "can't open 'temp.file': $!";
print $FOO "2\n1\n";
close $FOO;
pasm_output_is(<<'CODE', <<'OUTPUT', "open and readline");
open P0, "temp.file"
set S0, ""
set S1, ""
readline S0, P0
readline S1, P0
print S1
print S0
end
CODE
1
2
OUTPUT
open $FOO, '>', "temp.file"; # Clobber previous contents
close $FOO;
pasm_output_is(<<'CODE', <<'OUTPUT', "open & print");
set I0, -12
set N0, 2.2
set S0, "Foo"
new P0, .String
set P0, "Bar\n"
open P1, "temp.file"
print P1, I0
print P1, N0
print P1, S0
print P1, P0
close P1
open P2, "temp.file"
readline S1, P2
close P2
print S1
end
CODE
-122.200000FooBar
OUTPUT
open $FOO, '>', "temp.file"; # Clobber previous contents
close $FOO;
# write to file opened for reading
pasm_output_is(<<'CODE', <<'OUTPUT', "3-arg open");
open P1, "temp.file", "<"
print P1, "Foobar\n"
close P1
open P3, "temp.file", "<"
readline S1, P3
close P3
print S1
print "writing to file opened for reading\n"
end
CODE
writing to file opened for reading
OUTPUT
unlink("temp.file");
pasm_output_is(<<'CODE', <<'OUTPUT', 'open and close');
open P1, "temp.file"
print P1, "Hello, World!\n"
close P1
print "done\n"
end
CODE
done
OUTPUT
file_content_is("temp.file", <<'OUTPUT', 'file contents');
Hello, World!
OUTPUT
pasm_output_is(<<'CODE', '', 'append');
open P1, "temp.file", ">>"
print P1, "Parrot flies\n"
close P1
end
CODE
file_content_is("temp.file", <<'OUTPUT', 'append file contents');
Hello, World!
Parrot flies
OUTPUT
pasm_output_is(<<'CODE', '', 'write to file');
open P1, "temp.file", ">"
print P1, "Parrot overwrites\n"
close P1
end
CODE
file_content_is("temp.file", <<'OUTPUT', 'file contents');
Parrot overwrites
OUTPUT
unlink("temp.file");
pasm_output_is(<<'CODE', <<'OUT', 'standard file descriptors');
getstdin P0
getfd I0, P0
# I0 is 0 on Unix and non-Null on stdio and win32
print "ok 1\n"
getstdout P1
getfd I1, P1
if I1, OK_2
print "not "
OK_2: print "ok 2\n"
getstderr P2
getfd I2, P2
if I2, OK_3
print "not "
OK_3: print "ok 3\n"
end
CODE
ok 1
ok 2
ok 3
OUT
pasm_output_is(<<'CODE', <<'OUTPUT', 'printerr');
new P0, .String
set P0, "This is a test\n"
printerr 10
printerr "\n"
printerr 1.0
printerr "\n"
printerr "foo"
printerr "\n"
printerr P0
end
CODE
10
1.000000
foo
This is a test
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', 'puts method');
getstdout P2
can I0, P2, "puts"
if I0, ok1
print "not "
ok1: print "ok 1\n"
set_args "(0,0)", P2, "ok 2\n"
callmethodcc P2, "puts"
end
CODE
ok 1
ok 2
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', 'puts method - PIR');
.sub main :main
.local string s
s = "ok 2\n"
.local pmc io
io = getstdout
$I0 = can io, "puts"
if $I0 goto ok1
print "not "
ok1: print "ok 1\n"
io."puts"(s)
.end
CODE
ok 1
ok 2
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', 'callmethod puts');
getstderr P2 # the object
set S0, "puts" # method
set S5, "ok 1\n" # 2nd param
set_args "(0,0)", P2, S5
callmethodcc P2, S0
set S5, "ok 2\n"
set_args "(0,0)", P2, S5
callmethodcc P2, S0
end
CODE
ok 1
ok 2
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', 'seek/tell');
open P0, "temp.file", ">"
print P0, "Hello "
tell I0, P0
print P0, "World!"
seek P0, I0, 0
print P0, "Parrot!\n"
close P0
print "ok 1\n"
open P0, "temp.file", "<"
read S0, P0, 65635
print S0
end
CODE
ok 1
Hello Parrot!
OUTPUT
pasm_output_like(<<'CODE', <<'OUTPUT', '32bit seek: exception');
open P0, "temp.file", ">"
seek P0, -1, 0
print "error!\n"
end
CODE
/seek failed \(32bit\)/
OUTPUT
pasm_output_like(<<'CODE', <<'OUTPUT', '64bit seek: exception');
open P0, "temp.file", ">"
seek P0, -1, -1, 0
print "error!\n"
end
CODE
/seek failed \(64bit\)/
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "peek");
open P0, "temp.file", ">"
print P0, "a line\n"
close P0
open P0, "temp.file", "<"
peek S0, P0
print S0
peek S1, P0
print S1
print "\n"
read S2, P0, 2
peek S3, P0
print S3
print "\n"
end
CODE
aa
l
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "peek on an empty file");
open P0, "temp.file", ">"
close P0
open P0, "temp.file", "<"
peek S0, P0
eq S0, "", OK1
print "not "
OK1: print "ok 1\n"
end
CODE
ok 1
OUTPUT
unlink "temp.file";
pasm_output_like(<<'CODE', <<'OUTPUT', "layer names");
getstdin P0
set S0, P0[0]
print S0
print "-"
set S0, P0[1]
print S0
print "-"
set S0, P0[-1]
print S0
print "-"
set S0, P0[-2]
print S0
print "-"
set S0, P0[-3]
print S0
print "-"
end
CODE
/^(unix|win32|stdio)-buf-buf-\1--$/
OUTPUT
pasm_output_is(<<'CODE', <<'OUTPUT', "layer push, pop");
getstdin P0
push P0, "utf8"
set S0, P0[-1]
print S0
print "\n"
pop S1, P0
print S1
print "\n"
set S0, P0[-1]
print S0
print "\n"
end
CODE
utf8
utf8
buf
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', "substr after reading from file");
.sub _main
# Write something into a file
.local pmc out
out = open "temp.file", ">"
print out, "0123456789\n"
close out
# read file contents back in
.local pmc in
in = open "temp.file", "<"
.local string from_file
from_file = read in, 20
# Extract part of the read in file
.local string head_from_file
substr head_from_file, from_file, 0, 5, ''
print head_from_file
print "\n"
end
.end
CODE
01234
OUTPUT
pir_output_is(<<'CODE', <<'OUTPUT', "multiple substr after reading from file");
.sub _main
# Write something into a file
.local pmc out
out = open "temp.file", ">"
print out, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
close out
.local pmc in
.local string line
in = open 'temp.file', '<'
line = read in, 50000
close in
.local string sub_1
sub_1 = ''
.local string sub_2
sub_2 = ''
.local string sub_3
sub_3 = ''
substr sub_1, line, 0, 3
substr sub_2, line, 0, 3, ''
substr sub_3, line, 0, 3, ''
print "line: "
print line
print "sub_1: "
print sub_1
print "\n"
print "sub_2: "
print sub_2
print "\n"
print "sub_3: "
print sub_3
print "\n"
end
.end
CODE
line: 6789ABCDEFGHIJKLMNOPQRSTUVWXYZ
sub_1: 012
sub_2: 012
sub_3: 345
OUTPUT
pir_output_like(<<'CODE', <<'OUT', 'read on null PMC throws exception', todo => 'not yet implemented');
.sub main :main
null $P1
$S0 = read $P1, 1
end
.end
CODE
/some crazy exception/
OUT
open $FOO, '>', "temp.file"; # write utf8
print $FOO "T\xc3\xb6tsch\n";
close $FOO;
pir_output_is(<<'CODE', <<"OUTPUT", "utf8 read layer");
.sub main :main
.local pmc pio
.local int len
.include "stat.pasm"
.local string f
f = 'temp.file'
len = stat f, .STAT_FILESIZE
pio = open f, "<"
push pio, "utf8"
$S0 = read pio, len
close pio
$I1 = charset $S0
$S2 = charsetname $I1
print $S2
print "\n"
$I1 = encoding $S0
$S2 = encodingname $I1
print $S2
print "\n"
$I1 = find_charset 'iso-8859-1'
trans_charset $S1, $S0, $I1
print $S1
.end
CODE
unicode
utf8
T\xf6tsch
OUTPUT
pir_output_is(<<'CODE', <<"OUTPUT", "utf8 read layer - readline");
.sub main :main
.local pmc pio
.local string f
f = 'temp.file'
pio = open f, "<"
push pio, "utf8"
$S0 = readline pio
close pio
$I1 = charset $S0
$S2 = charsetname $I1
print $S2
print "\n"
$I1 = encoding $S0
$S2 = encodingname $I1
print $S2
print "\n"
$I1 = find_charset 'iso-8859-1'
trans_charset $S1, $S0, $I1
print $S1
.end
CODE
unicode
utf8
T\xf6tsch
OUTPUT
pir_output_is(<<'CODE', <<"OUTPUT", "utf8 read layer, read parts");
.sub main :main
.local pmc pio
.local int len
.include "stat.pasm"
.local string f
f = 'temp.file'
len = stat f, .STAT_FILESIZE
pio = open f, "<"
push pio, "utf8"
$S0 = read pio, 2
len -= 2
$S1 = read pio, len
$S0 .= $S1
close pio
$I1 = charset $S0
$S2 = charsetname $I1
print $S2
print "\n"
$I1 = encoding $S0
$S2 = encodingname $I1
print $S2
print "\n"
$I1 = find_charset 'iso-8859-1'
trans_charset $S1, $S0, $I1
print $S1
.end
CODE
unicode
utf8
T\xf6tsch
OUTPUT
pir_output_is(<<'CODE', <<"OUTPUT", "string read/write layer");
.sub main :main
.local pmc pio
.local string greeting
.local string layer
pio = getstdout
push pio, "string"
print "Hello"
print ", "
print "world!"
print "\n"
greeting = read pio, 1024
pop layer, pio
print greeting
print layer
print "\n"
.end
CODE
Hello, world!
string
OUTPUT
pir_output_is(<<'CODE', <<"OUTPUT", "PIO.slurp() - classmeth");
.sub main :main
$S0 = <<"EOS"
line 1
line 2
line 3
EOS
.local pmc pio, cl
pio = open "temp.file", ">"
print pio, $S0
close pio
cl = getclass 'ParrotIO'
$S1 = cl.'slurp'('temp.file')
if $S0 == $S1 goto ok
print "not "
ok:
print "ok\n"
.end
CODE
ok
OUTPUT
pir_output_is(<<'CODE', <<"OUTPUT", "PIO.slurp() - object");
.sub main :main
$S0 = <<"EOS"
line 1
line 2
line 3
EOS
.local pmc pio, pio2
pio = open "temp.file", ">"
print pio, $S0
close pio
pio2 = open "temp.file", "<"
$S1 = pio2.'slurp'('dummy')
if $S0 == $S1 goto ok
print "not "
ok:
print "ok\n"
.end
CODE
ok
OUTPUT
unlink("temp.file");
SKIP: {
skip 'broken on windows', 1 if $^O eq 'MSWin32';
pir_output_like(<<'CODE', <<"OUTPUT", "stat failed");
.sub main :main
.local pmc pio
.local int len
.include "stat.pasm"
.local string f
f = 'no_such_file'
len = stat f, .STAT_FILESIZE
print "never\n"
.end
CODE
/stat failed:/
OUTPUT
}
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: