examples3.t
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. #!./perl -w
  2. use strict ; 
  3. BEGIN {
  4.     unless(grep /blib/, @INC) {
  5.         chdir 't' if -d 't';
  6.         @INC = '../lib' if -d '../lib';
  7.     }
  8. }
  9. use BerkeleyDB; 
  10. use File::Path qw(rmtree);
  11. BEGIN 
  12. {
  13.     if ($BerkeleyDB::db_version < 3) {
  14.         print "1..0 # Skipping test, this needs Berkeley DB 3.x or bettern" ;
  15.         exit 0 ;
  16.     }
  17. }
  18. print "1..2n";
  19. my $FA = 0 ;
  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.     package LexFile ;
  30.     sub new
  31.     {
  32. my $self = shift ;
  33. unlink @_ ;
  34.   bless [ @_ ], $self ;
  35.     }
  36.     sub DESTROY
  37.     {
  38. my $self = shift ;
  39. unlink @{ $self } ;
  40.     }
  41. }
  42. sub ok
  43. {
  44.     my $no = shift ;
  45.     my $result = shift ;
  46.  
  47.     print "not " unless $result ;
  48.     print "ok $non" ;
  49. }
  50. {
  51.     package Redirect ;
  52.     use Symbol ;
  53.     sub new
  54.     {
  55.         my $class = shift ;
  56.         my $filename = shift ;
  57. my $fh = gensym ;
  58. open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
  59. my $real_stdout = select($fh) ;
  60. return bless [$fh, $real_stdout ] ;
  61.     }
  62.     sub DESTROY
  63.     {
  64.         my $self = shift ;
  65. close $self->[0] ;
  66. select($self->[1]) ;
  67.     }
  68. }
  69. sub docat
  70. {
  71.     my $file = shift;
  72.     local $/ = undef;
  73.     open(CAT,$file) || die "Cannot open $file:$!";
  74.     my $result = <CAT> || "" ;
  75.     close(CAT);
  76.     return $result;
  77. }
  78. sub docat_del
  79.     my $file = shift;
  80.     local $/ = undef;
  81.     open(CAT,$file) || die "Cannot open $file: $!";
  82.     my $result = <CAT> || "" ;
  83.     close(CAT);
  84.     unlink $file ;
  85.     return $result;
  86. }   
  87. my $Dfile = "dbhash.tmp";
  88. my $Dfile2 = "dbhash2.tmp";
  89. my $Dfile3 = "dbhash3.tmp";
  90. unlink $Dfile;
  91. umask(0) ;
  92. my $redirect = "xyzt" ;
  93. {
  94. my $redirect = "xyzt" ;
  95.  {
  96.     my $redirectObj = new Redirect $redirect ;
  97.     use strict ;
  98.     use BerkeleyDB ;
  99.     
  100.     my $filename = "fruit" ;
  101.     unlink $filename ;
  102.     my $db = new BerkeleyDB::Hash 
  103.                 -Filename => $filename, 
  104. -Flags    => DB_CREATE,
  105. -Property  => DB_DUP
  106.         or die "Cannot open file $filename: $! $BerkeleyDB::Errorn" ;
  107.     # Add a few key/value pairs to the file
  108.     $db->db_put("red", "apple") ;
  109.     $db->db_put("orange", "orange") ;
  110.     $db->db_put("green", "banana") ;
  111.     $db->db_put("yellow", "banana") ;
  112.     $db->db_put("red", "tomato") ;
  113.     $db->db_put("green", "apple") ;
  114.     
  115.     # print the contents of the file
  116.     my ($k, $v) = ("", "") ;
  117.     my $cursor = $db->db_cursor() ;
  118.     while ($cursor->c_get($k, $v, DB_NEXT) == 0)
  119.       { print "$k -> $vn" }
  120.       
  121.     undef $cursor ;
  122.     undef $db ;
  123.     unlink $filename ;
  124.  }
  125.   #print "[" . docat($redirect) . "]" ;
  126.   ok(1, docat_del($redirect) eq <<'EOM') ;
  127. orange -> orange
  128. yellow -> banana
  129. red -> apple
  130. red -> tomato
  131. green -> banana
  132. green -> apple
  133. EOM
  134. }
  135. {
  136. my $redirect = "xyzt" ;
  137.  {
  138.     my $redirectObj = new Redirect $redirect ;
  139.     use strict ;
  140.     use BerkeleyDB ;
  141.     
  142.     my $filename = "fruit" ;
  143.     unlink $filename ;
  144.     my $db = new BerkeleyDB::Hash 
  145.                 -Filename => $filename, 
  146. -Flags    => DB_CREATE,
  147. -Property  => DB_DUP | DB_DUPSORT
  148.         or die "Cannot open file $filename: $! $BerkeleyDB::Errorn" ;
  149.     # Add a few key/value pairs to the file
  150.     $db->db_put("red", "apple") ;
  151.     $db->db_put("orange", "orange") ;
  152.     $db->db_put("green", "banana") ;
  153.     $db->db_put("yellow", "banana") ;
  154.     $db->db_put("red", "tomato") ;
  155.     $db->db_put("green", "apple") ;
  156.     
  157.     # print the contents of the file
  158.     my ($k, $v) = ("", "") ;
  159.     my $cursor = $db->db_cursor() ;
  160.     while ($cursor->c_get($k, $v, DB_NEXT) == 0)
  161.       { print "$k -> $vn" }
  162.       
  163.     undef $cursor ;
  164.     undef $db ;
  165.     unlink $filename ;
  166.  }
  167.   #print "[" . docat($redirect) . "]" ;
  168.   ok(2, docat_del($redirect) eq <<'EOM') ;
  169. orange -> orange
  170. yellow -> banana
  171. red -> apple
  172. red -> tomato
  173. green -> apple
  174. green -> banana
  175. EOM
  176. }