TestConstruct01.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 2000-2002
  5.  * Sleepycat Software.  All rights reserved.
  6.  *
  7.  * $Id: TestConstruct01.cpp,v 1.5 2002/01/23 14:26:40 bostic Exp $
  8.  */
  9. /*
  10.  * Do some regression tests for constructors.
  11.  * Run normally (without arguments) it is a simple regression test.
  12.  * Run with a numeric argument, it repeats the regression a number
  13.  * of times, to try to determine if there are memory leaks.
  14.  */
  15. #ifndef NO_SYSTEM_INCLUDES
  16. #include <sys/types.h>
  17. #include <iostream.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifndef _MSC_VER
  22. #include <unistd.h>
  23. #endif
  24. #endif
  25. #include <iomanip.h>
  26. #include <db_cxx.h>
  27. #define ERR(a)  
  28.     do { 
  29.       cout << "FAIL: " << (a) << "n"; sysexit(1); 
  30.     } while (0)
  31. #define ERR2(a1,a2)  
  32.     do { 
  33.       cout << "FAIL: " << (a1) << ": " << (a2) << "n"; sysexit(1); 
  34.     } while (0)
  35. #define ERR3(a1,a2,a3)  
  36.     do { 
  37.       cout << "FAIL: " << (a1) << ": " << (a2) << ": " << (a3) << "n"; sysexit(1); 
  38.     } while (0)
  39. #define CHK(a)   
  40.     do { 
  41.       int _ret; 
  42.       if ((_ret = (a)) != 0) { 
  43.  ERR3("DB function " #a " has bad return", _ret, DbEnv::strerror(_ret)); 
  44.       } 
  45.     } while (0)
  46. #ifdef VERBOSE
  47. #define DEBUGOUT(a)          cout << a << "n"
  48. #else
  49. #define DEBUGOUT(a)
  50. #endif
  51. #define CONSTRUCT01_DBNAME         "construct01.db"
  52. #define CONSTRUCT01_DBDIR          "."
  53. #define CONSTRUCT01_DBFULLPATH     (CONSTRUCT01_DBDIR "/" CONSTRUCT01_DBNAME)
  54. int itemcount; // count the number of items in the database
  55. // A good place to put a breakpoint...
  56. //
  57. void sysexit(int status)
  58. {
  59. exit(status);
  60. }
  61. void check_file_removed(const char *name, int fatal)
  62. {
  63. unlink(name);
  64. #if 0
  65. if (access(name, 0) == 0) {
  66. if (fatal)
  67. cout << "FAIL: ";
  68. cout << "File "" << name << "" still exists after runn";
  69. if (fatal)
  70. sysexit(1);
  71. }
  72. #endif
  73. }
  74. // Check that key/data for 0 - count-1 are already present,
  75. // and write a key/data for count.  The key and data are
  76. // both "0123...N" where N == count-1.
  77. //
  78. // For some reason on Windows, we need to open using the full pathname
  79. // of the file when there is no environment, thus the 'has_env'
  80. // variable.
  81. //
  82. void rundb(Db *db, int count, int has_env)
  83. {
  84. const char *name;
  85. if (has_env)
  86. name = CONSTRUCT01_DBNAME;
  87. else
  88. name = CONSTRUCT01_DBFULLPATH;
  89. db->set_error_stream(&cerr);
  90. // We don't really care about the pagesize, but we do want
  91. // to make sure adjusting Db specific variables works before
  92. // opening the db.
  93. //
  94. CHK(db->set_pagesize(1024));
  95. CHK(db->open(NULL, name, NULL, DB_BTREE, count ? 0 : DB_CREATE, 0664));
  96. // The bit map of keys we've seen
  97. long bitmap = 0;
  98. // The bit map of keys we expect to see
  99. long expected = (1 << (count+1)) - 1;
  100. char outbuf[10];
  101. int i;
  102. for (i=0; i<count; i++) {
  103. outbuf[i] = '0' + i;
  104. }
  105. outbuf[i++] = '';
  106. Dbt key(outbuf, i);
  107. Dbt data(outbuf, i);
  108. DEBUGOUT("Put: " << outbuf);
  109. CHK(db->put(0, &key, &data, DB_NOOVERWRITE));
  110. // Acquire a cursor for the table.
  111. Dbc *dbcp;
  112. CHK(db->cursor(NULL, &dbcp, 0));
  113. // Walk through the table, checking
  114. Dbt readkey;
  115. Dbt readdata;
  116. while (dbcp->get(&readkey, &readdata, DB_NEXT) == 0) {
  117. char *key_string = (char *)readkey.get_data();
  118. char *data_string = (char *)readdata.get_data();
  119. DEBUGOUT("Got: " << key_string << ": " << data_string);
  120. int len = strlen(key_string);
  121. long bit = (1 << len);
  122. if (len > count) {
  123. ERR("reread length is bad");
  124. }
  125. else if (strcmp(data_string, key_string) != 0) {
  126. ERR("key/data don't match");
  127. }
  128. else if ((bitmap & bit) != 0) {
  129. ERR("key already seen");
  130. }
  131. else if ((expected & bit) == 0) {
  132. ERR("key was not expected");
  133. }
  134. else {
  135. bitmap |= bit;
  136. expected &= ~(bit);
  137. for (i=0; i<len; i++) {
  138. if (key_string[i] != ('0' + i)) {
  139. cout << " got " << key_string
  140.      << " (" << (int)key_string[i] << ")"
  141.      << ", wanted " << i
  142.      << " (" << (int)('0' + i) << ")"
  143.      << " at position " << i << "n";
  144. ERR("key is corrupt");
  145. }
  146. }
  147. }
  148. }
  149. if (expected != 0) {
  150. cout << " expected more keys, bitmap is: " << expected << "n";
  151. ERR("missing keys in database");
  152. }
  153. CHK(dbcp->close());
  154. CHK(db->close(0));
  155. }
  156. void t1(int except_flag)
  157. {
  158. cout << "  Running test 1:n";
  159. Db db(0, except_flag);
  160. rundb(&db, itemcount++, 0);
  161. cout << "  finished.n";
  162. }
  163. void t2(int except_flag)
  164. {
  165. cout << "  Running test 2:n";
  166. Db db(0, except_flag);
  167. rundb(&db, itemcount++, 0);
  168. cout << "  finished.n";
  169. }
  170. void t3(int except_flag)
  171. {
  172. cout << "  Running test 3:n";
  173. Db db(0, except_flag);
  174. rundb(&db, itemcount++, 0);
  175. cout << "  finished.n";
  176. }
  177. void t4(int except_flag)
  178. {
  179. cout << "  Running test 4:n";
  180. DbEnv env(except_flag);
  181. CHK(env.open(CONSTRUCT01_DBDIR, DB_CREATE | DB_INIT_MPOOL, 0));
  182. Db db(&env, 0);
  183. CHK(db.close(0));
  184. CHK(env.close(0));
  185. cout << "  finished.n";
  186. }
  187. void t5(int except_flag)
  188. {
  189. cout << "  Running test 5:n";
  190. DbEnv env(except_flag);
  191. CHK(env.open(CONSTRUCT01_DBDIR, DB_CREATE | DB_INIT_MPOOL, 0));
  192. Db db(&env, 0);
  193. rundb(&db, itemcount++, 1);
  194. // Note we cannot reuse the old Db!
  195. Db anotherdb(&env, 0);
  196. anotherdb.set_errpfx("test5");
  197. rundb(&anotherdb, itemcount++, 1);
  198. CHK(env.close(0));
  199. cout << "  finished.n";
  200. }
  201. void t6(int except_flag)
  202. {
  203. cout << "  Running test 6:n";
  204. /* From user [#2939] */
  205. int err;
  206. DbEnv* penv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
  207. penv->set_cachesize(0, 32 * 1024, 0);
  208. penv->open(CONSTRUCT01_DBDIR, DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL, 0);
  209. //LEAK: remove this block and leak disappears
  210. Db* pdb = new Db(penv,0);
  211. if ((err = pdb->close(0)) != 0) {
  212. fprintf(stderr, "Error closing Db: %sn", db_strerror(err));
  213. }
  214. delete pdb;
  215. //LEAK: remove this block and leak disappears
  216. if ((err = penv->close(0)) != 0) {
  217. fprintf(stderr, "Error closing DbEnv: %sn", db_strerror(err));
  218. }
  219. delete penv;
  220. // Make sure we get a message from C++ layer reminding us to close.
  221. cerr << "expected error: ";
  222. {
  223. DbEnv foo(DB_CXX_NO_EXCEPTIONS);
  224. foo.open(CONSTRUCT01_DBDIR, DB_CREATE, 0);
  225. }
  226. cerr << "should have received error.n";
  227. cout << "  finished.n";
  228. }
  229. // remove any existing environment or database
  230. void removeall()
  231. {
  232.     {
  233. DbEnv tmpenv(DB_CXX_NO_EXCEPTIONS);
  234. (void)tmpenv.remove(CONSTRUCT01_DBDIR, DB_FORCE);
  235.     }
  236. check_file_removed(CONSTRUCT01_DBFULLPATH, 1);
  237. for (int i=0; i<8; i++) {
  238. char buf[20];
  239. sprintf(buf, "__db.00%d", i);
  240. check_file_removed(buf, 1);
  241. }
  242. }
  243. int doall(int except_flag)
  244. {
  245. itemcount = 0;
  246. try {
  247. // before and after the run, removing any
  248. // old environment/database.
  249. //
  250. removeall();
  251. t1(except_flag);
  252. t2(except_flag);
  253. t3(except_flag);
  254. t4(except_flag);
  255. t5(except_flag);
  256. t6(except_flag);
  257. removeall();
  258. return 0;
  259. }
  260. catch (DbException &dbe) {
  261. ERR2("EXCEPTION RECEIVED", dbe.what());
  262. }
  263. return 1;
  264. }
  265. int main(int argc, char *argv[])
  266. {
  267. int iterations = 1;
  268. if (argc > 1) {
  269. iterations = atoi(argv[1]);
  270. if (iterations < 0) {
  271. ERR("Usage:  construct01 count");
  272. }
  273. }
  274. for (int i=0; i<iterations; i++) {
  275. if (iterations != 0) {
  276. cout << "(" << i << "/" << iterations << ") ";
  277. }
  278. cout << "construct01 running:n";
  279. if (doall(DB_CXX_NO_EXCEPTIONS) != 0) {
  280. ERR("SOME TEST FAILED FOR NO-EXCEPTION TEST");
  281. }
  282. else if (doall(0) != 0) {
  283. ERR("SOME TEST FAILED FOR EXCEPTION TEST");
  284. }
  285. else {
  286. cout << "nALL TESTS SUCCESSFULn";
  287. }
  288. }
  289. return 0;
  290. }