1 2 /* 3 ** The code in this file runs a few multi-threaded test cases using the 4 ** SQLite library. It can be compiled to an executable on unix using the 5 ** following command: 6 ** 7 ** gcc -O2 threadtest3.c sqlite3.c -ldl -lpthread -lm 8 ** 9 ** Then run the compiled program. The exit status is non-zero if any tests 10 ** failed (hopefully there is also some output to stdout to clarify what went 11 ** wrong). 12 ** 13 ** There are three parts to the code in this file, in the following order: 14 ** 15 ** 1. Code for the SQL aggregate function md5sum() copied from 16 ** tclsqlite.c in the SQLite distribution. The names of all the 17 ** types and functions in this section begin with "MD5" or "md5". 18 ** 19 ** 2. A set of utility functions that may be used to implement 20 ** multi-threaded test cases. These are all called by test code 21 ** via macros that help with error reporting. The macros are defined 22 ** immediately below this comment. 23 ** 24 ** 3. The test code itself. And a main() routine to drive the test 25 ** code. 26 */ 27 28 /************************************************************************* 29 ** Start of test code/infrastructure interface macros. 30 ** 31 ** The following macros constitute the interface between the test 32 ** programs and the test infrastructure. Test infrastructure code 33 ** does not itself use any of these macros. Test code should not 34 ** call any of the macroname_x() functions directly. 35 ** 36 ** See the header comments above the corresponding macroname_x() 37 ** function for a description of each interface. 38 */ 39 40 /* Database functions */ 41 #define opendb(w,x,y,z) (SEL(w), opendb_x(w,x,y,z)) 42 #define closedb(y,z) (SEL(y), closedb_x(y,z)) 43 44 /* Functions to execute SQL */ 45 #define sql_script(x,y,z) (SEL(x), sql_script_x(x,y,z)) 46 #define integrity_check(x,y) (SEL(x), integrity_check_x(x,y)) 47 #define execsql_i64(x,y,...) (SEL(x), execsql_i64_x(x,y,__VA_ARGS__)) 48 #define execsql_text(x,y,z,...) (SEL(x), execsql_text_x(x,y,z,__VA_ARGS__)) 49 #define execsql(x,y,...) (SEL(x), (void)execsql_i64_x(x,y,__VA_ARGS__)) 50 51 /* Thread functions */ 52 #define launch_thread(w,x,y,z) (SEL(w), launch_thread_x(w,x,y,z)) 53 #define join_all_threads(y,z) (SEL(y), join_all_threads_x(y,z)) 54 55 /* Timer functions */ 56 #define setstoptime(y,z) (SEL(y), setstoptime_x(y,z)) 57 #define timetostop(z) (SEL(z), timetostop_x(z)) 58 59 /* Report/clear errors. */ 60 #define test_error(z, ...) test_error_x(z, sqlite3_mprintf(__VA_ARGS__)) 61 #define clear_error(y,z) clear_error_x(y, z) 62 63 /* File-system operations */ 64 #define filesize(y,z) (SEL(y), filesize_x(y,z)) 65 #define filecopy(x,y,z) (SEL(x), filecopy_x(x,y,z)) 66 67 /* 68 ** End of test code/infrastructure interface macros. 69 *************************************************************************/ 70 71 72 73 74 #include <sqlite3.h> 75 #include <unistd.h> 76 #include <stdio.h> 77 #include <pthread.h> 78 #include <assert.h> 79 #include <sys/types.h> 80 #include <sys/stat.h> 81 #include <string.h> 82 #include <fcntl.h> 83 #include <errno.h> 84 85 /* 86 * This code implements the MD5 message-digest algorithm. 87 * The algorithm is due to Ron Rivest. This code was 88 * written by Colin Plumb in 1993, no copyright is claimed. 89 * This code is in the public domain; do with it what you wish. 90 * 91 * Equivalent code is available from RSA Data Security, Inc. 92 * This code has been tested against that, and is equivalent, 93 * except that you don't need to include two pages of legalese 94 * with every copy. 95 * 96 * To compute the message digest of a chunk of bytes, declare an 97 * MD5Context structure, pass it to MD5Init, call MD5Update as 98 * needed on buffers full of bytes, and then call MD5Final, which 99 * will fill a supplied 16-byte array with the digest. 100 */ 101 102 /* 103 * If compiled on a machine that doesn't have a 32-bit integer, 104 * you just set "uint32" to the appropriate datatype for an 105 * unsigned 32-bit integer. For example: 106 * 107 * cc -Duint32='unsigned long' md5.c 108 * 109 */ 110 #ifndef uint32 111 # define uint32 unsigned int 112 #endif 113 114 struct MD5Context { 115 int isInit; 116 uint32 buf[4]; 117 uint32 bits[2]; 118 unsigned char in[64]; 119 }; 120 typedef struct MD5Context MD5Context; 121 122 /* 123 * Note: this code is harmless on little-endian machines. 124 */ 125 static void byteReverse (unsigned char *buf, unsigned longs){ 126 uint32 t; 127 do { 128 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 129 ((unsigned)buf[1]<<8 | buf[0]); 130 *(uint32 *)buf = t; 131 buf += 4; 132 } while (--longs); 133 } 134 /* The four core functions - F1 is optimized somewhat */ 135 136 /* #define F1(x, y, z) (x & y | ~x & z) */ 137 #define F1(x, y, z) (z ^ (x & (y ^ z))) 138 #define F2(x, y, z) F1(z, x, y) 139 #define F3(x, y, z) (x ^ y ^ z) 140 #define F4(x, y, z) (y ^ (x | ~z)) 141 142 /* This is the central step in the MD5 algorithm. */ 143 #define MD5STEP(f, w, x, y, z, data, s) \ 144 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 145 146 /* 147 * The core of the MD5 algorithm, this alters an existing MD5 hash to 148 * reflect the addition of 16 longwords of new data. MD5Update blocks 149 * the data and converts bytes into longwords for this routine. 150 */ 151 static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 152 register uint32 a, b, c, d; 153 154 a = buf[0]; 155 b = buf[1]; 156 c = buf[2]; 157 d = buf[3]; 158 159 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 160 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 161 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 162 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 163 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 164 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 165 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 166 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 167 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 168 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 169 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 170 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 171 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 172 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 173 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 174 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 175 176 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 177 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 178 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 179 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 180 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 181 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 182 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 183 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 184 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 185 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 186 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 187 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 188 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 189 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 190 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 191 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 192 193 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 194 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 195 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 196 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 197 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 198 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 199 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 200 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 201 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 202 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 203 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 204 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 205 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 206 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 207 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 208 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 209 210 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 211 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 212 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 213 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 214 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 215 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 216 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 217 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 218 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 219 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 220 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 221 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 222 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 223 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 224 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 225 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 226 227 buf[0] += a; 228 buf[1] += b; 229 buf[2] += c; 230 buf[3] += d; 231 } 232 233 /* 234 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 235 * initialization constants. 236 */ 237 static void MD5Init(MD5Context *ctx){ 238 ctx->isInit = 1; 239 ctx->buf[0] = 0x67452301; 240 ctx->buf[1] = 0xefcdab89; 241 ctx->buf[2] = 0x98badcfe; 242 ctx->buf[3] = 0x10325476; 243 ctx->bits[0] = 0; 244 ctx->bits[1] = 0; 245 } 246 247 /* 248 * Update context to reflect the concatenation of another buffer full 249 * of bytes. 250 */ 251 static 252 void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 253 uint32 t; 254 255 /* Update bitcount */ 256 257 t = ctx->bits[0]; 258 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 259 ctx->bits[1]++; /* Carry from low to high */ 260 ctx->bits[1] += len >> 29; 261 262 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 263 264 /* Handle any leading odd-sized chunks */ 265 266 if ( t ) { 267 unsigned char *p = (unsigned char *)ctx->in + t; 268 269 t = 64-t; 270 if (len < t) { 271 memcpy(p, buf, len); 272 return; 273 } 274 memcpy(p, buf, t); 275 byteReverse(ctx->in, 16); 276 MD5Transform(ctx->buf, (uint32 *)ctx->in); 277 buf += t; 278 len -= t; 279 } 280 281 /* Process data in 64-byte chunks */ 282 283 while (len >= 64) { 284 memcpy(ctx->in, buf, 64); 285 byteReverse(ctx->in, 16); 286 MD5Transform(ctx->buf, (uint32 *)ctx->in); 287 buf += 64; 288 len -= 64; 289 } 290 291 /* Handle any remaining bytes of data. */ 292 293 memcpy(ctx->in, buf, len); 294 } 295 296 /* 297 * Final wrapup - pad to 64-byte boundary with the bit pattern 298 * 1 0* (64-bit count of bits processed, MSB-first) 299 */ 300 static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 301 unsigned count; 302 unsigned char *p; 303 304 /* Compute number of bytes mod 64 */ 305 count = (ctx->bits[0] >> 3) & 0x3F; 306 307 /* Set the first char of padding to 0x80. This is safe since there is 308 always at least one byte free */ 309 p = ctx->in + count; 310 *p++ = 0x80; 311 312 /* Bytes of padding needed to make 64 bytes */ 313 count = 64 - 1 - count; 314 315 /* Pad out to 56 mod 64 */ 316 if (count < 8) { 317 /* Two lots of padding: Pad the first block to 64 bytes */ 318 memset(p, 0, count); 319 byteReverse(ctx->in, 16); 320 MD5Transform(ctx->buf, (uint32 *)ctx->in); 321 322 /* Now fill the next block with 56 bytes */ 323 memset(ctx->in, 0, 56); 324 } else { 325 /* Pad block to 56 bytes */ 326 memset(p, 0, count-8); 327 } 328 byteReverse(ctx->in, 14); 329 330 /* Append length in bits and transform */ 331 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 332 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 333 334 MD5Transform(ctx->buf, (uint32 *)ctx->in); 335 byteReverse((unsigned char *)ctx->buf, 4); 336 memcpy(digest, ctx->buf, 16); 337 memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ 338 } 339 340 /* 341 ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 342 */ 343 static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 344 static char const zEncode[] = "0123456789abcdef"; 345 int i, j; 346 347 for(j=i=0; i<16; i++){ 348 int a = digest[i]; 349 zBuf[j++] = zEncode[(a>>4)&0xf]; 350 zBuf[j++] = zEncode[a & 0xf]; 351 } 352 zBuf[j] = 0; 353 } 354 355 /* 356 ** During testing, the special md5sum() aggregate function is available. 357 ** inside SQLite. The following routines implement that function. 358 */ 359 static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 360 MD5Context *p; 361 int i; 362 if( argc<1 ) return; 363 p = sqlite3_aggregate_context(context, sizeof(*p)); 364 if( p==0 ) return; 365 if( !p->isInit ){ 366 MD5Init(p); 367 } 368 for(i=0; i<argc; i++){ 369 const char *zData = (char*)sqlite3_value_text(argv[i]); 370 if( zData ){ 371 MD5Update(p, (unsigned char*)zData, strlen(zData)); 372 } 373 } 374 } 375 static void md5finalize(sqlite3_context *context){ 376 MD5Context *p; 377 unsigned char digest[16]; 378 char zBuf[33]; 379 p = sqlite3_aggregate_context(context, sizeof(*p)); 380 MD5Final(digest,p); 381 MD5DigestToBase16(digest, zBuf); 382 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 383 } 384 385 /************************************************************************* 386 ** End of copied md5sum() code. 387 */ 388 389 typedef sqlite3_int64 i64; 390 391 typedef struct Error Error; 392 typedef struct Sqlite Sqlite; 393 typedef struct Statement Statement; 394 395 typedef struct Threadset Threadset; 396 typedef struct Thread Thread; 397 398 /* Total number of errors in this process so far. */ 399 static int nGlobalErr = 0; 400 401 /* Set to true to run in "process" instead of "thread" mode. */ 402 static int bProcessMode = 0; 403 404 struct Error { 405 int rc; 406 int iLine; 407 char *zErr; 408 }; 409 410 struct Sqlite { 411 sqlite3 *db; /* Database handle */ 412 Statement *pCache; /* Linked list of cached statements */ 413 int nText; /* Size of array at aText[] */ 414 char **aText; /* Stored text results */ 415 }; 416 417 struct Statement { 418 sqlite3_stmt *pStmt; /* Pre-compiled statement handle */ 419 Statement *pNext; /* Next statement in linked-list */ 420 }; 421 422 struct Thread { 423 int iTid; /* Thread number within test */ 424 int iArg; /* Integer argument passed by caller */ 425 426 pthread_t tid; /* Thread id */ 427 char *(*xProc)(int, int); /* Thread main proc */ 428 Thread *pNext; /* Next in this list of threads */ 429 }; 430 431 struct Threadset { 432 int iMaxTid; /* Largest iTid value allocated so far */ 433 Thread *pThread; /* Linked list of threads */ 434 }; 435 436 static void free_err(Error *p){ 437 sqlite3_free(p->zErr); 438 p->zErr = 0; 439 p->rc = 0; 440 } 441 442 static void print_err(Error *p){ 443 if( p->rc!=SQLITE_OK ){ 444 printf("Error: (%d) \"%s\" at line %d\n", p->rc, p->zErr, p->iLine); 445 nGlobalErr++; 446 } 447 } 448 449 static void print_and_free_err(Error *p){ 450 print_err(p); 451 free_err(p); 452 } 453 454 static void system_error(Error *pErr, int iSys){ 455 pErr->rc = iSys; 456 pErr->zErr = (char *)sqlite3_malloc(512); 457 strerror_r(iSys, pErr->zErr, 512); 458 pErr->zErr[511] = '\0'; 459 } 460 461 static void sqlite_error( 462 Error *pErr, 463 Sqlite *pDb, 464 const char *zFunc 465 ){ 466 pErr->rc = sqlite3_errcode(pDb->db); 467 pErr->zErr = sqlite3_mprintf( 468 "sqlite3_%s() - %s (%d)", zFunc, sqlite3_errmsg(pDb->db), 469 sqlite3_extended_errcode(pDb->db) 470 ); 471 } 472 473 static void test_error_x( 474 Error *pErr, 475 char *zErr 476 ){ 477 if( pErr->rc==SQLITE_OK ){ 478 pErr->rc = 1; 479 pErr->zErr = zErr; 480 }else{ 481 sqlite3_free(zErr); 482 } 483 } 484 485 static void clear_error_x( 486 Error *pErr, 487 int rc 488 ){ 489 if( pErr->rc==rc ){ 490 pErr->rc = SQLITE_OK; 491 sqlite3_free(pErr->zErr); 492 pErr->zErr = 0; 493 } 494 } 495 496 static int busyhandler(void *pArg, int n){ 497 usleep(10*1000); 498 return 1; 499 } 500 501 static void opendb_x( 502 Error *pErr, /* IN/OUT: Error code */ 503 Sqlite *pDb, /* OUT: Database handle */ 504 const char *zFile, /* Database file name */ 505 int bDelete /* True to delete db file before opening */ 506 ){ 507 if( pErr->rc==SQLITE_OK ){ 508 int rc; 509 if( bDelete ) unlink(zFile); 510 rc = sqlite3_open(zFile, &pDb->db); 511 if( rc ){ 512 sqlite_error(pErr, pDb, "open"); 513 sqlite3_close(pDb->db); 514 pDb->db = 0; 515 }else{ 516 sqlite3_create_function( 517 pDb->db, "md5sum", -1, SQLITE_UTF8, 0, 0, md5step, md5finalize 518 ); 519 sqlite3_busy_handler(pDb->db, busyhandler, 0); 520 } 521 } 522 } 523 524 static void closedb_x( 525 Error *pErr, /* IN/OUT: Error code */ 526 Sqlite *pDb /* OUT: Database handle */ 527 ){ 528 int rc; 529 int i; 530 Statement *pIter; 531 Statement *pNext; 532 for(pIter=pDb->pCache; pIter; pIter=pNext){ 533 pNext = pIter->pNext; 534 sqlite3_finalize(pIter->pStmt); 535 sqlite3_free(pIter); 536 } 537 for(i=0; i<pDb->nText; i++){ 538 sqlite3_free(pDb->aText[i]); 539 } 540 sqlite3_free(pDb->aText); 541 rc = sqlite3_close(pDb->db); 542 if( rc && pErr->rc==SQLITE_OK ){ 543 pErr->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(pDb->db)); 544 } 545 memset(pDb, 0, sizeof(Sqlite)); 546 } 547 548 static void sql_script_x( 549 Error *pErr, /* IN/OUT: Error code */ 550 Sqlite *pDb, /* Database handle */ 551 const char *zSql /* SQL script to execute */ 552 ){ 553 if( pErr->rc==SQLITE_OK ){ 554 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr); 555 } 556 } 557 558 static Statement *getSqlStatement( 559 Error *pErr, /* IN/OUT: Error code */ 560 Sqlite *pDb, /* Database handle */ 561 const char *zSql /* SQL statement */ 562 ){ 563 Statement *pRet; 564 int rc; 565 566 for(pRet=pDb->pCache; pRet; pRet=pRet->pNext){ 567 if( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ){ 568 return pRet; 569 } 570 } 571 572 pRet = sqlite3_malloc(sizeof(Statement)); 573 rc = sqlite3_prepare_v2(pDb->db, zSql, -1, &pRet->pStmt, 0); 574 if( rc!=SQLITE_OK ){ 575 sqlite_error(pErr, pDb, "prepare_v2"); 576 return 0; 577 } 578 assert( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ); 579 580 pRet->pNext = pDb->pCache; 581 pDb->pCache = pRet; 582 return pRet; 583 } 584 585 static sqlite3_stmt *getAndBindSqlStatement( 586 Error *pErr, /* IN/OUT: Error code */ 587 Sqlite *pDb, /* Database handle */ 588 va_list ap /* SQL followed by parameters */ 589 ){ 590 Statement *pStatement; /* The SQLite statement wrapper */ 591 sqlite3_stmt *pStmt; /* The SQLite statement to return */ 592 int i; /* Used to iterate through parameters */ 593 594 pStatement = getSqlStatement(pErr, pDb, va_arg(ap, const char *)); 595 if( !pStatement ) return 0; 596 pStmt = pStatement->pStmt; 597 for(i=1; i<=sqlite3_bind_parameter_count(pStmt); i++){ 598 const char *zName = sqlite3_bind_parameter_name(pStmt, i); 599 void * pArg = va_arg(ap, void*); 600 601 switch( zName[1] ){ 602 case 'i': 603 sqlite3_bind_int64(pStmt, i, *(i64 *)pArg); 604 break; 605 606 default: 607 pErr->rc = 1; 608 pErr->zErr = sqlite3_mprintf("Cannot discern type: \"%s\"", zName); 609 pStmt = 0; 610 break; 611 } 612 } 613 614 return pStmt; 615 } 616 617 static i64 execsql_i64_x( 618 Error *pErr, /* IN/OUT: Error code */ 619 Sqlite *pDb, /* Database handle */ 620 ... /* SQL and pointers to parameter values */ 621 ){ 622 i64 iRet = 0; 623 if( pErr->rc==SQLITE_OK ){ 624 sqlite3_stmt *pStmt; /* SQL statement to execute */ 625 va_list ap; /* ... arguments */ 626 int i; /* Used to iterate through parameters */ 627 va_start(ap, pDb); 628 pStmt = getAndBindSqlStatement(pErr, pDb, ap); 629 if( pStmt ){ 630 int rc; 631 int first = 1; 632 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 633 if( first && sqlite3_column_count(pStmt)>0 ){ 634 iRet = sqlite3_column_int64(pStmt, 0); 635 } 636 first = 0; 637 } 638 if( SQLITE_OK!=sqlite3_reset(pStmt) ){ 639 sqlite_error(pErr, pDb, "reset"); 640 } 641 } 642 va_end(ap); 643 } 644 return iRet; 645 } 646 647 static char * execsql_text_x( 648 Error *pErr, /* IN/OUT: Error code */ 649 Sqlite *pDb, /* Database handle */ 650 int iSlot, /* Db handle slot to store text in */ 651 ... /* SQL and pointers to parameter values */ 652 ){ 653 char *zRet = 0; 654 655 if( iSlot>=pDb->nText ){ 656 int nByte = sizeof(char *)*(iSlot+1); 657 pDb->aText = (char **)sqlite3_realloc(pDb->aText, nByte); 658 memset(&pDb->aText[pDb->nText], 0, sizeof(char*)*(iSlot+1-pDb->nText)); 659 pDb->nText = iSlot+1; 660 } 661 662 if( pErr->rc==SQLITE_OK ){ 663 sqlite3_stmt *pStmt; /* SQL statement to execute */ 664 va_list ap; /* ... arguments */ 665 int i; /* Used to iterate through parameters */ 666 va_start(ap, iSlot); 667 pStmt = getAndBindSqlStatement(pErr, pDb, ap); 668 if( pStmt ){ 669 int rc; 670 int first = 1; 671 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 672 if( first && sqlite3_column_count(pStmt)>0 ){ 673 zRet = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 674 sqlite3_free(pDb->aText[iSlot]); 675 pDb->aText[iSlot] = zRet; 676 } 677 first = 0; 678 } 679 if( SQLITE_OK!=sqlite3_reset(pStmt) ){ 680 sqlite_error(pErr, pDb, "reset"); 681 } 682 } 683 va_end(ap); 684 } 685 686 return zRet; 687 } 688 689 static void integrity_check_x( 690 Error *pErr, /* IN/OUT: Error code */ 691 Sqlite *pDb /* Database handle */ 692 ){ 693 if( pErr->rc==SQLITE_OK ){ 694 Statement *pStatement; /* Statement to execute */ 695 int rc; /* Return code */ 696 char *zErr = 0; /* Integrity check error */ 697 698 pStatement = getSqlStatement(pErr, pDb, "PRAGMA integrity_check"); 699 if( pStatement ){ 700 sqlite3_stmt *pStmt = pStatement->pStmt; 701 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 702 const char *z = sqlite3_column_text(pStmt, 0); 703 if( strcmp(z, "ok") ){ 704 if( zErr==0 ){ 705 zErr = sqlite3_mprintf("%s", z); 706 }else{ 707 zErr = sqlite3_mprintf("%z\n%s", zErr, z); 708 } 709 } 710 } 711 sqlite3_reset(pStmt); 712 713 if( zErr ){ 714 pErr->zErr = zErr; 715 pErr->rc = 1; 716 } 717 } 718 } 719 } 720 721 static void *launch_thread_main(void *pArg){ 722 Thread *p = (Thread *)pArg; 723 return (void *)p->xProc(p->iTid, p->iArg); 724 } 725 726 static void launch_thread_x( 727 Error *pErr, /* IN/OUT: Error code */ 728 Threadset *pThreads, /* Thread set */ 729 char *(*xProc)(int, int), /* Proc to run */ 730 int iArg /* Argument passed to thread proc */ 731 ){ 732 if( pErr->rc==SQLITE_OK ){ 733 int iTid = ++pThreads->iMaxTid; 734 Thread *p; 735 int rc; 736 737 p = (Thread *)sqlite3_malloc(sizeof(Thread)); 738 memset(p, 0, sizeof(Thread)); 739 p->iTid = iTid; 740 p->iArg = iArg; 741 p->xProc = xProc; 742 743 rc = pthread_create(&p->tid, NULL, launch_thread_main, (void *)p); 744 if( rc!=0 ){ 745 system_error(pErr, rc); 746 sqlite3_free(p); 747 }else{ 748 p->pNext = pThreads->pThread; 749 pThreads->pThread = p; 750 } 751 } 752 } 753 754 static void join_all_threads_x( 755 Error *pErr, /* IN/OUT: Error code */ 756 Threadset *pThreads /* Thread set */ 757 ){ 758 Thread *p; 759 Thread *pNext; 760 for(p=pThreads->pThread; p; p=pNext){ 761 void *ret; 762 pNext = p->pNext; 763 int rc; 764 rc = pthread_join(p->tid, &ret); 765 if( rc!=0 ){ 766 if( pErr->rc==SQLITE_OK ) system_error(pErr, rc); 767 }else{ 768 printf("Thread %d says: %s\n", p->iTid, (ret==0 ? "..." : (char *)ret)); 769 } 770 sqlite3_free(p); 771 } 772 pThreads->pThread = 0; 773 } 774 775 static i64 filesize_x( 776 Error *pErr, 777 const char *zFile 778 ){ 779 i64 iRet = 0; 780 if( pErr->rc==SQLITE_OK ){ 781 struct stat sStat; 782 if( stat(zFile, &sStat) ){ 783 iRet = -1; 784 }else{ 785 iRet = sStat.st_size; 786 } 787 } 788 return iRet; 789 } 790 791 static void filecopy_x( 792 Error *pErr, 793 const char *zFrom, 794 const char *zTo 795 ){ 796 if( pErr->rc==SQLITE_OK ){ 797 i64 nByte = filesize_x(pErr, zFrom); 798 if( nByte<0 ){ 799 test_error_x(pErr, sqlite3_mprintf("no such file: %s", zFrom)); 800 }else{ 801 i64 iOff; 802 char aBuf[1024]; 803 int fd1; 804 int fd2; 805 unlink(zTo); 806 807 fd1 = open(zFrom, O_RDONLY); 808 if( fd1<0 ){ 809 system_error(pErr, errno); 810 return; 811 } 812 fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL, 0644); 813 if( fd2<0 ){ 814 system_error(pErr, errno); 815 close(fd1); 816 return; 817 } 818 819 iOff = 0; 820 while( iOff<nByte ){ 821 int nCopy = sizeof(aBuf); 822 if( nCopy+iOff>nByte ){ 823 nCopy = nByte - iOff; 824 } 825 if( nCopy!=read(fd1, aBuf, nCopy) ){ 826 system_error(pErr, errno); 827 break; 828 } 829 if( nCopy!=write(fd2, aBuf, nCopy) ){ 830 system_error(pErr, errno); 831 break; 832 } 833 iOff += nCopy; 834 } 835 836 close(fd1); 837 close(fd2); 838 } 839 } 840 } 841 842 /* 843 ** Used by setstoptime() and timetostop(). 844 */ 845 static double timelimit = 0.0; 846 static sqlite3_vfs *pTimelimitVfs = 0; 847 848 static void setstoptime_x( 849 Error *pErr, /* IN/OUT: Error code */ 850 int nMs /* Milliseconds until "stop time" */ 851 ){ 852 if( pErr->rc==SQLITE_OK ){ 853 double t; 854 int rc; 855 pTimelimitVfs = sqlite3_vfs_find(0); 856 rc = pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t); 857 if( rc!=SQLITE_OK ){ 858 pErr->rc = rc; 859 }else{ 860 timelimit = t + ((double)nMs)/(1000.0*60.0*60.0*24.0); 861 } 862 } 863 } 864 865 static int timetostop_x( 866 Error *pErr /* IN/OUT: Error code */ 867 ){ 868 int ret = 1; 869 if( pErr->rc==SQLITE_OK ){ 870 double t; 871 int rc; 872 rc = pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t); 873 if( rc!=SQLITE_OK ){ 874 pErr->rc = rc; 875 }else{ 876 ret = (t >= timelimit); 877 } 878 } 879 return ret; 880 } 881 882 /* 883 ** The "Set Error Line" macro. 884 */ 885 #define SEL(e) ((e)->iLine = ((e)->rc ? (e)->iLine : __LINE__)) 886 887 888 /************************************************************************* 889 ************************************************************************** 890 ************************************************************************** 891 ** End infrastructure. Begin tests. 892 */ 893 894 #define WALTHREAD1_NTHREAD 10 895 #define WALTHREAD3_NTHREAD 6 896 897 static char *walthread1_thread(int iTid, int iArg){ 898 Error err = {0}; /* Error code and message */ 899 Sqlite db = {0}; /* SQLite database connection */ 900 int nIter = 0; /* Iterations so far */ 901 902 opendb(&err, &db, "test.db", 0); 903 while( !timetostop(&err) ){ 904 const char *azSql[] = { 905 "SELECT md5sum(x) FROM t1 WHERE rowid != (SELECT max(rowid) FROM t1)", 906 "SELECT x FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)", 907 }; 908 char *z1, *z2, *z3; 909 910 execsql(&err, &db, "BEGIN"); 911 integrity_check(&err, &db); 912 z1 = execsql_text(&err, &db, 1, azSql[0]); 913 z2 = execsql_text(&err, &db, 2, azSql[1]); 914 z3 = execsql_text(&err, &db, 3, azSql[0]); 915 execsql(&err, &db, "COMMIT"); 916 917 if( strcmp(z1, z2) || strcmp(z1, z3) ){ 918 test_error(&err, "Failed read: %s %s %s", z1, z2, z3); 919 } 920 921 sql_script(&err, &db, 922 "BEGIN;" 923 "INSERT INTO t1 VALUES(randomblob(100));" 924 "INSERT INTO t1 VALUES(randomblob(100));" 925 "INSERT INTO t1 SELECT md5sum(x) FROM t1;" 926 "COMMIT;" 927 ); 928 nIter++; 929 } 930 closedb(&err, &db); 931 932 print_and_free_err(&err); 933 return sqlite3_mprintf("%d iterations", nIter); 934 } 935 936 static char *walthread1_ckpt_thread(int iTid, int iArg){ 937 Error err = {0}; /* Error code and message */ 938 Sqlite db = {0}; /* SQLite database connection */ 939 int nCkpt = 0; /* Checkpoints so far */ 940 941 opendb(&err, &db, "test.db", 0); 942 while( !timetostop(&err) ){ 943 usleep(500*1000); 944 execsql(&err, &db, "PRAGMA wal_checkpoint"); 945 if( err.rc==SQLITE_OK ) nCkpt++; 946 clear_error(&err, SQLITE_BUSY); 947 } 948 closedb(&err, &db); 949 950 print_and_free_err(&err); 951 return sqlite3_mprintf("%d checkpoints", nCkpt); 952 } 953 954 static void walthread1(int nMs){ 955 Error err = {0}; /* Error code and message */ 956 Sqlite db = {0}; /* SQLite database connection */ 957 Threadset threads = {0}; /* Test threads */ 958 int i; /* Iterator variable */ 959 960 opendb(&err, &db, "test.db", 1); 961 sql_script(&err, &db, 962 "PRAGMA journal_mode = WAL;" 963 "CREATE TABLE t1(x PRIMARY KEY);" 964 "INSERT INTO t1 VALUES(randomblob(100));" 965 "INSERT INTO t1 VALUES(randomblob(100));" 966 "INSERT INTO t1 SELECT md5sum(x) FROM t1;" 967 ); 968 969 setstoptime(&err, nMs); 970 for(i=0; i<WALTHREAD1_NTHREAD; i++){ 971 launch_thread(&err, &threads, walthread1_thread, 0); 972 } 973 launch_thread(&err, &threads, walthread1_ckpt_thread, 0); 974 join_all_threads(&err, &threads); 975 976 print_and_free_err(&err); 977 } 978 979 static char *walthread2_thread(int iTid, int iArg){ 980 Error err = {0}; /* Error code and message */ 981 Sqlite db = {0}; /* SQLite database connection */ 982 int anTrans[2] = {0, 0}; /* Number of WAL and Rollback transactions */ 983 984 const char *zJournal = "PRAGMA journal_mode = WAL"; 985 if( iArg ){ zJournal = "PRAGMA journal_mode = DELETE"; } 986 987 while( !timetostop(&err) ){ 988 int journal_exists = 0; 989 int wal_exists = 0; 990 991 opendb(&err, &db, "test.db", 0); 992 993 sql_script(&err, &db, zJournal); 994 clear_error(&err, SQLITE_BUSY); 995 sql_script(&err, &db, "BEGIN"); 996 sql_script(&err, &db, "INSERT INTO t1 VALUES(NULL, randomblob(100))"); 997 998 journal_exists = (filesize(&err, "test.db-journal") >= 0); 999 wal_exists = (filesize(&err, "test.db-wal") >= 0); 1000 if( (journal_exists+wal_exists)!=1 ){ 1001 test_error(&err, "File system looks incorrect (%d, %d)", 1002 journal_exists, wal_exists 1003 ); 1004 } 1005 anTrans[journal_exists]++; 1006 1007 sql_script(&err, &db, "COMMIT"); 1008 integrity_check(&err, &db); 1009 closedb(&err, &db); 1010 } 1011 1012 print_and_free_err(&err); 1013 return sqlite3_mprintf("W %d R %d", anTrans[0], anTrans[1]); 1014 } 1015 1016 static void walthread2(int nMs){ 1017 Error err = {0}; 1018 Sqlite db = {0}; 1019 Threadset threads = {0}; 1020 1021 opendb(&err, &db, "test.db", 1); 1022 sql_script(&err, &db, "CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE)"); 1023 closedb(&err, &db); 1024 1025 setstoptime(&err, nMs); 1026 launch_thread(&err, &threads, walthread2_thread, 0); 1027 launch_thread(&err, &threads, walthread2_thread, 0); 1028 launch_thread(&err, &threads, walthread2_thread, 1); 1029 launch_thread(&err, &threads, walthread2_thread, 1); 1030 join_all_threads(&err, &threads); 1031 1032 print_and_free_err(&err); 1033 } 1034 1035 static char *walthread3_thread(int iTid, int iArg){ 1036 Error err = {0}; /* Error code and message */ 1037 Sqlite db = {0}; /* SQLite database connection */ 1038 i64 iNextWrite; /* Next value this thread will write */ 1039 1040 opendb(&err, &db, "test.db", 0); 1041 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 10"); 1042 1043 iNextWrite = iArg+1; 1044 while( 1 ){ 1045 i64 sum1; 1046 i64 sum2; 1047 int stop = 0; /* True to stop executing (test timed out) */ 1048 1049 while( 0==(stop = timetostop(&err)) ){ 1050 i64 iMax = execsql_i64(&err, &db, "SELECT max(cnt) FROM t1"); 1051 if( iMax+1==iNextWrite ) break; 1052 } 1053 if( stop ) break; 1054 1055 sum1 = execsql_i64(&err, &db, "SELECT sum(cnt) FROM t1"); 1056 sum2 = execsql_i64(&err, &db, "SELECT sum(sum1) FROM t1"); 1057 execsql_i64(&err, &db, 1058 "INSERT INTO t1 VALUES(:iNextWrite, :iSum1, :iSum2)", 1059 &iNextWrite, &sum1, &sum2 1060 ); 1061 integrity_check(&err, &db); 1062 1063 iNextWrite += WALTHREAD3_NTHREAD; 1064 } 1065 1066 closedb(&err, &db); 1067 print_and_free_err(&err); 1068 return 0; 1069 } 1070 1071 static void walthread3(int nMs){ 1072 Error err = {0}; 1073 Sqlite db = {0}; 1074 Threadset threads = {0}; 1075 int i; 1076 1077 opendb(&err, &db, "test.db", 1); 1078 sql_script(&err, &db, 1079 "PRAGMA journal_mode = WAL;" 1080 "CREATE TABLE t1(cnt PRIMARY KEY, sum1, sum2);" 1081 "CREATE INDEX i1 ON t1(sum1);" 1082 "CREATE INDEX i2 ON t1(sum2);" 1083 "INSERT INTO t1 VALUES(0, 0, 0);" 1084 ); 1085 closedb(&err, &db); 1086 1087 setstoptime(&err, nMs); 1088 for(i=0; i<WALTHREAD3_NTHREAD; i++){ 1089 launch_thread(&err, &threads, walthread3_thread, i); 1090 } 1091 join_all_threads(&err, &threads); 1092 1093 print_and_free_err(&err); 1094 } 1095 1096 static char *walthread4_reader_thread(int iTid, int iArg){ 1097 Error err = {0}; /* Error code and message */ 1098 Sqlite db = {0}; /* SQLite database connection */ 1099 1100 opendb(&err, &db, "test.db", 0); 1101 while( !timetostop(&err) ){ 1102 integrity_check(&err, &db); 1103 } 1104 closedb(&err, &db); 1105 1106 print_and_free_err(&err); 1107 return 0; 1108 } 1109 1110 static char *walthread4_writer_thread(int iTid, int iArg){ 1111 Error err = {0}; /* Error code and message */ 1112 Sqlite db = {0}; /* SQLite database connection */ 1113 i64 iRow = 1; 1114 1115 opendb(&err, &db, "test.db", 0); 1116 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 15;"); 1117 while( !timetostop(&err) ){ 1118 execsql_i64( 1119 &err, &db, "REPLACE INTO t1 VALUES(:iRow, randomblob(300))", &iRow 1120 ); 1121 iRow++; 1122 if( iRow==10 ) iRow = 0; 1123 } 1124 closedb(&err, &db); 1125 1126 print_and_free_err(&err); 1127 return 0; 1128 } 1129 1130 static void walthread4(int nMs){ 1131 Error err = {0}; 1132 Sqlite db = {0}; 1133 Threadset threads = {0}; 1134 1135 opendb(&err, &db, "test.db", 1); 1136 sql_script(&err, &db, 1137 "PRAGMA journal_mode = WAL;" 1138 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);" 1139 ); 1140 closedb(&err, &db); 1141 1142 setstoptime(&err, nMs); 1143 launch_thread(&err, &threads, walthread4_reader_thread, 0); 1144 launch_thread(&err, &threads, walthread4_writer_thread, 0); 1145 join_all_threads(&err, &threads); 1146 1147 print_and_free_err(&err); 1148 } 1149 1150 static char *walthread5_thread(int iTid, int iArg){ 1151 Error err = {0}; /* Error code and message */ 1152 Sqlite db = {0}; /* SQLite database connection */ 1153 i64 nRow; 1154 1155 opendb(&err, &db, "test.db", 0); 1156 nRow = execsql_i64(&err, &db, "SELECT count(*) FROM t1"); 1157 closedb(&err, &db); 1158 1159 if( nRow!=65536 ) test_error(&err, "Bad row count: %d", (int)nRow); 1160 print_and_free_err(&err); 1161 return 0; 1162 } 1163 static void walthread5(int nMs){ 1164 Error err = {0}; 1165 Sqlite db = {0}; 1166 Threadset threads = {0}; 1167 1168 opendb(&err, &db, "test.db", 1); 1169 sql_script(&err, &db, 1170 "PRAGMA wal_autocheckpoint = 0;" 1171 "PRAGMA page_size = 1024;" 1172 "PRAGMA journal_mode = WAL;" 1173 "CREATE TABLE t1(x);" 1174 "BEGIN;" 1175 "INSERT INTO t1 VALUES(randomblob(900));" 1176 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */" 1177 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */" 1178 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */" 1179 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */" 1180 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */" 1181 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */" 1182 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */" 1183 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */" 1184 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */" 1185 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */" 1186 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */" 1187 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */" 1188 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */" 1189 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */" 1190 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32768 */" 1191 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 65536 */" 1192 "COMMIT;" 1193 ); 1194 filecopy(&err, "test.db", "test_sv.db"); 1195 filecopy(&err, "test.db-wal", "test_sv.db-wal"); 1196 closedb(&err, &db); 1197 1198 filecopy(&err, "test_sv.db", "test.db"); 1199 filecopy(&err, "test_sv.db-wal", "test.db-wal"); 1200 1201 if( err.rc==SQLITE_OK ){ 1202 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal")); 1203 printf(" DB file is %d.\n", (int)filesize(&err,"test.db")); 1204 } 1205 1206 setstoptime(&err, nMs); 1207 launch_thread(&err, &threads, walthread5_thread, 0); 1208 launch_thread(&err, &threads, walthread5_thread, 0); 1209 launch_thread(&err, &threads, walthread5_thread, 0); 1210 launch_thread(&err, &threads, walthread5_thread, 0); 1211 launch_thread(&err, &threads, walthread5_thread, 0); 1212 join_all_threads(&err, &threads); 1213 1214 if( err.rc==SQLITE_OK ){ 1215 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal")); 1216 printf(" DB file is %d.\n", (int)filesize(&err,"test.db")); 1217 } 1218 1219 print_and_free_err(&err); 1220 } 1221 1222 int main(int argc, char **argv){ 1223 struct ThreadTest { 1224 void (*xTest)(int); 1225 const char *zTest; 1226 int nMs; 1227 } aTest[] = { 1228 { walthread1, "walthread1", 20000 }, 1229 { walthread2, "walthread2", 20000 }, 1230 { walthread3, "walthread3", 20000 }, 1231 { walthread4, "walthread4", 20000 }, 1232 { walthread5, "walthread5", 1000 }, 1233 }; 1234 1235 int i; 1236 char *zTest = 0; 1237 int nTest = 0; 1238 int bTestfound = 0; 1239 int bPrefix = 0; 1240 1241 if( argc>2 ) goto usage; 1242 if( argc==2 ){ 1243 zTest = argv[1]; 1244 nTest = strlen(zTest); 1245 if( zTest[nTest-1]=='*' ){ 1246 nTest--; 1247 bPrefix = 1; 1248 } 1249 } 1250 1251 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 1252 1253 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){ 1254 char const *z = aTest[i].zTest; 1255 int n = strlen(z); 1256 if( !zTest || ((bPrefix || n==nTest) && 0==strncmp(zTest, z, nTest)) ){ 1257 printf("Running %s for %d seconds...\n", z, aTest[i].nMs/1000); 1258 aTest[i].xTest(aTest[i].nMs); 1259 bTestfound++; 1260 } 1261 } 1262 if( bTestfound==0 ) goto usage; 1263 1264 printf("Total of %d errors across all tests\n", nGlobalErr); 1265 return (nGlobalErr>0 ? 255 : 0); 1266 1267 usage: 1268 printf("Usage: %s [testname|testprefix*]\n", argv[0]); 1269 printf("Available tests are:\n"); 1270 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){ 1271 printf(" %s\n", aTest[i].zTest); 1272 } 1273 1274 return 254; 1275 } 1276 1277 1278