os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/sidedelete.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/sidedelete.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,92 @@
     1.4 +# 2007 Dec 12
     1.5 +#
     1.6 +# The author disclaims copyright to this source code. In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +#
    1.15 +# This file contains test cases for stressing database
    1.16 +# changes that involve side effects that delete rows from
    1.17 +# the table being changed.  Ticket #2832 shows that in
    1.18 +# older versions of SQLite that behavior was implemented
    1.19 +# incorrectly and resulted in corrupt database files.
    1.20 +#
    1.21 +# $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
    1.22 +#
    1.23 +
    1.24 +set testdir [file dirname $argv0]
    1.25 +source $testdir/tester.tcl
    1.26 +
    1.27 +# The sequence table is created to store a sequence of integers
    1.28 +# starting with 1.  This is used to reinitialize other tables
    1.29 +# as part of other tests.
    1.30 +#
    1.31 +do_test sidedelete-1.1 {
    1.32 +  execsql {
    1.33 +    CREATE TABLE sequence(a INTEGER PRIMARY KEY);
    1.34 +    INSERT INTO sequence VALUES(1);
    1.35 +    INSERT INTO sequence VALUES(2);
    1.36 +  }
    1.37 +  for {set i 0} {$i<8} {incr i} {
    1.38 +    execsql {
    1.39 +      INSERT INTO sequence SELECT a+(SELECT max(a) FROM sequence) FROM sequence;
    1.40 +    }
    1.41 +  }
    1.42 +  execsql {SELECT count(*) FROM sequence}
    1.43 +} {512}
    1.44 +
    1.45 +# Make a series of changes using an UPDATE OR REPLACE and a
    1.46 +# correlated subquery.  This would cause database corruption
    1.47 +# prior to the fix for ticket #2832.
    1.48 +#
    1.49 +do_test sidedelete-2.0 {
    1.50 +  execsql {
    1.51 +    CREATE TABLE t1(a PRIMARY KEY, b);
    1.52 +    CREATE TABLE chng(a PRIMARY KEY, b);
    1.53 +    SELECT count(*) FROM t1;
    1.54 +    SELECT count(*) FROM chng;
    1.55 +  }
    1.56 +} {0 0}
    1.57 +for {set i 2} {$i<=100} {incr i} {
    1.58 +  set n [expr {($i+2)/2}]
    1.59 +  do_test sidedelete-2.$i.1 {
    1.60 +    execsql {
    1.61 +      DELETE FROM t1;
    1.62 +      INSERT INTO t1 SELECT a, a FROM sequence WHERE a<=$i;
    1.63 +      DELETE FROM chng;
    1.64 +      INSERT INTO chng SELECT a*2, a*2+1 FROM sequence WHERE a<=$i/2;
    1.65 +      UPDATE OR REPLACE t1 SET a=(SELECT b FROM chng WHERE a=t1.a);
    1.66 +      SELECT count(*), sum(a) FROM t1;
    1.67 +    }
    1.68 +  } [list $n [expr {$n*$n-1}]]
    1.69 +  integrity_check sidedelete-2.$i.2
    1.70 +}
    1.71 +
    1.72 +# This will cause stacks leaks but not database corruption prior
    1.73 +# to the #2832 fix.
    1.74 +#
    1.75 +do_test sidedelete-3.0 {
    1.76 +  execsql {
    1.77 +     DROP TABLE t1;
    1.78 +     CREATE TABLE t1(a PRIMARY KEY);
    1.79 +     SELECT * FROM t1;
    1.80 +  }
    1.81 +} {}
    1.82 +for {set i 1} {$i<=100} {incr i} {
    1.83 +  set n [expr {($i+1)/2}]
    1.84 +  do_test sidedelete-3.$i.1 {
    1.85 +    execsql {
    1.86 +      DELETE FROM t1;
    1.87 +      INSERT INTO t1 SELECT a FROM sequence WHERE a<=$i;
    1.88 +      UPDATE OR REPLACE t1 SET a=a+1;
    1.89 +      SELECT count(*), sum(a) FROM t1;
    1.90 +    }
    1.91 +  } [list $n [expr {$n*($n+1)}]]
    1.92 +  integrity_check sidedelete-3.$i.2
    1.93 +}
    1.94 +
    1.95 +finish_test