/ Check-in [a8f6341d]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add pager test cases. Change a condition in pager.c to NEVER().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a8f6341d3b12d64ef56ed05226e3b4f183b8957d
User & Date: dan 2010-07-01 15:09:48
Context
2010-07-01
19:01
Add tests to pager1.test and pagerfault.test. check-in: c6e75950 user: dan tags: trunk
15:09
Add pager test cases. Change a condition in pager.c to NEVER(). check-in: a8f6341d user: dan tags: trunk
2010-06-30
10:36
Add further test cases. Fix an assert() in pager.c. check-in: 8e65c0e3 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Changes to src/pager.c.

5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
/*
** Return TRUE if the pager is in a state where it is OK to change the
** journalmode.  Journalmode changes can only happen when the database
** is unmodified.
*/
int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
  if( pPager->dbModified ) return 0;
  if( isOpen(pPager->jfd) && pPager->journalOff>0 ) return 0;
  return 1;
}

/*
** Get/set the size-limit used for persistent journal files.
**
** Setting the size limit to -1 means no limit is enforced.







|







5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
/*
** Return TRUE if the pager is in a state where it is OK to change the
** journalmode.  Journalmode changes can only happen when the database
** is unmodified.
*/
int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
  if( pPager->dbModified ) return 0;
  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
  return 1;
}

/*
** Get/set the size-limit used for persistent journal files.
**
** Setting the size limit to -1 means no limit is enforced.

Changes to src/test1.c.

1898
1899
1900
1901
1902
1903
1904

1905
1906
1907
1908
1909
1910
1911
static int sqlite_abort(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  assert( interp==0 );   /* This will always fail */

  return TCL_OK;
}

/*
** The following routine is a user-defined SQL function whose purpose
** is to test the sqlite_set_result() API.
*/







>







1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
static int sqlite_abort(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  assert( interp==0 );   /* This will always fail */
  abort();
  return TCL_OK;
}

/*
** The following routine is a user-defined SQL function whose purpose
** is to test the sqlite_set_result() API.
*/

Changes to src/test_vfs.c.

571
572
573
574
575
576
577








578
579

580
581
582
583
584
585
586
587
....
1296
1297
1298
1299
1300
1301
1302

1303
1304
1305
1306
1307
1308
1309
....
1322
1323
1324
1325
1326
1327
1328





1329
1330
1331
1332
1333
1334
1335
....
1355
1356
1357
1358
1359
1360
1361

1362
1363
1364
1365
1366
1367
1368
....
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
  Tcl_IncrRefCount(pId);
  pFd->pShmId = pId;
  Tcl_ResetResult(p->interp);

  rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, pFd->pReal, flags, pOutFlags);
  if( pFd->pReal->pMethods ){
    sqlite3_io_methods *pMethods;








    pMethods = (sqlite3_io_methods *)ckalloc(sizeof(sqlite3_io_methods));
    memcpy(pMethods, &tvfs_io_methods, sizeof(sqlite3_io_methods));

    if( ((Testvfs *)pVfs->pAppData)->isNoshm ){
      pMethods->xShmOpen = 0;
      pMethods->xShmClose = 0;
      pMethods->xShmLock = 0;
      pMethods->xShmBarrier = 0;
      pMethods->xShmMap = 0;
    }
    pFile->pMethods = pMethods;
................................................................................
  int nByte;                      /* Bytes of space to allocate at p */

  int i;
  int isNoshm = 0;                /* True if -noshm is passed */
  int isDefault = 0;              /* True if -default is passed */
  int szOsFile = 0;               /* Value passed to -szosfile */
  int mxPathname = -1;            /* Value passed to -mxpathname */


  if( objc<2 || 0!=(objc%2) ) goto bad_args;
  for(i=2; i<objc; i += 2){
    int nSwitch;
    char *zSwitch;
    zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch); 

................................................................................
        return TCL_ERROR;
      }
    }
    else if( nSwitch>2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){
      if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){
        return TCL_ERROR;
      }





    }
    else{
      goto bad_args;
    }
  }

  if( szOsFile<sizeof(TestvfsFile) ){
................................................................................

  p->zName = (char *)&p[1];
  memcpy(p->zName, zVfs, strlen(zVfs)+1);

  pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs));
  memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
  pVfs->pAppData = (void *)p;

  pVfs->zName = p->zName;
  pVfs->mxPathname = p->pParent->mxPathname;
  if( mxPathname>=0 && mxPathname<pVfs->mxPathname ){
    pVfs->mxPathname = mxPathname;
  }
  pVfs->szOsFile = szOsFile;
  p->pVfs = pVfs;
................................................................................
  p->mask = TESTVFS_ALL_MASK;

  sqlite3_vfs_register(pVfs, isDefault);

  return TCL_OK;

 bad_args:
  Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT?");
  return TCL_ERROR;
}

int Sqlitetestvfs_Init(Tcl_Interp *interp){
  Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0);
  return TCL_OK;
}

#endif







>
>
>
>
>
>
>
>
|
|
>
|







 







>







 







>
>
>
>
>







 







>







 







|









571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
....
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
....
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
....
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
....
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
  Tcl_IncrRefCount(pId);
  pFd->pShmId = pId;
  Tcl_ResetResult(p->interp);

  rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, pFd->pReal, flags, pOutFlags);
  if( pFd->pReal->pMethods ){
    sqlite3_io_methods *pMethods;
    int nByte;

    if( pVfs->iVersion>1 ){
      nByte = sizeof(sqlite3_io_methods);
    }else{
      nByte = offsetof(sqlite3_io_methods, xShmOpen);
    }

    pMethods = (sqlite3_io_methods *)ckalloc(nByte);
    memcpy(pMethods, &tvfs_io_methods, nByte);
    pMethods->iVersion = pVfs->iVersion;
    if( pVfs->iVersion>1 && ((Testvfs *)pVfs->pAppData)->isNoshm ){
      pMethods->xShmOpen = 0;
      pMethods->xShmClose = 0;
      pMethods->xShmLock = 0;
      pMethods->xShmBarrier = 0;
      pMethods->xShmMap = 0;
    }
    pFile->pMethods = pMethods;
................................................................................
  int nByte;                      /* Bytes of space to allocate at p */

  int i;
  int isNoshm = 0;                /* True if -noshm is passed */
  int isDefault = 0;              /* True if -default is passed */
  int szOsFile = 0;               /* Value passed to -szosfile */
  int mxPathname = -1;            /* Value passed to -mxpathname */
  int iVersion = 2;               /* Value passed to -iversion */

  if( objc<2 || 0!=(objc%2) ) goto bad_args;
  for(i=2; i<objc; i += 2){
    int nSwitch;
    char *zSwitch;
    zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch); 

................................................................................
        return TCL_ERROR;
      }
    }
    else if( nSwitch>2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){
      if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){
        return TCL_ERROR;
      }
    }
    else if( nSwitch>2 && 0==strncmp("-iversion", zSwitch, nSwitch) ){
      if( Tcl_GetIntFromObj(interp, objv[i+1], &iVersion) ){
        return TCL_ERROR;
      }
    }
    else{
      goto bad_args;
    }
  }

  if( szOsFile<sizeof(TestvfsFile) ){
................................................................................

  p->zName = (char *)&p[1];
  memcpy(p->zName, zVfs, strlen(zVfs)+1);

  pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs));
  memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
  pVfs->pAppData = (void *)p;
  pVfs->iVersion = iVersion;
  pVfs->zName = p->zName;
  pVfs->mxPathname = p->pParent->mxPathname;
  if( mxPathname>=0 && mxPathname<pVfs->mxPathname ){
    pVfs->mxPathname = mxPathname;
  }
  pVfs->szOsFile = szOsFile;
  p->pVfs = pVfs;
................................................................................
  p->mask = TESTVFS_ALL_MASK;

  sqlite3_vfs_register(pVfs, isDefault);

  return TCL_OK;

 bad_args:
  Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?");
  return TCL_ERROR;
}

int Sqlitetestvfs_Init(Tcl_Interp *interp){
  Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0);
  return TCL_OK;
}

#endif

Changes to test/pager1.test.

1088
1089
1090
1091
1092
1093
1094






















1095
1096
1097
1098
1099
1100
1101
....
1752
1753
1754
1755
1756
1757
1758


1759


1760







































































































































































1761
1762
  }] {
    do_execsql_test pager1-7.1.$tn.1 $sql $res
    catch { set J -1 ; set J [file size test.db-journal] }
    catch { set W -1 ; set W [file size test.db-wal] }
    do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
  }
}























#-------------------------------------------------------------------------
# The following tests, pager1-8.*, test that the special filenames 
# ":memory:" and "" open temporary databases.
#
foreach {tn filename} {
  1 :memory:
................................................................................
    BEGIN;
    INSERT INTO t2 VALUES('xxxx');
  }
  recursive_select 32
  execsql COMMIT
} {}



  










































































































































































finish_test








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
....
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
  }] {
    do_execsql_test pager1-7.1.$tn.1 $sql $res
    catch { set J -1 ; set J [file size test.db-journal] }
    catch { set W -1 ; set W [file size test.db-wal] }
    do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
  }
}

do_test pager1-7.2.1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA locking_mode = EXCLUSIVE;
    CREATE TABLE t1(a, b);
    BEGIN;
      PRAGMA journal_mode = delete;
      PRAGMA journal_mode = truncate;
  }
} {exclusive delete truncate}
do_test pager1-7.2.2 {
  execsql { INSERT INTO t1 VALUES(1, 2) }
  execsql { PRAGMA journal_mode = persist }
} {truncate}
do_test pager1-7.2.3 {
  execsql { COMMIT }
  execsql {
    PRAGMA journal_mode = persist;
    PRAGMA journal_size_limit;
  }
} {persist -1}

#-------------------------------------------------------------------------
# The following tests, pager1-8.*, test that the special filenames 
# ":memory:" and "" open temporary databases.
#
foreach {tn filename} {
  1 :memory:
................................................................................
    BEGIN;
    INSERT INTO t2 VALUES('xxxx');
  }
  recursive_select 32
  execsql COMMIT
} {}

#-------------------------------------------------------------------------
# Test that a WAL database may not be opened if:
#
#   pager1-21.1.*: The VFS has an iVersion less than 2, or
#   pager1-21.2.*: The VFS does not provide xShmXXX() methods.
#
do_test pager1-21.0 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA journal_mode = WAL;
    CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
    INSERT INTO ko DEFAULT VALUES;
  }
} {wal}
do_test pager1-21.1 {
  testvfs tv -noshm 1
  sqlite3 db2 test.db -vfs tv
  catchsql { SELECT * FROM ko } db2
} {1 {unable to open database file}}
db2 close
tv delete
do_test pager1-21.2 {
breakpoint
  testvfs tv -iversion 1
  sqlite3 db2 test.db -vfs tv
  catchsql { SELECT * FROM ko } db2
} {1 {unable to open database file}}
db2 close
tv delete

#-------------------------------------------------------------------------
# Test that a "PRAGMA wal_checkpoint":
#
#   pager1-22.1.*: is a no-op on a non-WAL db, and
#   pager1-22.2.*: does not cause xSync calls with a synchronous=off db.
#
do_test pager1-22.1.1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
    INSERT INTO ko DEFAULT VALUES;
  }
  execsql { PRAGMA wal_checkpoint }
} {}
do_test pager1-22.2.1 {
  testvfs tv -default 1
  tv filter xSync
  tv script xSyncCb
  proc xSyncCb {args} {incr ::synccount}
  set ::synccount 0
  sqlite3 db test.db
  execsql {
    PRAGMA synchronous = off;
    PRAGMA journal_mode = WAL;
    INSERT INTO ko DEFAULT VALUES;
  }
  execsql { PRAGMA wal_checkpoint }
  set synccount
} {0}
db close
tv delete

#-------------------------------------------------------------------------
# Tests for changing journal mode.
#
#   pager1-23.1.*: Test that when changing from PERSIST to DELETE mode,
#                  the journal file is deleted.
#
#   pager1-23.2.*: Same test as above, but while a shared lock is held
#                  on the database file.
#
#   pager1-23.3.*: Same test as above, but while a reserved lock is held
#                  on the database file.
#
#   pager1-23.4.*: And, for fun, while holding an exclusive lock.
#
#   pager1-23.5.*: Try to set various different journal modes with an
#                  in-memory database (only MEMORY and OFF should work).
#
do_test pager1-23.1.1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA journal_mode = PERSIST;
    CREATE TABLE t1(a, b);
  }
  file exists test.db-journal
} {1}
do_test pager1-23.1.2 {
  execsql { PRAGMA journal_mode = DELETE }
  file exists test.db-journal
} {0}

do_test pager1-23.2.1 {
  execsql {
    PRAGMA journal_mode = PERSIST;
    INSERT INTO t1 VALUES('Canberra', 'ACT');
  }
  db eval { SELECT * FROM t1 } {
    db eval { PRAGMA journal_mode = DELETE }
  }
  execsql { PRAGMA journal_mode }
} {delete}
do_test pager1-23.2.2 {
  file exists test.db-journal
} {0}

do_test pager1-23.3.1 {
  execsql {
    PRAGMA journal_mode = PERSIST;
    INSERT INTO t1 VALUES('Darwin', 'NT');
    BEGIN IMMEDIATE;
  }
  db eval { PRAGMA journal_mode = DELETE }
  execsql { PRAGMA journal_mode }
} {delete}
do_test pager1-23.3.2 {
  file exists test.db-journal
} {0}
do_test pager1-23.3.3 {
  execsql COMMIT
} {}

do_test pager1-23.4.1 {
  execsql {
    PRAGMA journal_mode = PERSIST;
    INSERT INTO t1 VALUES('Adelaide', 'SA');
    BEGIN EXCLUSIVE;
  }
  db eval { PRAGMA journal_mode = DELETE }
  execsql { PRAGMA journal_mode }
} {delete}
do_test pager1-23.4.2 {
  file exists test.db-journal
} {0}
do_test pager1-23.4.3 {
  execsql COMMIT
} {}

do_test pager1-23.5.1 {
  faultsim_delete_and_reopen
  sqlite3 db :memory:
} {}
foreach {tn mode possible} {
  2  off      1
  3  memory   1
  4  persist  0
  5  delete   0
  6  wal      0
  7  truncate 0
} {
  do_test pager1-23.5.$tn.1 {
    execsql "PRAGMA journal_mode = off"
    execsql "PRAGMA journal_mode = $mode"
  } [if $possible {list $mode} {list off}]
  do_test pager1-23.5.$tn.2 {
    execsql "PRAGMA journal_mode = memory"
    execsql "PRAGMA journal_mode = $mode"
  } [if $possible {list $mode} {list memory}]
}
do_test pager1-23.8.1 {
  execsql {PRAGMA locking_mode = normal}
} {exclusive}
do_test pager1-23.8.2 {
  execsql {PRAGMA locking_mode = exclusive}
} {exclusive}
do_test pager1-23.8.3 {
  execsql {PRAGMA locking_mode}
} {exclusive}
do_test pager1-23.8.4 {
  execsql {PRAGMA main.locking_mode}
} {exclusive}


finish_test

Changes to test/pagerfault.test.

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
...
708
709
710
711
712
713
714


715


































































































































































































716
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE TABLE xx(a, b) }
} -test {
  faultsim_test_result {0 {}}
}

}


#---------------------------------------------------------------------------
# Test fault injection into a small backup operation.
#
do_test pagerfault-14-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
................................................................................
  }
} -test {
  faultsim_test_result {0 {exclusive wal delete wal persist}}
  faultsim_integrity_check
}







































































































































































































finish_test







<
<







 







>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

610
611
612
613
614
615
616


617
618
619
620
621
622
623
...
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE TABLE xx(a, b) }
} -test {
  faultsim_test_result {0 {}}
}




#---------------------------------------------------------------------------
# Test fault injection into a small backup operation.
#
do_test pagerfault-14-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
................................................................................
  }
} -test {
  faultsim_test_result {0 {exclusive wal delete wal persist}}
  faultsim_integrity_check
}


#-------------------------------------------------------------------------
# Test fault injection while changing into and out of WAL mode.
#
do_test pagerfault-17-pre1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1862, 'Botha');
    INSERT INTO t1 VALUES(1870, 'Smuts');
    INSERT INTO t1 VALUES(1866, 'Hertzog');
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-17a -prep {
  faultsim_restore_and_reopen
} -body {
  execsql {
    PRAGMA journal_mode = wal;
    PRAGMA journal_mode = delete;
  }
} -test {
  faultsim_test_result {0 {wal delete}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17b -prep {
  faultsim_restore_and_reopen
  execsql { PRAGMA synchronous = OFF }
} -body {
  execsql {
    PRAGMA journal_mode = wal;
    INSERT INTO t1 VALUES(22, 'Clarke');
    PRAGMA journal_mode = delete;
  }
} -test {
  faultsim_test_result {0 {wal delete}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17c -prep {
  faultsim_restore_and_reopen
  execsql { 
    PRAGMA locking_mode = exclusive;
    PRAGMA journal_mode = wal;
  }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17d -prep {
  faultsim_restore_and_reopen
  sqlite3 db2 test.db
  execsql { PRAGMA journal_mode = delete }
  execsql { PRAGMA journal_mode = wal }
  execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {1 {database is locked}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17e -prep {
  faultsim_restore_and_reopen
  sqlite3 db2 test.db
  execsql { PRAGMA journal_mode = delete }
  execsql { PRAGMA journal_mode = wal }
  set ::chan [launch_testfixture]
  testfixture $::chan {
    sqlite3 db test.db
    db eval { INSERT INTO t1 VALUES(101, 'Latham') }
  }
  catch { testfixture $::chan sqlite_abort }
  catch { close $::chan }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# Test fault-injection when changing from journal_mode=persist to 
# journal_mode=delete (this involves deleting the journal file).
#
do_test pagerfault-18-pre1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE qq(x);
    INSERT INTO qq VALUES('Herbert');
    INSERT INTO qq VALUES('Macalister');
    INSERT INTO qq VALUES('Mackenzie');
    INSERT INTO qq VALUES('Lilley');
    INSERT INTO qq VALUES('Palmer');
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-18 -prep {
  faultsim_restore_and_reopen
  execsql {
    PRAGMA journal_mode = PERSIST;
    INSERT INTO qq VALUES('Beatty');
  }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}

}

do_faultsim_test pagerfault-19a -prep {
  sqlite3 db :memory:
  db func a_string a_string
  execsql {
    PRAGMA auto_vacuum = FULL;
    BEGIN;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
    COMMIT;
  }
} -body {
  execsql { 
    CREATE TABLE t2(a, b);
    INSERT INTO t2 SELECT * FROM t1; 
    DELETE FROM t1;
  }
} -test {
  faultsim_test_result {0 {}}
}

do_test pagerfault-19-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA auto_vacuum = FULL;
    CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
    CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
    CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
    CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
    CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
    CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-19b -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { 
    BEGIN;
      UPDATE t4 SET x = x+1;
      UPDATE t6 SET x = x+1;
      SAVEPOINT one;
        UPDATE t3 SET x = x+1;
        SAVEPOINT two;
          DROP TABLE t2;
      ROLLBACK TO one;
    COMMIT;
    SELECT * FROM t3;
    SELECT * FROM t4;
    SELECT * FROM t6;
  }
} -test {
  faultsim_test_result {0 {3 5 7}}
}

do_test pagerfault-20-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z));
    INSERT INTO x1 VALUES(a_string(400), a_string(500), a_string(600));
    INSERT INTO x1 SELECT a_string(600), a_string(400), a_string(500) FROM x1;
    INSERT INTO x1 SELECT a_string(500), a_string(600), a_string(400) FROM x1;
    INSERT INTO x1 SELECT a_string(400), a_string(500), a_string(600) FROM x1;
    INSERT INTO x1 SELECT a_string(600), a_string(400), a_string(500) FROM x1;
    INSERT INTO x1 SELECT a_string(500), a_string(600), a_string(400) FROM x1;
    INSERT INTO x1 SELECT a_string(400), a_string(500), a_string(600) FROM x1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-20 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
} -body {
  execsql { 
    BEGIN;
    UPDATE x1 SET z = a_string(300);
    DELETE FROM x1 WHERE rowid<32;
    COMMIT;
  }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
  set nRow [db one {SELECT count(*) FROM x1}]
  if {$nRow!=33 && $nRow!=64} {error "Wrong number of rows $nRow"}
}

finish_test