5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
17 $Dfile = "dbhash.tmp";
22 # Check the interface to HASHINFO
24 #$dbh = TIEHASH DB_File::HASHINFO ;
25 $dbh = new DB_File::HASHINFO ;
26 print (($dbh->{bsize} == undef) ? "ok 1\n" : "not ok 1\n") ;
27 print (($dbh->{ffactor} == undef) ? "ok 2\n" : "not ok 2\n") ;
28 print (($dbh->{nelem} == undef) ? "ok 3\n" : "not ok 3\n") ;
29 print (($dbh->{cachesize} == undef) ? "ok 4\n" : "not ok 4\n") ;
30 print (($dbh->{hash} == undef) ? "ok 5\n" : "not ok 5\n") ;
31 print (($dbh->{lorder} == undef) ? "ok 6\n" : "not ok 6\n") ;
33 $dbh->{bsize} = 3000 ;
34 print ($dbh->{bsize} == 3000 ? "ok 7\n" : "not ok 7\n") ;
36 $dbh->{ffactor} = 9000 ;
37 print ($dbh->{ffactor} == 9000 ? "ok 8\n" : "not ok 8\n") ;
40 print (($dbh->{nelem} == 400) ? "ok 9\n" : "not ok 9\n") ;
42 $dbh->{cachesize} = 65 ;
43 print (($dbh->{cachesize} == 65) ? "ok 10\n" : "not ok 10\n") ;
45 $dbh->{hash} = "abc" ;
46 print (($dbh->{hash} eq "abc") ? "ok 11\n" : "not ok 11\n") ;
48 $dbh->{lorder} = 1234 ;
49 print ($dbh->{lorder} == 1234 ? "ok 12\n" : "not ok 12\n") ;
51 # Check that an invalid entry is caught both for store & fetch
52 eval '$dbh->{fred} = 1234' ;
53 print ($@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ ? "ok 13\n" : "not ok 13\n") ;
54 eval '$q = $dbh->{fred}' ;
55 print ($@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ ? "ok 14\n" : "not ok 14\n") ;
57 # Now check the interface to HASH
59 print (($X = tie(%h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 15\n" : "not ok 15");
61 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
62 $blksize,$blocks) = stat($Dfile);
63 print (($mode & 0777) == 0640 ? "ok 16\n" : "not ok 16\n");
65 while (($key,$value) = each(%h)) {
68 print (!$i ? "ok 17\n" : "not ok 17\n");
70 $h{'goner1'} = 'snork';
73 print ($h{'abc'} eq 'ABC' ? "ok 18\n" : "not ok 18\n") ;
74 print (defined $h{'jimmy'} ? "not ok 19\n" : "ok 19\n");
77 $h{'jkl','mno'} = "JKL\034MNO";
78 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
95 $h{'goner2'} = 'snork';
99 # IMPORTANT - $X must be undefined before the untie otherwise the
100 # underlying DB close routine will not get called.
105 # tie to the same file again, do not supply a type - should default to HASH
106 print (($X = tie(%h,DB_File,$Dfile, O_RDWR, 0640)) ? "ok 20\n" : "not ok 20: $!\n");
108 # Modify an entry from the previous tie
129 $h{'goner3'} = 'snork';
132 $X->DELETE('goner3');
135 @values = values(%h);
137 if ($#keys == 29 && $#values == 29) {print "ok 21\n";} else {print "not ok 21\n";}
139 while (($key,$value) = each(%h)) {
140 if ($key eq $keys[$i] && $value eq $values[$i] && $key gt $value) {
142 $i++ if $key eq $value;
146 if ($i == 30) {print "ok 22\n";} else {print "not ok 22\n";}
148 @keys = ('blurfl', keys(%h), 'dyick');
149 if ($#keys == 31) {print "ok 23\n";} else {print "not ok 23\n";}
152 print ($h{'foo'} eq '' ? "ok 24\n" : "not ok 24\n") ;
155 print ($h{''} eq 'bar' ? "ok 25\n" : "not ok 25\n") ;
157 # check cache overflow and numeric keys and contents
159 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
160 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
161 print ($ok ? "ok 26\n" : "not ok 26\n");
163 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
164 $blksize,$blocks) = stat($Dfile);
165 print ($size > 0 ? "ok 27\n" : "not ok 27\n");
167 @h{0..200} = 200..400;
169 print join(':',200..400) eq join(':',@foo) ? "ok 28\n" : "not ok 28\n";
172 # Now check all the non-tie specific stuff
174 # Check NOOVERWRITE will make put fail when attempting to overwrite
175 # an existing record.
177 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
178 print ($status == 1 ? "ok 29\n" : "not ok 29\n") ;
180 # check that the value of the key 'x' has not been changed by the
182 print ($h{'x'} eq 'X' ? "ok 30\n" : "not ok 30\n") ;
185 $status = $X->put('key', 'value') ;
186 print ($status == 0 ? "ok 31\n" : "not ok 31\n") ;
188 #check that previous put can be retrieved
189 $status = $X->get('key', $value) ;
190 print ($status == 0 ? "ok 32\n" : "not ok 32\n") ;
191 print ($value eq 'value' ? "ok 33\n" : "not ok 33\n") ;
193 # Attempting to delete an existing key should work
195 $status = $X->del('q') ;
196 print ($status == 0 ? "ok 34\n" : "not ok 34\n") ;
198 # Make sure that the key deleted, cannot be retrieved
199 print (($h{'q'} eq undef) ? "ok 35\n" : "not ok 35\n") ;
201 # Attempting to delete a non-existant key should fail
203 $status = $X->del('joe') ;
204 print ($status == 1 ? "ok 36\n" : "not ok 36\n") ;
206 # Check the get interface
208 # First a non-existing key
209 $status = $X->get('aaaa', $value) ;
210 print ($status == 1 ? "ok 37\n" : "not ok 37\n") ;
212 # Next an existing key
213 $status = $X->get('a', $value) ;
214 print ($status == 0 ? "ok 38\n" : "not ok 38\n") ;
215 print ($value eq 'A' ? "ok 39\n" : "not ok 39\n") ;
220 # ditto, but use put to replace the key/value pair.
222 # use seq to walk backwards through a file - check that this reversed is
224 # check seq FIRST/LAST
230 print ($status == 0 ? "ok 40\n" : "not ok 40\n") ;
237 print ($status != 0 ? "ok 41\n" : "not ok 41\n") ;
244 # Now try an in memory file
245 print (($X = tie(%h, DB_File,undef, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 42\n" : "not ok 42");
247 # fd with an in memory file should return fail
249 print ($status == -1 ? "ok 43\n" : "not ok 43\n") ;