# 2008 Feb 19
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# The focus of this file is testing the r-tree extension.
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
} 
source [file join [file dirname [info script]] rtree_util.tcl]
source $testdir/tester.tcl

ifcapable !rtree {
  finish_test
  return
}

set ::NROW   1000
set ::NDEL     10
set ::NSELECT 100

if {[info exists G(isquick)] && $G(isquick)} {
  set ::NROW 100
  set ::NSELECT 10
}

foreach module {rtree_i32 rtree} {
  if {$module=="rtree_i32"} {set etype INT} {set etype REAL}
  for {set nDim 1} {$nDim <= 5} {incr nDim} {
  
    do_test rtree2-$module.$nDim.1 {
      set cols [list]
      foreach c [list c0 c1 c2 c3 c4 c5 c6 c7 c8 c9] {
        lappend cols "$c $etype"
      }
      set cols [join [lrange $cols 0 [expr {$nDim*2-1}]] ", "]
      execsql " 
        CREATE VIRTUAL TABLE t1 USING ${module}(ii, $cols);
        CREATE TABLE t2 (ii, $cols);
      "
    } {}
  
    do_test rtree2-$module.$nDim.2 {
      db transaction {
        for {set ii 0} {$ii < $::NROW} {incr ii} {
          #puts "Row $ii"
          set values [list]
          for {set jj 0} {$jj<$nDim*2} {incr jj} {
            lappend values [expr int(rand()*1000)]
          }
          set values [join $values ,]
          #puts [rtree_treedump db t1]
          #puts "INSERT INTO t2 VALUES($ii, $values)"
          set rc [catch {db eval "INSERT INTO t1 VALUES($ii, $values)"}]
          if {$rc} {
            incr ii -1
          } else {
            db eval "INSERT INTO t2 VALUES($ii, $values)"
          }
          #if {[rtree_check db t1]} {
            #puts [rtree_treedump db t1]
            #exit
          #}
        }
      }
  
      set t1 [execsql {SELECT * FROM t1 ORDER BY ii}]
      set t2 [execsql {SELECT * FROM t2 ORDER BY ii}]
      set rc [expr {$t1 eq $t2}]
      if {$rc != 1} {
        puts $t1
        puts $t2
      }
      set rc
    } {1}
  
    do_rtree_integrity_test rtree2-$module.$nDim.3 t1
  
    set OPS [list < > <= >= =]
    for {set ii 0} {$ii < $::NSELECT} {incr ii} {
      do_test rtree2-$module.$nDim.4.$ii.1 {
        set where [list]
        foreach look_three_dots! {. . .} {
          set colidx [expr int(rand()*($nDim*2+1))-1]
          if {$colidx<0} {
            set col ii
          } else {
            set col "c$colidx"
          }
          set op  [lindex $OPS [expr int(rand()*[llength $OPS])]]
          set val [expr int(rand()*1000)]
          lappend where "$col $op $val"
        }
        set where [join $where " AND "]
  
        set t1 [execsql "SELECT * FROM t1 WHERE $where ORDER BY ii"]
        set t2 [execsql "SELECT * FROM t2 WHERE $where ORDER BY ii"]
        set rc [expr {$t1 eq $t2}]
        if {$rc != 1} {
          #puts $where
          puts $t1
          puts $t2
          #puts [rtree_treedump db t1]
          #breakpoint
          #set t1 [execsql "SELECT * FROM t1 WHERE $where ORDER BY ii"]
          #exit
        }
        set rc
      } {1}
    }
  
    for {set ii 0} {$ii < $::NROW} {incr ii $::NDEL} {
      #puts [rtree_treedump db t1]
      do_test rtree2-$module.$nDim.5.$ii.1 {
        execsql "DELETE FROM t2 WHERE ii <= $::ii"
        execsql "DELETE FROM t1 WHERE ii <= $::ii"
  
        set t1 [execsql {SELECT * FROM t1 ORDER BY ii}]
        set t2 [execsql {SELECT * FROM t2 ORDER BY ii}]
        set rc [expr {$t1 eq $t2}]
        if {$rc != 1} {
          puts $t1
          puts $t2
        }
        set rc
      } {1}
      do_rtree_integrity_test rtree2-$module.$nDim.5.$ii.2 t1
    }
  
    do_test rtree2-$module.$nDim.6 {
      execsql {
        DROP TABLE t1;
        DROP TABLE t2;
      }
    } {}
  }
}

finish_test
