First public contribution.
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** Code for testing the pager.c module in SQLite. This code
13 ** is not included in the SQLite library. It is used for automated
14 ** testing of the SQLite library.
16 ** $Id: test2.c,v 1.62 2008/09/29 11:49:48 danielk1977 Exp $
18 #include "sqliteInt.h"
25 ** Interpret an SQLite error number
27 static char *errorName(int rc){
30 case SQLITE_OK: zName = "SQLITE_OK"; break;
31 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
32 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
33 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
34 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
35 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
36 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
37 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
38 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
39 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
40 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
41 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
42 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
43 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
44 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
45 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
46 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
47 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
48 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
49 default: zName = "SQLITE_Unknown"; break;
55 ** Page size and reserved size used for testing.
57 static int test_pagesize = 1024;
60 ** Usage: pager_open FILENAME N-PAGE
64 static int pager_open(
66 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
67 int argc, /* Number of arguments */
68 const char **argv /* Text of each argument */
76 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
77 " FILENAME N-PAGE\"", 0);
80 if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR;
81 rc = sqlite3PagerOpen(sqlite3_vfs_find(0), &pPager, argv[1], 0, 0,
82 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
84 Tcl_AppendResult(interp, errorName(rc), 0);
87 sqlite3PagerSetCachesize(pPager, nPage);
88 pageSize = test_pagesize;
89 sqlite3PagerSetPagesize(pPager, &pageSize);
90 sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager);
91 Tcl_AppendResult(interp, zBuf, 0);
96 ** Usage: pager_close ID
98 ** Close the given pager.
100 static int pager_close(
102 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
103 int argc, /* Number of arguments */
104 const char **argv /* Text of each argument */
109 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
113 pPager = sqlite3TestTextToPtr(argv[1]);
114 rc = sqlite3PagerClose(pPager);
116 Tcl_AppendResult(interp, errorName(rc), 0);
123 ** Usage: pager_rollback ID
127 static int pager_rollback(
129 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
130 int argc, /* Number of arguments */
131 const char **argv /* Text of each argument */
136 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
140 pPager = sqlite3TestTextToPtr(argv[1]);
141 rc = sqlite3PagerRollback(pPager);
143 Tcl_AppendResult(interp, errorName(rc), 0);
150 ** Usage: pager_commit ID
152 ** Commit all changes
154 static int pager_commit(
156 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
157 int argc, /* Number of arguments */
158 const char **argv /* Text of each argument */
163 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
167 pPager = sqlite3TestTextToPtr(argv[1]);
168 rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0, 0);
170 Tcl_AppendResult(interp, errorName(rc), 0);
173 rc = sqlite3PagerCommitPhaseTwo(pPager);
175 Tcl_AppendResult(interp, errorName(rc), 0);
182 ** Usage: pager_stmt_begin ID
184 ** Start a new checkpoint.
186 static int pager_stmt_begin(
188 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
189 int argc, /* Number of arguments */
190 const char **argv /* Text of each argument */
195 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
199 pPager = sqlite3TestTextToPtr(argv[1]);
200 rc = sqlite3PagerStmtBegin(pPager);
202 Tcl_AppendResult(interp, errorName(rc), 0);
209 ** Usage: pager_stmt_rollback ID
211 ** Rollback changes to a checkpoint
213 static int pager_stmt_rollback(
215 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
216 int argc, /* Number of arguments */
217 const char **argv /* Text of each argument */
222 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
226 pPager = sqlite3TestTextToPtr(argv[1]);
227 rc = sqlite3PagerStmtRollback(pPager);
229 Tcl_AppendResult(interp, errorName(rc), 0);
236 ** Usage: pager_stmt_commit ID
238 ** Commit changes to a checkpoint
240 static int pager_stmt_commit(
242 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
243 int argc, /* Number of arguments */
244 const char **argv /* Text of each argument */
249 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
253 pPager = sqlite3TestTextToPtr(argv[1]);
254 rc = sqlite3PagerStmtCommit(pPager);
256 Tcl_AppendResult(interp, errorName(rc), 0);
263 ** Usage: pager_stats ID
265 ** Return pager statistics.
267 static int pager_stats(
269 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
270 int argc, /* Number of arguments */
271 const char **argv /* Text of each argument */
276 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
280 pPager = sqlite3TestTextToPtr(argv[1]);
281 a = sqlite3PagerStats(pPager);
283 static char *zName[] = {
284 "ref", "page", "max", "size", "state", "err",
285 "hit", "miss", "ovfl",
288 Tcl_AppendElement(interp, zName[i]);
289 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",a[i]);
290 Tcl_AppendElement(interp, zBuf);
296 ** Usage: pager_pagecount ID
298 ** Return the size of the database file.
300 static int pager_pagecount(
302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
303 int argc, /* Number of arguments */
304 const char **argv /* Text of each argument */
310 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
314 pPager = sqlite3TestTextToPtr(argv[1]);
315 sqlite3PagerPagecount(pPager, &nPage);
316 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage);
317 Tcl_AppendResult(interp, zBuf, 0);
322 ** Usage: page_get ID PGNO
324 ** Return a pointer to a page from the database.
328 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
329 int argc, /* Number of arguments */
330 const char **argv /* Text of each argument */
338 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
342 pPager = sqlite3TestTextToPtr(argv[1]);
343 if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
344 rc = sqlite3PagerGet(pPager, pgno, &pPage);
346 Tcl_AppendResult(interp, errorName(rc), 0);
349 sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage);
350 Tcl_AppendResult(interp, zBuf, 0);
355 ** Usage: page_lookup ID PGNO
357 ** Return a pointer to a page if the page is already in cache.
358 ** If not in cache, return an empty string.
360 static int page_lookup(
362 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
363 int argc, /* Number of arguments */
364 const char **argv /* Text of each argument */
371 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
375 pPager = sqlite3TestTextToPtr(argv[1]);
376 if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
377 pPage = sqlite3PagerLookup(pPager, pgno);
379 sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage);
380 Tcl_AppendResult(interp, zBuf, 0);
386 ** Usage: pager_truncate ID PGNO
388 static int pager_truncate(
390 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
391 int argc, /* Number of arguments */
392 const char **argv /* Text of each argument */
398 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
402 pPager = sqlite3TestTextToPtr(argv[1]);
403 if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
404 rc = sqlite3PagerTruncate(pPager, pgno);
406 Tcl_AppendResult(interp, errorName(rc), 0);
414 ** Usage: page_unref PAGE
416 ** Drop a pointer to a page.
418 static int page_unref(
420 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
421 int argc, /* Number of arguments */
422 const char **argv /* Text of each argument */
427 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
431 pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
432 rc = sqlite3PagerUnref(pPage);
434 Tcl_AppendResult(interp, errorName(rc), 0);
441 ** Usage: page_read PAGE
443 ** Return the content of a page
445 static int page_read(
447 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
448 int argc, /* Number of arguments */
449 const char **argv /* Text of each argument */
454 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
458 pPage = sqlite3TestTextToPtr(argv[1]);
459 memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf));
460 Tcl_AppendResult(interp, zBuf, 0);
465 ** Usage: page_number PAGE
467 ** Return the page number for a page.
469 static int page_number(
471 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
472 int argc, /* Number of arguments */
473 const char **argv /* Text of each argument */
478 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
482 pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
483 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3PagerPagenumber(pPage));
484 Tcl_AppendResult(interp, zBuf, 0);
489 ** Usage: page_write PAGE DATA
491 ** Write something into a page.
493 static int page_write(
495 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
496 int argc, /* Number of arguments */
497 const char **argv /* Text of each argument */
503 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
507 pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
508 rc = sqlite3PagerWrite(pPage);
510 Tcl_AppendResult(interp, errorName(rc), 0);
513 pData = sqlite3PagerGetData(pPage);
514 strncpy(pData, argv[2], test_pagesize-1);
515 pData[test_pagesize-1] = 0;
519 #ifndef SQLITE_OMIT_DISKIO
521 ** Usage: fake_big_file N FILENAME
523 ** Write a few bytes at the N megabyte point of FILENAME. This will
524 ** create a large file. If the file was a valid SQLite database, then
525 ** the next time the database is opened, SQLite will begin allocating
526 ** new pages after N. If N is 2096 or bigger, this will test the
527 ** ability of SQLite to write to large files.
529 static int fake_big_file(
531 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
532 int argc, /* Number of arguments */
533 const char **argv /* Text of each argument */
536 sqlite3_file *fd = 0;
541 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
542 " N-MEGABYTES FILE\"", 0);
545 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
547 pVfs = sqlite3_vfs_find(0);
548 rc = sqlite3OsOpenMalloc(pVfs, argv[2], &fd,
549 (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0
552 Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0);
557 rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset);
558 sqlite3OsCloseFree(fd);
560 Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0);
569 ** sqlite3BitvecBuiltinTest SIZE PROGRAM
571 ** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control.
572 ** See comments on sqlite3BitvecBuiltinTest() for additional information.
574 static int testBitvecBuiltinTest(
576 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
577 int argc, /* Number of arguments */
578 const char **argv /* Text of each argument */
585 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
586 " SIZE PROGRAM\"", (void*)0);
588 if( Tcl_GetInt(interp, argv[1], &sz) ) return TCL_ERROR;
590 while( nProg<99 && *z ){
591 while( *z && !isdigit(*z) ){ z++; }
593 aProg[nProg++] = atoi(z);
594 while( isdigit(*z) ){ z++; }
597 rc = sqlite3_test_control(SQLITE_TESTCTRL_BITVEC_TEST, sz, aProg);
598 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
603 ** Register commands with the TCL interpreter.
605 int Sqlitetest2_Init(Tcl_Interp *interp){
606 extern int sqlite3_io_error_persist;
607 extern int sqlite3_io_error_pending;
608 extern int sqlite3_io_error_hit;
609 extern int sqlite3_io_error_hardhit;
610 extern int sqlite3_diskfull_pending;
611 extern int sqlite3_diskfull;
612 extern int sqlite3_pager_n_sort_bucket;
617 { "pager_open", (Tcl_CmdProc*)pager_open },
618 { "pager_close", (Tcl_CmdProc*)pager_close },
619 { "pager_commit", (Tcl_CmdProc*)pager_commit },
620 { "pager_rollback", (Tcl_CmdProc*)pager_rollback },
621 { "pager_stmt_begin", (Tcl_CmdProc*)pager_stmt_begin },
622 { "pager_stmt_commit", (Tcl_CmdProc*)pager_stmt_commit },
623 { "pager_stmt_rollback", (Tcl_CmdProc*)pager_stmt_rollback },
624 { "pager_stats", (Tcl_CmdProc*)pager_stats },
625 { "pager_pagecount", (Tcl_CmdProc*)pager_pagecount },
626 { "page_get", (Tcl_CmdProc*)page_get },
627 { "page_lookup", (Tcl_CmdProc*)page_lookup },
628 { "page_unref", (Tcl_CmdProc*)page_unref },
629 { "page_read", (Tcl_CmdProc*)page_read },
630 { "page_write", (Tcl_CmdProc*)page_write },
631 { "page_number", (Tcl_CmdProc*)page_number },
632 { "pager_truncate", (Tcl_CmdProc*)pager_truncate },
633 #ifndef SQLITE_OMIT_DISKIO
634 { "fake_big_file", (Tcl_CmdProc*)fake_big_file },
636 { "sqlite3BitvecBuiltinTest",(Tcl_CmdProc*)testBitvecBuiltinTest},
639 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
640 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
642 Tcl_LinkVar(interp, "sqlite_io_error_pending",
643 (char*)&sqlite3_io_error_pending, TCL_LINK_INT);
644 Tcl_LinkVar(interp, "sqlite_io_error_persist",
645 (char*)&sqlite3_io_error_persist, TCL_LINK_INT);
646 Tcl_LinkVar(interp, "sqlite_io_error_hit",
647 (char*)&sqlite3_io_error_hit, TCL_LINK_INT);
648 Tcl_LinkVar(interp, "sqlite_io_error_hardhit",
649 (char*)&sqlite3_io_error_hardhit, TCL_LINK_INT);
650 Tcl_LinkVar(interp, "sqlite_diskfull_pending",
651 (char*)&sqlite3_diskfull_pending, TCL_LINK_INT);
652 Tcl_LinkVar(interp, "sqlite_diskfull",
653 (char*)&sqlite3_diskfull, TCL_LINK_INT);
654 Tcl_LinkVar(interp, "sqlite_pending_byte",
655 (char*)&sqlite3_pending_byte, TCL_LINK_INT);
656 Tcl_LinkVar(interp, "sqlite_pager_n_sort_bucket",
657 (char*)&sqlite3_pager_n_sort_bucket, TCL_LINK_INT);