Commit | Line | Data |
a0d0e21e |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | @INC = '../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 | |
15 | print "1..43\n"; |
16 | |
55d68b4a |
17 | $Dfile = "dbhash.tmp"; |
a0d0e21e |
18 | unlink $Dfile; |
19 | |
20 | umask(0); |
21 | |
22 | # Check the interface to HASHINFO |
23 | |
55d68b4a |
24 | #$dbh = TIEHASH DB_File::HASHINFO ; |
25 | $dbh = new DB_File::HASHINFO ; |
a0d0e21e |
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") ; |
32 | |
33 | $dbh->{bsize} = 3000 ; |
34 | print ($dbh->{bsize} == 3000 ? "ok 7\n" : "not ok 7\n") ; |
35 | |
36 | $dbh->{ffactor} = 9000 ; |
37 | print ($dbh->{ffactor} == 9000 ? "ok 8\n" : "not ok 8\n") ; |
38 | # |
39 | $dbh->{nelem} = 400 ; |
40 | print (($dbh->{nelem} == 400) ? "ok 9\n" : "not ok 9\n") ; |
41 | |
42 | $dbh->{cachesize} = 65 ; |
43 | print (($dbh->{cachesize} == 65) ? "ok 10\n" : "not ok 10\n") ; |
44 | |
45 | $dbh->{hash} = "abc" ; |
46 | print (($dbh->{hash} eq "abc") ? "ok 11\n" : "not ok 11\n") ; |
47 | |
48 | $dbh->{lorder} = 1234 ; |
49 | print ($dbh->{lorder} == 1234 ? "ok 12\n" : "not ok 12\n") ; |
50 | |
51 | # Check that an invalid entry is caught both for store & fetch |
52 | eval '$dbh->{fred} = 1234' ; |
55d68b4a |
53 | print ($@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ ? "ok 13\n" : "not ok 13\n") ; |
a0d0e21e |
54 | eval '$q = $dbh->{fred}' ; |
55d68b4a |
55 | print ($@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ ? "ok 14\n" : "not ok 14\n") ; |
a0d0e21e |
56 | |
57 | # Now check the interface to HASH |
58 | |
59 | print (($X = tie(%h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 15\n" : "not ok 15"); |
60 | |
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"); |
64 | |
65 | while (($key,$value) = each(%h)) { |
66 | $i++; |
67 | } |
68 | print (!$i ? "ok 17\n" : "not ok 17\n"); |
69 | |
70 | $h{'goner1'} = 'snork'; |
71 | |
72 | $h{'abc'} = 'ABC'; |
55d68b4a |
73 | print ($h{'abc'} eq 'ABC' ? "ok 18\n" : "not ok 18\n") ; |
a0d0e21e |
74 | print (defined $h{'jimmy'} ? "not ok 19\n" : "ok 19\n"); |
75 | |
76 | $h{'def'} = 'DEF'; |
77 | $h{'jkl','mno'} = "JKL\034MNO"; |
78 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
79 | $h{'a'} = 'A'; |
80 | |
81 | #$h{'b'} = 'B'; |
82 | $X->STORE('b', 'B') ; |
83 | |
84 | $h{'c'} = 'C'; |
85 | |
86 | #$h{'d'} = 'D'; |
87 | $X->put('d', 'D') ; |
88 | |
89 | $h{'e'} = 'E'; |
90 | $h{'f'} = 'F'; |
91 | $h{'g'} = 'X'; |
92 | $h{'h'} = 'H'; |
93 | $h{'i'} = 'I'; |
94 | |
95 | $h{'goner2'} = 'snork'; |
96 | delete $h{'goner2'}; |
97 | |
98 | |
99 | # IMPORTANT - $X must be undefined before the untie otherwise the |
100 | # underlying DB close routine will not get called. |
101 | undef $X ; |
102 | untie(%h); |
103 | |
104 | |
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"); |
107 | |
108 | # Modify an entry from the previous tie |
109 | $h{'g'} = 'G'; |
110 | |
111 | $h{'j'} = 'J'; |
112 | $h{'k'} = 'K'; |
113 | $h{'l'} = 'L'; |
114 | $h{'m'} = 'M'; |
115 | $h{'n'} = 'N'; |
116 | $h{'o'} = 'O'; |
117 | $h{'p'} = 'P'; |
118 | $h{'q'} = 'Q'; |
119 | $h{'r'} = 'R'; |
120 | $h{'s'} = 'S'; |
121 | $h{'t'} = 'T'; |
122 | $h{'u'} = 'U'; |
123 | $h{'v'} = 'V'; |
124 | $h{'w'} = 'W'; |
125 | $h{'x'} = 'X'; |
126 | $h{'y'} = 'Y'; |
127 | $h{'z'} = 'Z'; |
128 | |
129 | $h{'goner3'} = 'snork'; |
130 | |
131 | delete $h{'goner1'}; |
132 | $X->DELETE('goner3'); |
133 | |
134 | @keys = keys(%h); |
135 | @values = values(%h); |
136 | |
137 | if ($#keys == 29 && $#values == 29) {print "ok 21\n";} else {print "not ok 21\n";} |
138 | |
55d68b4a |
139 | while (($key,$value) = each(%h)) { |
a0d0e21e |
140 | if ($key eq $keys[$i] && $value eq $values[$i] && $key gt $value) { |
141 | $key =~ y/a-z/A-Z/; |
142 | $i++ if $key eq $value; |
143 | } |
144 | } |
145 | |
146 | if ($i == 30) {print "ok 22\n";} else {print "not ok 22\n";} |
147 | |
55d68b4a |
148 | @keys = ('blurfl', keys(%h), 'dyick'); |
a0d0e21e |
149 | if ($#keys == 31) {print "ok 23\n";} else {print "not ok 23\n";} |
150 | |
151 | $h{'foo'} = ''; |
152 | print ($h{'foo'} eq '' ? "ok 24\n" : "not ok 24\n") ; |
153 | |
154 | $h{''} = 'bar'; |
155 | print ($h{''} eq 'bar' ? "ok 25\n" : "not ok 25\n") ; |
156 | |
157 | # check cache overflow and numeric keys and contents |
158 | $ok = 1; |
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"); |
162 | |
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"); |
166 | |
167 | @h{0..200} = 200..400; |
168 | @foo = @h{0..200}; |
169 | print join(':',200..400) eq join(':',@foo) ? "ok 28\n" : "not ok 28\n"; |
170 | |
171 | |
172 | # Now check all the non-tie specific stuff |
173 | |
174 | # Check NOOVERWRITE will make put fail when attempting to overwrite |
175 | # an existing record. |
176 | |
177 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
178 | print ($status == 1 ? "ok 29\n" : "not ok 29\n") ; |
179 | |
180 | # check that the value of the key 'x' has not been changed by the |
181 | # previous test |
182 | print ($h{'x'} eq 'X' ? "ok 30\n" : "not ok 30\n") ; |
183 | |
184 | # standard put |
185 | $status = $X->put('key', 'value') ; |
186 | print ($status == 0 ? "ok 31\n" : "not ok 31\n") ; |
187 | |
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") ; |
192 | |
193 | # Attempting to delete an existing key should work |
194 | |
195 | $status = $X->del('q') ; |
196 | print ($status == 0 ? "ok 34\n" : "not ok 34\n") ; |
197 | |
198 | # Make sure that the key deleted, cannot be retrieved |
199 | print (($h{'q'} eq undef) ? "ok 35\n" : "not ok 35\n") ; |
200 | |
201 | # Attempting to delete a non-existant key should fail |
202 | |
203 | $status = $X->del('joe') ; |
204 | print ($status == 1 ? "ok 36\n" : "not ok 36\n") ; |
205 | |
206 | # Check the get interface |
207 | |
208 | # First a non-existing key |
209 | $status = $X->get('aaaa', $value) ; |
210 | print ($status == 1 ? "ok 37\n" : "not ok 37\n") ; |
211 | |
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") ; |
216 | |
217 | # seq |
218 | # ### |
219 | |
220 | # ditto, but use put to replace the key/value pair. |
221 | |
222 | # use seq to walk backwards through a file - check that this reversed is |
223 | |
224 | # check seq FIRST/LAST |
225 | |
226 | # sync |
227 | # #### |
228 | |
229 | $status = $X->sync ; |
230 | print ($status == 0 ? "ok 40\n" : "not ok 40\n") ; |
231 | |
232 | |
233 | # fd |
234 | # ## |
235 | |
236 | $status = $X->fd ; |
237 | print ($status != 0 ? "ok 41\n" : "not ok 41\n") ; |
238 | |
239 | undef $X ; |
240 | untie %h ; |
241 | |
242 | unlink $Dfile; |
243 | |
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"); |
246 | |
247 | # fd with an in memory file should return fail |
248 | $status = $X->fd ; |
249 | print ($status == -1 ? "ok 43\n" : "not ok 43\n") ; |
250 | |
251 | untie %h ; |
252 | undef $X ; |
253 | |
254 | exit ; |