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