Fix thinko in db-recno.t
[p5sagit/p5-mst-13.2.git] / t / lib / db-recno.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
3BEGIN {
55497cff 4 @INC = '../lib' if -d '../lib' ;
a0d0e21e 5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0\n";
8 exit 0;
9 }
10}
11
12use DB_File;
13use Fcntl;
55d68b4a 14use strict ;
25268f15 15use vars qw($dbh $Dfile $bad_ones) ;
a0d0e21e 16
55d68b4a 17sub ok
18{
19 my $no = shift ;
20 my $result = shift ;
a0d0e21e 21
55d68b4a 22 print "not " unless $result ;
23 print "ok $no\n" ;
6250ba0a 24
25 return $result ;
26}
27
28sub bad_one
29{
25268f15 30 print STDERR <<EOM unless $bad_ones++ ;
31#
6250ba0a 32# Some older versions of Berkeley DB will fail tests 51, 53 and 55.
33#
34# You can safely ignore the errors if you're never going to use the
35# broken functionality (recno databases with a modified bval).
36# Otherwise you'll have to upgrade your DB library.
37#
38# If you want to upgrade Berkeley DB, the most recent version is 1.85.
39# Check out http://www.bostic.com/db for more details.
40#
41EOM
55d68b4a 42}
43
36477c24 44print "1..55\n";
55d68b4a 45
46my $Dfile = "recno.tmp";
47unlink $Dfile ;
a0d0e21e 48
49umask(0);
50
51# Check the interface to RECNOINFO
52
55d68b4a 53my $dbh = new DB_File::RECNOINFO ;
55497cff 54ok(1, $dbh->{bval} == 0 ) ;
55ok(2, $dbh->{cachesize} == 0) ;
56ok(3, $dbh->{psize} == 0) ;
57ok(4, $dbh->{flags} == 0) ;
58ok(5, $dbh->{lorder} == 0);
59ok(6, $dbh->{reclen} == 0);
60ok(7, $dbh->{bfname} eq "");
a0d0e21e 61
62$dbh->{bval} = 3000 ;
f6b705ef 63ok(8, $dbh->{bval} == 3000 );
a0d0e21e 64
65$dbh->{cachesize} = 9000 ;
f6b705ef 66ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 67
68$dbh->{psize} = 400 ;
f6b705ef 69ok(10, $dbh->{psize} == 400 );
a0d0e21e 70
71$dbh->{flags} = 65 ;
f6b705ef 72ok(11, $dbh->{flags} == 65 );
a0d0e21e 73
74$dbh->{lorder} = 123 ;
f6b705ef 75ok(12, $dbh->{lorder} == 123 );
a0d0e21e 76
77$dbh->{reclen} = 1234 ;
f6b705ef 78ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 79
80$dbh->{bfname} = 1234 ;
f6b705ef 81ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 82
83
84# Check that an invalid entry is caught both for store & fetch
85eval '$dbh->{fred} = 1234' ;
f6b705ef 86ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 87eval 'my $q = $dbh->{fred}' ;
f6b705ef 88ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 89
90# Now check the interface to RECNOINFO
91
55d68b4a 92my $X ;
93my @h ;
94ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 95
053b5721 96ok(18, ( (stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)) ;
a0d0e21e 97
55d68b4a 98#my $l = @h ;
99my $l = $X->length ;
f6b705ef 100ok(19, !$l );
a0d0e21e 101
55d68b4a 102my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 103
104$h[0] = shift @data ;
f6b705ef 105ok(20, $h[0] eq 'a' );
a0d0e21e 106
55d68b4a 107my $ i;
a0d0e21e 108foreach (@data)
109 { $h[++$i] = $_ }
110
111unshift (@data, 'a') ;
112
f6b705ef 113ok(21, defined $h[1] );
114ok(22, ! defined $h[16] );
115ok(23, $X->length == @data );
a0d0e21e 116
117
118# Overwrite an entry & check fetch it
119$h[3] = 'replaced' ;
120$data[3] = 'replaced' ;
f6b705ef 121ok(24, $h[3] eq 'replaced' );
a0d0e21e 122
123#PUSH
55d68b4a 124my @push_data = qw(added to the end) ;
f6b705ef 125#my push (@h, @push_data) ;
a0d0e21e 126$X->push(@push_data) ;
127push (@data, @push_data) ;
f6b705ef 128ok(25, $h[++$i] eq 'added' );
129ok(26, $h[++$i] eq 'to' );
130ok(27, $h[++$i] eq 'the' );
131ok(28, $h[++$i] eq 'end' );
a0d0e21e 132
133# POP
f6b705ef 134my $popped = pop (@data) ;
135#my $value = pop(@h) ;
55d68b4a 136my $value = $X->pop ;
f6b705ef 137ok(29, $value eq $popped) ;
a0d0e21e 138
139# SHIFT
140#$value = shift @h
141$value = $X->shift ;
f6b705ef 142my $shifted = shift @data ;
143ok(30, $value eq $shifted );
a0d0e21e 144
145# UNSHIFT
146
147# empty list
148$X->unshift ;
f6b705ef 149ok(31, $X->length == @data );
a0d0e21e 150
55d68b4a 151my @new_data = qw(add this to the start of the array) ;
a0d0e21e 152#unshift @h, @new_data ;
153$X->unshift (@new_data) ;
154unshift (@data, @new_data) ;
f6b705ef 155ok(32, $X->length == @data );
156ok(33, $h[0] eq "add") ;
157ok(34, $h[1] eq "this") ;
158ok(35, $h[2] eq "to") ;
159ok(36, $h[3] eq "the") ;
160ok(37, $h[4] eq "start") ;
161ok(38, $h[5] eq "of") ;
162ok(39, $h[6] eq "the") ;
163ok(40, $h[7] eq "array") ;
164ok(41, $h[8] eq $data[8]) ;
a0d0e21e 165
166# SPLICE
167
168# Now both arrays should be identical
169
55d68b4a 170my $ok = 1 ;
171my $j = 0 ;
a0d0e21e 172foreach (@data)
173{
174 $ok = 0, last if $_ ne $h[$j ++] ;
175}
f6b705ef 176ok(42, $ok );
a0d0e21e 177
55d68b4a 178# Neagtive subscripts
179
180# get the last element of the array
f6b705ef 181ok(43, $h[-1] eq $data[-1] );
182ok(44, $h[-1] eq $h[$X->length -1] );
55d68b4a 183
184# get the first element using a negative subscript
185eval '$h[ - ( $X->length)] = "abcd"' ;
f6b705ef 186ok(45, $@ eq "" );
187ok(46, $h[0] eq "abcd" );
55d68b4a 188
189# now try to read before the start of the array
190eval '$h[ - (1 + $X->length)] = 1234' ;
f6b705ef 191ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 192
a0d0e21e 193# IMPORTANT - $X must be undefined before the untie otherwise the
194# underlying DB close routine will not get called.
195undef $X ;
196untie(@h);
197
198unlink $Dfile;
199
36477c24 200{
201 # Check bval defaults to \n
202
203 my @h = () ;
204 my $dbh = new DB_File::RECNOINFO ;
205 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
206 $h[0] = "abc" ;
207 $h[1] = "def" ;
208 $h[3] = "ghi" ;
209 untie @h ;
210 my $x = `cat $Dfile` ;
36477c24 211 unlink $Dfile;
6250ba0a 212 ok(49, $x eq "abc\ndef\n\nghi\n") ;
36477c24 213}
214
215{
216 # Change bval
217
218 my @h = () ;
219 my $dbh = new DB_File::RECNOINFO ;
220 $dbh->{bval} = "-" ;
221 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
222 $h[0] = "abc" ;
223 $h[1] = "def" ;
224 $h[3] = "ghi" ;
225 untie @h ;
226 my $x = `cat $Dfile` ;
36477c24 227 unlink $Dfile;
6250ba0a 228 my $ok = ($x eq "abc-def--ghi-") ;
229 bad_one() unless $ok ;
230 ok(51, $ok) ;
36477c24 231}
232
233{
234 # Check R_FIXEDLEN with default bval (space)
235
236 my @h = () ;
237 my $dbh = new DB_File::RECNOINFO ;
238 $dbh->{flags} = R_FIXEDLEN ;
239 $dbh->{reclen} = 5 ;
240 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
241 $h[0] = "abc" ;
242 $h[1] = "def" ;
243 $h[3] = "ghi" ;
244 untie @h ;
245 my $x = `cat $Dfile` ;
36477c24 246 unlink $Dfile;
6250ba0a 247 my $ok = ($x eq "abc def ghi ") ;
248 bad_one() unless $ok ;
249 ok(53, $ok) ;
36477c24 250}
251
252{
253 # Check R_FIXEDLEN with user-defined bval
254
255 my @h = () ;
256 my $dbh = new DB_File::RECNOINFO ;
257 $dbh->{flags} = R_FIXEDLEN ;
258 $dbh->{bval} = "-" ;
259 $dbh->{reclen} = 5 ;
260 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
261 $h[0] = "abc" ;
262 $h[1] = "def" ;
263 $h[3] = "ghi" ;
264 untie @h ;
265 my $x = `cat $Dfile` ;
36477c24 266 unlink $Dfile;
6250ba0a 267 my $ok = ($x eq "abc--def-------ghi--") ;
268 bad_one() unless $ok ;
269 ok(55, $ok) ;
36477c24 270}
271
a0d0e21e 272exit ;