Upgrade to Tie::File 0.90, from mjd.
[p5sagit/p5-mst-13.2.git] / lib / Tie / File / t / 04_splice.t
1 #!/usr/bin/perl
2
3 #
4 # Check SPLICE function's effect on the file
5 # (07_rv_splice.t checks its return value)
6 #
7 # Each call to 'check_contents' actually performs two tests.
8 # First, it calls the tied object's own 'check_integrity' method,
9 # which makes sure that the contents of the read cache and offset tables
10 # accurately reflect the contents of the file.  
11 # Then, it checks the actual contents of the file against the expected
12 # contents.
13
14 my $file = "tf$$.txt";
15 $: = Tie::File::_default_recsep();
16 my $data = "rec0$:rec1$:rec2$:";
17 print "1..101\n";
18
19 init_file($data);
20
21 my $N = 1;
22 use Tie::File;
23 print "ok $N\n"; $N++;  # partial credit just for showing up
24
25 my $o = tie @a, 'Tie::File', $file;
26 print $o ? "ok $N\n" : "not ok $N\n";
27 $N++;
28
29 $: = $o->{recsep};
30 my $n;
31
32 # (3-22) splicing at the beginning
33 splice(@a, 0, 0, "rec4");
34 check_contents("rec4$:$data");
35 splice(@a, 0, 1, "rec5");       # same length
36 check_contents("rec5$:$data");
37 splice(@a, 0, 1, "record5");    # longer
38 check_contents("record5$:$data");
39
40 splice(@a, 0, 1, "r5");         # shorter
41 check_contents("r5$:$data");
42 splice(@a, 0, 1);               # removal
43 check_contents("$data");
44 splice(@a, 0, 0);               # no-op
45 check_contents("$data");
46 splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
47 check_contents("r7$:rec8$:$data");
48 splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
49 check_contents("rec7$:record8$:rec9$:$data");
50
51 splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
52 check_contents("record9$:rec10$:$data");
53 splice(@a, 0, 2);               # delete more than one
54 check_contents("$data");
55
56
57 # (23-42) splicing in the middle
58 splice(@a, 1, 0, "rec4");
59 check_contents("rec0$:rec4$:rec1$:rec2$:");
60 splice(@a, 1, 1, "rec5");       # same length
61 check_contents("rec0$:rec5$:rec1$:rec2$:");
62 splice(@a, 1, 1, "record5");    # longer
63 check_contents("rec0$:record5$:rec1$:rec2$:");
64
65 splice(@a, 1, 1, "r5");         # shorter
66 check_contents("rec0$:r5$:rec1$:rec2$:");
67 splice(@a, 1, 1);               # removal
68 check_contents("$data");
69 splice(@a, 1, 0);               # no-op
70 check_contents("$data");
71 splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
72 check_contents("rec0$:r7$:rec8$:rec1$:rec2$:");
73 splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
74 check_contents("rec0$:rec7$:record8$:rec9$:rec1$:rec2$:");
75
76 splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
77 check_contents("rec0$:record9$:rec10$:rec1$:rec2$:");
78 splice(@a, 1, 2);               # delete more than one
79 check_contents("$data");
80
81 # (43-62) splicing at the end
82 splice(@a, 3, 0, "rec4");
83 check_contents("$ {data}rec4$:");
84 splice(@a, 3, 1, "rec5");       # same length
85 check_contents("$ {data}rec5$:");
86 splice(@a, 3, 1, "record5");    # longer
87 check_contents("$ {data}record5$:");
88
89 splice(@a, 3, 1, "r5");         # shorter
90 check_contents("$ {data}r5$:");
91 splice(@a, 3, 1);               # removal
92 check_contents("$data");
93 splice(@a, 3, 0);               # no-op
94 check_contents("$data");
95 splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
96 check_contents("$ {data}r7$:rec8$:");
97 splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
98 check_contents("$ {data}rec7$:record8$:rec9$:");
99
100 splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
101 check_contents("$ {data}record9$:rec10$:");
102 splice(@a, 3, 2);               # delete more than one
103 check_contents("$data");
104
105 # (63-82) splicing with negative subscript
106 splice(@a, -1, 0, "rec4");
107 check_contents("rec0$:rec1$:rec4$:rec2$:");
108 splice(@a, -1, 1, "rec5");       # same length
109 check_contents("rec0$:rec1$:rec4$:rec5$:");
110 splice(@a, -1, 1, "record5");    # longer
111 check_contents("rec0$:rec1$:rec4$:record5$:");
112
113 splice(@a, -1, 1, "r5");         # shorter
114 check_contents("rec0$:rec1$:rec4$:r5$:");
115 splice(@a, -1, 1);               # removal
116 check_contents("rec0$:rec1$:rec4$:");
117 splice(@a, -1, 0);               # no-op  
118 check_contents("rec0$:rec1$:rec4$:");
119 splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
120 check_contents("rec0$:rec1$:r7$:rec8$:rec4$:");
121 splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
122 check_contents("rec0$:rec1$:r7$:rec8$:rec7$:record8$:rec9$:");
123
124 splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
125 check_contents("rec0$:rec1$:r7$:rec8$:record9$:rec10$:");
126 splice(@a, -4, 3);               # delete more than one
127 check_contents("rec0$:rec1$:rec10$:");
128
129 # (83-84) scrub it all out
130 splice(@a, 0, 3);
131 check_contents("");
132
133 # (85-86) put some back in
134 splice(@a, 0, 0, "rec0", "rec1");
135 check_contents("rec0$:rec1$:");
136
137 # (87-88) what if we remove too many records?
138 splice(@a, 0, 17);
139 check_contents("");
140
141 # (89-92) In the past, splicing past the end was not correctly detected
142 # (1.14)
143 splice(@a, 89, 3);
144 check_contents("");
145 splice(@a, @a, 3);
146 check_contents("");
147
148 # (93-96) Also we did not emulate splice's freaky behavior when inserting
149 # past the end of the array (1.14)
150 splice(@a, 89, 0, "I", "like", "pie");
151 check_contents("I$:like$:pie$:");
152 splice(@a, 89, 0, "pie pie pie");
153 check_contents("I$:like$:pie$:pie pie pie$:");
154
155 # (97) Splicing with too large a negative number should be fatal
156 # This test ignored because it causes 5.6.1 and 5.7.2 to dump core
157 # NOT MY FAULT
158 if ($] < 5.006 || $] > 5.007003) {
159   eval { splice(@a, -7, 0) };
160   print $@ =~ /^Modification of non-creatable array value attempted, subscript -7/
161       ? "ok $N\n" : "not ok $N \# \$\@ was '$@'\n";
162 } else { 
163   print "ok $N \# skipped (5.6.0 through 5.7.3 dump core here.)\n";
164 }
165 $N++;
166        
167 # (98-101) Test default arguments
168 splice @a, 0, 0, (0..11);
169 splice @a, 4;
170 check_contents("0$:1$:2$:3$:");
171 splice @a;
172 check_contents("");
173     
174
175 sub init_file {
176   my $data = shift;
177   open F, "> $file" or die $!;
178   binmode F;
179   print F $data;
180   close F;
181 }
182
183 use POSIX 'SEEK_SET';
184 sub check_contents {
185   my $x = shift;
186   my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
187   local *FH = $o->{fh};
188   seek FH, 0, SEEK_SET;
189   print $integrity ? "ok $N\n" : "not ok $N\n";
190   $N++;
191   my $a;
192   { local $/; $a = <FH> }
193   $a = "" unless defined $a;
194   if ($a eq $x) {
195     print "ok $N\n";
196   } else {
197     ctrlfix($a, $x);
198     print "not ok $N\n# expected <$x>, got <$a>\n";
199   }
200   $N++;
201 }
202
203
204 sub ctrlfix {
205   for (@_) {
206     s/\n/\\n/g;
207     s/\r/\\r/g;
208   }
209 }
210
211 END {
212   undef $o;
213   untie @a;
214   1 while unlink $file;
215 }
216