xref: /sqlite-3.40.0/ext/rtree/rtree3.test (revision f703b42d)
1# 2008 Feb 19
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# The focus of this file is testing that the r-tree correctly handles
13# out-of-memory conditions.
14#
15
16if {![info exists testdir]} {
17  set testdir [file join [file dirname [info script]] .. .. test]
18}
19source $testdir/tester.tcl
20source $testdir/malloc_common.tcl
21ifcapable !rtree {
22  finish_test
23  return
24}
25
26set ::TMPDBERROR [list 1 \
27  {unable to open a temporary database file for storing temporary tables}
28]
29
30# Test summary:
31#
32#   rtree3-1: Test OOM in simple CREATE TABLE, INSERT, DELETE and SELECT
33#             commands on an almost empty table.
34#
35#   rtree3-2: Test OOM in a DROP TABLE command.
36#
37#   rtree3-3a: Test OOM during a transaction to insert 100 pseudo-random rows.
38#
39#   rtree3-3b: Test OOM during a transaction deleting all entries in the
40#              database constructed in [rtree3-3a] in pseudo-random order.
41#
42#   rtree3-4a: OOM during "SELECT count(*) FROM ..." on a big table.
43#
44#   rtree3-4b: OOM while deleting rows from a big table.
45#
46#   rtree3-5: Test OOM while inserting rows into a big table.
47#
48#   rtree3-6: Test OOM while deleting all rows of a table, one at a time.
49#
50#   rtree3-7: OOM during an ALTER TABLE RENAME TABLE command.
51#
52#   rtree3-8: Test OOM while registering the r-tree module with sqlite.
53#
54#   rtree3-11: OOM following a constraint failure
55#
56do_faultsim_test rtree3-1 -faults oom* -prep {
57  faultsim_delete_and_reopen
58} -body {
59  execsql {
60    BEGIN TRANSACTION;
61    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
62    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
63    INSERT INTO rt VALUES(NULL, 13, 15, 17, 19);
64    DELETE FROM rt WHERE ii = 1;
65    SELECT * FROM rt;
66    SELECT ii FROM rt WHERE ii = 2;
67    COMMIT;
68  }
69}
70
71do_test rtree3-2.prep {
72  faultsim_delete_and_reopen
73  execsql {
74    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
75    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
76  }
77  faultsim_save_and_close
78} {}
79do_faultsim_test rtree3-2 -faults oom* -prep {
80  faultsim_restore_and_reopen
81} -body {
82  execsql { DROP TABLE rt }
83}
84
85do_malloc_test rtree3-3.prep {
86  faultsim_delete_and_reopen
87  execsql {
88    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2, +a1, +a2);
89    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
90  }
91  faultsim_save_and_close
92} {}
93
94do_faultsim_test rtree3-3a -faults oom* -prep {
95  faultsim_restore_and_reopen
96} -body {
97  db eval BEGIN
98  for {set ii 0} {$ii < 100} {incr ii} {
99    set f [expr rand()]
100    db eval {INSERT INTO rt VALUES(NULL, $f*10.0, $f*10.0, $f*15.0, $f*15.0)}
101  }
102  db eval COMMIT
103}
104faultsim_save_and_close
105
106do_faultsim_test rtree3-3b -faults oom* -prep {
107  faultsim_restore_and_reopen
108} -body {
109  db eval BEGIN
110  for {set ii 0} {$ii < 100} {incr ii} {
111    set f [expr rand()]
112    db eval { DELETE FROM rt WHERE x1<($f*10.0) AND x1>($f*10.5) }
113  }
114  db eval COMMIT
115}
116
117do_test rtree3-4.prep {
118  faultsim_delete_and_reopen
119  execsql {
120    BEGIN;
121    PRAGMA page_size = 512;
122    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
123  }
124  for {set i 0} {$i < 1500} {incr i} {
125    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
126  }
127  execsql { COMMIT }
128  faultsim_save_and_close
129} {}
130
131do_faultsim_test rtree3-4a -faults oom-* -prep {
132  faultsim_restore_and_reopen
133} -body {
134  db eval { SELECT count(*) FROM rt }
135} -test {
136  faultsim_test_result {0 1500}
137}
138
139do_faultsim_test rtree3-4b -faults oom-transient -prep {
140  faultsim_restore_and_reopen
141} -body {
142  db eval { DELETE FROM rt WHERE ii BETWEEN 1 AND 100 }
143} -test {
144  faultsim_test_result {0 {}}
145}
146
147do_test rtree3-5.prep {
148  faultsim_delete_and_reopen
149  execsql {
150    BEGIN;
151    PRAGMA page_size = 512;
152    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
153  }
154  for {set i 0} {$i < 100} {incr i} {
155    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
156  }
157  execsql { COMMIT }
158  faultsim_save_and_close
159} {}
160do_faultsim_test rtree3-5 -faults oom-* -prep {
161  faultsim_restore_and_reopen
162} -body {
163  for {set i 100} {$i < 110} {incr i} {
164    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
165  }
166} -test {
167  faultsim_test_result {0 {}}
168}
169
170do_test rtree3-6.prep {
171  faultsim_delete_and_reopen
172  execsql {
173    BEGIN;
174    PRAGMA page_size = 512;
175    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
176  }
177  for {set i 0} {$i < 50} {incr i} {
178    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
179  }
180  execsql { COMMIT }
181  faultsim_save_and_close
182} {}
183do_faultsim_test rtree3-6 -faults oom-* -prep {
184  faultsim_restore_and_reopen
185} -body {
186  execsql BEGIN
187  for {set i 0} {$i < 50} {incr i} {
188    execsql { DELETE FROM rt WHERE ii=$i }
189  }
190  execsql COMMIT
191} -test {
192  faultsim_test_result {0 {}}
193}
194
195do_test rtree3-7.prep {
196  faultsim_delete_and_reopen
197  execsql { CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2) }
198  faultsim_save_and_close
199} {}
200do_faultsim_test rtree3-7 -faults oom-* -prep {
201  faultsim_restore_and_reopen
202} -body {
203  execsql { ALTER TABLE rt RENAME TO rt2 }
204} -test {
205  faultsim_test_result {0 {}} $::TMPDBERROR
206}
207
208do_faultsim_test rtree3-8 -faults oom-* -prep {
209  catch { db close }
210} -body {
211  sqlite3 db test.db
212}
213
214do_faultsim_test rtree3-9 -faults oom-* -prep {
215  sqlite3 db :memory:
216} -body {
217  set rc [register_cube_geom db]
218  if {$rc != "SQLITE_OK"} { error $rc }
219} -test {
220  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
221}
222
223do_test rtree3-10.prep {
224  faultsim_delete_and_reopen
225  execsql {
226    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2, z1, z2);
227    INSERT INTO rt VALUES(1,  10, 10, 10, 11, 11, 11);
228    INSERT INTO rt VALUES(2,  5, 6, 6, 7, 7, 8);
229  }
230  faultsim_save_and_close
231} {}
232do_faultsim_test rtree3-10 -faults oom-* -prep {
233  faultsim_restore_and_reopen
234  register_cube_geom db
235  execsql { SELECT * FROM rt }
236} -body {
237  execsql { SELECT ii FROM rt WHERE ii MATCH cube(4.5, 5.5, 6.5, 1, 1, 1) }
238} -test {
239  faultsim_test_result {0 2}
240}
241
242
243do_test rtree3-11.prep {
244  faultsim_delete_and_reopen
245  execsql {
246    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
247    INSERT INTO rt VALUES(1, 2, 3, 4, 5);
248  }
249  faultsim_save_and_close
250} {}
251do_faultsim_test rtree3-10.1 -faults oom-* -prep {
252  faultsim_restore_and_reopen
253  execsql { SELECT * FROM rt }
254} -body {
255  execsql { INSERT INTO rt VALUES(1, 2, 3, 4, 5) }
256} -test {
257  faultsim_test_result {1 {UNIQUE constraint failed: rt.ii}} \
258                       {1 {constraint failed}}
259}
260do_faultsim_test rtree3-10.2 -faults oom-* -prep {
261  faultsim_restore_and_reopen
262  execsql { SELECT * FROM rt }
263} -body {
264  execsql { INSERT INTO rt VALUES(2, 2, 3, 5, 4) }
265} -test {
266  faultsim_test_result {1 {rtree constraint failed: rt.(y1<=y2)}} \
267                       {1 {constraint failed}}
268}
269
270finish_test
271