4 @INC = '../lib' if -d '../lib' ;
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
15 use vars qw($dbh $Dfile $bad_ones $FA) ;
17 # full tied array support started in Perl 5.004_57
18 # Double check to see if it is available.
21 sub try::TIEARRAY { bless [], "try" }
22 sub try::FETCHSIZE { $FA = 1 }
35 print "not " unless $result ;
43 print STDERR <<EOM unless $bad_ones++ ;
45 # Some older versions of Berkeley DB will fail tests 51, 53 and 55.
47 # You can safely ignore the errors if you're never going to use the
48 # broken functionality (recno databases with a modified bval).
49 # Otherwise you'll have to upgrade your DB library.
51 # If you want to upgrade Berkeley DB, the most recent version is 1.85.
52 # Check out http://www.bostic.com/db for more details.
59 my $Dfile = "recno.tmp";
64 # Check the interface to RECNOINFO
66 my $dbh = new DB_File::RECNOINFO ;
67 ok(1, ! defined $dbh->{bval}) ;
68 ok(2, ! defined $dbh->{cachesize}) ;
69 ok(3, ! defined $dbh->{psize}) ;
70 ok(4, ! defined $dbh->{flags}) ;
71 ok(5, ! defined $dbh->{lorder}) ;
72 ok(6, ! defined $dbh->{reclen}) ;
73 ok(7, ! defined $dbh->{bfname}) ;
76 ok(8, $dbh->{bval} == 3000 );
78 $dbh->{cachesize} = 9000 ;
79 ok(9, $dbh->{cachesize} == 9000 );
82 ok(10, $dbh->{psize} == 400 );
85 ok(11, $dbh->{flags} == 65 );
87 $dbh->{lorder} = 123 ;
88 ok(12, $dbh->{lorder} == 123 );
90 $dbh->{reclen} = 1234 ;
91 ok(13, $dbh->{reclen} == 1234 );
93 $dbh->{bfname} = 1234 ;
94 ok(14, $dbh->{bfname} == 1234 );
97 # Check that an invalid entry is caught both for store & fetch
98 eval '$dbh->{fred} = 1234' ;
99 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
100 eval 'my $q = $dbh->{fred}' ;
101 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
103 # Now check the interface to RECNOINFO
107 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
109 ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
110 || $^O eq 'MSWin32' || $^O eq 'amigaos') ;
114 ok(19, ($FA ? @h == 0 : !$l) );
116 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
118 $h[0] = shift @data ;
119 ok(20, $h[0] eq 'a' );
125 unshift (@data, 'a') ;
127 ok(21, defined $h[1] );
128 ok(22, ! defined $h[16] );
129 ok(23, $FA ? @h == @data : $X->length == @data );
132 # Overwrite an entry & check fetch it
134 $data[3] = 'replaced' ;
135 ok(24, $h[3] eq 'replaced' );
138 my @push_data = qw(added to the end) ;
139 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
140 push (@data, @push_data) ;
141 ok(25, $h[++$i] eq 'added' );
142 ok(26, $h[++$i] eq 'to' );
143 ok(27, $h[++$i] eq 'the' );
144 ok(28, $h[++$i] eq 'end' );
147 my $popped = pop (@data) ;
148 my $value = ($FA ? pop @h : $X->pop) ;
149 ok(29, $value eq $popped) ;
152 $value = ($FA ? shift @h : $X->shift) ;
153 my $shifted = shift @data ;
154 ok(30, $value eq $shifted );
159 ($FA ? unshift @h : $X->unshift) ;
160 ok(31, ($FA ? @h == @data : $X->length == @data ));
162 my @new_data = qw(add this to the start of the array) ;
163 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
164 unshift (@data, @new_data) ;
165 ok(32, $FA ? @h == @data : $X->length == @data );
166 ok(33, $h[0] eq "add") ;
167 ok(34, $h[1] eq "this") ;
168 ok(35, $h[2] eq "to") ;
169 ok(36, $h[3] eq "the") ;
170 ok(37, $h[4] eq "start") ;
171 ok(38, $h[5] eq "of") ;
172 ok(39, $h[6] eq "the") ;
173 ok(40, $h[7] eq "array") ;
174 ok(41, $h[8] eq $data[8]) ;
178 # Now both arrays should be identical
184 $ok = 0, last if $_ ne $h[$j ++] ;
188 # Neagtive subscripts
190 # get the last element of the array
191 ok(43, $h[-1] eq $data[-1] );
192 ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
194 # get the first element using a negative subscript
195 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
197 ok(46, $h[0] eq "abcd" );
199 # now try to read before the start of the array
200 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
201 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
203 # IMPORTANT - $X must be undefined before the untie otherwise the
204 # underlying DB close routine will not get called.
214 open(CAT,$file) || die "Cannot open $file:$!";
222 # Check bval defaults to \n
225 my $dbh = new DB_File::RECNOINFO ;
226 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
231 my $x = docat($Dfile) ;
233 ok(49, $x eq "abc\ndef\n\nghi\n") ;
240 my $dbh = new DB_File::RECNOINFO ;
242 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
247 my $x = docat($Dfile) ;
249 my $ok = ($x eq "abc-def--ghi-") ;
250 bad_one() unless $ok ;
255 # Check R_FIXEDLEN with default bval (space)
258 my $dbh = new DB_File::RECNOINFO ;
259 $dbh->{flags} = R_FIXEDLEN ;
261 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
266 my $x = docat($Dfile) ;
268 my $ok = ($x eq "abc def ghi ") ;
269 bad_one() unless $ok ;
274 # Check R_FIXEDLEN with user-defined bval
277 my $dbh = new DB_File::RECNOINFO ;
278 $dbh->{flags} = R_FIXEDLEN ;
281 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
286 my $x = docat($Dfile) ;
288 my $ok = ($x eq "abc--def-------ghi--") ;
289 bad_one() unless $ok ;
294 # check that attempting to tie an associative array to a DB_RECNO will fail
296 my $filename = "xyz" ;
298 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
299 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
310 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
316 use vars qw( @ISA @EXPORT) ;
321 @EXPORT = @DB_File::EXPORT ;
327 $self->SUPER::STORE($key, $value * 2) ;
333 $self->SUPER::FETCH($key) - 1 ;
340 $self->SUPER::put($key, $value * 3) ;
345 $self->SUPER::get($_[0], $_[1]) ;
353 my $value = $self->FETCH($key) ;
354 return "[[$value]]" ;
362 BEGIN { push @INC, '.'; }
364 main::ok(57, $@ eq "") ;
368 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
371 main::ok(58, $@ eq "") ;
373 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
374 main::ok(59, $@ eq "") ;
375 main::ok(60, $ret == 5) ;
378 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
379 main::ok(61, $@ eq "") ;
380 main::ok(62, $ret == 10) ;
382 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
383 main::ok(63, $@ eq "" ) ;
384 main::ok(64, $ret == 1) ;
386 $ret = eval '$X->A_new_method(1) ' ;
387 main::ok(65, $@ eq "") ;
388 main::ok(66, $ret eq "[[11]]") ;
392 unlink "SubDB.pm", "recno.tmp" ;
401 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
406 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
409 my $x = docat($Dfile) ;
410 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
412 # $# sets array to same length
413 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
417 { $self->STORESIZE(4) }
418 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
422 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
424 # $# sets array to bigger
425 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
429 { $self->STORESIZE(7) }
430 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
434 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
436 # $# sets array smaller
437 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
441 { $self->STORESIZE(3) }
442 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
446 ok(78, $x eq "abc\ndef\nghi\n") ;