doc f7abe7
[p5sagit/p5-mst-13.2.git] / cpan / Tie-File / t / 27_iwrite.t
1 #!/usr/bin/perl
2 #
3 # Unit tests of _iwrite function
4 #
5 # _iwrite($self, $data, $start, $end)
6 #
7 # 'i' here is for 'insert'.  This writes $data at absolute position $start
8 # in the file, copying the data at that position downwards---
9 # but only down to position $end.  Data at or past $end is not moved
10 # or even examined.    Since there isn't enough room for the full copy
11 # (Because we inserted $data at the beginning) we copy as much as possible
12 # and return a string containing the remainder.
13
14 my $file = "tf$$.txt";
15 $| = 1;
16
17 print "1..203\n";
18
19 my $N = 1;
20 my $oldfile;
21 use Tie::File;
22 print "ok $N\n"; $N++;
23
24 $: = Tie::File::_default_recsep();
25
26 $FLEN = 40970;   # Use files of this length
27 $oldfile = mkrand($FLEN);
28 print "# MOF tests\n";
29 # (2-85) These were generated by 'gentests.pl' to cover all possible cases
30 # (I hope)
31 # Legend:
32 #         x: data is entirely contained within one block
33 #        x>: data runs from the middle to the end of the block
34 #        <x: data runs from the start to the middle of the block
35 #       <x>: data occupies precisely one block
36 #      x><x: data overlaps one block boundary
37 #     <x><x: data runs from the start of one block into the middle of the next
38 #     x><x>: data runs from the middle of one block to the end of the next
39 #    <x><x>: data occupies two blocks exactly
40 # <x><x><x>: data occupies three blocks exactly
41 #         0: data is null
42 #
43 # For each possible alignment of the old and new data, we investigate
44 # up to three situations: old data is shorter, old and new data are the
45 # same length, and new data is shorter.
46 #
47 # try($pos, $old, $new) means to run a test where the area being
48 # written into starts at position $pos, the area being written into
49 # has length $old, and and the new data has length $new.
50 try( 8605,  2394,  2394);  # old=x        , new=x        ; old = new
51 try( 9768,  1361,   664);  # old=x        , new=x        ; old > new
52 try( 9955,  6429,  6429);  # old=x>       , new=x        ; old = new
53 try(10550,  5834,  4123);  # old=x>       , new=x        ; old > new
54 try(14580,  6158,   851);  # old=x><x     , new=x        ; old > new
55 try(13442, 11134,  1572);  # old=x><x>    , new=x        ; old > new
56 try( 8192,   514,   514);  # old=<x       , new=<x       ; old = new
57 try( 8192,  2196,   858);  # old=<x       , new=<x       ; old > new
58 try( 8192,  8192,  8192);  # old=<x>      , new=<x       ; old = new
59 try( 8192,  8192,  1290);  # old=<x>      , new=<x       ; old > new
60 try( 8192, 10575,  6644);  # old=<x><x    , new=<x       ; old > new
61 try( 8192, 16384,  5616);  # old=<x><x>   , new=<x       ; old > new
62 try( 8192, 24576,  6253);  # old=<x><x><x>, new=<x       ; old > new
63 try( 9965,  6419,  6419);  # old=x>       , new=x>       ; old = new
64 try(16059,  6102,   325);  # old=x><x     , new=x>       ; old > new
65 try( 9503, 15073,  6881);  # old=x><x>    , new=x>       ; old > new
66 try(16316,  1605,  1605);  # old=x><x     , new=x><x     ; old = new
67 try(16093,  4074,   993);  # old=x><x     , new=x><x     ; old > new
68 try(14739,  9837,  9837);  # old=x><x>    , new=x><x     ; old = new
69 try(14071, 10505,  7344);  # old=x><x>    , new=x><x     ; old > new
70 try( 8192,  8192,  8192);  # old=<x>      , new=<x>      ; old = new
71 try( 8192, 14817,  8192);  # old=<x><x    , new=<x>      ; old > new
72 try( 8192, 16384,  8192);  # old=<x><x>   , new=<x>      ; old > new
73 try( 8192, 24576,  8192);  # old=<x><x><x>, new=<x>      ; old > new
74 try( 8192,  9001,  9001);  # old=<x><x    , new=<x><x    ; old = new
75 try( 8192, 11760, 10274);  # old=<x><x    , new=<x><x    ; old > new
76 try( 8192, 16384, 10781);  # old=<x><x>   , new=<x><x    ; old > new
77 try( 8192, 24576,  9284);  # old=<x><x><x>, new=<x><x    ; old > new
78 try(14761,  9815,  9815);  # old=x><x>    , new=x><x>    ; old = new
79 try( 8192, 16384, 16384);  # old=<x><x>   , new=<x><x>   ; old = new
80 try( 8192, 24576, 16384);  # old=<x><x><x>, new=<x><x>   ; old > new
81 try( 8192, 24576, 24576);  # old=<x><x><x>, new=<x><x><x>; old = new
82 try( 8771,   776,     0);  # old=x        , new=0        ; old > new
83 try( 8192,  2813,     0);  # old=<x       , new=0        ; old > new
84 try(13945,  2439,     0);  # old=x>       , new=0        ; old > new
85 try(14493,  6090,     0);  # old=x><x     , new=0        ; old > new
86 try( 8192,  8192,     0);  # old=<x>      , new=0        ; old > new
87 try( 8192, 10030,     0);  # old=<x><x    , new=0        ; old > new
88 try(14983,  9593,     0);  # old=x><x>    , new=0        ; old > new
89 try( 8192, 16384,     0);  # old=<x><x>   , new=0        ; old > new
90 try( 8192, 24576,     0);  # old=<x><x><x>, new=0        ; old > new
91 try(10489,     0,     0);  # old=0        , new=0        ; old = new
92
93 print "# SOF tests\n";
94 # (86-133)
95 # These tests all take place at the start of the file
96 try(    0,  4868,  4868);  # old=<x       , new=<x       ; old = new
97 try(    0,   147,   118);  # old=<x       , new=<x       ; old > new
98 try(    0,  8192,  8192);  # old=<x>      , new=<x       ; old = new
99 try(    0,  8192,  4574);  # old=<x>      , new=<x       ; old > new
100 try(    0, 11891,  1917);  # old=<x><x    , new=<x       ; old > new
101 try(    0, 16384,  5155);  # old=<x><x>   , new=<x       ; old > new
102 try(    0, 24576,  2953);  # old=<x><x><x>, new=<x       ; old > new
103 try(    0,  8192,  8192);  # old=<x>      , new=<x>      ; old = new
104 try(    0, 11083,  8192);  # old=<x><x    , new=<x>      ; old > new
105 try(    0, 16384,  8192);  # old=<x><x>   , new=<x>      ; old > new
106 try(    0, 24576,  8192);  # old=<x><x><x>, new=<x>      ; old > new
107 try(    0, 14126, 14126);  # old=<x><x    , new=<x><x    ; old = new
108 try(    0, 12002,  9034);  # old=<x><x    , new=<x><x    ; old > new
109 try(    0, 16384, 13258);  # old=<x><x>   , new=<x><x    ; old > new
110 try(    0, 24576, 14367);  # old=<x><x><x>, new=<x><x    ; old > new
111 try(    0, 16384, 16384);  # old=<x><x>   , new=<x><x>   ; old = new
112 try(    0, 24576, 16384);  # old=<x><x><x>, new=<x><x>   ; old > new
113 try(    0, 24576, 24576);  # old=<x><x><x>, new=<x><x><x>; old = new
114 try(    0,  6530,     0);  # old=<x       , new=0        ; old > new
115 try(    0,  8192,     0);  # old=<x>      , new=0        ; old > new
116 try(    0, 14707,     0);  # old=<x><x    , new=0        ; old > new
117 try(    0, 16384,     0);  # old=<x><x>   , new=0        ; old > new
118 try(    0, 24576,     0);  # old=<x><x><x>, new=0        ; old > new
119 try(    0,     0,     0);  # old=0        , new=0        ; old = new
120
121 print "# EOF tests 1\n";
122 # (134-169)
123 # These tests all take place at the end of the file
124 $FLEN = 40960;  # Force the file to be exactly 40960 bytes long
125 $oldfile = mkrand($FLEN);
126 try(32768,  8192,  8192);  # old=<x>      , new=<x       ; old = new
127 try(32768,  8192,  4026);  # old=<x>      , new=<x       ; old > new
128 try(24576, 16384,  1917);  # old=<x><x>   , new=<x       ; old > new
129 try(16384, 24576,  3818);  # old=<x><x><x>, new=<x       ; old > new
130 try(32768,  8192,  8192);  # old=<x>      , new=<x>      ; old = new
131 try(24576, 16384,  8192);  # old=<x><x>   , new=<x>      ; old > new
132 try(16384, 24576,  8192);  # old=<x><x><x>, new=<x>      ; old > new
133 try(24576, 16384, 12221);  # old=<x><x>   , new=<x><x    ; old > new
134 try(16384, 24576, 15030);  # old=<x><x><x>, new=<x><x    ; old > new
135 try(24576, 16384, 16384);  # old=<x><x>   , new=<x><x>   ; old = new
136 try(16384, 24576, 16384);  # old=<x><x><x>, new=<x><x>   ; old > new
137 try(16384, 24576, 24576);  # old=<x><x><x>, new=<x><x><x>; old = new
138 try(35973,  4987,     0);  # old=x>       , new=0        ; old > new
139 try(32768,  8192,     0);  # old=<x>      , new=0        ; old > new
140 try(29932, 11028,     0);  # old=x><x>    , new=0        ; old > new
141 try(24576, 16384,     0);  # old=<x><x>   , new=0        ; old > new
142 try(16384, 24576,     0);  # old=<x><x><x>, new=0        ; old > new
143 try(40960,     0,     0);  # old=0        , new=0        ; old = new
144
145 print "# EOF tests 2\n";
146 # (170-203)
147 # These tests all take place at the end of the file
148 $FLEN = 42000;  # Force the file to be exactly 42000 bytes long
149 $oldfile = mkrand($FLEN);
150 try(41683,   317,   317);  # old=x        , new=x        ; old = new
151 try(41225,   775,   405);  # old=x        , new=x        ; old > new
152 try(35709,  6291,   284);  # old=x><x     , new=x        ; old > new
153 try(40960,  1040,  1040);  # old=<x       , new=<x       ; old = new
154 try(40960,  1040,   378);  # old=<x       , new=<x       ; old > new
155 try(32768,  9232,  5604);  # old=<x><x    , new=<x       ; old > new
156 try(39994,  2006,   966);  # old=x><x     , new=x>       ; old > new
157 try(36725,  5275,  5275);  # old=x><x     , new=x><x     ; old = new
158 try(37990,  4010,  3199);  # old=x><x     , new=x><x     ; old > new
159 try(32768,  9232,  8192);  # old=<x><x    , new=<x>      ; old > new
160 try(32768,  9232,  9232);  # old=<x><x    , new=<x><x    ; old = new
161 try(32768,  9232,  8795);  # old=<x><x    , new=<x><x    ; old > new
162 try(41500,   500,     0);  # old=x        , new=0        ; old > new
163 try(40960,  1040,     0);  # old=<x       , new=0        ; old > new
164 try(35272,  6728,     0);  # old=x><x     , new=0        ; old > new
165 try(32768,  9232,     0);  # old=<x><x    , new=0        ; old > new
166 try(42000,     0,     0);  # old=0        , new=0        ; old = new
167
168 sub mkrand {
169   my $len = shift;
170   srand $len;
171   my @c = ('a' .. 'z', 'A' .. 'Z', 0..9, $:);
172   my $d = "";
173   $d .= $c[rand @c] until length($d) >= $len;
174   substr($d, $len) = ""; # chop it off to the proper length
175   $d;
176 }
177
178 sub try {
179   my ($s, $len, $newlen) = @_;
180   my $e = $s + $len;
181
182   open F, "> $file" or die "Couldn't open file $file: $!";
183   binmode F;
184
185   print F $oldfile;
186   close F;
187
188   die "wrong length!" unless -s $file == $FLEN;
189
190   my $newdata = "-" x $newlen;
191   my $expected = $oldfile;
192
193   my $expected_return = substr($expected, $e - $newlen, $newlen, "");
194   substr($expected, $s, 0, $newdata);
195
196   my $o = tie my @lines, 'Tie::File', $file or die $!;
197   my $actual_return = $o->_iwrite($newdata, $s, $e);
198   undef $o; untie @lines;
199
200   open F, "< $file" or die "Couldn't open file $file: $!";
201   binmode F;
202   my $actual;
203   { local $/;
204     $actual = <F>;
205   }
206   close F;
207
208   my ($alen, $xlen) = (length $actual, length $expected);
209   unless ($alen == $xlen) {
210     print "# try(@_) expected file length $xlen, actual $alen!\n";
211   }
212   print $actual eq $expected ? "ok $N\n" : "not ok $N\n";
213   $N++;
214
215   if (! defined $actual_return && ! defined $expected_return) {
216     print "ok $N\n";
217   } elsif (! defined $actual_return || ! defined $expected_return) {
218     print "not ok $N\n";
219   } else {
220     print $actual_return eq $expected_return ? "ok $N\n" : "not ok $N\n";
221   }
222   $N++;
223 }
224
225 sub ctrlfix {
226   for (@_) {
227     s/\n/\\n/g;
228     s/\r/\\r/g;
229   }
230 }
231
232 END {
233   1 while unlink $file;
234 }
235