sl@0
|
1 |
# 2007 Dec 12
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
#
|
sl@0
|
12 |
# This file contains test cases for stressing database
|
sl@0
|
13 |
# changes that involve side effects that delete rows from
|
sl@0
|
14 |
# the table being changed. Ticket #2832 shows that in
|
sl@0
|
15 |
# older versions of SQLite that behavior was implemented
|
sl@0
|
16 |
# incorrectly and resulted in corrupt database files.
|
sl@0
|
17 |
#
|
sl@0
|
18 |
# $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
|
sl@0
|
19 |
#
|
sl@0
|
20 |
|
sl@0
|
21 |
set testdir [file dirname $argv0]
|
sl@0
|
22 |
source $testdir/tester.tcl
|
sl@0
|
23 |
|
sl@0
|
24 |
# The sequence table is created to store a sequence of integers
|
sl@0
|
25 |
# starting with 1. This is used to reinitialize other tables
|
sl@0
|
26 |
# as part of other tests.
|
sl@0
|
27 |
#
|
sl@0
|
28 |
do_test sidedelete-1.1 {
|
sl@0
|
29 |
execsql {
|
sl@0
|
30 |
CREATE TABLE sequence(a INTEGER PRIMARY KEY);
|
sl@0
|
31 |
INSERT INTO sequence VALUES(1);
|
sl@0
|
32 |
INSERT INTO sequence VALUES(2);
|
sl@0
|
33 |
}
|
sl@0
|
34 |
for {set i 0} {$i<8} {incr i} {
|
sl@0
|
35 |
execsql {
|
sl@0
|
36 |
INSERT INTO sequence SELECT a+(SELECT max(a) FROM sequence) FROM sequence;
|
sl@0
|
37 |
}
|
sl@0
|
38 |
}
|
sl@0
|
39 |
execsql {SELECT count(*) FROM sequence}
|
sl@0
|
40 |
} {512}
|
sl@0
|
41 |
|
sl@0
|
42 |
# Make a series of changes using an UPDATE OR REPLACE and a
|
sl@0
|
43 |
# correlated subquery. This would cause database corruption
|
sl@0
|
44 |
# prior to the fix for ticket #2832.
|
sl@0
|
45 |
#
|
sl@0
|
46 |
do_test sidedelete-2.0 {
|
sl@0
|
47 |
execsql {
|
sl@0
|
48 |
CREATE TABLE t1(a PRIMARY KEY, b);
|
sl@0
|
49 |
CREATE TABLE chng(a PRIMARY KEY, b);
|
sl@0
|
50 |
SELECT count(*) FROM t1;
|
sl@0
|
51 |
SELECT count(*) FROM chng;
|
sl@0
|
52 |
}
|
sl@0
|
53 |
} {0 0}
|
sl@0
|
54 |
for {set i 2} {$i<=100} {incr i} {
|
sl@0
|
55 |
set n [expr {($i+2)/2}]
|
sl@0
|
56 |
do_test sidedelete-2.$i.1 {
|
sl@0
|
57 |
execsql {
|
sl@0
|
58 |
DELETE FROM t1;
|
sl@0
|
59 |
INSERT INTO t1 SELECT a, a FROM sequence WHERE a<=$i;
|
sl@0
|
60 |
DELETE FROM chng;
|
sl@0
|
61 |
INSERT INTO chng SELECT a*2, a*2+1 FROM sequence WHERE a<=$i/2;
|
sl@0
|
62 |
UPDATE OR REPLACE t1 SET a=(SELECT b FROM chng WHERE a=t1.a);
|
sl@0
|
63 |
SELECT count(*), sum(a) FROM t1;
|
sl@0
|
64 |
}
|
sl@0
|
65 |
} [list $n [expr {$n*$n-1}]]
|
sl@0
|
66 |
integrity_check sidedelete-2.$i.2
|
sl@0
|
67 |
}
|
sl@0
|
68 |
|
sl@0
|
69 |
# This will cause stacks leaks but not database corruption prior
|
sl@0
|
70 |
# to the #2832 fix.
|
sl@0
|
71 |
#
|
sl@0
|
72 |
do_test sidedelete-3.0 {
|
sl@0
|
73 |
execsql {
|
sl@0
|
74 |
DROP TABLE t1;
|
sl@0
|
75 |
CREATE TABLE t1(a PRIMARY KEY);
|
sl@0
|
76 |
SELECT * FROM t1;
|
sl@0
|
77 |
}
|
sl@0
|
78 |
} {}
|
sl@0
|
79 |
for {set i 1} {$i<=100} {incr i} {
|
sl@0
|
80 |
set n [expr {($i+1)/2}]
|
sl@0
|
81 |
do_test sidedelete-3.$i.1 {
|
sl@0
|
82 |
execsql {
|
sl@0
|
83 |
DELETE FROM t1;
|
sl@0
|
84 |
INSERT INTO t1 SELECT a FROM sequence WHERE a<=$i;
|
sl@0
|
85 |
UPDATE OR REPLACE t1 SET a=a+1;
|
sl@0
|
86 |
SELECT count(*), sum(a) FROM t1;
|
sl@0
|
87 |
}
|
sl@0
|
88 |
} [list $n [expr {$n*($n+1)}]]
|
sl@0
|
89 |
integrity_check sidedelete-3.$i.2
|
sl@0
|
90 |
}
|
sl@0
|
91 |
|
sl@0
|
92 |
finish_test
|