3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file contains tests for the r-tree module. Specifically, it tests
12 # that custom r-tree queries (geometry callbacks) work.
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
18 source $testdir/tester.tcl
19 ifcapable !rtree { finish_test ; return }
20 ifcapable rtree_int_only { finish_test; return }
24 do_execsql_test rtree9-1.1 {
25 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2, y1, y2, z1, z2);
26 INSERT INTO rt VALUES(1, 1, 2, 1, 2, 1, 2);
28 do_execsql_test rtree9-1.2 {
29 SELECT * FROM rt WHERE id MATCH cube(0, 0, 0, 2, 2, 2);
30 } {1 1.0 2.0 1.0 2.0 1.0 2.0}
31 do_execsql_test rtree9-1.3 {
32 SELECT * FROM rt WHERE id MATCH cube(3, 3, 3, 2, 2, 2);
34 do_execsql_test rtree9-1.4 {
39 for {set i 0} {$i < 1000} {incr i} {
41 set y [expr ($i/10)%10]
42 set z [expr ($i/100)%10]
43 execsql { INSERT INTO rt VALUES($i, $x, $x+1, $y, $y+1, $z, $z+1) }
45 do_execsql_test rtree9-2.1 {
46 SELECT id FROM rt WHERE id MATCH cube(2.5, 2.5, 2.5, 1, 1, 1) ORDER BY id;
47 } {222 223 232 233 322 323 332 333}
48 do_execsql_test rtree9-2.2 {
49 SELECT id FROM rt WHERE id MATCH cube(5.5, 5.5, 5.5, 1, 1, 1) ORDER BY id;
50 } {555 556 565 566 655 656 665 666}
53 do_execsql_test rtree9-3.1 {
54 CREATE VIRTUAL TABLE rt32 USING rtree_i32(id, x1, x2, y1, y2, z1, z2);
56 for {set i 0} {$i < 1000} {incr i} {
58 set y [expr ($i/10)%10]
59 set z [expr ($i/100)%10]
60 execsql { INSERT INTO rt32 VALUES($i, $x, $x+1, $y, $y+1, $z, $z+1) }
62 do_execsql_test rtree9-3.2 {
63 SELECT id FROM rt32 WHERE id MATCH cube(3, 3, 3, 1, 1, 1) ORDER BY id;
64 } {222 223 224 232 233 234 242 243 244 322 323 324 332 333 334 342 343 344 422 423 424 432 433 434 442 443 444}
65 do_execsql_test rtree9-3.3 {
66 SELECT id FROM rt32 WHERE id MATCH cube(5.5, 5.5, 5.5, 1, 1, 1) ORDER BY id;
67 } {555 556 565 566 655 656 665 666}
70 do_catchsql_test rtree9-4.1 {
71 SELECT id FROM rt32 WHERE id MATCH cube(5.5, 5.5, 1, 1, 1) ORDER BY id;
72 } {1 {SQL logic error or missing database}}
73 for {set x 2} {$x<200} {incr x 2} {
74 do_catchsql_test rtree9-4.2.[expr $x/2] {
75 SELECT id FROM rt WHERE id MATCH randomblob($x)
76 } {1 {SQL logic error or missing database}}
78 do_catchsql_test rtree9-4.3 {
79 SELECT id FROM rt WHERE id MATCH CAST(
80 (cube(5.5, 5.5, 5.5, 1, 1, 1) || X'1234567812345678') AS blob
82 } {1 {SQL logic error or missing database}}
85 #-------------------------------------------------------------------------
86 # Test the example 2d "circle" geometry callback.
88 register_circle_geom db
91 do_execsql_test rtree9-5.1 {
92 CREATE VIRTUAL TABLE rt2 USING rtree(id, xmin, xmax, ymin, ymax);
94 INSERT INTO rt2 VALUES(1, 1, 2, 1, 2);
95 INSERT INTO rt2 VALUES(2, 1, 2, -2, -1);
96 INSERT INTO rt2 VALUES(3, -2, -1, -2, -1);
97 INSERT INTO rt2 VALUES(4, -2, -1, 1, 2);
99 INSERT INTO rt2 VALUES(5, 2, 3, 2, 3);
100 INSERT INTO rt2 VALUES(6, 2, 3, -3, -2);
101 INSERT INTO rt2 VALUES(7, -3, -2, -3, -2);
102 INSERT INTO rt2 VALUES(8, -3, -2, 2, 3);
104 INSERT INTO rt2 VALUES(9, 1.8, 3, 1.8, 3);
105 INSERT INTO rt2 VALUES(10, 1.8, 3, -3, -1.8);
106 INSERT INTO rt2 VALUES(11, -3, -1.8, -3, -1.8);
107 INSERT INTO rt2 VALUES(12, -3, -1.8, 1.8, 3);
109 INSERT INTO rt2 VALUES(13, -15, 15, 1.8, 2.2);
110 INSERT INTO rt2 VALUES(14, -15, 15, -2.2, -1.8);
111 INSERT INTO rt2 VALUES(15, 1.8, 2.2, -15, 15);
112 INSERT INTO rt2 VALUES(16, -2.2, -1.8, -15, 15);
114 INSERT INTO rt2 VALUES(17, -100, 100, -100, 100);
117 do_execsql_test rtree9-5.2 {
118 SELECT id FROM rt2 WHERE id MATCH circle(0.0, 0.0, 2.0);
119 } {1 2 3 4 13 14 15 16 17}
121 do_execsql_test rtree9-5.3 {
122 UPDATE rt2 SET xmin=xmin+5, ymin=ymin+5, xmax=xmax+5, ymax=ymax+5;
123 SELECT id FROM rt2 WHERE id MATCH circle(5.0, 5.0, 2.0);
124 } {1 2 3 4 13 14 15 16 17}