From 153eda0aa469406a29a532735afc8bcae0b51a6b Mon Sep 17 00:00:00 2001
From: dan <dan@noemail.net>
Date: Mon, 21 Jun 2010 07:45:47 +0000
Subject: [PATCH] Add further pager tests.

FossilOrigin-Name: 4104b175a8c3560a7680f3d2b54416821bb2e19d
---
 manifest               |  19 +++--
 manifest.uuid          |   2 +-
 test/pager1.test       | 182 +++++++++++++++++------------------------
 test/pager2.test       | 117 ++++++++++++++++++++++++++
 test/pagerfault.test   |  24 ++++++
 test/permutations.test |   1 +
 test/tester.tcl        |  10 +++
 7 files changed, 240 insertions(+), 115 deletions(-)
 create mode 100644 test/pager2.test

diff --git a/manifest b/manifest
index 7f2143caf9..231f496cdf 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\slatest\strunk\schange.
-D 2010-06-21T06:00:16
+C Add\sfurther\spager\stests.
+D 2010-06-21T07:45:47
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -534,13 +534,14 @@ F test/notify2.test 195a467e021f74197be2c4fb02d6dee644b8d8db
 F test/notnull.test cc7c78340328e6112a13c3e311a9ab3127114347
 F test/null.test a8b09b8ed87852742343b33441a9240022108993
 F test/openv2.test af02ed0a9cbc0d2a61b8f35171d4d117e588e4ec
-F test/pager1.test 2842b3567589fc4881928a7febeb9342442ea449
-F test/pagerfault.test 55c7d602eaba7c08312616a4c6482606d50eed6a
+F test/pager1.test e086d98a5312a7395d579117a1211d9110faffcc
+F test/pager2.test ad062a51030dc1e2749f506528db4cc5bae6474c
+F test/pagerfault.test e67e9c18bf7b4bb8cc8d458d3a5ecc980f18a225
 F test/pageropt.test 8146bf448cf09e87bb1867c2217b921fb5857806
 F test/pagesize.test 76aa9f23ecb0741a4ed9d2e16c5fa82671f28efb
 F test/pcache.test eebc4420b37cb07733ae9b6e99c9da7c40dd6d58
 F test/pcache2.test 0d85f2ab6963aee28c671d4c71bec038c00a1d16
-F test/permutations.test 97cfca3f7c390fa530c6f5c63d73eb67e5b0cd9d
+F test/permutations.test 9296368f1d14d9e042f146a804ca38f551d35435
 F test/pragma.test 6960f9efbce476f70ba9ee2171daf5042f9e3d8a
 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
 F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea
@@ -611,7 +612,7 @@ F test/tclsqlite.test 8c154101e704170c2be10f137a5499ac2c6da8d3
 F test/tempdb.test 800c36623d67a2ad1f58784b9c5644e0405af6e6
 F test/temptable.test f42121a0d29a62f00f93274464164177ab1cc24a
 F test/temptrigger.test b0273db072ce5f37cf19140ceb1f0d524bbe9f05
-F test/tester.tcl 7912c3c8768320fd7bcb217637c2f0a607fbbc24
+F test/tester.tcl ab89e8e592ff26e2b65ff3cae9de5f26863ae766
 F test/thread001.test a3e6a7254d1cb057836cb3145b60c10bf5b7e60f
 F test/thread002.test afd20095e6e845b405df4f2c920cb93301ca69db
 F test/thread003.test b824d4f52b870ae39fc5bae4d8070eca73085dca
@@ -824,7 +825,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 59be370e52ec814c45efa6cbac45b6df94661b54 822a0283c6bc1c75001f3d1c528a4ff89c6b039c
-R cd73ab3d3f6400b82e1db5686eff6200
+P f6d26e07b70965e332b1589804ca938593a5f432
+R 57527134db79eb4730ccc85bc2c2f806
 U dan
-Z 0c29ece94fc927539688ff52cbe59deb
+Z 286faa52a0364bba3d1b0d11286b24cb
diff --git a/manifest.uuid b/manifest.uuid
index 6b35f2cebc..e246eaf3dc 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-f6d26e07b70965e332b1589804ca938593a5f432
\ No newline at end of file
+4104b175a8c3560a7680f3d2b54416821bb2e19d
\ No newline at end of file
diff --git a/test/pager1.test b/test/pager1.test
index 33b5c2ae04..313c0f36e0 100644
--- a/test/pager1.test
+++ b/test/pager1.test
@@ -14,6 +14,7 @@ set testdir [file dirname $argv0]
 source $testdir/tester.tcl
 source $testdir/lock_common.tcl
 source $testdir/malloc_common.tcl
+source $testdir/wal_common.tcl
 
 #
 # pager1-1.*: Test inter-process locking (clients in multiple processes).
@@ -28,13 +29,8 @@ source $testdir/malloc_common.tcl
 #
 # pager1-6.*: Cases related to "PRAGMA max_page_count"
 #
-
-proc do_execsql_test {testname sql result} {
-  uplevel do_test $testname [list "execsql {$sql}"] [list $result]
-}
-proc do_catchsql_test {testname sql result} {
-  uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
-}
+# pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
+#
 
 set a_string_counter 1
 proc a_string {n} {
@@ -216,104 +212,6 @@ do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
 do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
 do_execsql_test pager1-3.6 { COMMIT } {}
 
-set otn 0
-testvfs tv -default 1
-foreach code [list {
-  set s 512
-} {
-  set s 1024
-  set sql { PRAGMA journal_mode = memory }
-} {
-  set s 1024
-  set sql { 
-    PRAGMA journal_mode = memory;
-    PRAGMA locking_mode = exclusive;
-  }
-} {
-  set s 2048
-  tv devchar safe_append
-} {
-  set s 4096
-} {
-  set s 4096
-  set sql { PRAGMA journal_mode = WAL }
-} {
-  set s 8192
-  set sql { PRAGMA synchronous = off }
-}] {
-
-  incr otn
-  set sql ""
-  tv devchar {}
-  eval $code
-  tv sectorsize $s
-  
-  do_test pager1-3.7.$otn.0 {
-    faultsim_delete_and_reopen
-    execsql $sql
-    execsql {
-      PRAGMA cache_size = 10;
-      CREATE TABLE t1(i INTEGER PRIMARY KEY, j blob);
-    }
-  } {}
-
-  set tn 0
-  set lowpoint 0
-  foreach x {
-    100 x 0 100
-  x
-    70 22 96 59 96 50 22 56 21 16 37 64 43 40  0 38 22 38 55  0  6   
-    43 62 32 93 54 18 13 29 45 66 29 25 61 31 53 82 75 25 96 86 10 69   
-     2 29  6 60 80 95 42 82 85 50 68 96 90 39 78 69 87 97 48 74 65 43   
-  x
-    86 34 26 50 41 85 58 44 89 22  6 51 45 46 58 32 97  6  1 12 32  2   
-    69 39 48 71 33 31  5 58 90 43 24 54 12  9 18 57  4 38 91 42 27 45   
-    50 38 56 29 10  0 26 37 83  1 78 15 47 30 75 62 46 29 68  5 30  4   
-    27 96 33 95 79 75 56 10 29 70 32 75 52 88  5 36 50 57 46 63 88 65   
-  x
-    44 95 64 20 24 35 69 61 61  2 35 92 42 46 23 98 78  1 38 72 79 35   
-    94 37 13 59  5 93 27 58 80 75 58  7 67 13 10 76 84  4  8 70 81 45   
-     8 41 98  5 60 26 92 29 91 90  2 62 40  4  5 22 80 15 83 76 52 88   
-    29  5 68 73 72  7 54 17 89 32 81 94 51 28 53 71  8 42 54 59 70 79   
-  x
-  } {
-    incr tn
-    set now [db one {SELECT count(i) FROM t1}]
-    if {$x == "x"} {
-      execsql { COMMIT ; BEGIN }
-      set lowpoint $now
-      do_test pager1.3.7.$otn.$tn { 
-        sqlite3 db2 test.db
-        execsql {
-          SELECT COALESCE(max(i), 0) FROM t1;
-          PRAGMA integrity_check;
-        } 
-      } [list $lowpoint ok]
-      db2 close
-    } else {
-      if {$now > $x } {
-        if { $x>=$lowpoint } {
-          execsql "ROLLBACK TO sp_$x"
-        } else {
-          execsql "DELETE FROM t1 WHERE i>$x"
-          set lowpoint $x
-        }
-      } elseif {$now < $x} {
-        for {set k $now} {$k < $x} {incr k} {
-          execsql "SAVEPOINT sp_$k"
-          execsql { INSERT INTO t1(j) VALUES(randomblob(1500)) }
-        }
-      }
-      do_execsql_test pager1.3.7.$otn.$tn { 
-        SELECT COALESCE(max(i), 0) FROM t1;
-        PRAGMA integrity_check;
-      } [list $x ok]
-    }
-  }
-}
-db close
-tv delete
-
 #-------------------------------------------------------------------------
 # Hot journal rollback related test cases.
 #
@@ -620,5 +518,79 @@ do_test pager1-6.2 {
   }
 } {1 {database or disk is full}}
 
+
+#-------------------------------------------------------------------------
+# The following tests work with "PRAGMA journal_mode=TRUNCATE" and
+# "PRAGMA locking_mode=EXCLUSIVE".
+#
+# Each test is specified with 5 variables. As follows:
+#
+#   $tn:  Test Number. Used as part of the [do_test] test names.
+#   $sql: SQL to execute.
+#   $res: Expected result of executing $sql.
+#   $js:  The expected size of the journal file, in bytes, after executing
+#         the SQL script. Or -1 if the journal is not expected to exist.
+#   $ws:  The expected size of the WAL file, in bytes, after executing
+#         the SQL script. Or -1 if the WAL is not expected to exist.
+#
+faultsim_delete_and_reopen
+foreach {tn sql res js ws} [subst {
+
+  1  {
+    CREATE TABLE t1(a, b);
+    PRAGMA auto_vacuum=OFF;
+    PRAGMA synchronous=NORMAL;
+    PRAGMA page_size=1024;
+    PRAGMA locking_mode=EXCLUSIVE;
+    PRAGMA journal_mode=TRUNCATE;
+    INSERT INTO t1 VALUES(1, 2);
+  } {exclusive truncate} 0 -1
+
+  2  {
+    BEGIN IMMEDIATE;
+      SELECT * FROM t1;
+    COMMIT;
+  } {1 2} 0 -1
+
+  3  {
+    BEGIN;
+      SELECT * FROM t1;
+    COMMIT;
+  } {1 2} 0 -1
+
+  4  { PRAGMA journal_mode = WAL }    wal    -1 -1
+  5  { INSERT INTO t1 VALUES(3, 4) }  {}     -1 [wal_file_size 1 1024]
+  6  { PRAGMA locking_mode = NORMAL } normal -1 [wal_file_size 1 1024]
+  7  { INSERT INTO t1 VALUES(5, 6); } {}     -1 [wal_file_size 2 1024]
+
+  8  { PRAGMA journal_mode = TRUNCATE } truncate          0 -1
+  9  { INSERT INTO t1 VALUES(7, 8) }    {}                0 -1
+  10 { SELECT * FROM t1 }               {1 2 3 4 5 6 7 8} 0 -1
+
+}] {
+  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-8.1 {
+  faultsim_delete_and_reopen
+  db close
+  sqlite3 db :memory:
+  execsql {
+    CREATE TABLE x1(x);
+    INSERT INTO x1 VALUES('Charles');
+    INSERT INTO x1 VALUES('James');
+    INSERT INTO x1 VALUES('Mary');
+    SELECT * FROM x1;
+  }
+} {Charles James Mary}
+do_test pager1-8.2 {
+  db close
+  sqlite3 db :memory:
+  catchsql { SELECT * FROM x1 }
+} {1 {no such table: x1}}
+
 finish_test
 
diff --git a/test/pager2.test b/test/pager2.test
new file mode 100644
index 0000000000..855850f056
--- /dev/null
+++ b/test/pager2.test
@@ -0,0 +1,117 @@
+# 2010 June 15
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/lock_common.tcl
+source $testdir/malloc_common.tcl
+
+set otn 0
+testvfs tv -default 1
+foreach code [list {
+  set s 512
+} {
+  set s 1024
+  set sql { PRAGMA journal_mode = memory }
+} {
+  set s 1024
+  set sql { 
+    PRAGMA journal_mode = memory;
+    PRAGMA locking_mode = exclusive;
+  }
+} {
+  set s 2048
+  tv devchar safe_append
+} {
+  set s 4096
+} {
+  set s 4096
+  set sql { PRAGMA journal_mode = WAL }
+} {
+  set s 8192
+  set sql { PRAGMA synchronous = off }
+}] {
+
+  incr otn
+  set sql ""
+  tv devchar {}
+  eval $code
+  tv sectorsize $s
+  
+  do_test pager2-1.$otn.0 {
+    faultsim_delete_and_reopen
+    execsql $sql
+    execsql {
+      PRAGMA cache_size = 10;
+      CREATE TABLE t1(i INTEGER PRIMARY KEY, j blob);
+    }
+  } {}
+
+  set tn 0
+  set lowpoint 0
+  foreach x {
+    100 x 0 100
+  x
+    70 22 96 59 96 50 22 56 21 16 37 64 43 40  0 38 22 38 55  0  6   
+    43 62 32 93 54 18 13 29 45 66 29 25 61 31 53 82 75 25 96 86 10 69   
+     2 29  6 60 80 95 42 82 85 50 68 96 90 39 78 69 87 97 48 74 65 43   
+  x
+    86 34 26 50 41 85 58 44 89 22  6 51 45 46 58 32 97  6  1 12 32  2   
+    69 39 48 71 33 31  5 58 90 43 24 54 12  9 18 57  4 38 91 42 27 45   
+    50 38 56 29 10  0 26 37 83  1 78 15 47 30 75 62 46 29 68  5 30  4   
+    27 96 33 95 79 75 56 10 29 70 32 75 52 88  5 36 50 57 46 63 88 65   
+  x
+    44 95 64 20 24 35 69 61 61  2 35 92 42 46 23 98 78  1 38 72 79 35   
+    94 37 13 59  5 93 27 58 80 75 58  7 67 13 10 76 84  4  8 70 81 45   
+     8 41 98  5 60 26 92 29 91 90  2 62 40  4  5 22 80 15 83 76 52 88   
+    29  5 68 73 72  7 54 17 89 32 81 94 51 28 53 71  8 42 54 59 70 79   
+  x
+  } {
+    incr tn
+    set now [db one {SELECT count(i) FROM t1}]
+    if {$x == "x"} {
+      execsql { COMMIT ; BEGIN }
+      set lowpoint $now
+      do_test pager2.1.$otn.$tn { 
+        sqlite3 db2 test.db
+        execsql {
+          SELECT COALESCE(max(i), 0) FROM t1;
+          PRAGMA integrity_check;
+        } 
+      } [list $lowpoint ok]
+      db2 close
+    } else {
+      if {$now > $x } {
+        if { $x>=$lowpoint } {
+          execsql "ROLLBACK TO sp_$x"
+        } else {
+          execsql "DELETE FROM t1 WHERE i>$x"
+          set lowpoint $x
+        }
+      } elseif {$now < $x} {
+        for {set k $now} {$k < $x} {incr k} {
+          execsql "SAVEPOINT sp_$k"
+          execsql { INSERT INTO t1(j) VALUES(randomblob(1500)) }
+        }
+      }
+      do_execsql_test pager2.1.$otn.$tn { 
+        SELECT COALESCE(max(i), 0) FROM t1;
+        PRAGMA integrity_check;
+      } [list $x ok]
+    }
+  }
+}
+db close
+tv delete
+
+
+finish_test
diff --git a/test/pagerfault.test b/test/pagerfault.test
index 81a62ed7ea..0af8d51807 100644
--- a/test/pagerfault.test
+++ b/test/pagerfault.test
@@ -241,6 +241,30 @@ do_faultsim_test pagerfault-5.3 -prep {
   faultsim_test_result {0 {}}
 }
 
+#-------------------------------------------------------------------------
+# Test fault-injection as part of a commit when using 
+# journal_mode=TRUNCATE.
+#
+do_test pagerfault-6-pre1 {
+  faultsim_delete_and_reopen
+  db func a_string a_string
+  execsql {
+    CREATE TABLE t1(a UNIQUE, b UNIQUE);
+    INSERT INTO t1 VALUES(a_string(200), a_string(300));
+  }
+  faultsim_save_and_close
+} {}
+do_faultsim_test pagerfault-6.1 -prep {
+  faultsim_restore_and_reopen
+  db func a_string a_string
+  execsql { PRAGMA journal_mode = TRUNCATE }
+} -body {
+  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
+} -test {
+  faultsim_test_result {0 {}}
+  faultsim_integrity_check
+}
+
 # The following was an attempt to get a bitvec malloc to fail. Didn't work.
 #
 # do_test pagerfault-6-pre1 {
diff --git a/test/permutations.test b/test/permutations.test
index 27f795676a..e1846c13ac 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -169,6 +169,7 @@ test_suite "coverage-pager" -description {
   Coverage tests for file pager.c.
 } -files {
   pager1.test
+  pager2.test
   pagerfault.test
   journal2.test
 } 
diff --git a/test/tester.tcl b/test/tester.tcl
index 625bdcc3ee..d6ce7eac28 100644
--- a/test/tester.tcl
+++ b/test/tester.tcl
@@ -49,6 +49,8 @@
 #      crashsql               ARGS...
 #      integrity_check        TESTNAME ?DB?
 #      do_test                TESTNAME SCRIPT EXPECTED
+#      do_execsql_test        TESTNAME SQL EXPECTED
+#      do_catchsql_test       TESTNAME SQL EXPECTED
 #
 # Commands providing a lower level interface to the global test counters:
 #
@@ -316,6 +318,14 @@ proc do_test {name cmd expected} {
   }
   flush stdout
 }
+    
+proc do_execsql_test {testname sql result} {
+  uplevel do_test $testname [list "execsql {$sql}"] [list $result]
+}
+proc do_catchsql_test {testname sql result} {
+  uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
+}
+
 
 # Run an SQL script.  
 # Return the number of microseconds per statement.