Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
55497cff |
4 | @INC = '../lib' if -d '../lib' ; |
a0d0e21e |
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 | |
f6b705ef |
15 | print "1..91\n"; |
16 | |
17 | sub ok |
18 | { |
19 | my $no = shift ; |
20 | my $result = shift ; |
21 | |
22 | print "not " unless $result ; |
23 | print "ok $no\n" ; |
24 | } |
a0d0e21e |
25 | |
55497cff |
26 | sub lexical |
27 | { |
28 | my(@a) = unpack ("C*", $a) ; |
29 | my(@b) = unpack ("C*", $b) ; |
30 | |
31 | my $len = (@a > @b ? @b : @a) ; |
32 | my $i = 0 ; |
33 | |
34 | foreach $i ( 0 .. $len -1) { |
35 | return $a[$i] - $b[$i] if $a[$i] != $b[$i] ; |
36 | } |
37 | |
38 | return @a - @b ; |
39 | } |
40 | |
55d68b4a |
41 | $Dfile = "dbbtree.tmp"; |
a0d0e21e |
42 | unlink $Dfile; |
43 | |
44 | umask(0); |
45 | |
46 | # Check the interface to BTREEINFO |
47 | |
f6b705ef |
48 | my $dbh = new DB_File::BTREEINFO ; |
55497cff |
49 | ok(1, $dbh->{flags} == 0) ; |
50 | ok(2, $dbh->{cachesize} == 0) ; |
51 | ok(3, $dbh->{psize} == 0) ; |
52 | ok(4, $dbh->{lorder} == 0) ; |
53 | ok(5, $dbh->{minkeypage} == 0) ; |
54 | ok(6, $dbh->{maxkeypage} == 0) ; |
ac1ad7f0 |
55 | { |
56 | local $^W = 0 ; |
57 | ok(7, $dbh->{compare} == undef) ; |
58 | ok(8, $dbh->{prefix} == undef) ; |
59 | } |
a0d0e21e |
60 | |
61 | $dbh->{flags} = 3000 ; |
f6b705ef |
62 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
63 | |
64 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
65 | ok(10, $dbh->{cachesize} == 9000); |
66 | |
a0d0e21e |
67 | $dbh->{psize} = 400 ; |
f6b705ef |
68 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
69 | |
70 | $dbh->{lorder} = 65 ; |
f6b705ef |
71 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
72 | |
73 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
74 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
75 | |
76 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
77 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
78 | |
79 | $dbh->{compare} = 1234 ; |
f6b705ef |
80 | ok(15, $dbh->{compare} == 1234) ; |
a0d0e21e |
81 | |
82 | $dbh->{prefix} = 1234 ; |
f6b705ef |
83 | ok(16, $dbh->{prefix} == 1234 ); |
a0d0e21e |
84 | |
85 | # Check that an invalid entry is caught both for store & fetch |
86 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
87 | ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
a0d0e21e |
88 | eval '$q = $dbh->{fred}' ; |
f6b705ef |
89 | ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
90 | |
91 | # Now check the interface to BTREE |
92 | |
f6b705ef |
93 | ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
a0d0e21e |
94 | |
95 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
96 | $blksize,$blocks) = stat($Dfile); |
053b5721 |
97 | ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) ); |
a0d0e21e |
98 | |
99 | while (($key,$value) = each(%h)) { |
100 | $i++; |
101 | } |
f6b705ef |
102 | ok(21, !$i ) ; |
a0d0e21e |
103 | |
104 | $h{'goner1'} = 'snork'; |
105 | |
106 | $h{'abc'} = 'ABC'; |
f6b705ef |
107 | ok(22, $h{'abc'} eq 'ABC' ); |
108 | ok(23, ! defined $h{'jimmy'} ) ; |
109 | ok(24, ! exists $h{'jimmy'} ) ; |
110 | ok(25, defined $h{'abc'} ) ; |
a0d0e21e |
111 | |
112 | $h{'def'} = 'DEF'; |
113 | $h{'jkl','mno'} = "JKL\034MNO"; |
114 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
115 | $h{'a'} = 'A'; |
116 | |
117 | #$h{'b'} = 'B'; |
118 | $X->STORE('b', 'B') ; |
119 | |
120 | $h{'c'} = 'C'; |
121 | |
122 | #$h{'d'} = 'D'; |
123 | $X->put('d', 'D') ; |
124 | |
125 | $h{'e'} = 'E'; |
126 | $h{'f'} = 'F'; |
127 | $h{'g'} = 'X'; |
128 | $h{'h'} = 'H'; |
129 | $h{'i'} = 'I'; |
130 | |
131 | $h{'goner2'} = 'snork'; |
132 | delete $h{'goner2'}; |
133 | |
134 | |
135 | # IMPORTANT - $X must be undefined before the untie otherwise the |
136 | # underlying DB close routine will not get called. |
137 | undef $X ; |
138 | untie(%h); |
139 | |
140 | |
141 | # tie to the same file again |
f6b705ef |
142 | ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
143 | |
144 | # Modify an entry from the previous tie |
145 | $h{'g'} = 'G'; |
146 | |
147 | $h{'j'} = 'J'; |
148 | $h{'k'} = 'K'; |
149 | $h{'l'} = 'L'; |
150 | $h{'m'} = 'M'; |
151 | $h{'n'} = 'N'; |
152 | $h{'o'} = 'O'; |
153 | $h{'p'} = 'P'; |
154 | $h{'q'} = 'Q'; |
155 | $h{'r'} = 'R'; |
156 | $h{'s'} = 'S'; |
157 | $h{'t'} = 'T'; |
158 | $h{'u'} = 'U'; |
159 | $h{'v'} = 'V'; |
160 | $h{'w'} = 'W'; |
161 | $h{'x'} = 'X'; |
162 | $h{'y'} = 'Y'; |
163 | $h{'z'} = 'Z'; |
164 | |
165 | $h{'goner3'} = 'snork'; |
166 | |
167 | delete $h{'goner1'}; |
168 | $X->DELETE('goner3'); |
169 | |
170 | @keys = keys(%h); |
171 | @values = values(%h); |
172 | |
f6b705ef |
173 | ok(27, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
174 | |
f6b705ef |
175 | $i = 0 ; |
a0d0e21e |
176 | while (($key,$value) = each(%h)) { |
2f52a358 |
177 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
178 | $key =~ y/a-z/A-Z/; |
179 | $i++ if $key eq $value; |
180 | } |
181 | } |
182 | |
f6b705ef |
183 | ok(28, $i == 30) ; |
a0d0e21e |
184 | |
55d68b4a |
185 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
186 | ok(29, $#keys == 31) ; |
a0d0e21e |
187 | |
188 | #Check that the keys can be retrieved in order |
55497cff |
189 | my @b = keys %h ; |
190 | my @c = sort lexical @b ; |
191 | ok(30, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
192 | |
193 | $h{'foo'} = ''; |
f6b705ef |
194 | ok(31, $h{'foo'} eq '' ) ; |
a0d0e21e |
195 | |
196 | $h{''} = 'bar'; |
f6b705ef |
197 | ok(32, $h{''} eq 'bar' ); |
a0d0e21e |
198 | |
199 | # check cache overflow and numeric keys and contents |
200 | $ok = 1; |
201 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
202 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
203 | ok(33, $ok); |
a0d0e21e |
204 | |
205 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
206 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
207 | ok(34, $size > 0 ); |
a0d0e21e |
208 | |
209 | @h{0..200} = 200..400; |
210 | @foo = @h{0..200}; |
f6b705ef |
211 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
212 | |
213 | # Now check all the non-tie specific stuff |
214 | |
215 | |
216 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
217 | # an existing record. |
218 | |
219 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
220 | ok(36, $status == 1 ); |
a0d0e21e |
221 | |
222 | # check that the value of the key 'x' has not been changed by the |
223 | # previous test |
f6b705ef |
224 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
225 | |
226 | # standard put |
227 | $status = $X->put('key', 'value') ; |
f6b705ef |
228 | ok(38, $status == 0 ); |
a0d0e21e |
229 | |
230 | #check that previous put can be retrieved |
f6b705ef |
231 | $value = 0 ; |
a0d0e21e |
232 | $status = $X->get('key', $value) ; |
f6b705ef |
233 | ok(39, $status == 0 ); |
234 | ok(40, $value eq 'value' ); |
a0d0e21e |
235 | |
236 | # Attempting to delete an existing key should work |
237 | |
238 | $status = $X->del('q') ; |
f6b705ef |
239 | ok(41, $status == 0 ); |
a0d0e21e |
240 | $status = $X->del('') ; |
f6b705ef |
241 | ok(42, $status == 0 ); |
a0d0e21e |
242 | |
243 | # Make sure that the key deleted, cannot be retrieved |
ac1ad7f0 |
244 | ok(43, ! defined $h{'q'}) ; |
245 | ok(44, ! defined $h{''}) ; |
a0d0e21e |
246 | |
247 | undef $X ; |
248 | untie %h ; |
249 | |
f6b705ef |
250 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
251 | |
252 | # Attempting to delete a non-existant key should fail |
253 | |
254 | $status = $X->del('joe') ; |
f6b705ef |
255 | ok(46, $status == 1 ); |
a0d0e21e |
256 | |
257 | # Check the get interface |
258 | |
259 | # First a non-existing key |
260 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
261 | ok(47, $status == 1 ); |
a0d0e21e |
262 | |
263 | # Next an existing key |
264 | $status = $X->get('a', $value) ; |
f6b705ef |
265 | ok(48, $status == 0 ); |
266 | ok(49, $value eq 'A' ); |
a0d0e21e |
267 | |
268 | # seq |
269 | # ### |
270 | |
271 | # use seq to find an approximate match |
272 | $key = 'ke' ; |
273 | $value = '' ; |
274 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
275 | ok(50, $status == 0 ); |
276 | ok(51, $key eq 'key' ); |
277 | ok(52, $value eq 'value' ); |
a0d0e21e |
278 | |
279 | # seq when the key does not match |
280 | $key = 'zzz' ; |
281 | $value = '' ; |
282 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
283 | ok(53, $status == 1 ); |
a0d0e21e |
284 | |
285 | |
286 | # use seq to set the cursor, then delete the record @ the cursor. |
287 | |
288 | $key = 'x' ; |
289 | $value = '' ; |
290 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
291 | ok(54, $status == 0 ); |
292 | ok(55, $key eq 'x' ); |
293 | ok(56, $value eq 'X' ); |
a0d0e21e |
294 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
295 | ok(57, $status == 0 ); |
a0d0e21e |
296 | $status = $X->get('x', $value) ; |
f6b705ef |
297 | ok(58, $status == 1 ); |
a0d0e21e |
298 | |
299 | # ditto, but use put to replace the key/value pair. |
300 | $key = 'y' ; |
301 | $value = '' ; |
302 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
303 | ok(59, $status == 0 ); |
304 | ok(60, $key eq 'y' ); |
305 | ok(61, $value eq 'Y' ); |
a0d0e21e |
306 | |
307 | $key = "replace key" ; |
308 | $value = "replace value" ; |
309 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
310 | ok(62, $status == 0 ); |
311 | ok(63, $key eq 'replace key' ); |
312 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
313 | $status = $X->get('y', $value) ; |
f6b705ef |
314 | ok(65, $status == 1 ); |
a0d0e21e |
315 | |
316 | # use seq to walk forwards through a file |
317 | |
318 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
319 | ok(66, $status == 0 ); |
a0d0e21e |
320 | $previous = $key ; |
321 | |
322 | $ok = 1 ; |
323 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
324 | { |
325 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
326 | } |
327 | |
f6b705ef |
328 | ok(67, $status == 1 ); |
329 | ok(68, $ok == 1 ); |
a0d0e21e |
330 | |
331 | # use seq to walk backwards through a file |
332 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
333 | ok(69, $status == 0 ); |
a0d0e21e |
334 | $previous = $key ; |
335 | |
336 | $ok = 1 ; |
337 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
338 | { |
339 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
340 | #print "key = [$key] value = [$value]\n" ; |
341 | } |
342 | |
f6b705ef |
343 | ok(70, $status == 1 ); |
344 | ok(71, $ok == 1 ); |
a0d0e21e |
345 | |
346 | |
347 | # check seq FIRST/LAST |
348 | |
349 | # sync |
350 | # #### |
351 | |
352 | $status = $X->sync ; |
f6b705ef |
353 | ok(72, $status == 0 ); |
a0d0e21e |
354 | |
355 | |
356 | # fd |
357 | # ## |
358 | |
359 | $status = $X->fd ; |
f6b705ef |
360 | ok(73, $status != 0 ); |
a0d0e21e |
361 | |
362 | |
363 | undef $X ; |
364 | untie %h ; |
365 | |
366 | unlink $Dfile; |
367 | |
368 | # Now try an in memory file |
f6b705ef |
369 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
370 | |
371 | # fd with an in memory file should return failure |
372 | $status = $Y->fd ; |
f6b705ef |
373 | ok(75, $status == -1 ); |
a0d0e21e |
374 | |
55d68b4a |
375 | |
a0d0e21e |
376 | undef $Y ; |
377 | untie %h ; |
378 | |
55d68b4a |
379 | # Duplicate keys |
380 | my $bt = new DB_File::BTREEINFO ; |
381 | $bt->{flags} = R_DUP ; |
f6b705ef |
382 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
383 | |
384 | $hh{'Wall'} = 'Larry' ; |
385 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
386 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
387 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
388 | $hh{'Smith'} = 'John' ; |
389 | $hh{'mouse'} = 'mickey' ; |
390 | |
391 | # first work in scalar context |
f6b705ef |
392 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
393 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
394 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
395 | |
396 | # now in list context |
397 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
398 | ok(80, "@unknown" eq "" ); |
55d68b4a |
399 | |
400 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
401 | ok(81, "@smith" eq "John" ); |
55d68b4a |
402 | |
760ac839 |
403 | { |
f6b705ef |
404 | my @wall = $YY->get_dup('Wall') ; |
405 | my %wall ; |
406 | @wall{@wall} = @wall ; |
407 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
408 | } |
55d68b4a |
409 | |
410 | # hash |
411 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
412 | ok(83, keys %unknown == 0 ); |
55d68b4a |
413 | |
414 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
415 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
416 | |
f6b705ef |
417 | my %wall = $YY->get_dup('Wall', 1) ; |
418 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
419 | && $wall{'Brick'} == 2); |
55d68b4a |
420 | |
421 | undef $YY ; |
422 | untie %hh ; |
423 | unlink $Dfile; |
424 | |
425 | |
8e07c86e |
426 | # test multiple callbacks |
427 | $Dfile1 = "btree1" ; |
428 | $Dfile2 = "btree2" ; |
429 | $Dfile3 = "btree3" ; |
430 | |
36477c24 |
431 | $dbh1 = new DB_File::BTREEINFO ; |
ac1ad7f0 |
432 | { local $^W = 0 ; |
433 | $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; } |
8e07c86e |
434 | |
36477c24 |
435 | $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
436 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
437 | |
36477c24 |
438 | $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
439 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
440 | |
441 | |
f6b705ef |
442 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
443 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
444 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
445 | |
446 | @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
ac1ad7f0 |
447 | { local $^W = 0 ; |
448 | @srt_1 = sort { $a <=> $b } @Keys ; } |
8e07c86e |
449 | @srt_2 = sort { $a cmp $b } @Keys ; |
450 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
451 | |
452 | foreach (@Keys) { |
ac1ad7f0 |
453 | { local $^W = 0 ; |
454 | $h{$_} = 1 ; } |
8e07c86e |
455 | $g{$_} = 1 ; |
456 | $k{$_} = 1 ; |
457 | } |
458 | |
459 | sub ArrayCompare |
460 | { |
461 | my($a, $b) = @_ ; |
462 | |
463 | return 0 if @$a != @$b ; |
464 | |
465 | foreach (1 .. length @$a) |
466 | { |
467 | return 0 unless $$a[$_] eq $$b[$_] ; |
468 | } |
469 | |
470 | 1 ; |
471 | } |
472 | |
f6b705ef |
473 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
474 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
475 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
476 | |
477 | untie %h ; |
478 | untie %g ; |
479 | untie %k ; |
480 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
481 | |
f6b705ef |
482 | # clear |
483 | # ##### |
484 | |
485 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
486 | foreach (1 .. 10) |
487 | { $h{$_} = $_ * 100 } |
488 | |
489 | # check that there are 10 elements in the hash |
490 | $i = 0 ; |
491 | while (($key,$value) = each(%h)) { |
492 | $i++; |
493 | } |
494 | ok(90, $i == 10); |
495 | |
496 | # now clear the hash |
497 | %h = () ; |
498 | |
499 | # check it is empty |
500 | $i = 0 ; |
501 | while (($key,$value) = each(%h)) { |
502 | $i++; |
503 | } |
504 | ok(91, $i == 0); |
505 | |
506 | untie %h ; |
507 | unlink $Dfile1 ; |
508 | |
a0d0e21e |
509 | exit ; |