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

MySQL数据库

开发平台:

Visual C++

  1. #!./perl -w
  2. # ID: %I%, %G%   
  3. use strict ;
  4. BEGIN {
  5.     unless(grep /blib/, @INC) {
  6.         chdir 't' if -d 't';
  7.         @INC = '../lib' if -d '../lib';
  8.     }
  9. }
  10. use BerkeleyDB; 
  11. use File::Path qw(rmtree);
  12. print "1..46n";
  13. {
  14.     package LexFile ;
  15.     sub new
  16.     {
  17. my $self = shift ;
  18. unlink @_ ;
  19.   bless [ @_ ], $self ;
  20.     }
  21.     sub DESTROY
  22.     {
  23. my $self = shift ;
  24. unlink @{ $self } ;
  25.     }
  26. }
  27. sub ok
  28. {
  29.     my $no = shift ;
  30.     my $result = shift ;
  31.  
  32.     print "not " unless $result ;
  33.     print "ok $non" ;
  34. }
  35. my $Dfile = "dbhash.tmp";
  36. unlink $Dfile;
  37. umask(0) ;
  38. {
  39.    # DBM Filter tests
  40.    use strict ;
  41.    my (%h, $db) ;
  42.    my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  43.    unlink $Dfile;
  44.    sub checkOutput
  45.    {
  46.        my($fk, $sk, $fv, $sv) = @_ ;
  47.        return
  48.            $fetch_key eq $fk && $store_key eq $sk && 
  49.    $fetch_value eq $fv && $store_value eq $sv &&
  50.    $_ eq 'original' ;
  51.    }
  52.    
  53.     ok 1, $db = tie %h, 'BerkeleyDB::Hash', 
  54.      -Filename   => $Dfile, 
  55.         -Flags      => DB_CREATE; 
  56.    $db->filter_fetch_key   (sub { $fetch_key = $_ }) ;
  57.    $db->filter_store_key   (sub { $store_key = $_ }) ;
  58.    $db->filter_fetch_value (sub { $fetch_value = $_}) ;
  59.    $db->filter_store_value (sub { $store_value = $_ }) ;
  60.    $_ = "original" ;
  61.    $h{"fred"} = "joe" ;
  62.    #                   fk   sk     fv   sv
  63.    ok 2, checkOutput( "", "fred", "", "joe") ;
  64.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  65.    ok 3, $h{"fred"} eq "joe";
  66.    #                   fk    sk     fv    sv
  67.    ok 4, checkOutput( "", "fred", "joe", "") ;
  68.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  69.    ok 5, $db->FIRSTKEY() eq "fred" ;
  70.    #                    fk     sk  fv  sv
  71.    ok 6, checkOutput( "fred", "", "", "") ;
  72.    # replace the filters, but remember the previous set
  73.    my ($old_fk) = $db->filter_fetch_key   
  74.     (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
  75.    my ($old_sk) = $db->filter_store_key   
  76.     (sub { $_ = lc $_ ; $store_key = $_ }) ;
  77.    my ($old_fv) = $db->filter_fetch_value 
  78.     (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
  79.    my ($old_sv) = $db->filter_store_value 
  80.     (sub { s/o/x/g; $store_value = $_ }) ;
  81.    
  82.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  83.    $h{"Fred"} = "Joe" ;
  84.    #                   fk   sk     fv    sv
  85.    ok 7, checkOutput( "", "fred", "", "Jxe") ;
  86.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  87.    ok 8, $h{"Fred"} eq "[Jxe]";
  88.    #                   fk   sk     fv    sv
  89.    ok 9, checkOutput( "", "fred", "[Jxe]", "") ;
  90.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  91.    ok 10, $db->FIRSTKEY() eq "FRED" ;
  92.    #                   fk   sk     fv    sv
  93.    ok 11, checkOutput( "FRED", "", "", "") ;
  94.    # put the original filters back
  95.    $db->filter_fetch_key   ($old_fk);
  96.    $db->filter_store_key   ($old_sk);
  97.    $db->filter_fetch_value ($old_fv);
  98.    $db->filter_store_value ($old_sv);
  99.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  100.    $h{"fred"} = "joe" ;
  101.    ok 12, checkOutput( "", "fred", "", "joe") ;
  102.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  103.    ok 13, $h{"fred"} eq "joe";
  104.    ok 14, checkOutput( "", "fred", "joe", "") ;
  105.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  106.    ok 15, $db->FIRSTKEY() eq "fred" ;
  107.    ok 16, checkOutput( "fred", "", "", "") ;
  108.    # delete the filters
  109.    $db->filter_fetch_key   (undef);
  110.    $db->filter_store_key   (undef);
  111.    $db->filter_fetch_value (undef);
  112.    $db->filter_store_value (undef);
  113.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  114.    $h{"fred"} = "joe" ;
  115.    ok 17, checkOutput( "", "", "", "") ;
  116.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  117.    ok 18, $h{"fred"} eq "joe";
  118.    ok 19, checkOutput( "", "", "", "") ;
  119.    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
  120.    ok 20, $db->FIRSTKEY() eq "fred" ;
  121.    ok 21, checkOutput( "", "", "", "") ;
  122.    undef $db ;
  123.    untie %h;
  124.    unlink $Dfile;
  125. }
  126. {    
  127.     # DBM Filter with a closure
  128.     use strict ;
  129.     my (%h, $db) ;
  130.     unlink $Dfile;
  131.     ok 22, $db = tie %h, 'BerkeleyDB::Hash', 
  132.      -Filename   => $Dfile, 
  133.         -Flags      => DB_CREATE; 
  134.     my %result = () ;
  135.     sub Closure
  136.     {
  137.         my ($name) = @_ ;
  138. my $count = 0 ;
  139. my @kept = () ;
  140. return sub { ++$count ; 
  141.      push @kept, $_ ; 
  142.      $result{$name} = "$name - $count: [@kept]" ;
  143.    }
  144.     }
  145.     $db->filter_store_key(Closure("store key"))  ;
  146.     $db->filter_store_value(Closure("store value")) ;
  147.     $db->filter_fetch_key(Closure("fetch key")) ;
  148.     $db->filter_fetch_value(Closure("fetch value")) ;
  149.     $_ = "original" ;
  150.     $h{"fred"} = "joe" ;
  151.     ok 23, $result{"store key"} eq "store key - 1: [fred]" ;
  152.     ok 24, $result{"store value"} eq "store value - 1: [joe]" ;
  153.     ok 25, ! defined $result{"fetch key"}  ;
  154.     ok 26, ! defined $result{"fetch value"}  ;
  155.     ok 27, $_ eq "original"  ;
  156.     ok 28, $db->FIRSTKEY() eq "fred"  ;
  157.     ok 29, $result{"store key"} eq "store key - 1: [fred]" ;
  158.     ok 30, $result{"store value"} eq "store value - 1: [joe]" ;
  159.     ok 31, $result{"fetch key"} eq "fetch key - 1: [fred]" ;
  160.     ok 32, ! defined $result{"fetch value"}  ;
  161.     ok 33, $_ eq "original"  ;
  162.     $h{"jim"}  = "john" ;
  163.     ok 34, $result{"store key"} eq "store key - 2: [fred jim]" ;
  164.     ok 35, $result{"store value"} eq "store value - 2: [joe john]" ;
  165.     ok 36, $result{"fetch key"} eq "fetch key - 1: [fred]" ;
  166.     ok 37, ! defined $result{"fetch value"}  ;
  167.     ok 38, $_ eq "original"  ;
  168.     ok 39, $h{"fred"} eq "joe" ;
  169.     ok 40, $result{"store key"} eq "store key - 3: [fred jim fred]" ;
  170.     ok 41, $result{"store value"} eq "store value - 2: [joe john]" ;
  171.     ok 42, $result{"fetch key"} eq "fetch key - 1: [fred]" ;
  172.     ok 43, $result{"fetch value"} eq "fetch value - 1: [joe]" ;
  173.     ok 44, $_ eq "original" ;
  174.     undef $db ;
  175.     untie %h;
  176.     unlink $Dfile;
  177. }
  178. {
  179.    # DBM Filter recursion detection
  180.    use strict ;
  181.    my (%h, $db) ;
  182.    unlink $Dfile;
  183.     ok 45, $db = tie %h, 'BerkeleyDB::Hash', 
  184.      -Filename   => $Dfile, 
  185.         -Flags      => DB_CREATE; 
  186.    $db->filter_store_key (sub { $_ = $h{$_} }) ;
  187.    eval '$h{1} = 1234' ;
  188.    ok 46, $@ =~ /^BerkeleyDB Aborting: recursion detected in filter_store_key at/ ;
  189.    #print "[$@]n" ;
  190.    
  191.    undef $db ;
  192.    untie %h;
  193.    unlink $Dfile;
  194. }