/ Hex Artifact Content
Login

Artifact ace05e729a36e342d40cf94e9efc7b4723d9dcdf:


0000: 23 20 32 30 31 30 20 53 65 70 74 65 6d 62 65 72  # 2010 September
0010: 20 32 32 0a 23 0a 23 20 54 68 65 20 61 75 74 68   22.#.# The auth
0020: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0030: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0040: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0050: 70 6c 61 63 65 20 6f 66 0a 23 20 61 20 6c 65 67  place of.# a leg
0060: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
0070: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23  is a blessing:.#
0080: 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  .#    May you do
0090: 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
00a0: 69 6c 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75  il..#    May you
00b0: 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
00c0: 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
00d0: 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
00e0: 73 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20  s..#    May you 
00f0: 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
0100: 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
0110: 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23  than you give..#
0120: 0a 23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .#**************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 23 20 54 68 69 73  *********.# This
0170: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
0180: 65 73 74 73 20 66 6f 72 20 74 68 65 20 72 2d 74  ests for the r-t
0190: 72 65 65 20 6d 6f 64 75 6c 65 2e 20 53 70 65 63  ree module. Spec
01a0: 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 74 65 73  ifically, it tes
01b0: 74 73 0a 23 20 74 68 61 74 20 63 6f 72 72 75 70  ts.# that corrup
01c0: 74 20 6f 72 20 69 6e 63 6f 6e 73 69 73 74 65 6e  t or inconsisten
01d0: 74 20 64 61 74 61 62 61 73 65 73 20 64 6f 20 6e  t databases do n
01e0: 6f 74 20 63 61 75 73 65 20 63 72 61 73 68 65 73  ot cause crashes
01f0: 20 69 6e 20 74 68 65 20 72 2d 74 72 65 65 0a 23   in the r-tree.#
0200: 20 6d 6f 64 75 6c 65 2e 0a 23 20 0a 0a 69 66 20   module..# ..if 
0210: 7b 21 5b 69 6e 66 6f 20 65 78 69 73 74 73 20 74  {![info exists t
0220: 65 73 74 64 69 72 5d 7d 20 7b 0a 20 20 73 65 74  estdir]} {.  set
0230: 20 74 65 73 74 64 69 72 20 5b 66 69 6c 65 20 6a   testdir [file j
0240: 6f 69 6e 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d  oin [file dirnam
0250: 65 20 5b 69 6e 66 6f 20 73 63 72 69 70 74 5d 5d  e [info script]]
0260: 20 2e 2e 20 2e 2e 20 74 65 73 74 5d 0a 7d 20 0a   .. .. test].} .
0270: 73 6f 75 72 63 65 20 24 74 65 73 74 64 69 72 2f  source $testdir/
0280: 74 65 73 74 65 72 2e 74 63 6c 0a 69 66 63 61 70  tester.tcl.ifcap
0290: 61 62 6c 65 20 21 72 74 72 65 65 20 7b 20 66 69  able !rtree { fi
02a0: 6e 69 73 68 5f 74 65 73 74 20 3b 20 72 65 74 75  nish_test ; retu
02b0: 72 6e 20 7d 0a 0a 70 72 6f 63 20 63 72 65 61 74  rn }..proc creat
02c0: 65 5f 74 31 20 7b 7d 20 7b 0a 20 20 64 62 20 63  e_t1 {} {.  db c
02d0: 6c 6f 73 65 0a 20 20 66 6f 72 63 65 64 65 6c 65  lose.  forcedele
02e0: 74 65 20 74 65 73 74 2e 64 62 0a 20 20 73 71 6c  te test.db.  sql
02f0: 69 74 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a  ite3 db test.db.
0300: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
0310: 50 52 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65  PRAGMA page_size
0320: 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 43 52 45   = 1024;.    CRE
0330: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
0340: 45 20 74 31 20 55 53 49 4e 47 20 72 74 72 65 65  E t1 USING rtree
0350: 28 69 64 2c 20 78 31 2c 20 78 32 2c 20 79 31 2c  (id, x1, x2, y1,
0360: 20 79 32 29 3b 0a 20 20 7d 0a 7d 0a 70 72 6f 63   y2);.  }.}.proc
0370: 20 70 6f 70 75 6c 61 74 65 5f 74 31 20 7b 7d 20   populate_t1 {} 
0380: 7b 0a 20 20 65 78 65 63 73 71 6c 20 42 45 47 49  {.  execsql BEGI
0390: 4e 0a 20 20 66 6f 72 20 7b 73 65 74 20 69 20 30  N.  for {set i 0
03a0: 7d 20 7b 24 69 20 3c 20 35 30 30 7d 20 7b 69 6e  } {$i < 500} {in
03b0: 63 72 20 69 7d 20 7b 0a 20 20 20 20 73 65 74 20  cr i} {.    set 
03c0: 78 32 20 5b 65 78 70 72 20 24 69 2b 35 5d 0a 20  x2 [expr $i+5]. 
03d0: 20 20 20 73 65 74 20 79 32 20 5b 65 78 70 72 20     set y2 [expr 
03e0: 24 69 2b 35 5d 0a 20 20 20 20 65 78 65 63 73 71  $i+5].    execsq
03f0: 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  l { INSERT INTO 
0400: 74 31 20 56 41 4c 55 45 53 28 24 69 2c 20 24 69  t1 VALUES($i, $i
0410: 2c 20 24 78 32 2c 20 24 69 2c 20 24 79 32 29 20  , $x2, $i, $y2) 
0420: 7d 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  }.  }.  execsql 
0430: 43 4f 4d 4d 49 54 0a 7d 0a 0a 70 72 6f 63 20 74  COMMIT.}..proc t
0440: 72 75 6e 63 61 74 65 5f 6e 6f 64 65 20 7b 6e 6f  runcate_node {no
0450: 64 65 6e 6f 20 6e 54 72 75 6e 63 7d 20 7b 0a 20  deno nTrunc} {. 
0460: 20 73 65 74 20 62 6c 6f 62 20 5b 64 62 20 6f 6e   set blob [db on
0470: 65 20 7b 53 45 4c 45 43 54 20 64 61 74 61 20 46  e {SELECT data F
0480: 52 4f 4d 20 74 31 5f 6e 6f 64 65 20 57 48 45 52  ROM t1_node WHER
0490: 45 20 6e 6f 64 65 6e 6f 3d 24 6e 6f 64 65 6e 6f  E nodeno=$nodeno
04a0: 7d 5d 0a 20 20 69 66 20 7b 24 6e 54 72 75 6e 63  }].  if {$nTrunc
04b0: 3c 30 7d 20 7b 73 65 74 20 6e 54 72 75 6e 63 20  <0} {set nTrunc 
04c0: 22 65 6e 64 2d 24 6e 54 72 75 6e 63 22 7d 0a 20  "end-$nTrunc"}. 
04d0: 20 73 65 74 20 62 6c 6f 62 20 5b 73 74 72 69 6e   set blob [strin
04e0: 67 20 72 61 6e 67 65 20 24 62 6c 6f 62 20 30 20  g range $blob 0 
04f0: 24 6e 54 72 75 6e 63 5d 0a 20 20 64 62 20 65 76  $nTrunc].  db ev
0500: 61 6c 20 7b 20 55 50 44 41 54 45 20 74 31 5f 6e  al { UPDATE t1_n
0510: 6f 64 65 20 53 45 54 20 64 61 74 61 20 3d 20 24  ode SET data = $
0520: 62 6c 6f 62 20 57 48 45 52 45 20 6e 6f 64 65 6e  blob WHERE noden
0530: 6f 3d 24 6e 6f 64 65 6e 6f 20 7d 0a 7d 0a 0a 70  o=$nodeno }.}..p
0540: 72 6f 63 20 73 65 74 5f 74 72 65 65 5f 64 65 70  roc set_tree_dep
0550: 74 68 20 7b 74 62 6c 20 7b 6e 65 77 76 61 6c 75  th {tbl {newvalu
0560: 65 20 22 22 7d 7d 20 7b 0a 20 20 73 65 74 20 62  e ""}} {.  set b
0570: 6c 6f 62 20 5b 64 62 20 6f 6e 65 20 22 53 45 4c  lob [db one "SEL
0580: 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20 24 7b  ECT data FROM ${
0590: 74 62 6c 7d 5f 6e 6f 64 65 20 57 48 45 52 45 20  tbl}_node WHERE 
05a0: 6e 6f 64 65 6e 6f 3d 31 22 5d 0a 0a 20 20 69 66  nodeno=1"]..  if
05b0: 20 7b 24 6e 65 77 76 61 6c 75 65 20 3d 3d 20 22   {$newvalue == "
05c0: 22 7d 20 7b 0a 20 20 20 20 62 69 6e 61 72 79 20  "} {.    binary 
05d0: 73 63 61 6e 20 24 62 6c 6f 62 20 53 75 20 6f 6c  scan $blob Su ol
05e0: 64 76 61 6c 75 65 0a 20 20 20 20 72 65 74 75 72  dvalue.    retur
05f0: 6e 20 24 6f 6c 64 76 61 6c 75 65 0a 20 20 7d 0a  n $oldvalue.  }.
0600: 0a 20 20 73 65 74 20 62 6c 6f 62 20 5b 62 69 6e  .  set blob [bin
0610: 61 72 79 20 66 6f 72 6d 61 74 20 53 75 61 2a 20  ary format Sua* 
0620: 24 6e 65 77 76 61 6c 75 65 20 5b 73 74 72 69 6e  $newvalue [strin
0630: 67 20 72 61 6e 67 65 20 24 62 6c 6f 62 20 32 20  g range $blob 2 
0640: 65 6e 64 5d 5d 0a 20 20 64 62 20 65 76 61 6c 20  end]].  db eval 
0650: 22 55 50 44 41 54 45 20 24 7b 74 62 6c 7d 5f 6e  "UPDATE ${tbl}_n
0660: 6f 64 65 20 53 45 54 20 64 61 74 61 20 3d 20 5c  ode SET data = \
0670: 24 62 6c 6f 62 20 57 48 45 52 45 20 6e 6f 64 65  $blob WHERE node
0680: 6e 6f 3d 31 22 0a 20 20 72 65 74 75 72 6e 20 5b  no=1".  return [
0690: 73 65 74 5f 74 72 65 65 5f 64 65 70 74 68 20 24  set_tree_depth $
06a0: 74 62 6c 5d 0a 7d 0a 0a 70 72 6f 63 20 73 65 74  tbl].}..proc set
06b0: 5f 65 6e 74 72 79 5f 63 6f 75 6e 74 20 7b 74 62  _entry_count {tb
06c0: 6c 20 6e 6f 64 65 6e 6f 20 7b 6e 65 77 76 61 6c  l nodeno {newval
06d0: 75 65 20 22 22 7d 7d 20 7b 0a 20 20 73 65 74 20  ue ""}} {.  set 
06e0: 62 6c 6f 62 20 5b 64 62 20 6f 6e 65 20 22 53 45  blob [db one "SE
06f0: 4c 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20 24  LECT data FROM $
0700: 7b 74 62 6c 7d 5f 6e 6f 64 65 20 57 48 45 52 45  {tbl}_node WHERE
0710: 20 6e 6f 64 65 6e 6f 3d 24 6e 6f 64 65 6e 6f 22   nodeno=$nodeno"
0720: 5d 0a 0a 20 20 69 66 20 7b 24 6e 65 77 76 61 6c  ]..  if {$newval
0730: 75 65 20 3d 3d 20 22 22 7d 20 7b 0a 20 20 20 20  ue == ""} {.    
0740: 62 69 6e 61 72 79 20 73 63 61 6e 20 5b 73 74 72  binary scan [str
0750: 69 6e 67 20 72 61 6e 67 65 20 24 62 6c 6f 62 20  ing range $blob 
0760: 32 20 65 6e 64 5d 20 53 75 20 6f 6c 64 76 61 6c  2 end] Su oldval
0770: 75 65 0a 20 20 20 20 72 65 74 75 72 6e 20 24 6f  ue.    return $o
0780: 6c 64 76 61 6c 75 65 0a 20 20 7d 0a 0a 20 20 73  ldvalue.  }..  s
0790: 65 74 20 62 6c 6f 62 20 5b 62 69 6e 61 72 79 20  et blob [binary 
07a0: 66 6f 72 6d 61 74 20 61 2a 53 75 61 2a 20 5c 0a  format a*Sua* \.
07b0: 20 20 20 20 5b 73 74 72 69 6e 67 20 72 61 6e 67      [string rang
07c0: 65 20 24 62 6c 6f 62 20 30 20 31 5d 20 24 6e 65  e $blob 0 1] $ne
07d0: 77 76 61 6c 75 65 20 5b 73 74 72 69 6e 67 20 72  wvalue [string r
07e0: 61 6e 67 65 20 24 62 6c 6f 62 20 34 20 65 6e 64  ange $blob 4 end
07f0: 5d 0a 20 20 5d 0a 20 20 64 62 20 65 76 61 6c 20  ].  ].  db eval 
0800: 22 55 50 44 41 54 45 20 24 7b 74 62 6c 7d 5f 6e  "UPDATE ${tbl}_n
0810: 6f 64 65 20 53 45 54 20 64 61 74 61 20 3d 20 5c  ode SET data = \
0820: 24 62 6c 6f 62 20 57 48 45 52 45 20 6e 6f 64 65  $blob WHERE node
0830: 6e 6f 3d 24 6e 6f 64 65 6e 6f 22 0a 20 20 72 65  no=$nodeno".  re
0840: 74 75 72 6e 20 5b 73 65 74 5f 65 6e 74 72 79 5f  turn [set_entry_
0850: 63 6f 75 6e 74 20 24 74 62 6c 20 24 6e 6f 64 65  count $tbl $node
0860: 6e 6f 5d 0a 7d 0a 0a 0a 70 72 6f 63 20 64 6f 5f  no].}...proc do_
0870: 63 6f 72 72 75 70 74 69 6f 6e 5f 74 65 73 74 73  corruption_tests
0880: 20 7b 70 72 65 66 69 78 20 61 72 67 73 7d 20 7b   {prefix args} {
0890: 0a 20 20 73 65 74 20 74 65 73 74 61 72 72 61 79  .  set testarray
08a0: 20 5b 6c 69 6e 64 65 78 20 24 61 72 67 73 20 65   [lindex $args e
08b0: 6e 64 5d 0a 20 20 73 65 74 20 65 72 72 6f 72 6d  nd].  set errorm
08c0: 73 67 20 7b 64 61 74 61 62 61 73 65 20 64 69 73  sg {database dis
08d0: 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f  k image is malfo
08e0: 72 6d 65 64 7d 0a 0a 20 20 66 6f 72 65 61 63 68  rmed}..  foreach
08f0: 20 7b 7a 20 76 61 6c 75 65 7d 20 5b 6c 72 61 6e   {z value} [lran
0900: 67 65 20 24 61 72 67 73 20 30 20 65 6e 64 2d 31  ge $args 0 end-1
0910: 5d 20 7b 0a 20 20 20 20 73 65 74 20 6e 20 5b 73  ] {.    set n [s
0920: 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 24 7a 5d  tring length $z]
0930: 0a 20 20 20 20 69 66 20 7b 24 6e 3e 3d 32 20 26  .    if {$n>=2 &
0940: 26 20 5b 73 74 72 69 6e 67 20 65 71 75 61 6c 20  & [string equal 
0950: 2d 6c 65 6e 67 74 68 20 24 6e 20 24 7a 20 22 2d  -length $n $z "-
0960: 65 72 72 6f 72 22 5d 7d 20 7b 0a 20 20 20 20 20  error"]} {.     
0970: 20 73 65 74 20 65 72 72 6f 72 6d 73 67 20 24 76   set errormsg $v
0980: 61 6c 75 65 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  alue.    }.  }..
0990: 20 20 66 6f 72 65 61 63 68 20 7b 74 6e 20 73 71    foreach {tn sq
09a0: 6c 7d 20 24 74 65 73 74 61 72 72 61 79 20 7b 0a  l} $testarray {.
09b0: 20 20 20 20 64 6f 5f 63 61 74 63 68 73 71 6c 5f      do_catchsql_
09c0: 74 65 73 74 20 24 70 72 65 66 69 78 2e 24 74 6e  test $prefix.$tn
09d0: 20 24 73 71 6c 20 5b 6c 69 73 74 20 31 20 24 65   $sql [list 1 $e
09e0: 72 72 6f 72 6d 73 67 5d 0a 20 20 7d 0a 7d 0a 0a  rrormsg].  }.}..
09f0: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
0a00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73  ----------.# Tes
0a40: 74 20 74 68 65 20 6c 69 62 72 61 72 69 65 73 20  t the libraries 
0a50: 72 65 73 70 6f 6e 73 65 20 69 66 20 74 68 65 20  response if the 
0a60: 25 5f 6e 6f 64 65 20 74 61 62 6c 65 20 69 73 20  %_node table is 
0a70: 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79  completely empty
0a80: 0a 23 20 28 69 2e 65 2e 20 74 68 65 20 72 6f 6f  .# (i.e. the roo
0a90: 74 20 6e 6f 64 65 20 69 73 20 6d 69 73 73 69 6e  t node is missin
0aa0: 67 29 2c 20 6f 72 20 68 61 73 20 62 65 65 6e 20  g), or has been 
0ab0: 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
0ac0: 20 64 61 74 61 62 61 73 65 0a 23 20 65 6e 74 69   database.# enti
0ad0: 72 65 6c 79 2e 0a 23 0a 63 72 65 61 74 65 5f 74  rely..#.create_t
0ae0: 31 0a 70 6f 70 75 6c 61 74 65 5f 74 31 0a 64 6f  1.populate_t1.do
0af0: 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 72 74  _execsql_test rt
0b00: 72 65 65 41 2d 31 2e 30 20 7b 0a 20 20 44 45 4c  reeA-1.0 {.  DEL
0b10: 45 54 45 20 46 52 4f 4d 20 74 31 5f 6e 6f 64 65  ETE FROM t1_node
0b20: 3b 0a 7d 20 7b 7d 0a 0a 64 6f 5f 63 6f 72 72 75  ;.} {}..do_corru
0b30: 70 74 69 6f 6e 5f 74 65 73 74 73 20 72 74 72 65  ption_tests rtre
0b40: 65 41 2d 31 2e 31 20 7b 0a 20 20 31 20 20 20 22  eA-1.1 {.  1   "
0b50: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
0b60: 22 0a 20 20 32 20 20 20 22 53 45 4c 45 43 54 20  ".  2   "SELECT 
0b70: 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  * FROM t1 WHERE 
0b80: 72 6f 77 69 64 3d 35 22 0a 20 20 33 20 20 20 22  rowid=5".  3   "
0b90: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
0ba0: 41 4c 55 45 53 28 31 30 30 30 2c 20 31 2c 20 32  ALUES(1000, 1, 2
0bb0: 2c 20 33 2c 20 34 29 22 0a 20 20 34 20 20 20 22  , 3, 4)".  4   "
0bc0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
0bd0: 20 57 48 45 52 45 20 78 31 3c 31 30 20 41 4e 44   WHERE x1<10 AND
0be0: 20 78 32 3e 31 32 22 0a 7d 0a 0a 64 6f 5f 65 78   x2>12".}..do_ex
0bf0: 65 63 73 71 6c 5f 74 65 73 74 20 20 72 74 72 65  ecsql_test  rtre
0c00: 65 41 2d 31 2e 32 2e 30 20 7b 20 44 52 4f 50 20  eA-1.2.0 { DROP 
0c10: 54 41 42 4c 45 20 74 31 5f 6e 6f 64 65 20 7d 20  TABLE t1_node } 
0c20: 7b 7d 0a 64 6f 5f 63 6f 72 72 75 70 74 69 6f 6e  {}.do_corruption
0c30: 5f 74 65 73 74 73 20 72 74 72 65 65 41 2d 31 2e  _tests rtreeA-1.
0c40: 32 20 2d 65 72 72 6f 72 20 22 53 51 4c 20 6c 6f  2 -error "SQL lo
0c50: 67 69 63 20 65 72 72 6f 72 20 6f 72 20 6d 69 73  gic error or mis
0c60: 73 69 6e 67 20 64 61 74 61 62 61 73 65 22 20 7b  sing database" {
0c70: 0a 20 20 31 20 20 20 22 53 45 4c 45 43 54 20 2a  .  1   "SELECT *
0c80: 20 46 52 4f 4d 20 74 31 22 0a 20 20 32 20 20 20   FROM t1".  2   
0c90: 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  "SELECT * FROM t
0ca0: 31 20 57 48 45 52 45 20 72 6f 77 69 64 3d 35 22  1 WHERE rowid=5"
0cb0: 0a 20 20 33 20 20 20 22 49 4e 53 45 52 54 20 49  .  3   "INSERT I
0cc0: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31 30  NTO t1 VALUES(10
0cd0: 30 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 29 22  00, 1, 2, 3, 4)"
0ce0: 0a 20 20 34 20 20 20 22 53 45 4c 45 43 54 20 2a  .  4   "SELECT *
0cf0: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 78   FROM t1 WHERE x
0d00: 31 3c 31 30 20 41 4e 44 20 78 32 3e 31 32 22 0a  1<10 AND x2>12".
0d10: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
0d20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
0d60: 54 65 73 74 20 74 68 65 20 6c 69 62 72 61 72 69  Test the librari
0d70: 65 73 20 72 65 73 70 6f 6e 73 65 20 69 66 20 73  es response if s
0d80: 6f 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72 69  ome of the entri
0d90: 65 73 20 69 6e 20 74 68 65 20 25 5f 6e 6f 64 65  es in the %_node
0da0: 20 74 61 62 6c 65 20 0a 23 20 61 72 65 20 74 68   table .# are th
0db0: 65 20 77 72 6f 6e 67 20 73 69 7a 65 2e 0a 23 0a  e wrong size..#.
0dc0: 63 72 65 61 74 65 5f 74 31 0a 70 6f 70 75 6c 61  create_t1.popula
0dd0: 74 65 5f 74 31 0a 64 6f 5f 74 65 73 74 20 72 74  te_t1.do_test rt
0de0: 72 65 65 41 2d 32 2e 31 2e 30 20 7b 0a 20 20 73  reeA-2.1.0 {.  s
0df0: 65 74 20 6e 6f 64 65 73 20 5b 64 62 20 65 76 61  et nodes [db eva
0e00: 6c 20 7b 73 65 6c 65 63 74 20 6e 6f 64 65 6e 6f  l {select nodeno
0e10: 20 46 52 4f 4d 20 74 31 5f 6e 6f 64 65 7d 5d 0a   FROM t1_node}].
0e20: 20 20 66 6f 72 65 61 63 68 20 7b 61 20 62 20 63    foreach {a b c
0e30: 7d 20 24 6e 6f 64 65 73 20 7b 20 74 72 75 6e 63  } $nodes { trunc
0e40: 61 74 65 5f 6e 6f 64 65 20 24 63 20 32 30 30 20  ate_node $c 200 
0e50: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 63 6f 72 72 75 70  }.} {}.do_corrup
0e60: 74 69 6f 6e 5f 74 65 73 74 73 20 72 74 72 65 65  tion_tests rtree
0e70: 41 2d 32 2e 31 20 7b 0a 20 20 31 20 20 20 22 53  A-2.1 {.  1   "S
0e80: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 22  ELECT * FROM t1"
0e90: 0a 20 20 32 20 20 20 22 53 45 4c 45 43 54 20 2a  .  2   "SELECT *
0ea0: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 72   FROM t1 WHERE r
0eb0: 6f 77 69 64 3d 35 22 0a 20 20 33 20 20 20 22 49  owid=5".  3   "I
0ec0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
0ed0: 4c 55 45 53 28 31 30 30 30 2c 20 31 2c 20 32 2c  LUES(1000, 1, 2,
0ee0: 20 33 2c 20 34 29 22 0a 20 20 34 20 20 20 22 53   3, 4)".  4   "S
0ef0: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20  ELECT * FROM t1 
0f00: 57 48 45 52 45 20 78 31 3c 31 30 20 41 4e 44 20  WHERE x1<10 AND 
0f10: 78 32 3e 31 32 22 0a 7d 0a 0a 63 72 65 61 74 65  x2>12".}..create
0f20: 5f 74 31 0a 70 6f 70 75 6c 61 74 65 5f 74 31 0a  _t1.populate_t1.
0f30: 64 6f 5f 74 65 73 74 20 72 74 72 65 65 41 2d 32  do_test rtreeA-2
0f40: 2e 32 2e 30 20 7b 20 74 72 75 6e 63 61 74 65 5f  .2.0 { truncate_
0f50: 6e 6f 64 65 20 31 20 32 30 30 20 7d 20 7b 7d 0a  node 1 200 } {}.
0f60: 64 6f 5f 63 6f 72 72 75 70 74 69 6f 6e 5f 74 65  do_corruption_te
0f70: 73 74 73 20 72 74 72 65 65 41 2d 32 2e 32 20 7b  sts rtreeA-2.2 {
0f80: 0a 20 20 31 20 20 20 22 53 45 4c 45 43 54 20 2a  .  1   "SELECT *
0f90: 20 46 52 4f 4d 20 74 31 22 0a 20 20 32 20 20 20   FROM t1".  2   
0fa0: 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  "SELECT * FROM t
0fb0: 31 20 57 48 45 52 45 20 72 6f 77 69 64 3d 35 22  1 WHERE rowid=5"
0fc0: 0a 20 20 33 20 20 20 22 49 4e 53 45 52 54 20 49  .  3   "INSERT I
0fd0: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31 30  NTO t1 VALUES(10
0fe0: 30 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 29 22  00, 1, 2, 3, 4)"
0ff0: 0a 20 20 34 20 20 20 22 53 45 4c 45 43 54 20 2a  .  4   "SELECT *
1000: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 78   FROM t1 WHERE x
1010: 31 3c 31 30 20 41 4e 44 20 78 32 3e 31 32 22 0a  1<10 AND x2>12".
1020: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
1030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1040: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1060: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1070: 53 65 74 20 74 68 65 20 22 64 65 70 74 68 22 20  Set the "depth" 
1080: 6f 66 20 74 68 65 20 74 72 65 65 20 73 74 6f 72  of the tree stor
1090: 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 6e  ed on the root n
10a0: 6f 64 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e  ode incorrectly.
10b0: 20 54 65 73 74 0a 23 20 74 68 61 74 20 74 68 69   Test.# that thi
10c0: 73 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65  s does not cause
10d0: 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 23   any problems..#
10e0: 0a 63 72 65 61 74 65 5f 74 31 0a 70 6f 70 75 6c  .create_t1.popul
10f0: 61 74 65 5f 74 31 0a 64 6f 5f 74 65 73 74 20 72  ate_t1.do_test r
1100: 74 72 65 65 41 2d 33 2e 31 2e 30 2e 31 20 7b 20  treeA-3.1.0.1 { 
1110: 73 65 74 5f 74 72 65 65 5f 64 65 70 74 68 20 74  set_tree_depth t
1120: 31 20 7d 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20  1 } {1}.do_test 
1130: 72 74 72 65 65 41 2d 33 2e 31 2e 30 2e 32 20 7b  rtreeA-3.1.0.2 {
1140: 20 73 65 74 5f 74 72 65 65 5f 64 65 70 74 68 20   set_tree_depth 
1150: 74 31 20 33 20 7d 20 7b 33 7d 0a 64 6f 5f 63 6f  t1 3 } {3}.do_co
1160: 72 72 75 70 74 69 6f 6e 5f 74 65 73 74 73 20 72  rruption_tests r
1170: 74 72 65 65 41 2d 33 2e 31 20 7b 0a 20 20 31 20  treeA-3.1 {.  1 
1180: 20 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d    "SELECT * FROM
1190: 20 74 31 22 0a 20 20 32 20 20 20 22 53 45 4c 45   t1".  2   "SELE
11a0: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
11b0: 52 45 20 72 6f 77 69 64 3d 35 22 0a 20 20 33 20  RE rowid=5".  3 
11c0: 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74    "INSERT INTO t
11d0: 31 20 56 41 4c 55 45 53 28 31 30 30 30 2c 20 31  1 VALUES(1000, 1
11e0: 2c 20 32 2c 20 33 2c 20 34 29 22 0a 7d 0a 0a 64  , 2, 3, 4)".}..d
11f0: 6f 5f 74 65 73 74 20 72 74 72 65 65 41 2d 33 2e  o_test rtreeA-3.
1200: 32 2e 30 20 7b 20 73 65 74 5f 74 72 65 65 5f 64  2.0 { set_tree_d
1210: 65 70 74 68 20 74 31 20 31 30 30 30 20 7d 20 7b  epth t1 1000 } {
1220: 31 30 30 30 7d 0a 64 6f 5f 63 6f 72 72 75 70 74  1000}.do_corrupt
1230: 69 6f 6e 5f 74 65 73 74 73 20 72 74 72 65 65 41  ion_tests rtreeA
1240: 2d 33 2e 32 20 7b 0a 20 20 31 20 20 20 22 53 45  -3.2 {.  1   "SE
1250: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 22 0a  LECT * FROM t1".
1260: 20 20 32 20 20 20 22 53 45 4c 45 43 54 20 2a 20    2   "SELECT * 
1270: 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 72 6f  FROM t1 WHERE ro
1280: 77 69 64 3d 35 22 0a 20 20 33 20 20 20 22 49 4e  wid=5".  3   "IN
1290: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
12a0: 55 45 53 28 31 30 30 30 2c 20 31 2c 20 32 2c 20  UES(1000, 1, 2, 
12b0: 33 2c 20 34 29 22 0a 7d 0a 0a 63 72 65 61 74 65  3, 4)".}..create
12c0: 5f 74 31 0a 70 6f 70 75 6c 61 74 65 5f 74 31 0a  _t1.populate_t1.
12d0: 64 6f 5f 74 65 73 74 20 72 74 72 65 65 41 2d 33  do_test rtreeA-3
12e0: 2e 33 2e 30 20 7b 20 0a 20 20 65 78 65 63 73 71  .3.0 { .  execsq
12f0: 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  l { DELETE FROM 
1300: 74 31 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  t1 WHERE rowid =
1310: 20 30 20 7d 0a 20 20 73 65 74 5f 74 72 65 65 5f   0 }.  set_tree_
1320: 64 65 70 74 68 20 74 31 20 36 35 35 33 35 0a 7d  depth t1 65535.}
1330: 20 7b 36 35 35 33 35 7d 0a 64 6f 5f 63 6f 72 72   {65535}.do_corr
1340: 75 70 74 69 6f 6e 5f 74 65 73 74 73 20 72 74 72  uption_tests rtr
1350: 65 65 41 2d 33 2e 33 20 7b 0a 20 20 31 20 20 20  eeA-3.3 {.  1   
1360: 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  "SELECT * FROM t
1370: 31 22 0a 20 20 32 20 20 20 22 53 45 4c 45 43 54  1".  2   "SELECT
1380: 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   * FROM t1 WHERE
1390: 20 72 6f 77 69 64 3d 35 22 0a 20 20 33 20 20 20   rowid=5".  3   
13a0: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20  "INSERT INTO t1 
13b0: 56 41 4c 55 45 53 28 31 30 30 30 2c 20 31 2c 20  VALUES(1000, 1, 
13c0: 32 2c 20 33 2c 20 34 29 22 0a 7d 0a 0a 23 2d 2d  2, 3, 4)".}..#--
13d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1410: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 53 65 74 20 74 68  -------.# Set th
1420: 65 20 22 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  e "number of ent
1430: 72 69 65 73 22 20 66 69 65 6c 64 20 6f 6e 20 73  ries" field on s
1440: 6f 6d 65 20 6e 6f 64 65 73 20 69 6e 63 6f 72 72  ome nodes incorr
1450: 65 63 74 6c 79 2e 0a 23 0a 63 72 65 61 74 65 5f  ectly..#.create_
1460: 74 31 0a 70 6f 70 75 6c 61 74 65 5f 74 31 0a 64  t1.populate_t1.d
1470: 6f 5f 74 65 73 74 20 72 74 72 65 65 41 2d 34 2e  o_test rtreeA-4.
1480: 31 2e 30 20 7b 20 0a 20 20 73 65 74 5f 65 6e 74  1.0 { .  set_ent
1490: 72 79 5f 63 6f 75 6e 74 20 74 31 20 31 20 34 30  ry_count t1 1 40
14a0: 30 30 0a 7d 20 7b 34 30 30 30 7d 0a 64 6f 5f 63  00.} {4000}.do_c
14b0: 6f 72 72 75 70 74 69 6f 6e 5f 74 65 73 74 73 20  orruption_tests 
14c0: 72 74 72 65 65 41 2d 34 2e 31 20 7b 0a 20 20 31  rtreeA-4.1 {.  1
14d0: 20 20 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f     "SELECT * FRO
14e0: 4d 20 74 31 22 0a 20 20 32 20 20 20 22 53 45 4c  M t1".  2   "SEL
14f0: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48  ECT * FROM t1 WH
1500: 45 52 45 20 72 6f 77 69 64 3d 35 22 0a 20 20 33  ERE rowid=5".  3
1510: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
1520: 74 31 20 56 41 4c 55 45 53 28 31 30 30 30 2c 20  t1 VALUES(1000, 
1530: 31 2c 20 32 2c 20 33 2c 20 34 29 22 0a 20 20 34  1, 2, 3, 4)".  4
1540: 20 20 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f     "SELECT * FRO
1550: 4d 20 74 31 20 57 48 45 52 45 20 78 31 3c 31 30  M t1 WHERE x1<10
1560: 20 41 4e 44 20 78 32 3e 31 32 22 0a 7d 0a 0a 23   AND x2>12".}..#
1570: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1580: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1590: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 52 65 6d 6f  ---------.# Remo
15c0: 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ve entries from 
15d0: 74 68 65 20 25 5f 70 61 72 65 6e 74 20 74 61 62  the %_parent tab
15e0: 6c 65 20 61 6e 64 20 63 68 65 63 6b 20 74 68 61  le and check tha
15f0: 74 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a  t this does not.
1600: 23 20 63 61 75 73 65 20 61 20 63 72 61 73 68 2e  # cause a crash.
1610: 0a 23 0a 63 72 65 61 74 65 5f 74 31 0a 70 6f 70  .#.create_t1.pop
1620: 75 6c 61 74 65 5f 74 31 0a 64 6f 5f 65 78 65 63  ulate_t1.do_exec
1630: 73 71 6c 5f 74 65 73 74 20 72 74 72 65 65 41 2d  sql_test rtreeA-
1640: 35 2e 31 2e 30 20 7b 20 44 45 4c 45 54 45 20 46  5.1.0 { DELETE F
1650: 52 4f 4d 20 74 31 5f 70 61 72 65 6e 74 20 7d 20  ROM t1_parent } 
1660: 7b 7d 0a 64 6f 5f 63 6f 72 72 75 70 74 69 6f 6e  {}.do_corruption
1670: 5f 74 65 73 74 73 20 72 74 72 65 65 41 2d 35 2e  _tests rtreeA-5.
1680: 31 20 7b 0a 20 20 31 20 20 20 22 44 45 4c 45 54  1 {.  1   "DELET
1690: 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  E FROM t1 WHERE 
16a0: 72 6f 77 69 64 20 3d 20 35 22 0a 20 20 32 20 20  rowid = 5".  2  
16b0: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31   "DELETE FROM t1
16c0: 22 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ".}..#----------
16d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1710: 23 20 41 64 64 20 73 6f 6d 65 20 62 61 64 20 65  # Add some bad e
1720: 6e 74 72 69 65 73 20 74 6f 20 74 68 65 20 25 5f  ntries to the %_
1730: 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 0a 23 0a  parent table..#.
1740: 63 72 65 61 74 65 5f 74 31 0a 70 6f 70 75 6c 61  create_t1.popula
1750: 74 65 5f 74 31 0a 64 6f 5f 65 78 65 63 73 71 6c  te_t1.do_execsql
1760: 5f 74 65 73 74 20 72 74 72 65 65 41 2d 36 2e 31  _test rtreeA-6.1
1770: 2e 30 20 7b 20 0a 20 20 55 50 44 41 54 45 20 74  .0 { .  UPDATE t
1780: 31 5f 70 61 72 65 6e 74 20 73 65 74 20 70 61 72  1_parent set par
1790: 65 6e 74 6e 6f 64 65 20 3d 20 70 61 72 65 6e 74  entnode = parent
17a0: 6e 6f 64 65 2b 31 0a 7d 20 7b 7d 0a 64 6f 5f 63  node+1.} {}.do_c
17b0: 6f 72 72 75 70 74 69 6f 6e 5f 74 65 73 74 73 20  orruption_tests 
17c0: 72 74 72 65 65 41 2d 36 2e 31 20 7b 0a 20 20 31  rtreeA-6.1 {.  1
17d0: 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
17e0: 74 31 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  t1 WHERE rowid =
17f0: 20 35 22 0a 20 20 32 20 20 20 22 55 50 44 41 54   5".  2   "UPDAT
1800: 45 20 74 31 20 53 45 54 20 78 31 3d 78 31 2b 31  E t1 SET x1=x1+1
1810: 2c 20 78 32 3d 78 32 2b 31 22 0a 7d 0a 0a 0a 66  , x2=x2+1".}...f
1820: 69 6e 69 73 68 5f 74 65 73 74 0a                 inish_test.