5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
17 $Dfile = "Op.db-hash";
22 # Check the interface to HASHINFO
24 $dbh = TIEHASH DB_File::HASHINFO ;
25 print (($dbh->{bsize} == undef) ? "ok 1\n" : "not ok 1\n") ;
26 print (($dbh->{ffactor} == undef) ? "ok 2\n" : "not ok 2\n") ;
27 print (($dbh->{nelem} == undef) ? "ok 3\n" : "not ok 3\n") ;
28 print (($dbh->{cachesize} == undef) ? "ok 4\n" : "not ok 4\n") ;
29 print (($dbh->{hash} == undef) ? "ok 5\n" : "not ok 5\n") ;
30 print (($dbh->{lorder} == undef) ? "ok 6\n" : "not ok 6\n") ;
32 $dbh->{bsize} = 3000 ;
33 print ($dbh->{bsize} == 3000 ? "ok 7\n" : "not ok 7\n") ;
35 $dbh->{ffactor} = 9000 ;
36 print ($dbh->{ffactor} == 9000 ? "ok 8\n" : "not ok 8\n") ;
39 print (($dbh->{nelem} == 400) ? "ok 9\n" : "not ok 9\n") ;
41 $dbh->{cachesize} = 65 ;
42 print (($dbh->{cachesize} == 65) ? "ok 10\n" : "not ok 10\n") ;
44 $dbh->{hash} = "abc" ;
45 print (($dbh->{hash} eq "abc") ? "ok 11\n" : "not ok 11\n") ;
47 $dbh->{lorder} = 1234 ;
48 print ($dbh->{lorder} == 1234 ? "ok 12\n" : "not ok 12\n") ;
50 # Check that an invalid entry is caught both for store & fetch
51 eval '$dbh->{fred} = 1234' ;
52 print ($@ eq '' ? "ok 13\n" : "not ok 13\n") ;
53 eval '$q = $dbh->{fred}' ;
54 print ($@ eq '' ? "ok 14\n" : "not ok 14\n") ;
56 # Now check the interface to HASH
58 print (($X = tie(%h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 15\n" : "not ok 15");
60 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
61 $blksize,$blocks) = stat($Dfile);
62 print (($mode & 0777) == 0640 ? "ok 16\n" : "not ok 16\n");
64 while (($key,$value) = each(%h)) {
67 print (!$i ? "ok 17\n" : "not ok 17\n");
69 $h{'goner1'} = 'snork';
72 print ($h{'abc'} == 'ABC' ? "ok 18\n" : "not ok 18\n") ;
73 print (defined $h{'jimmy'} ? "not ok 19\n" : "ok 19\n");
76 $h{'jkl','mno'} = "JKL\034MNO";
77 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
94 $h{'goner2'} = 'snork';
98 # IMPORTANT - $X must be undefined before the untie otherwise the
99 # underlying DB close routine will not get called.
104 # tie to the same file again, do not supply a type - should default to HASH
105 print (($X = tie(%h,DB_File,$Dfile, O_RDWR, 0640)) ? "ok 20\n" : "not ok 20: $!\n");
107 # Modify an entry from the previous tie
128 $h{'goner3'} = 'snork';
131 $X->DELETE('goner3');
134 @values = values(%h);
136 if ($#keys == 29 && $#values == 29) {print "ok 21\n";} else {print "not ok 21\n";}
138 while (($key,$value) = each(h)) {
139 if ($key eq $keys[$i] && $value eq $values[$i] && $key gt $value) {
141 $i++ if $key eq $value;
145 if ($i == 30) {print "ok 22\n";} else {print "not ok 22\n";}
147 @keys = ('blurfl', keys(h), 'dyick');
148 if ($#keys == 31) {print "ok 23\n";} else {print "not ok 23\n";}
151 print ($h{'foo'} eq '' ? "ok 24\n" : "not ok 24\n") ;
154 print ($h{''} eq 'bar' ? "ok 25\n" : "not ok 25\n") ;
156 # check cache overflow and numeric keys and contents
158 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
159 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
160 print ($ok ? "ok 26\n" : "not ok 26\n");
162 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
163 $blksize,$blocks) = stat($Dfile);
164 print ($size > 0 ? "ok 27\n" : "not ok 27\n");
166 @h{0..200} = 200..400;
168 print join(':',200..400) eq join(':',@foo) ? "ok 28\n" : "not ok 28\n";
171 # Now check all the non-tie specific stuff
173 # Check NOOVERWRITE will make put fail when attempting to overwrite
174 # an existing record.
176 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
177 print ($status == 1 ? "ok 29\n" : "not ok 29\n") ;
179 # check that the value of the key 'x' has not been changed by the
181 print ($h{'x'} eq 'X' ? "ok 30\n" : "not ok 30\n") ;
184 $status = $X->put('key', 'value') ;
185 print ($status == 0 ? "ok 31\n" : "not ok 31\n") ;
187 #check that previous put can be retrieved
188 $status = $X->get('key', $value) ;
189 print ($status == 0 ? "ok 32\n" : "not ok 32\n") ;
190 print ($value eq 'value' ? "ok 33\n" : "not ok 33\n") ;
192 # Attempting to delete an existing key should work
194 $status = $X->del('q') ;
195 print ($status == 0 ? "ok 34\n" : "not ok 34\n") ;
197 # Make sure that the key deleted, cannot be retrieved
198 print (($h{'q'} eq undef) ? "ok 35\n" : "not ok 35\n") ;
200 # Attempting to delete a non-existant key should fail
202 $status = $X->del('joe') ;
203 print ($status == 1 ? "ok 36\n" : "not ok 36\n") ;
205 # Check the get interface
207 # First a non-existing key
208 $status = $X->get('aaaa', $value) ;
209 print ($status == 1 ? "ok 37\n" : "not ok 37\n") ;
211 # Next an existing key
212 $status = $X->get('a', $value) ;
213 print ($status == 0 ? "ok 38\n" : "not ok 38\n") ;
214 print ($value eq 'A' ? "ok 39\n" : "not ok 39\n") ;
219 # ditto, but use put to replace the key/value pair.
221 # use seq to walk backwards through a file - check that this reversed is
223 # check seq FIRST/LAST
229 print ($status == 0 ? "ok 40\n" : "not ok 40\n") ;
236 print ($status != 0 ? "ok 41\n" : "not ok 41\n") ;
243 # Now try an in memory file
244 print (($X = tie(%h, DB_File,undef, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 42\n" : "not ok 42");
246 # fd with an in memory file should return fail
248 print ($status == -1 ? "ok 43\n" : "not ok 43\n") ;