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