with DEBUG_LEAKING_SCALARS, dump multiply-freed scalars
[p5sagit/p5-mst-13.2.git] / ext / PerlIO / t / scalar.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     unless (find PerlIO::Layer 'perlio') {
7         print "1..0 # Skip: not perlio\n";
8         exit 0;
9     }
10     require Config;
11     if (($Config::Config{'extensions'} !~ m!\bPerlIO/scalar\b!) ){
12         print "1..0 # Skip -- Perl configured without PerlIO::scalar module\n";
13         exit 0;
14     }
15 }
16
17 use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); # Not 0, 1, 2 everywhere.
18
19 $| = 1;
20
21 use Test::More tests => 51;
22
23 my $fh;
24 my $var = "aaa\n";
25 ok(open($fh,"+<",\$var));
26
27 is(<$fh>, $var);
28
29 ok(eof($fh));
30
31 ok(seek($fh,0,SEEK_SET));
32 ok(!eof($fh));
33
34 ok(print $fh "bbb\n");
35 is($var, "bbb\n");
36 $var = "foo\nbar\n";
37 ok(seek($fh,0,SEEK_SET));
38 ok(!eof($fh));
39 is(<$fh>, "foo\n");
40 ok(close $fh, $!);
41
42 # Test that semantics are similar to normal file-based I/O
43 # Check that ">" clobbers the scalar
44 $var = "Something";
45 open $fh, ">", \$var;
46 is($var, "");
47 #  Check that file offset set to beginning of scalar
48 my $off = tell($fh);
49 is($off, 0);
50 # Check that writes go where they should and update the offset
51 $var = "Something";
52 print $fh "Brea";
53 $off = tell($fh);
54 is($off, 4);
55 is($var, "Breathing");
56 close $fh;
57
58 # Check that ">>" appends to the scalar
59 $var = "Something ";
60 open $fh, ">>", \$var;
61 $off = tell($fh);
62 is($off, 10);
63 is($var, "Something ");
64 #  Check that further writes go to the very end of the scalar
65 $var .= "else ";
66 is($var, "Something else ");
67
68 $off = tell($fh);
69 is($off, 10);
70
71 print $fh "is here";
72 is($var, "Something else is here");
73 close $fh;
74
75 # Check that updates to the scalar from elsewhere do not
76 # cause problems
77 $var = "line one\nline two\line three\n";
78 open $fh, "<", \$var;
79 while (<$fh>) {
80     $var = "foo";
81 }
82 close $fh;
83 is($var, "foo");
84
85 # Check that dup'ing the handle works
86
87 $var = '';
88 open $fh, "+>", \$var;
89 print $fh "xxx\n";
90 open $dup,'+<&',$fh;
91 print $dup "yyy\n";
92 seek($dup,0,SEEK_SET);
93 is(<$dup>, "xxx\n");
94 is(<$dup>, "yyy\n");
95 close($fh);
96 close($dup);
97
98 open $fh, '<', \42;
99 is(<$fh>, "42", "reading from non-string scalars");
100 close $fh;
101
102 { package P; sub TIESCALAR {bless{}} sub FETCH { "shazam" } }
103 tie $p, P; open $fh, '<', \$p;
104 is(<$fh>, "shazam", "reading from magic scalars");
105
106 {
107     use warnings;
108     my $warn = 0;
109     local $SIG{__WARN__} = sub { $warn++ };
110     open my $fh, '>', \my $scalar;
111     print $fh "foo";
112     close $fh;
113     is($warn, 0, "no warnings when writing to an undefined scalar");
114 }
115
116 my $data = "a non-empty PV";
117 $data = undef;
118 open(MEM, '<', \$data) or die "Fail: $!\n";
119 my $x = join '', <MEM>;
120 is($x, '');
121
122 {
123     # [perl #35929] verify that works with $/ (i.e. test PerlIOScalar_unread)
124     my $s = <<'EOF';
125 line A
126 line B
127 a third line
128 EOF
129     open(F, '<', \$s) or die "Could not open string as a file";
130     local $/ = "";
131     my $ln = <F>;
132     close F;
133     is($ln, $s, "[perl #35929]");
134 }
135
136 # [perl #40267] PerlIO::scalar doesn't respect readonly-ness
137 {
138     ok(!(defined open(F, '>', \undef)), "[perl #40267] - $!");
139     close F;
140
141     my $ro = \43;
142     ok(!(defined open(F, '>', $ro)), $!);
143     close F;
144     # but we can read from it
145     ok(open(F, '<', $ro), $!);
146     is(<F>, 43);
147     close F;
148 }
149
150 {
151     # Check that we zero fill when needed when seeking,
152     # and that seeking negative off the string does not do bad things.
153
154     my $foo;
155
156     ok(open(F, '>', \$foo));
157
158     # Seeking forward should zero fill.
159
160     ok(seek(F, 50, SEEK_SET));
161     print F "x";
162     is(length($foo), 51);
163     like($foo, qr/^\0{50}x$/);
164
165     is(tell(F), 51);
166     ok(seek(F, 0, SEEK_SET));
167     is(length($foo), 51);
168
169     # Seeking forward again should zero fill but only the new bytes.
170
171     ok(seek(F, 100, SEEK_SET));
172     print F "y";
173     is(length($foo), 101);
174     like($foo, qr/^\0{50}x\0{49}y$/);
175     is(tell(F), 101);
176
177     # Seeking back and writing should not zero fill.
178
179     ok(seek(F, 75, SEEK_SET));
180     print F "z";
181     is(length($foo), 101);
182     like($foo, qr/^\0{50}x\0{24}z\0{24}y$/);
183     is(tell(F), 76);
184
185     # Seeking negative should not do funny business.
186
187     ok(!seek(F,  -50, SEEK_SET), $!);
188     ok(seek(F, 0, SEEK_SET));
189     ok(!seek(F,  -50, SEEK_CUR), $!);
190     ok(!seek(F, -150, SEEK_END), $!);
191 }
192