Re: Namespace cleanup: Does SDBM need binary compatibility?
[p5sagit/p5-mst-13.2.git] / t / lib / db-recno.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 use strict ;
15 use vars qw($dbh $Dfile) ;
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 print "1..47\n";
27
28 my $Dfile = "recno.tmp";
29 unlink $Dfile ;
30
31 umask(0);
32
33 # Check the interface to RECNOINFO
34
35 my $dbh = new DB_File::RECNOINFO ;
36 ok(1, $dbh->{bval} == 0 ) ;
37 ok(2, $dbh->{cachesize} == 0) ;
38 ok(3, $dbh->{psize} == 0) ;
39 ok(4, $dbh->{flags} == 0) ;
40 ok(5, $dbh->{lorder} == 0);
41 ok(6, $dbh->{reclen} == 0);
42 ok(7, $dbh->{bfname} eq "");
43
44 $dbh->{bval} = 3000 ;
45 ok(8, $dbh->{bval} == 3000 );
46
47 $dbh->{cachesize} = 9000 ;
48 ok(9, $dbh->{cachesize} == 9000 );
49
50 $dbh->{psize} = 400 ;
51 ok(10, $dbh->{psize} == 400 );
52
53 $dbh->{flags} = 65 ;
54 ok(11, $dbh->{flags} == 65 );
55
56 $dbh->{lorder} = 123 ;
57 ok(12, $dbh->{lorder} == 123 );
58
59 $dbh->{reclen} = 1234 ;
60 ok(13, $dbh->{reclen} == 1234 );
61
62 $dbh->{bfname} = 1234 ;
63 ok(14, $dbh->{bfname} == 1234 );
64
65
66 # Check that an invalid entry is caught both for store & fetch
67 eval '$dbh->{fred} = 1234' ;
68 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
69 eval 'my $q = $dbh->{fred}' ;
70 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
71
72 # Now check the interface to RECNOINFO
73
74 my $X  ;
75 my @h ;
76 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
77
78 ok(18, ( (stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)) ;
79
80 #my $l = @h ;
81 my $l = $X->length ;
82 ok(19, !$l );
83
84 my @data = qw( a b c d ever f g h  i j k longername m n o p) ;
85
86 $h[0] = shift @data ;
87 ok(20, $h[0] eq 'a' );
88
89 my $ i;
90 foreach (@data)
91   { $h[++$i] = $_ }
92
93 unshift (@data, 'a') ;
94
95 ok(21, defined $h[1] );
96 ok(22, ! defined $h[16] );
97 ok(23, $X->length == @data );
98
99
100 # Overwrite an entry & check fetch it
101 $h[3] = 'replaced' ;
102 $data[3] = 'replaced' ;
103 ok(24, $h[3] eq 'replaced' );
104
105 #PUSH
106 my @push_data = qw(added to the end) ;
107 #my push (@h, @push_data) ;
108 $X->push(@push_data) ;
109 push (@data, @push_data) ;
110 ok(25, $h[++$i] eq 'added' );
111 ok(26, $h[++$i] eq 'to' );
112 ok(27, $h[++$i] eq 'the' );
113 ok(28, $h[++$i] eq 'end' );
114
115 # POP
116 my $popped = pop (@data) ;
117 #my $value = pop(@h) ;
118 my $value = $X->pop ;
119 ok(29, $value eq $popped) ;
120
121 # SHIFT
122 #$value = shift @h
123 $value = $X->shift ;
124 my $shifted = shift @data ;
125 ok(30, $value eq $shifted );
126
127 # UNSHIFT
128
129 # empty list
130 $X->unshift ;
131 ok(31, $X->length == @data );
132
133 my @new_data = qw(add this to the start of the array) ;
134 #unshift @h, @new_data ;
135 $X->unshift (@new_data) ;
136 unshift (@data, @new_data) ;
137 ok(32, $X->length == @data );
138 ok(33, $h[0] eq "add") ;
139 ok(34, $h[1] eq "this") ;
140 ok(35, $h[2] eq "to") ;
141 ok(36, $h[3] eq "the") ;
142 ok(37, $h[4] eq "start") ;
143 ok(38, $h[5] eq "of") ;
144 ok(39, $h[6] eq "the") ;
145 ok(40, $h[7] eq "array") ;
146 ok(41, $h[8] eq $data[8]) ;
147
148 # SPLICE
149
150 # Now both arrays should be identical
151
152 my $ok = 1 ;
153 my $j = 0 ;
154 foreach (@data)
155 {
156    $ok = 0, last if $_ ne $h[$j ++] ; 
157 }
158 ok(42, $ok );
159
160 # Neagtive subscripts
161
162 # get the last element of the array
163 ok(43, $h[-1] eq $data[-1] );
164 ok(44, $h[-1] eq $h[$X->length -1] );
165
166 # get the first element using a negative subscript
167 eval '$h[ - ( $X->length)] = "abcd"' ;
168 ok(45, $@ eq "" );
169 ok(46, $h[0] eq "abcd" );
170
171 # now try to read before the start of the array
172 eval '$h[ - (1 + $X->length)] = 1234' ;
173 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
174
175 # IMPORTANT - $X must be undefined before the untie otherwise the
176 #             underlying DB close routine will not get called.
177 undef $X ;
178 untie(@h);
179
180 unlink $Dfile;
181
182 exit ;