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