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