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

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 Config;
  11. #
  12. #BEGIN {
  13. #    if(-d "lib" && -f "TEST") {
  14. #        if ($Config{'extensions'} !~ /bBerkeleyDBb/ ) {
  15. #            print "1..74n";
  16. #            exit 0;
  17. #        }
  18. #    }
  19. #}
  20. use BerkeleyDB; 
  21. use File::Path qw(rmtree);
  22. BEGIN
  23. {
  24.     if ($BerkeleyDB::db_version < 3.1) {
  25.         print "1..0 # Skipping test, this needs Berkeley DB 3.1.x or bettern" ;
  26.         exit 0 ;
  27.     }
  28. }     
  29. print "1..25n";
  30. my %DB_errors = (
  31.     'DB_INCOMPLETE' => "DB_INCOMPLETE: Sync was unable to complete",
  32.     'DB_KEYEMPTY' => "DB_KEYEMPTY: Non-existent key/data pair",
  33.     'DB_KEYEXIST' => "DB_KEYEXIST: Key/data pair already exists",
  34.     'DB_LOCK_DEADLOCK'  => "DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock",
  35.     'DB_LOCK_NOTGRANTED' => "DB_LOCK_NOTGRANTED: Lock not granted",
  36.     'DB_NOTFOUND' => "DB_NOTFOUND: No matching key/data pair found",
  37.     'DB_OLD_VERSION' => "DB_OLDVERSION: Database requires a version upgrade",
  38.     'DB_RUNRECOVERY' => "DB_RUNRECOVERY: Fatal error, run database recovery",
  39. ) ;
  40. {
  41.     package LexFile ;
  42.     sub new
  43.     {
  44. my $self = shift ;
  45. unlink @_ ;
  46.   bless [ @_ ], $self ;
  47.     }
  48.     sub DESTROY
  49.     {
  50. my $self = shift ;
  51. unlink @{ $self } ;
  52.     }
  53. }
  54. sub ok
  55. {
  56.     my $no = shift ;
  57.     my $result = shift ;
  58.  
  59.     print "not " unless $result ;
  60.     print "ok $non" ;
  61. }
  62. my $Dfile = "dbhash.tmp";
  63. my $Dfile2 = "dbhash2.tmp";
  64. my $Dfile3 = "dbhash3.tmp";
  65. unlink $Dfile;
  66. umask(0) ;
  67. {
  68.     # c_count
  69.     my $lex = new LexFile $Dfile ;
  70.     my %hash ;
  71.     ok 1, my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
  72.       -Property  => DB_DUP,
  73.                                       -Flags    => DB_CREATE ;
  74.     $hash{'Wall'} = 'Larry' ;
  75.     $hash{'Wall'} = 'Stone' ;
  76.     $hash{'Smith'} = 'John' ;
  77.     $hash{'Wall'} = 'Brick' ;
  78.     $hash{'Wall'} = 'Brick' ;
  79.     $hash{'mouse'} = 'mickey' ;
  80.     ok 2, keys %hash == 6 ;
  81.     # create a cursor
  82.     ok 3, my $cursor = $db->db_cursor() ;
  83.     my $key = "Wall" ;
  84.     my $value ;
  85.     ok 4, $cursor->c_get($key, $value, DB_SET) == 0 ;
  86.     ok 5, $key eq "Wall" && $value eq "Larry" ;
  87.     my $count ;
  88.     ok 6, $cursor->c_count($count) == 0 ;
  89.     ok 7, $count == 4 ;
  90.     $key = "Smith" ;
  91.     ok 8, $cursor->c_get($key, $value, DB_SET) == 0 ;
  92.     ok 9, $key eq "Smith" && $value eq "John" ;
  93.     ok 10, $cursor->c_count($count) == 0 ;
  94.     ok 11, $count == 1 ;
  95.     undef $db ;
  96.     undef $cursor ;
  97.     untie %hash ;
  98. }
  99. {
  100.     # db_key_range
  101.     my $lex = new LexFile $Dfile ;
  102.     my %hash ;
  103.     ok 12, my $db = tie %hash, 'BerkeleyDB::Btree', -Filename => $Dfile,
  104.       -Property  => DB_DUP,
  105.                                       -Flags    => DB_CREATE ;
  106.     $hash{'Wall'} = 'Larry' ;
  107.     $hash{'Wall'} = 'Stone' ;
  108.     $hash{'Smith'} = 'John' ;
  109.     $hash{'Wall'} = 'Brick' ;
  110.     $hash{'Wall'} = 'Brick' ;
  111.     $hash{'mouse'} = 'mickey' ;
  112.     ok 13, keys %hash == 6 ;
  113.     my $key = "Wall" ;
  114.     my ($less, $equal, $greater) ;
  115.     ok 14, $db->db_key_range($key, $less, $equal, $greater) == 0 ;
  116.     ok 15, $less != 0 ;
  117.     ok 16, $equal != 0 ;
  118.     ok 17, $greater != 0 ;
  119.     $key = "Smith" ;
  120.     ok 18, $db->db_key_range($key, $less, $equal, $greater) == 0 ;
  121.     ok 19, $less == 0 ;
  122.     ok 20, $equal != 0 ;
  123.     ok 21, $greater != 0 ;
  124.     $key = "NotThere" ;
  125.     ok 22, $db->db_key_range($key, $less, $equal, $greater) == 0 ;
  126.     ok 23, $less == 0 ;
  127.     ok 24, $equal == 0 ;
  128.     ok 25, $greater == 1 ;
  129.     undef $db ;
  130.     untie %hash ;
  131. }