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