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