home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / t / lib / db-recno.t < prev    next >
Text File  |  1999-08-01  |  20KB  |  840 lines

  1. #!./perl -w
  2.  
  3. BEGIN {
  4.     unshift @INC, '../lib' if -d '../lib' ;
  5.     require Config; import Config;
  6.     if ($Config{'extensions'} !~ /\bDB_File\b/) {
  7.     print "1..0 # Skip: DB_File was not built\n";
  8.     exit 0;
  9.     }
  10. }
  11.  
  12. use DB_File; 
  13. use Fcntl;
  14. use strict ;
  15. use vars qw($dbh $Dfile $bad_ones $FA) ;
  16.  
  17. # full tied array support started in Perl 5.004_57
  18. # Double check to see if it is available.
  19.  
  20. {
  21.     sub try::TIEARRAY { bless [], "try" }
  22.     sub try::FETCHSIZE { $FA = 1 }
  23.     $FA = 0 ;
  24.     my @a ; 
  25.     tie @a, 'try' ;
  26.     my $a = @a ;
  27. }
  28.  
  29.  
  30. sub ok
  31. {
  32.     my $no = shift ;
  33.     my $result = shift ;
  34.  
  35.     print "not " unless $result ;
  36.     print "ok $no\n" ;
  37.  
  38.     return $result ;
  39. }
  40.  
  41. {
  42.     package Redirect ;
  43.     use Symbol ;
  44.  
  45.     sub new
  46.     {
  47.         my $class = shift ;
  48.         my $filename = shift ;
  49.     my $fh = gensym ;
  50.     open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
  51.     my $real_stdout = select($fh) ;
  52.     return bless [$fh, $real_stdout ] ;
  53.  
  54.     }
  55.     sub DESTROY
  56.     {
  57.         my $self = shift ;
  58.     close $self->[0] ;
  59.     select($self->[1]) ;
  60.     }
  61. }
  62.  
  63. sub docat
  64. {
  65.     my $file = shift;
  66.     local $/ = undef;
  67.     open(CAT,$file) || die "Cannot open $file:$!";
  68.     my $result = <CAT>;
  69.     close(CAT);
  70.     return $result;
  71. }
  72.  
  73. sub docat_del
  74.     my $file = shift;
  75.     local $/ = undef;
  76.     open(CAT,$file) || die "Cannot open $file: $!";
  77.     my $result = <CAT>;
  78.     close(CAT);
  79.     unlink $file ;
  80.     return $result;
  81. }   
  82.  
  83. sub bad_one
  84. {
  85.     print STDERR <<EOM unless $bad_ones++ ;
  86. #
  87. # Some older versions of Berkeley DB version 1 will fail tests 51,
  88. # 53 and 55.
  89. #
  90. # You can safely ignore the errors if you're never going to use the
  91. # broken functionality (recno databases with a modified bval). 
  92. # Otherwise you'll have to upgrade your DB library.
  93. #
  94. # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
  95. # last versions that were released. Berkeley DB version 2 is continually
  96. # being updated -- Check out http://www.sleepycat.com/ for more details.
  97. #
  98. EOM
  99. }
  100.  
  101. print "1..126\n";
  102.  
  103. my $Dfile = "recno.tmp";
  104. unlink $Dfile ;
  105.  
  106. umask(0);
  107.  
  108. # Check the interface to RECNOINFO
  109.  
  110. my $dbh = new DB_File::RECNOINFO ;
  111. ok(1, ! defined $dbh->{bval}) ;
  112. ok(2, ! defined $dbh->{cachesize}) ;
  113. ok(3, ! defined $dbh->{psize}) ;
  114. ok(4, ! defined $dbh->{flags}) ;
  115. ok(5, ! defined $dbh->{lorder}) ;
  116. ok(6, ! defined $dbh->{reclen}) ;
  117. ok(7, ! defined $dbh->{bfname}) ;
  118.  
  119. $dbh->{bval} = 3000 ;
  120. ok(8, $dbh->{bval} == 3000 );
  121.  
  122. $dbh->{cachesize} = 9000 ;
  123. ok(9, $dbh->{cachesize} == 9000 );
  124.  
  125. $dbh->{psize} = 400 ;
  126. ok(10, $dbh->{psize} == 400 );
  127.  
  128. $dbh->{flags} = 65 ;
  129. ok(11, $dbh->{flags} == 65 );
  130.  
  131. $dbh->{lorder} = 123 ;
  132. ok(12, $dbh->{lorder} == 123 );
  133.  
  134. $dbh->{reclen} = 1234 ;
  135. ok(13, $dbh->{reclen} == 1234 );
  136.  
  137. $dbh->{bfname} = 1234 ;
  138. ok(14, $dbh->{bfname} == 1234 );
  139.  
  140.  
  141. # Check that an invalid entry is caught both for store & fetch
  142. eval '$dbh->{fred} = 1234' ;
  143. ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
  144. eval 'my $q = $dbh->{fred}' ;
  145. ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
  146.  
  147. # Now check the interface to RECNOINFO
  148.  
  149. my $X  ;
  150. my @h ;
  151. ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
  152.  
  153. ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
  154.     ||  $^O eq 'MSWin32' || $^O eq 'amigaos') ;
  155.  
  156. #my $l = @h ;
  157. my $l = $X->length ;
  158. ok(19, ($FA ? @h == 0 : !$l) );
  159.  
  160. my @data = qw( a b c d ever f g h  i j k longername m n o p) ;
  161.  
  162. $h[0] = shift @data ;
  163. ok(20, $h[0] eq 'a' );
  164.  
  165. my $ i;
  166. foreach (@data)
  167.   { $h[++$i] = $_ }
  168.  
  169. unshift (@data, 'a') ;
  170.  
  171. ok(21, defined $h[1] );
  172. ok(22, ! defined $h[16] );
  173. ok(23, $FA ? @h == @data : $X->length == @data );
  174.  
  175.  
  176. # Overwrite an entry & check fetch it
  177. $h[3] = 'replaced' ;
  178. $data[3] = 'replaced' ;
  179. ok(24, $h[3] eq 'replaced' );
  180.  
  181. #PUSH
  182. my @push_data = qw(added to the end) ;
  183. ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
  184. push (@data, @push_data) ;
  185. ok(25, $h[++$i] eq 'added' );
  186. ok(26, $h[++$i] eq 'to' );
  187. ok(27, $h[++$i] eq 'the' );
  188. ok(28, $h[++$i] eq 'end' );
  189.  
  190. # POP
  191. my $popped = pop (@data) ;
  192. my $value = ($FA ? pop @h : $X->pop) ;
  193. ok(29, $value eq $popped) ;
  194.  
  195. # SHIFT
  196. $value = ($FA ? shift @h : $X->shift) ;
  197. my $shifted = shift @data ;
  198. ok(30, $value eq $shifted );
  199.  
  200. # UNSHIFT
  201.  
  202. # empty list
  203. ($FA ? unshift @h : $X->unshift) ;
  204. ok(31, ($FA ? @h == @data : $X->length == @data ));
  205.  
  206. my @new_data = qw(add this to the start of the array) ;
  207. $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
  208. unshift (@data, @new_data) ;
  209. ok(32, $FA ? @h == @data : $X->length == @data );
  210. ok(33, $h[0] eq "add") ;
  211. ok(34, $h[1] eq "this") ;
  212. ok(35, $h[2] eq "to") ;
  213. ok(36, $h[3] eq "the") ;
  214. ok(37, $h[4] eq "start") ;
  215. ok(38, $h[5] eq "of") ;
  216. ok(39, $h[6] eq "the") ;
  217. ok(40, $h[7] eq "array") ;
  218. ok(41, $h[8] eq $data[8]) ;
  219.  
  220. # SPLICE
  221.  
  222. # Now both arrays should be identical
  223.  
  224. my $ok = 1 ;
  225. my $j = 0 ;
  226. foreach (@data)
  227. {
  228.    $ok = 0, last if $_ ne $h[$j ++] ; 
  229. }
  230. ok(42, $ok );
  231.  
  232. # Neagtive subscripts
  233.  
  234. # get the last element of the array
  235. ok(43, $h[-1] eq $data[-1] );
  236. ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
  237.  
  238. # get the first element using a negative subscript
  239. eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
  240. ok(45, $@ eq "" );
  241. ok(46, $h[0] eq "abcd" );
  242.  
  243. # now try to read before the start of the array
  244. eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
  245. ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
  246.  
  247. # IMPORTANT - $X must be undefined before the untie otherwise the
  248. #             underlying DB close routine will not get called.
  249. undef $X ;
  250. untie(@h);
  251.  
  252. unlink $Dfile;
  253.  
  254.  
  255. {
  256.     # Check bval defaults to \n
  257.  
  258.     my @h = () ;
  259.     my $dbh = new DB_File::RECNOINFO ;
  260.     ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
  261.     $h[0] = "abc" ;
  262.     $h[1] = "def" ;
  263.     $h[3] = "ghi" ;
  264.     untie @h ;
  265.     my $x = docat($Dfile) ;
  266.     unlink $Dfile;
  267.     ok(49, $x eq "abc\ndef\n\nghi\n") ;
  268. }
  269.  
  270. {
  271.     # Change bval
  272.  
  273.     my @h = () ;
  274.     my $dbh = new DB_File::RECNOINFO ;
  275.     $dbh->{bval} = "-" ;
  276.     ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
  277.     $h[0] = "abc" ;
  278.     $h[1] = "def" ;
  279.     $h[3] = "ghi" ;
  280.     untie @h ;
  281.     my $x = docat($Dfile) ;
  282.     unlink $Dfile;
  283.     my $ok = ($x eq "abc-def--ghi-") ;
  284.     bad_one() unless $ok ;
  285.     ok(51, $ok) ;
  286. }
  287.  
  288. {
  289.     # Check R_FIXEDLEN with default bval (space)
  290.  
  291.     my @h = () ;
  292.     my $dbh = new DB_File::RECNOINFO ;
  293.     $dbh->{flags} = R_FIXEDLEN ;
  294.     $dbh->{reclen} = 5 ;
  295.     ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
  296.     $h[0] = "abc" ;
  297.     $h[1] = "def" ;
  298.     $h[3] = "ghi" ;
  299.     untie @h ;
  300.     my $x = docat($Dfile) ;
  301.     unlink $Dfile;
  302.     my $ok = ($x eq "abc  def       ghi  ") ;
  303.     bad_one() unless $ok ;
  304.     ok(53, $ok) ;
  305. }
  306.  
  307. {
  308.     # Check R_FIXEDLEN with user-defined bval
  309.  
  310.     my @h = () ;
  311.     my $dbh = new DB_File::RECNOINFO ;
  312.     $dbh->{flags} = R_FIXEDLEN ;
  313.     $dbh->{bval} = "-" ;
  314.     $dbh->{reclen} = 5 ;
  315.     ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
  316.     $h[0] = "abc" ;
  317.     $h[1] = "def" ;
  318.     $h[3] = "ghi" ;
  319.     untie @h ;
  320.     my $x = docat($Dfile) ;
  321.     unlink $Dfile;
  322.     my $ok = ($x eq "abc--def-------ghi--") ;
  323.     bad_one() unless $ok ;
  324.     ok(55, $ok) ;
  325. }
  326.  
  327. {
  328.     # check that attempting to tie an associative array to a DB_RECNO will fail
  329.  
  330.     my $filename = "xyz" ;
  331.     my %x ;
  332.     eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
  333.     ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
  334.     unlink $filename ;
  335. }
  336.  
  337. {
  338.    # sub-class test
  339.  
  340.    package Another ;
  341.  
  342.    use strict ;
  343.  
  344.    open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
  345.    print FILE <<'EOM' ;
  346.  
  347.    package SubDB ;
  348.  
  349.    use strict ;
  350.    use vars qw( @ISA @EXPORT) ;
  351.  
  352.    require Exporter ;
  353.    use DB_File;
  354.    @ISA=qw(DB_File);
  355.    @EXPORT = @DB_File::EXPORT ;
  356.  
  357.    sub STORE { 
  358.     my $self = shift ;
  359.         my $key = shift ;
  360.         my $value = shift ;
  361.         $self->SUPER::STORE($key, $value * 2) ;
  362.    }
  363.  
  364.    sub FETCH { 
  365.     my $self = shift ;
  366.         my $key = shift ;
  367.         $self->SUPER::FETCH($key) - 1 ;
  368.    }
  369.  
  370.    sub put { 
  371.     my $self = shift ;
  372.         my $key = shift ;
  373.         my $value = shift ;
  374.         $self->SUPER::put($key, $value * 3) ;
  375.    }
  376.  
  377.    sub get { 
  378.     my $self = shift ;
  379.         $self->SUPER::get($_[0], $_[1]) ;
  380.     $_[1] -= 2 ;
  381.    }
  382.  
  383.    sub A_new_method
  384.    {
  385.     my $self = shift ;
  386.         my $key = shift ;
  387.         my $value = $self->FETCH($key) ;
  388.     return "[[$value]]" ;
  389.    }
  390.  
  391.    1 ;
  392. EOM
  393.  
  394.     close FILE ;
  395.  
  396.     BEGIN { push @INC, '.'; } 
  397.     eval 'use SubDB ; ';
  398.     main::ok(57, $@ eq "") ;
  399.     my @h ;
  400.     my $X ;
  401.     eval '
  402.     $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
  403.     ' ;
  404.  
  405.     main::ok(58, $@ eq "") ;
  406.  
  407.     my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
  408.     main::ok(59, $@ eq "") ;
  409.     main::ok(60, $ret == 5) ;
  410.  
  411.     my $value = 0;
  412.     $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
  413.     main::ok(61, $@ eq "") ;
  414.     main::ok(62, $ret == 10) ;
  415.  
  416.     $ret = eval ' R_NEXT eq main::R_NEXT ' ;
  417.     main::ok(63, $@ eq "" ) ;
  418.     main::ok(64, $ret == 1) ;
  419.  
  420.     $ret = eval '$X->A_new_method(1) ' ;
  421.     main::ok(65, $@ eq "") ;
  422.     main::ok(66, $ret eq "[[11]]") ;
  423.  
  424.     undef $X;
  425.     untie(@h);
  426.     unlink "SubDB.pm", "recno.tmp" ;
  427.  
  428. }
  429.  
  430. {
  431.  
  432.     # test $#
  433.     my $self ;
  434.     unlink $Dfile;
  435.     ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
  436.     $h[0] = "abc" ;
  437.     $h[1] = "def" ;
  438.     $h[2] = "ghi" ;
  439.     $h[3] = "jkl" ;
  440.     ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
  441.     undef $self ;
  442.     untie @h ;
  443.     my $x = docat($Dfile) ;
  444.     ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
  445.  
  446.     # $# sets array to same length
  447.     ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
  448.     if ($FA)
  449.       { $#h = 3 }
  450.     else 
  451.       { $self->STORESIZE(4) }
  452.     ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
  453.     undef $self ;
  454.     untie @h ;
  455.     $x = docat($Dfile) ;
  456.     ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
  457.  
  458.     # $# sets array to bigger
  459.     ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
  460.     if ($FA)
  461.       { $#h = 6 }
  462.     else 
  463.       { $self->STORESIZE(7) }
  464.     ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
  465.     undef $self ;
  466.     untie @h ;
  467.     $x = docat($Dfile) ;
  468.     ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
  469.  
  470.     # $# sets array smaller
  471.     ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
  472.     if ($FA)
  473.       { $#h = 2 }
  474.     else 
  475.       { $self->STORESIZE(3) }
  476.     ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
  477.     undef $self ;
  478.     untie @h ;
  479.     $x = docat($Dfile) ;
  480.     ok(78, $x eq "abc\ndef\nghi\n") ;
  481.  
  482.     unlink $Dfile;
  483.  
  484.  
  485. }
  486.  
  487. {
  488.    # DBM Filter tests
  489.    use strict ;
  490.    my (@h, $db) ;
  491.    my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  492.    unlink $Dfile;
  493.  
  494.    sub checkOutput
  495.    {
  496.        my($fk, $sk, $fv, $sv) = @_ ;
  497.        return
  498.            $fetch_key eq $fk && $store_key eq $sk && 
  499.        $fetch_value eq $fv && $store_value eq $sv &&
  500.        $_ eq 'original' ;
  501.    }
  502.    
  503.    ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
  504.  
  505.    $db->filter_fetch_key   (sub { $fetch_key = $_ }) ;
  506.    $db->filter_store_key   (sub { $store_key = $_ }) ;
  507.    $db->filter_fetch_value (sub { $fetch_value = $_}) ;
  508.    $db->filter_store_value (sub { $store_value = $_ }) ;
  509.  
  510.    $_ = "original" ;
  511.  
  512.    $h[0] = "joe" ;
  513.    #                   fk   sk     fv   sv
  514.    ok(80, checkOutput( "", 0, "", "joe")) ;
  515.  
  516.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  517.    ok(81, $h[0] eq "joe");
  518.    #                   fk  sk  fv    sv
  519.    ok(82, checkOutput( "", 0, "joe", "")) ;
  520.  
  521.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  522.    ok(83, $db->FIRSTKEY() == 0) ;
  523.    #                    fk     sk  fv  sv
  524.    ok(84, checkOutput( 0, "", "", "")) ;
  525.  
  526.    # replace the filters, but remember the previous set
  527.    my ($old_fk) = $db->filter_fetch_key   
  528.                (sub { ++ $_ ; $fetch_key = $_ }) ;
  529.    my ($old_sk) = $db->filter_store_key   
  530.                (sub { $_ *= 2 ; $store_key = $_ }) ;
  531.    my ($old_fv) = $db->filter_fetch_value 
  532.                (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
  533.    my ($old_sv) = $db->filter_store_value 
  534.                (sub { s/o/x/g; $store_value = $_ }) ;
  535.    
  536.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  537.    $h[1] = "Joe" ;
  538.    #                   fk   sk     fv    sv
  539.    ok(85, checkOutput( "", 2, "", "Jxe")) ;
  540.  
  541.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  542.    ok(86, $h[1] eq "[Jxe]");
  543.    #                   fk   sk     fv    sv
  544.    ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
  545.  
  546.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  547.    ok(88, $db->FIRSTKEY() == 1) ;
  548.    #                   fk   sk     fv    sv
  549.    ok(89, checkOutput( 1, "", "", "")) ;
  550.    
  551.    # put the original filters back
  552.    $db->filter_fetch_key   ($old_fk);
  553.    $db->filter_store_key   ($old_sk);
  554.    $db->filter_fetch_value ($old_fv);
  555.    $db->filter_store_value ($old_sv);
  556.  
  557.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  558.    $h[0] = "joe" ;
  559.    ok(90, checkOutput( "", 0, "", "joe")) ;
  560.  
  561.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  562.    ok(91, $h[0] eq "joe");
  563.    ok(92, checkOutput( "", 0, "joe", "")) ;
  564.  
  565.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  566.    ok(93, $db->FIRSTKEY() == 0) ;
  567.    ok(94, checkOutput( 0, "", "", "")) ;
  568.  
  569.    # delete the filters
  570.    $db->filter_fetch_key   (undef);
  571.    $db->filter_store_key   (undef);
  572.    $db->filter_fetch_value (undef);
  573.    $db->filter_store_value (undef);
  574.  
  575.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  576.    $h[0] = "joe" ;
  577.    ok(95, checkOutput( "", "", "", "")) ;
  578.  
  579.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  580.    ok(96, $h[0] eq "joe");
  581.    ok(97, checkOutput( "", "", "", "")) ;
  582.  
  583.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  584.    ok(98, $db->FIRSTKEY() == 0) ;
  585.    ok(99, checkOutput( "", "", "", "")) ;
  586.  
  587.    undef $db ;
  588.    untie @h;
  589.    unlink $Dfile;
  590. }
  591.  
  592. {    
  593.     # DBM Filter with a closure
  594.  
  595.     use strict ;
  596.     my (@h, $db) ;
  597.  
  598.     unlink $Dfile;
  599.     ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
  600.  
  601.     my %result = () ;
  602.  
  603.     sub Closure
  604.     {
  605.         my ($name) = @_ ;
  606.     my $count = 0 ;
  607.     my @kept = () ;
  608.  
  609.     return sub { ++$count ; 
  610.              push @kept, $_ ; 
  611.              $result{$name} = "$name - $count: [@kept]" ;
  612.            }
  613.     }
  614.  
  615.     $db->filter_store_key(Closure("store key")) ;
  616.     $db->filter_store_value(Closure("store value")) ;
  617.     $db->filter_fetch_key(Closure("fetch key")) ;
  618.     $db->filter_fetch_value(Closure("fetch value")) ;
  619.  
  620.     $_ = "original" ;
  621.  
  622.     $h[0] = "joe" ;
  623.     ok(101, $result{"store key"} eq "store key - 1: [0]");
  624.     ok(102, $result{"store value"} eq "store value - 1: [joe]");
  625.     ok(103, ! defined $result{"fetch key"} );
  626.     ok(104, ! defined $result{"fetch value"} );
  627.     ok(105, $_ eq "original") ;
  628.  
  629.     ok(106, $db->FIRSTKEY() == 0 ) ;
  630.     ok(107, $result{"store key"} eq "store key - 1: [0]");
  631.     ok(108, $result{"store value"} eq "store value - 1: [joe]");
  632.     ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
  633.     ok(110, ! defined $result{"fetch value"} );
  634.     ok(111, $_ eq "original") ;
  635.  
  636.     $h[7]  = "john" ;
  637.     ok(112, $result{"store key"} eq "store key - 2: [0 7]");
  638.     ok(113, $result{"store value"} eq "store value - 2: [joe john]");
  639.     ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
  640.     ok(115, ! defined $result{"fetch value"} );
  641.     ok(116, $_ eq "original") ;
  642.  
  643.     ok(117, $h[0] eq "joe");
  644.     ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
  645.     ok(119, $result{"store value"} eq "store value - 2: [joe john]");
  646.     ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
  647.     ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
  648.     ok(122, $_ eq "original") ;
  649.  
  650.     undef $db ;
  651.     untie @h;
  652.     unlink $Dfile;
  653. }        
  654.  
  655. {
  656.    # DBM Filter recursion detection
  657.    use strict ;
  658.    my (@h, $db) ;
  659.    unlink $Dfile;
  660.  
  661.    ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
  662.  
  663.    $db->filter_store_key (sub { $_ = $h[0] }) ;
  664.  
  665.    eval '$h[1] = 1234' ;
  666.    ok(124, $@ =~ /^recursion detected in filter_store_key at/ );
  667.    
  668.    undef $db ;
  669.    untie @h;
  670.    unlink $Dfile;
  671. }
  672.  
  673.  
  674. {
  675.    # Examples from the POD
  676.  
  677.   my $file = "xyzt" ;
  678.   {
  679.     my $redirect = new Redirect $file ;
  680.  
  681.     use strict ;
  682.     use DB_File ;
  683.  
  684.     my $filename = "text" ;
  685.     unlink $filename ;
  686.  
  687.     my @h ;
  688.     my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO 
  689.         or die "Cannot open file 'text': $!\n" ;
  690.  
  691.     # Add a few key/value pairs to the file
  692.     $h[0] = "orange" ;
  693.     $h[1] = "blue" ;
  694.     $h[2] = "yellow" ;
  695.  
  696.     $FA ? push @h, "green", "black" 
  697.         : $x->push("green", "black") ;
  698.  
  699.     my $elements = $FA ? scalar @h : $x->length ;
  700.     print "The array contains $elements entries\n" ;
  701.  
  702.     my $last = $FA ? pop @h : $x->pop ;
  703.     print "popped $last\n" ;
  704.  
  705.     $FA ? unshift @h, "white" 
  706.         : $x->unshift("white") ;
  707.     my $first = $FA ? shift @h : $x->shift ;
  708.     print "shifted $first\n" ;
  709.  
  710.     # Check for existence of a key
  711.     print "Element 1 Exists with value $h[1]\n" if $h[1] ;
  712.  
  713.     # use a negative index
  714.     print "The last element is $h[-1]\n" ;
  715.     print "The 2nd last element is $h[-2]\n" ;
  716.  
  717.     undef $x ;
  718.     untie @h ;
  719.  
  720.     unlink $filename ;
  721.   }  
  722.  
  723.   ok(125, docat_del($file) eq <<'EOM') ;
  724. The array contains 5 entries
  725. popped black
  726. shifted white
  727. Element 1 Exists with value blue
  728. The last element is green
  729. The 2nd last element is yellow
  730. EOM
  731.  
  732.   my $save_output = "xyzt" ;
  733.   {
  734.     my $redirect = new Redirect $save_output ;
  735.  
  736.     use strict ;
  737.     use vars qw(@h $H $file $i) ;
  738.     use DB_File ;
  739.     use Fcntl ;
  740.     
  741.     $file = "text" ;
  742.  
  743.     unlink $file ;
  744.  
  745.     $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO 
  746.         or die "Cannot open file $file: $!\n" ;
  747.     
  748.     # first create a text file to play with
  749.     $h[0] = "zero" ;
  750.     $h[1] = "one" ;
  751.     $h[2] = "two" ;
  752.     $h[3] = "three" ;
  753.     $h[4] = "four" ;
  754.  
  755.     
  756.     # Print the records in order.
  757.     #
  758.     # The length method is needed here because evaluating a tied
  759.     # array in a scalar context does not return the number of
  760.     # elements in the array.  
  761.  
  762.     print "\nORIGINAL\n" ;
  763.     foreach $i (0 .. $H->length - 1) {
  764.         print "$i: $h[$i]\n" ;
  765.     }
  766.  
  767.     # use the push & pop methods
  768.     $a = $H->pop ;
  769.     $H->push("last") ;
  770.     print "\nThe last record was [$a]\n" ;
  771.  
  772.     # and the shift & unshift methods
  773.     $a = $H->shift ;
  774.     $H->unshift("first") ;
  775.     print "The first record was [$a]\n" ;
  776.  
  777.     # Use the API to add a new record after record 2.
  778.     $i = 2 ;
  779.     $H->put($i, "Newbie", R_IAFTER) ;
  780.  
  781.     # and a new record before record 1.
  782.     $i = 1 ;
  783.     $H->put($i, "New One", R_IBEFORE) ;
  784.  
  785.     # delete record 3
  786.     $H->del(3) ;
  787.  
  788.     # now print the records in reverse order
  789.     print "\nREVERSE\n" ;
  790.     for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
  791.       { print "$i: $h[$i]\n" }
  792.  
  793.     # same again, but use the API functions instead
  794.     print "\nREVERSE again\n" ;
  795.     my ($s, $k, $v)  = (0, 0, 0) ;
  796.     for ($s = $H->seq($k, $v, R_LAST) ; 
  797.              $s == 0 ; 
  798.              $s = $H->seq($k, $v, R_PREV))
  799.       { print "$k: $v\n" }
  800.  
  801.     undef $H ;
  802.     untie @h ;    
  803.  
  804.     unlink $file ;
  805.   }  
  806.  
  807.   ok(126, docat_del($save_output) eq <<'EOM') ;
  808.  
  809. ORIGINAL
  810. 0: zero
  811. 1: one
  812. 2: two
  813. 3: three
  814. 4: four
  815.  
  816. The last record was [four]
  817. The first record was [zero]
  818.  
  819. REVERSE
  820. 5: last
  821. 4: three
  822. 3: Newbie
  823. 2: one
  824. 1: New One
  825. 0: first
  826.  
  827. REVERSE again
  828. 5: last
  829. 4: three
  830. 3: Newbie
  831. 2: one
  832. 1: New One
  833. 0: first
  834. EOM
  835.    
  836. }
  837.  
  838. exit ;
  839.