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) ;
22 print "not " unless $result ;
30 print STDERR <<EOM unless $bad_ones++ ;
32 # Some older versions of Berkeley DB will fail tests 51, 53 and 55.
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.
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.
46 my $Dfile = "recno.tmp";
51 # Check the interface to RECNOINFO
53 my $dbh = new DB_File::RECNOINFO ;
54 ok(1, $dbh->{bval} == 0 ) ;
55 ok(2, $dbh->{cachesize} == 0) ;
56 ok(3, $dbh->{psize} == 0) ;
57 ok(4, $dbh->{flags} == 0) ;
58 ok(5, $dbh->{lorder} == 0);
59 ok(6, $dbh->{reclen} == 0);
60 ok(7, $dbh->{bfname} eq "");
63 ok(8, $dbh->{bval} == 3000 );
65 $dbh->{cachesize} = 9000 ;
66 ok(9, $dbh->{cachesize} == 9000 );
69 ok(10, $dbh->{psize} == 400 );
72 ok(11, $dbh->{flags} == 65 );
74 $dbh->{lorder} = 123 ;
75 ok(12, $dbh->{lorder} == 123 );
77 $dbh->{reclen} = 1234 ;
78 ok(13, $dbh->{reclen} == 1234 );
80 $dbh->{bfname} = 1234 ;
81 ok(14, $dbh->{bfname} == 1234 );
84 # Check that an invalid entry is caught both for store & fetch
85 eval '$dbh->{fred} = 1234' ;
86 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
87 eval 'my $q = $dbh->{fred}' ;
88 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
90 # Now check the interface to RECNOINFO
94 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
96 ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
97 || $^O eq 'amigaos') ;
103 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
105 $h[0] = shift @data ;
106 ok(20, $h[0] eq 'a' );
112 unshift (@data, 'a') ;
114 ok(21, defined $h[1] );
115 ok(22, ! defined $h[16] );
116 ok(23, $X->length == @data );
119 # Overwrite an entry & check fetch it
121 $data[3] = 'replaced' ;
122 ok(24, $h[3] eq 'replaced' );
125 my @push_data = qw(added to the end) ;
126 #my push (@h, @push_data) ;
127 $X->push(@push_data) ;
128 push (@data, @push_data) ;
129 ok(25, $h[++$i] eq 'added' );
130 ok(26, $h[++$i] eq 'to' );
131 ok(27, $h[++$i] eq 'the' );
132 ok(28, $h[++$i] eq 'end' );
135 my $popped = pop (@data) ;
136 #my $value = pop(@h) ;
137 my $value = $X->pop ;
138 ok(29, $value eq $popped) ;
143 my $shifted = shift @data ;
144 ok(30, $value eq $shifted );
150 ok(31, $X->length == @data );
152 my @new_data = qw(add this to the start of the array) ;
153 #unshift @h, @new_data ;
154 $X->unshift (@new_data) ;
155 unshift (@data, @new_data) ;
156 ok(32, $X->length == @data );
157 ok(33, $h[0] eq "add") ;
158 ok(34, $h[1] eq "this") ;
159 ok(35, $h[2] eq "to") ;
160 ok(36, $h[3] eq "the") ;
161 ok(37, $h[4] eq "start") ;
162 ok(38, $h[5] eq "of") ;
163 ok(39, $h[6] eq "the") ;
164 ok(40, $h[7] eq "array") ;
165 ok(41, $h[8] eq $data[8]) ;
169 # Now both arrays should be identical
175 $ok = 0, last if $_ ne $h[$j ++] ;
179 # Neagtive subscripts
181 # get the last element of the array
182 ok(43, $h[-1] eq $data[-1] );
183 ok(44, $h[-1] eq $h[$X->length -1] );
185 # get the first element using a negative subscript
186 eval '$h[ - ( $X->length)] = "abcd"' ;
188 ok(46, $h[0] eq "abcd" );
190 # now try to read before the start of the array
191 eval '$h[ - (1 + $X->length)] = 1234' ;
192 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
194 # IMPORTANT - $X must be undefined before the untie otherwise the
195 # underlying DB close routine will not get called.
202 # Check bval defaults to \n
205 my $dbh = new DB_File::RECNOINFO ;
206 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
211 my $x = `cat $Dfile` ;
213 ok(49, $x eq "abc\ndef\n\nghi\n") ;
220 my $dbh = new DB_File::RECNOINFO ;
222 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
227 my $x = `cat $Dfile` ;
229 my $ok = ($x eq "abc-def--ghi-") ;
230 bad_one() unless $ok ;
235 # Check R_FIXEDLEN with default bval (space)
238 my $dbh = new DB_File::RECNOINFO ;
239 $dbh->{flags} = R_FIXEDLEN ;
241 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
246 my $x = `cat $Dfile` ;
248 my $ok = ($x eq "abc def ghi ") ;
249 bad_one() unless $ok ;
254 # Check R_FIXEDLEN with user-defined bval
257 my $dbh = new DB_File::RECNOINFO ;
258 $dbh->{flags} = R_FIXEDLEN ;
261 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
266 my $x = `cat $Dfile` ;
268 my $ok = ($x eq "abc--def-------ghi--") ;
269 bad_one() unless $ok ;