applied patch, and undid change#1302 which it made unnecessary
[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 ;
045291aa 15use vars qw($dbh $Dfile $bad_ones $FA) ;
16
17# full tied array support started in Perl 5.004_57
a9fd575d 18# Double check to see if it is available.
19
20{
21 sub try::TIEARRAY { bless [], "try" }
22 sub try::FETCHSIZE { $FA = 1 }
23 $FA = 0 ;
24 my @a ;
25 tie @a, 'try' ;
26 my $a = @a ;
27}
28
a0d0e21e 29
55d68b4a 30sub ok
31{
32 my $no = shift ;
33 my $result = shift ;
a0d0e21e 34
55d68b4a 35 print "not " unless $result ;
36 print "ok $no\n" ;
6250ba0a 37
38 return $result ;
39}
40
41sub bad_one
42{
25268f15 43 print STDERR <<EOM unless $bad_ones++ ;
44#
6250ba0a 45# Some older versions of Berkeley DB will fail tests 51, 53 and 55.
46#
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.
50#
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.
53#
54EOM
55d68b4a 55}
56
045291aa 57print "1..78\n";
55d68b4a 58
59my $Dfile = "recno.tmp";
60unlink $Dfile ;
a0d0e21e 61
62umask(0);
63
64# Check the interface to RECNOINFO
65
55d68b4a 66my $dbh = new DB_File::RECNOINFO ;
3fe9a6f1 67ok(1, ! defined $dbh->{bval}) ;
68ok(2, ! defined $dbh->{cachesize}) ;
69ok(3, ! defined $dbh->{psize}) ;
70ok(4, ! defined $dbh->{flags}) ;
71ok(5, ! defined $dbh->{lorder}) ;
72ok(6, ! defined $dbh->{reclen}) ;
73ok(7, ! defined $dbh->{bfname}) ;
a0d0e21e 74
75$dbh->{bval} = 3000 ;
f6b705ef 76ok(8, $dbh->{bval} == 3000 );
a0d0e21e 77
78$dbh->{cachesize} = 9000 ;
f6b705ef 79ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 80
81$dbh->{psize} = 400 ;
f6b705ef 82ok(10, $dbh->{psize} == 400 );
a0d0e21e 83
84$dbh->{flags} = 65 ;
f6b705ef 85ok(11, $dbh->{flags} == 65 );
a0d0e21e 86
87$dbh->{lorder} = 123 ;
f6b705ef 88ok(12, $dbh->{lorder} == 123 );
a0d0e21e 89
90$dbh->{reclen} = 1234 ;
f6b705ef 91ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 92
93$dbh->{bfname} = 1234 ;
f6b705ef 94ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 95
96
97# Check that an invalid entry is caught both for store & fetch
98eval '$dbh->{fred} = 1234' ;
f6b705ef 99ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 100eval 'my $q = $dbh->{fred}' ;
f6b705ef 101ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 102
103# Now check the interface to RECNOINFO
104
55d68b4a 105my $X ;
106my @h ;
107ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 108
a9fd575d 109ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
110 || $^O eq 'MSWin32' || $^O eq 'amigaos') ;
a0d0e21e 111
55d68b4a 112#my $l = @h ;
113my $l = $X->length ;
045291aa 114ok(19, ($FA ? @h == 0 : !$l) );
a0d0e21e 115
55d68b4a 116my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 117
118$h[0] = shift @data ;
f6b705ef 119ok(20, $h[0] eq 'a' );
a0d0e21e 120
55d68b4a 121my $ i;
a0d0e21e 122foreach (@data)
123 { $h[++$i] = $_ }
124
125unshift (@data, 'a') ;
126
f6b705ef 127ok(21, defined $h[1] );
128ok(22, ! defined $h[16] );
045291aa 129ok(23, $FA ? @h == @data : $X->length == @data );
a0d0e21e 130
131
132# Overwrite an entry & check fetch it
133$h[3] = 'replaced' ;
134$data[3] = 'replaced' ;
f6b705ef 135ok(24, $h[3] eq 'replaced' );
a0d0e21e 136
137#PUSH
55d68b4a 138my @push_data = qw(added to the end) ;
045291aa 139($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
a0d0e21e 140push (@data, @push_data) ;
f6b705ef 141ok(25, $h[++$i] eq 'added' );
142ok(26, $h[++$i] eq 'to' );
143ok(27, $h[++$i] eq 'the' );
144ok(28, $h[++$i] eq 'end' );
a0d0e21e 145
146# POP
f6b705ef 147my $popped = pop (@data) ;
045291aa 148my $value = ($FA ? pop @h : $X->pop) ;
f6b705ef 149ok(29, $value eq $popped) ;
a0d0e21e 150
151# SHIFT
045291aa 152$value = ($FA ? shift @h : $X->shift) ;
f6b705ef 153my $shifted = shift @data ;
154ok(30, $value eq $shifted );
a0d0e21e 155
156# UNSHIFT
157
158# empty list
045291aa 159($FA ? unshift @h : $X->unshift) ;
160ok(31, ($FA ? @h == @data : $X->length == @data ));
a0d0e21e 161
55d68b4a 162my @new_data = qw(add this to the start of the array) ;
045291aa 163$FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
a0d0e21e 164unshift (@data, @new_data) ;
045291aa 165ok(32, $FA ? @h == @data : $X->length == @data );
f6b705ef 166ok(33, $h[0] eq "add") ;
167ok(34, $h[1] eq "this") ;
168ok(35, $h[2] eq "to") ;
169ok(36, $h[3] eq "the") ;
170ok(37, $h[4] eq "start") ;
171ok(38, $h[5] eq "of") ;
172ok(39, $h[6] eq "the") ;
173ok(40, $h[7] eq "array") ;
174ok(41, $h[8] eq $data[8]) ;
a0d0e21e 175
176# SPLICE
177
178# Now both arrays should be identical
179
55d68b4a 180my $ok = 1 ;
181my $j = 0 ;
a0d0e21e 182foreach (@data)
183{
184 $ok = 0, last if $_ ne $h[$j ++] ;
185}
f6b705ef 186ok(42, $ok );
a0d0e21e 187
55d68b4a 188# Neagtive subscripts
189
190# get the last element of the array
f6b705ef 191ok(43, $h[-1] eq $data[-1] );
045291aa 192ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
55d68b4a 193
194# get the first element using a negative subscript
045291aa 195eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
f6b705ef 196ok(45, $@ eq "" );
197ok(46, $h[0] eq "abcd" );
55d68b4a 198
199# now try to read before the start of the array
045291aa 200eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
f6b705ef 201ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 202
a0d0e21e 203# IMPORTANT - $X must be undefined before the untie otherwise the
204# underlying DB close routine will not get called.
205undef $X ;
206untie(@h);
207
208unlink $Dfile;
209
a6ed719b 210sub docat
211{
212 my $file = shift;
213 local $/ = undef;
214 open(CAT,$file) || die "Cannot open $file:$!";
215 my $result = <CAT>;
216 close(CAT);
217 return $result;
218}
219
220
36477c24 221{
222 # Check bval defaults to \n
223
224 my @h = () ;
225 my $dbh = new DB_File::RECNOINFO ;
226 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
227 $h[0] = "abc" ;
228 $h[1] = "def" ;
229 $h[3] = "ghi" ;
230 untie @h ;
a6ed719b 231 my $x = docat($Dfile) ;
36477c24 232 unlink $Dfile;
6250ba0a 233 ok(49, $x eq "abc\ndef\n\nghi\n") ;
36477c24 234}
235
236{
237 # Change bval
238
239 my @h = () ;
240 my $dbh = new DB_File::RECNOINFO ;
241 $dbh->{bval} = "-" ;
242 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
243 $h[0] = "abc" ;
244 $h[1] = "def" ;
245 $h[3] = "ghi" ;
246 untie @h ;
a6ed719b 247 my $x = docat($Dfile) ;
36477c24 248 unlink $Dfile;
6250ba0a 249 my $ok = ($x eq "abc-def--ghi-") ;
250 bad_one() unless $ok ;
251 ok(51, $ok) ;
36477c24 252}
253
254{
255 # Check R_FIXEDLEN with default bval (space)
256
257 my @h = () ;
258 my $dbh = new DB_File::RECNOINFO ;
259 $dbh->{flags} = R_FIXEDLEN ;
260 $dbh->{reclen} = 5 ;
261 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
262 $h[0] = "abc" ;
263 $h[1] = "def" ;
264 $h[3] = "ghi" ;
265 untie @h ;
a6ed719b 266 my $x = docat($Dfile) ;
36477c24 267 unlink $Dfile;
6250ba0a 268 my $ok = ($x eq "abc def ghi ") ;
269 bad_one() unless $ok ;
270 ok(53, $ok) ;
36477c24 271}
272
273{
274 # Check R_FIXEDLEN with user-defined bval
275
276 my @h = () ;
277 my $dbh = new DB_File::RECNOINFO ;
278 $dbh->{flags} = R_FIXEDLEN ;
279 $dbh->{bval} = "-" ;
280 $dbh->{reclen} = 5 ;
281 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
282 $h[0] = "abc" ;
283 $h[1] = "def" ;
284 $h[3] = "ghi" ;
285 untie @h ;
a6ed719b 286 my $x = docat($Dfile) ;
36477c24 287 unlink $Dfile;
6250ba0a 288 my $ok = ($x eq "abc--def-------ghi--") ;
289 bad_one() unless $ok ;
290 ok(55, $ok) ;
36477c24 291}
292
05475680 293{
294 # check that attempting to tie an associative array to a DB_RECNO will fail
295
296 my $filename = "xyz" ;
297 my %x ;
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/) ;
300 unlink $filename ;
301}
302
a6ed719b 303{
304 # sub-class test
305
306 package Another ;
307
308 use strict ;
309
310 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
311 print FILE <<'EOM' ;
312
313 package SubDB ;
314
315 use strict ;
316 use vars qw( @ISA @EXPORT) ;
317
318 require Exporter ;
319 use DB_File;
320 @ISA=qw(DB_File);
321 @EXPORT = @DB_File::EXPORT ;
322
323 sub STORE {
324 my $self = shift ;
325 my $key = shift ;
326 my $value = shift ;
327 $self->SUPER::STORE($key, $value * 2) ;
328 }
329
330 sub FETCH {
331 my $self = shift ;
332 my $key = shift ;
333 $self->SUPER::FETCH($key) - 1 ;
334 }
335
336 sub put {
337 my $self = shift ;
338 my $key = shift ;
339 my $value = shift ;
340 $self->SUPER::put($key, $value * 3) ;
341 }
342
343 sub get {
344 my $self = shift ;
345 $self->SUPER::get($_[0], $_[1]) ;
346 $_[1] -= 2 ;
347 }
348
349 sub A_new_method
350 {
351 my $self = shift ;
352 my $key = shift ;
353 my $value = $self->FETCH($key) ;
354 return "[[$value]]" ;
355 }
356
357 1 ;
358EOM
359
360 close FILE ;
361
045291aa 362 BEGIN { push @INC, '.'; }
a6ed719b 363 eval 'use SubDB ; ';
364 main::ok(57, $@ eq "") ;
365 my @h ;
366 my $X ;
367 eval '
368 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
369 ' ;
370
371 main::ok(58, $@ eq "") ;
372
373 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
374 main::ok(59, $@ eq "") ;
375 main::ok(60, $ret == 5) ;
376
377 my $value = 0;
378 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
379 main::ok(61, $@ eq "") ;
380 main::ok(62, $ret == 10) ;
381
382 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
383 main::ok(63, $@ eq "" ) ;
384 main::ok(64, $ret == 1) ;
385
386 $ret = eval '$X->A_new_method(1) ' ;
387 main::ok(65, $@ eq "") ;
388 main::ok(66, $ret eq "[[11]]") ;
389
fac76ed7 390 undef $X;
391 untie(@h);
a6ed719b 392 unlink "SubDB.pm", "recno.tmp" ;
393
394}
395
045291aa 396{
397
398 # test $#
399 my $self ;
400 unlink $Dfile;
401 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
402 $h[0] = "abc" ;
403 $h[1] = "def" ;
404 $h[2] = "ghi" ;
405 $h[3] = "jkl" ;
406 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
407 undef $self ;
408 untie @h ;
409 my $x = docat($Dfile) ;
410 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
411
412 # $# sets array to same length
413 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
414 if ($FA)
415 { $#h = 3 }
416 else
417 { $self->STORESIZE(4) }
418 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
419 undef $self ;
420 untie @h ;
421 $x = docat($Dfile) ;
422 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
423
424 # $# sets array to bigger
425 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
426 if ($FA)
427 { $#h = 6 }
428 else
429 { $self->STORESIZE(7) }
430 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
431 undef $self ;
432 untie @h ;
433 $x = docat($Dfile) ;
434 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
435
436 # $# sets array smaller
437 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
438 if ($FA)
439 { $#h = 2 }
440 else
441 { $self->STORESIZE(3) }
442 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
443 undef $self ;
444 untie @h ;
445 $x = docat($Dfile) ;
446 ok(78, $x eq "abc\ndef\nghi\n") ;
447
448 unlink $Dfile;
449
450
451}
452
a0d0e21e 453exit ;