home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / Preinstalled MacPerl (FAT) / t / lib / db-hash.t < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.0 KB  |  257 lines  |  [TEXT/McPL]

  1. #!./perl
  2.  
  3. BEGIN {
  4.     chdir 't' if -d 't';
  5.     chdir '::' unless -d 'lib';
  6. #    @INC = '../lib';
  7.     @INC = '::lib';
  8.     require Config; import Config;
  9.     if ($Config{'extensions'} !~ /\bDB_File\b/) {
  10.     print "1..0\n";
  11.     exit 0;
  12.     }
  13. }
  14.  
  15. use DB_File; 
  16. use Fcntl;
  17.  
  18. print "1..43\n";
  19.  
  20. $Dfile = "Op.db-hash";
  21. unlink $Dfile;
  22.  
  23. # umask(0);
  24.  
  25. # Check the interface to HASHINFO
  26.  
  27. $dbh = TIEHASH DB_File::HASHINFO ;
  28. print (($dbh->{bsize} == undef) ? "ok 1\n" : "not ok 1\n") ;
  29. print (($dbh->{ffactor} == undef) ? "ok 2\n" : "not ok 2\n") ;
  30. print (($dbh->{nelem} == undef) ? "ok 3\n" : "not ok 3\n") ;
  31. print (($dbh->{cachesize} == undef) ? "ok 4\n" : "not ok 4\n") ;
  32. print (($dbh->{hash} == undef) ? "ok 5\n" : "not ok 5\n") ;
  33. print (($dbh->{lorder} == undef) ? "ok 6\n" : "not ok 6\n") ;
  34.  
  35. $dbh->{bsize} = 3000 ;
  36. print ($dbh->{bsize} == 3000 ? "ok 7\n" : "not ok 7\n") ;
  37.  
  38. $dbh->{ffactor} = 9000 ;
  39. print ($dbh->{ffactor} == 9000 ? "ok 8\n" : "not ok 8\n") ;
  40. #
  41. $dbh->{nelem} = 400 ;
  42. print (($dbh->{nelem} == 400) ? "ok 9\n" : "not ok 9\n") ;
  43.  
  44. $dbh->{cachesize} = 65 ;
  45. print (($dbh->{cachesize} == 65) ? "ok 10\n" : "not ok 10\n") ;
  46.  
  47. $dbh->{hash} = "abc" ;
  48. print (($dbh->{hash} eq "abc") ? "ok 11\n" : "not ok 11\n") ;
  49.  
  50. $dbh->{lorder} = 1234 ;
  51. print ($dbh->{lorder} == 1234 ? "ok 12\n" : "not ok 12\n") ;
  52.  
  53. # Check that an invalid entry is caught both for store & fetch
  54. eval '$dbh->{fred} = 1234' ;
  55. print ($@ eq '' ? "ok 13\n" : "not ok 13\n") ;
  56. eval '$q = $dbh->{fred}' ;
  57. print ($@ eq '' ? "ok 14\n" : "not ok 14\n") ;
  58.  
  59. # Now check the interface to HASH
  60.  
  61. print (($X = tie(%h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 15\n" : "not ok 15");
  62.  
  63. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  64.    $blksize,$blocks) = stat($Dfile);
  65. print (($mode & 0777) == 0640 ? "ok 16\n" : "not ok 16\n");
  66.  
  67. while (($key,$value) = each(%h)) {
  68.     $i++;
  69. }
  70. print (!$i ? "ok 17\n" : "not ok 17\n");
  71.  
  72. $h{'goner1'} = 'snork';
  73.  
  74. $h{'abc'} = 'ABC';
  75. print ($h{'abc'} == 'ABC' ? "ok 18\n" : "not ok 18\n") ;
  76. print (defined $h{'jimmy'} ? "not ok 19\n" : "ok 19\n");
  77.  
  78. $h{'def'} = 'DEF';
  79. $h{'jkl','mno'} = "JKL\034MNO";
  80. $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
  81. $h{'a'} = 'A';
  82.  
  83. #$h{'b'} = 'B';
  84. $X->STORE('b', 'B') ;
  85.  
  86. $h{'c'} = 'C';
  87.  
  88. #$h{'d'} = 'D';
  89. $X->put('d', 'D') ;
  90.  
  91. $h{'e'} = 'E';
  92. $h{'f'} = 'F';
  93. $h{'g'} = 'X';
  94. $h{'h'} = 'H';
  95. $h{'i'} = 'I';
  96.  
  97. $h{'goner2'} = 'snork';
  98. delete $h{'goner2'};
  99.  
  100.  
  101. # IMPORTANT - $X must be undefined before the untie otherwise the
  102. #             underlying DB close routine will not get called.
  103. undef $X ;
  104. untie(%h);
  105.  
  106.  
  107. # tie to the same file again, do not supply a type - should default to HASH
  108. print (($X = tie(%h,DB_File,$Dfile, O_RDWR, 0640)) ? "ok 20\n" : "not ok 20: $!\n");
  109.  
  110. # Modify an entry from the previous tie
  111. $h{'g'} = 'G';
  112.  
  113. $h{'j'} = 'J';
  114. $h{'k'} = 'K';
  115. $h{'l'} = 'L';
  116. $h{'m'} = 'M';
  117. $h{'n'} = 'N';
  118. $h{'o'} = 'O';
  119. $h{'p'} = 'P';
  120. $h{'q'} = 'Q';
  121. $h{'r'} = 'R';
  122. $h{'s'} = 'S';
  123. $h{'t'} = 'T';
  124. $h{'u'} = 'U';
  125. $h{'v'} = 'V';
  126. $h{'w'} = 'W';
  127. $h{'x'} = 'X';
  128. $h{'y'} = 'Y';
  129. $h{'z'} = 'Z';
  130.  
  131. $h{'goner3'} = 'snork';
  132.  
  133. delete $h{'goner1'};
  134. $X->DELETE('goner3');
  135.  
  136. @keys = keys(%h);
  137. @values = values(%h);
  138.  
  139. if ($#keys == 29 && $#values == 29) {print "ok 21\n";} else {print "not ok 21\n";}
  140.  
  141. while (($key,$value) = each(h)) {
  142.     if ($key eq $keys[$i] && $value eq $values[$i] && $key gt $value) {
  143.     $key =~ y/a-z/A-Z/;
  144.     $i++ if $key eq $value;
  145.     }
  146. }
  147.  
  148. if ($i == 30) {print "ok 22\n";} else {print "not ok 22\n";}
  149.  
  150. @keys = ('blurfl', keys(h), 'dyick');
  151. if ($#keys == 31) {print "ok 23\n";} else {print "not ok 23\n";}
  152.  
  153. $h{'foo'} = '';
  154. print ($h{'foo'} eq '' ? "ok 24\n" : "not ok 24\n") ;
  155.  
  156. $h{''} = 'bar';
  157. print ($h{''} eq 'bar' ? "ok 25\n" : "not ok 25\n") ;
  158.  
  159. # check cache overflow and numeric keys and contents
  160. $ok = 1;
  161. for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
  162. for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
  163. print ($ok ? "ok 26\n" : "not ok 26\n");
  164.  
  165. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  166.    $blksize,$blocks) = stat($Dfile);
  167. print ($size > 0 ? "ok 27\n" : "not ok 27\n");
  168.  
  169. @h{0..200} = 200..400;
  170. @foo = @h{0..200};
  171. print join(':',200..400) eq join(':',@foo) ? "ok 28\n" : "not ok 28\n";
  172.  
  173.  
  174. # Now check all the non-tie specific stuff
  175.  
  176. # Check NOOVERWRITE will make put fail when attempting to overwrite
  177. # an existing record.
  178.  
  179. $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
  180. print ($status == 1 ? "ok 29\n" : "not ok 29\n") ;
  181.  
  182. # check that the value of the key 'x' has not been changed by the 
  183. # previous test
  184. print ($h{'x'} eq 'X' ? "ok 30\n" : "not ok 30\n") ;
  185.  
  186. # standard put
  187. $status = $X->put('key', 'value') ;
  188. print ($status == 0 ? "ok 31\n" : "not ok 31\n") ;
  189.  
  190. #check that previous put can be retrieved
  191. $status = $X->get('key', $value) ;
  192. print ($status == 0 ? "ok 32\n" : "not ok 32\n") ;
  193. print ($value eq 'value' ? "ok 33\n" : "not ok 33\n") ;
  194.  
  195. # Attempting to delete an existing key should work
  196.  
  197. $status = $X->del('q') ;
  198. print ($status == 0 ? "ok 34\n" : "not ok 34\n") ;
  199.  
  200. # Make sure that the key deleted, cannot be retrieved
  201. print (($h{'q'} eq undef) ? "ok 35\n" : "not ok 35\n") ;
  202.  
  203. # Attempting to delete a non-existant key should fail
  204.  
  205. $status = $X->del('joe') ;
  206. print ($status == 1 ? "ok 36\n" : "not ok 36\n") ;
  207.  
  208. # Check the get interface
  209.  
  210. # First a non-existing key
  211. $status = $X->get('aaaa', $value) ;
  212. print ($status == 1 ? "ok 37\n" : "not ok 37\n") ;
  213.  
  214. # Next an existing key
  215. $status = $X->get('a', $value) ;
  216. print ($status == 0 ? "ok 38\n" : "not ok 38\n") ;
  217. print ($value eq 'A' ? "ok 39\n" : "not ok 39\n") ;
  218.  
  219. # seq
  220. # ###
  221.  
  222. # ditto, but use put to replace the key/value pair.
  223.  
  224. # use seq to walk backwards through a file - check that this reversed is
  225.  
  226. # check seq FIRST/LAST
  227.  
  228. # sync
  229. # ####
  230.  
  231. $status = $X->sync ;
  232. print ($status == 0 ? "ok 40\n" : "not ok 40\n") ;
  233.  
  234.  
  235. # fd
  236. # ##
  237.  
  238. $status = $X->fd ;
  239. print ($status != 0 ? "ok 41\n" : "not ok 41\n") ;
  240.  
  241. undef $X ;
  242. untie %h ;
  243.  
  244. unlink $Dfile;
  245.  
  246. # Now try an in memory file
  247. print (($X = tie(%h, DB_File,undef, O_RDWR|O_CREAT, 0640, $DB_HASH )) ? "ok 42\n" : "not ok 42");
  248.  
  249. # fd with an in memory file should return fail
  250. $status = $X->fd ;
  251. print ($status == -1 ? "ok 43\n" : "not ok 43\n") ;
  252.  
  253. untie %h ;
  254. undef $X ;
  255.  
  256. exit ;
  257.