AmigaOS patches to 5.003_28
[p5sagit/p5-mst-13.2.git] / t / lib / db-btree.t
1 #!./perl -w
2
3 BEGIN {
4     @INC = '../lib' if -d '../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..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 }
25
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
41 $Dfile = "dbbtree.tmp";
42 unlink $Dfile;
43
44 umask(0);
45
46 # Check the interface to BTREEINFO
47
48 my $dbh = new DB_File::BTREEINFO ;
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) ;
55 {
56   local $^W = 0 ;
57   ok(7, $dbh->{compare} == undef) ;
58   ok(8, $dbh->{prefix} == undef) ;
59 }
60
61 $dbh->{flags} = 3000 ;
62 ok(9, $dbh->{flags} == 3000) ;
63
64 $dbh->{cachesize} = 9000 ;
65 ok(10, $dbh->{cachesize} == 9000);
66
67 $dbh->{psize} = 400 ;
68 ok(11, $dbh->{psize} == 400) ;
69
70 $dbh->{lorder} = 65 ;
71 ok(12, $dbh->{lorder} == 65) ;
72
73 $dbh->{minkeypage} = 123 ;
74 ok(13, $dbh->{minkeypage} == 123) ;
75
76 $dbh->{maxkeypage} = 1234 ;
77 ok(14, $dbh->{maxkeypage} == 1234 );
78
79 $dbh->{compare} = 1234 ;
80 ok(15, $dbh->{compare} == 1234) ;
81
82 $dbh->{prefix} = 1234 ;
83 ok(16, $dbh->{prefix} == 1234 );
84
85 # Check that an invalid entry is caught both for store & fetch
86 eval '$dbh->{fred} = 1234' ;
87 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
88 eval '$q = $dbh->{fred}' ;
89 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
90
91 # Now check the interface to BTREE
92
93 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
94
95 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
96    $blksize,$blocks) = stat($Dfile);
97 ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos');
98
99 while (($key,$value) = each(%h)) {
100     $i++;
101 }
102 ok(21, !$i ) ;
103
104 $h{'goner1'} = 'snork';
105
106 $h{'abc'} = 'ABC';
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'} ) ;
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
142 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
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
173 ok(27, $#keys == 29 && $#values == 29) ;
174
175 $i = 0 ;
176 while (($key,$value) = each(%h)) {
177     if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
178         $key =~ y/a-z/A-Z/;
179         $i++ if $key eq $value;
180     }
181 }
182
183 ok(28, $i == 30) ;
184
185 @keys = ('blurfl', keys(%h), 'dyick');
186 ok(29, $#keys == 31) ;
187
188 #Check that the keys can be retrieved in order
189 my @b = keys %h ;
190 my @c = sort lexical @b ;
191 ok(30, ArrayCompare(\@b, \@c)) ;
192
193 $h{'foo'} = '';
194 ok(31, $h{'foo'} eq '' ) ;
195
196 $h{''} = 'bar';
197 ok(32, $h{''} eq 'bar' );
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; }
203 ok(33, $ok);
204
205 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
206    $blksize,$blocks) = stat($Dfile);
207 ok(34, $size > 0 );
208
209 @h{0..200} = 200..400;
210 @foo = @h{0..200};
211 ok(35, join(':',200..400) eq join(':',@foo) );
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) ;
220 ok(36, $status == 1 );
221  
222 # check that the value of the key 'x' has not been changed by the 
223 # previous test
224 ok(37, $h{'x'} eq 'X' );
225
226 # standard put
227 $status = $X->put('key', 'value') ;
228 ok(38, $status == 0 );
229
230 #check that previous put can be retrieved
231 $value = 0 ;
232 $status = $X->get('key', $value) ;
233 ok(39, $status == 0 );
234 ok(40, $value eq 'value' );
235
236 # Attempting to delete an existing key should work
237
238 $status = $X->del('q') ;
239 ok(41, $status == 0 );
240 $status = $X->del('') ;
241 ok(42, $status == 0 );
242
243 # Make sure that the key deleted, cannot be retrieved
244 ok(43, ! defined $h{'q'}) ;
245 ok(44, ! defined $h{''}) ;
246
247 undef $X ;
248 untie %h ;
249
250 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
251
252 # Attempting to delete a non-existant key should fail
253
254 $status = $X->del('joe') ;
255 ok(46, $status == 1 );
256
257 # Check the get interface
258
259 # First a non-existing key
260 $status = $X->get('aaaa', $value) ;
261 ok(47, $status == 1 );
262
263 # Next an existing key
264 $status = $X->get('a', $value) ;
265 ok(48, $status == 0 );
266 ok(49, $value eq 'A' );
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) ;
275 ok(50, $status == 0 );
276 ok(51, $key eq 'key' );
277 ok(52, $value eq 'value' );
278
279 # seq when the key does not match
280 $key = 'zzz' ;
281 $value = '' ;
282 $status = $X->seq($key, $value, R_CURSOR) ;
283 ok(53, $status == 1 );
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) ;
291 ok(54, $status == 0 );
292 ok(55, $key eq 'x' );
293 ok(56, $value eq 'X' );
294 $status = $X->del(0, R_CURSOR) ;
295 ok(57, $status == 0 );
296 $status = $X->get('x', $value) ;
297 ok(58, $status == 1 );
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) ;
303 ok(59, $status == 0 );
304 ok(60, $key eq 'y' );
305 ok(61, $value eq 'Y' );
306
307 $key = "replace key" ;
308 $value = "replace value" ;
309 $status = $X->put($key, $value, R_CURSOR) ;
310 ok(62, $status == 0 );
311 ok(63, $key eq 'replace key' );
312 ok(64, $value eq 'replace value' );
313 $status = $X->get('y', $value) ;
314 ok(65, $status == 1 );
315
316 # use seq to walk forwards through a file 
317
318 $status = $X->seq($key, $value, R_FIRST) ;
319 ok(66, $status == 0 );
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
328 ok(67, $status == 1 );
329 ok(68, $ok == 1 );
330
331 # use seq to walk backwards through a file 
332 $status = $X->seq($key, $value, R_LAST) ;
333 ok(69, $status == 0 );
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
343 ok(70, $status == 1 );
344 ok(71, $ok == 1 );
345
346
347 # check seq FIRST/LAST
348
349 # sync
350 # ####
351
352 $status = $X->sync ;
353 ok(72, $status == 0 );
354
355
356 # fd
357 # ##
358
359 $status = $X->fd ;
360 ok(73, $status != 0 );
361
362
363 undef $X ;
364 untie %h ;
365
366 unlink $Dfile;
367
368 # Now try an in memory file
369 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
370
371 # fd with an in memory file should return failure
372 $status = $Y->fd ;
373 ok(75, $status == -1 );
374
375
376 undef $Y ;
377 untie %h ;
378
379 # Duplicate keys
380 my $bt = new DB_File::BTREEINFO ;
381 $bt->{flags} = R_DUP ;
382 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
383
384 $hh{'Wall'} = 'Larry' ;
385 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
386 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
387 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
388 $hh{'Smith'} = 'John' ;
389 $hh{'mouse'} = 'mickey' ;
390
391 # first work in scalar context
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 );
395
396 # now in list context
397 my @unknown = $YY->get_dup('Unknown') ;
398 ok(80, "@unknown" eq "" );
399
400 my @smith = $YY->get_dup('Smith') ;
401 ok(81, "@smith" eq "John" );
402
403 {
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'}) );
408 }
409
410 # hash
411 my %unknown = $YY->get_dup('Unknown', 1) ;
412 ok(83, keys %unknown == 0 );
413
414 my %smith = $YY->get_dup('Smith', 1) ;
415 ok(84, keys %smith == 1 && $smith{'John'}) ;
416
417 my %wall = $YY->get_dup('Wall', 1) ;
418 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 
419                 && $wall{'Brick'} == 2);
420
421 undef $YY ;
422 untie %hh ;
423 unlink $Dfile;
424
425
426 # test multiple callbacks
427 $Dfile1 = "btree1" ;
428 $Dfile2 = "btree2" ;
429 $Dfile3 = "btree3" ;
430  
431 $dbh1 = new DB_File::BTREEINFO ;
432 { local $^W = 0 ;
433   $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; }
434  
435 $dbh2 = new DB_File::BTREEINFO ;
436 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
437  
438 $dbh3 = new DB_File::BTREEINFO ;
439 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
440  
441  
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 ) ;
445  
446 @Keys = qw( 0123 12 -1234 9 987654321 def  ) ;
447 { local $^W = 0 ;
448   @srt_1 = sort { $a <=> $b } @Keys ; }
449 @srt_2 = sort { $a cmp $b } @Keys ;
450 @srt_3 = sort { length $a <=> length $b } @Keys ;
451  
452 foreach (@Keys) {
453     { local $^W = 0 ; 
454       $h{$_} = 1 ; }
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  
473 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
474 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
475 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
476
477 untie %h ;
478 untie %g ;
479 untie %k ;
480 unlink $Dfile1, $Dfile2, $Dfile3 ;
481
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
509 exit ;