# 2013 Feb 25
#
# 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.
#
#***********************************************************************
# This file implements regression tests for the SQLite library, focusing
# on the incremental vacuum feature.
#
# The tests in this file were added at the same time as optimizations 
# were made to:
#
#   * Truncate the database after a rollback mode commit, and
#
#   * Avoid moving pages to locations from which they may need to be moved
#     a second time if an incremental-vacuum proccess is allowed to vacuum
#     the entire database.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix incrvacuum3

# If this build of the library does not support auto-vacuum, omit this
# whole file.
ifcapable {!autovacuum || !pragma} {
  finish_test
  return
}

proc check_on_disk {} {

  # Copy the wal and journal files for database "test.db" to "test2.db".
  forcedelete test2.db test2.db-journal test2.db-wal
  if {[file exists test.db-journal]} { 
    forcecopy test.db-journal test2.db-journal 
  }
  if {[file exists test.db-wal]} { 
    forcecopy test.db-wal test2.db-wal 
  }

  # Now copy the database file itself. Do this using open/read/puts
  # instead of the [file copy] command in order to avoid attempting
  # to read the 512 bytes begining at offset $sqlite_pending_byte.
  #
  set sz [file size test.db]
  set fd [open test.db]
  set fd2 [open test2.db w]
  fconfigure $fd  -encoding binary -translation binary
  fconfigure $fd2 -encoding binary -translation binary
  if {$sz>$::sqlite_pending_byte} {
    puts -nonewline $fd2 [read $fd $::sqlite_pending_byte]
    seek $fd [expr $::sqlite_pending_byte+512]
    seek $fd2 [expr $::sqlite_pending_byte+512]
  }
  puts -nonewline $fd2 [read $fd]
  close $fd2
  close $fd

  # Open "test2.db" and check it is Ok.
  sqlite3 dbcheck test2.db
  set ret [dbcheck eval { PRAGMA integrity_check }]
  dbcheck close
  set ret
}

# Run these tests once in rollback journal mode, and once in wal mode.
#
foreach {T jrnl_mode} {
  1 delete
  2 wal
} {
  catch { db close }
  forcedelete test.db test.db-journal test.db-wal
  sqlite3 db test.db
  db eval {
    PRAGMA cache_size = 5;
    PRAGMA page_size = 1024;
    PRAGMA auto_vacuum = 2;
  }
  db eval "PRAGMA journal_mode = $jrnl_mode"
  
  foreach {tn sql} {
    1 {
      CREATE TABLE t1(x UNIQUE);
      INSERT INTO t1 VALUES(randomblob(400));
      INSERT INTO t1 VALUES(randomblob(400));
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    --   4
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    --   8
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  16
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  32
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  64
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 128
      INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 256
    }
  
    2 {
      DELETE FROM t1 WHERE rowid%8;
    }
  
    3 { 
      BEGIN;
        PRAGMA incremental_vacuum = 100;
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  64
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 128
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 256
      ROLLBACK;
    }
  
    4 { 
      BEGIN;
        SAVEPOINT one;
          PRAGMA incremental_vacuum = 100;
          SAVEPOINT two;
            INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  64
            INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 128
            INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 256
    }
  
    5 {   ROLLBACK to two }
  
    6 { ROLLBACK to one }
  
    7 { 
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  64
        PRAGMA incremental_vacuum = 1000;
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 128
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    -- 256
      ROLLBACK;
    }
  
    8 { 
      BEGIN;
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  64
        PRAGMA incremental_vacuum = 1000;
        INSERT INTO t1 SELECT randomblob(400) FROM t1;    --  128
      COMMIT;
    }
  } {
    do_execsql_test $T.1.$tn.1 $sql
    do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok
    do_test         $T.1.$tn.3 { check_on_disk }        ok
  }

  do_execsql_test $T.1.x.1 { PRAGMA freelist_count   } 0
  do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128
}

finish_test