]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Changes to fts3auto.test to test OR, AND and NOT operations.
authordan <dan@noemail.net>
Mon, 13 Jun 2011 13:48:36 +0000 (13:48 +0000)
committerdan <dan@noemail.net>
Mon, 13 Jun 2011 13:48:36 +0000 (13:48 +0000)
FossilOrigin-Name: e4ab6cdb101bbeb804820425cf569ee7dc2397fb

manifest
manifest.uuid
test/fts3auto.test

index c96551fae92c8f4fc951cc8d65f25f135a492708..8bdca7e9659f095076193d702dabb82bb8e5e279 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sbug\sexposed\sby\scombining\smatchinfo(),\sNEAR\sand\s"ORDER\sBY\srowid\sDESC".
-D 2011-06-13T09:11:01.953
+C Changes\sto\sfts3auto.test\sto\stest\sOR,\sAND\sand\sNOT\soperations.
+D 2011-06-13T13:48:36.083
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in c1d7a7f4fd8da6b1815032efca950e3d5125407e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -455,7 +455,7 @@ F test/fts3am.test 218aa6ba0dfc50c7c16b2022aac5c6be593d08d8
 F test/fts3an.test a49ccadc07a2f7d646ec1b81bc09da2d85a85b18
 F test/fts3ao.test b83f99f70e9eec85f27d75801a974b3f820e01f9
 F test/fts3atoken.test 402ef2f7c2fb4b3d4fa0587df6441c1447e799b3
-F test/fts3auto.test 696a2d32dd64a03aa47818c26ea64f8f27e7eb07
+F test/fts3auto.test 4aca0c0631fd40e79a45d8490828152b46ad4559
 F test/fts3aux1.test 0b02743955d56fc0d4d66236a26177bd1b726de0
 F test/fts3b.test e93bbb653e52afde110ad53bbd793f14fe7a8984
 F test/fts3c.test fc723a9cf10b397fdfc2b32e73c53c8b1ec02958
@@ -945,7 +945,7 @@ F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c
 F tool/symbols.sh bc2a3709940d47c8ac8e0a1fdf17ec801f015a00
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
 F tool/warnings.sh 347d974d143cf132f953b565fbc03026f19fcb4d
-P 4e8dd19eef04777d800977faf1859a405e396f30
-R 6b4754b974de210b10ad796c5876a1bc
+P 5f6b87f420f21749aa7c72e020c50aca74890086
+R ca51faeb6c7aef68bca6b7079ff7ebda
 U dan
-Z f1c93614cce7c70636f14d06ee6a3496
+Z 9a71ae3656c163debcbb9f929d0e7d22
index 5c8deb0eac2998308b9a73c6fa6d741ade0cc692..45225befd730b0aa6feaa7b2f2341461e1fa0773 100644 (file)
@@ -1 +1 @@
-5f6b87f420f21749aa7c72e020c50aca74890086
\ No newline at end of file
+e4ab6cdb101bbeb804820425cf569ee7dc2397fb
\ No newline at end of file
index fe87d9f2c7a2f8a31445d45cad716d23fc4d494f..97b4304f0ce0e5b9a48e4683057bb7050d4aa709 100644 (file)
@@ -16,32 +16,13 @@ ifcapable !fts3 { finish_test ; return }
 source $testdir/fts3_common.tcl
 source $testdir/malloc_common.tcl
 
-set testprefix fts3rnd2
+set testprefix fts3auto
+set sfep $sqlite_fts3_enable_parentheses
+set sqlite_fts3_enable_parentheses 1
 
-proc test_fts3_near_match {tn doc expr res} {
-  fts3_near_match $doc $expr -phrasecountvar p
-  uplevel do_test [list $tn] [list [list set {} $p]] [list $res]
-}
-
-# Simple test cases for C routine [fts3_near_match].
+#--------------------------------------------------------------------------
+# Start of Tcl procs used by tests.
 #
-test_fts3_near_match 1.1.1 {a b c a b} a                   {2}
-test_fts3_near_match 1.1.2 {a b c a b} {a 5 b 6 c}         {2 2 1}
-test_fts3_near_match 1.1.3 {a b c a b} {"a b"}             {2}
-test_fts3_near_match 1.1.4 {a b c a b} {"b c"}             {1}
-test_fts3_near_match 1.1.5 {a b c a b} {"c c"}             {0}
-
-test_fts3_near_match 1.2.1 "a b c d e f g" {b 2 f}         {0 0}
-test_fts3_near_match 1.2.2 "a b c d e f g" {b 3 f}         {1 1}
-test_fts3_near_match 1.2.3 "a b c d e f g" {f 2 b}         {0 0}
-test_fts3_near_match 1.2.4 "a b c d e f g" {f 3 b}         {1 1}
-test_fts3_near_match 1.2.5 "a b c d e f g" {"a b" 2 "f g"} {0 0}
-test_fts3_near_match 1.2.6 "a b c d e f g" {"a b" 3 "f g"} {1 1}
-
-set A "a b c d e f g h i j k l m n o p q r s t u v w x y z"
-test_fts3_near_match 1.3.1 $A {"c d" 5 "i j" 1 "e f"}      {0 0 0}
-test_fts3_near_match 1.3.2 $A {"c d" 5 "i j" 2 "e f"}      {1 1 1}
-
 proc mit {blob} {
   set scan(littleEndian) i*
   set scan(bigEndian) I*
@@ -62,24 +43,13 @@ proc fix_near_expr {expr} {
   return $out
 }
 
-proc do_near_test {tn tbl expr} {
+proc get_single_near_results {tbl expr arrayvar nullvar} {
+  upvar $arrayvar aMatchinfo
+  upvar $nullvar nullentry
+  catch {array unset aMatchinfo}
 
   set expr [fix_near_expr $expr]
 
-  # Create the MATCH expression from $expr
-  #
-  set match [lindex $expr 0]
-  if {[llength $match]>1} {
-    set match "\"$match\""
-  } 
-  foreach {nNear phrase} [lrange $expr 1 end] {
-    if {[llength $phrase]>1} {
-      append match " NEAR/$nNear \"$phrase\""
-    } else {
-      append match " NEAR/$nNear $phrase"
-    }
-  }
-
   # Calculate the expected results using [fts3_near_match]. The following
   # loop populates the "hits" and "counts" arrays as follows:
   # 
@@ -144,6 +114,116 @@ proc do_near_test {tn tbl expr} {
     set aMatchinfo($docid) $mi
   }
 
+  # Set up the nullentry output.
+  #
+  set nullentry [list]
+  for {set iPhrase 0} {$iPhrase<$nPhrase} {incr iPhrase} {
+    for {set iCol 0} {$iCol<$nCol} {incr iCol} {
+      lappend nullentry 0 $nDoc($iPhrase,$iCol) $nHit($iPhrase,$iCol)
+    }
+  }
+}
+
+
+proc matching_brackets {expr} {
+  if {[string range $expr 0 0]!="(" || [string range $expr end end] !=")"} { 
+    return 0 
+  }
+
+  set iBracket 1
+  set nExpr [string length $expr]
+  for {set i 1} {$iBracket && $i < $nExpr} {incr i} {
+    set c [string range $expr $i $i]
+    if {$c == "("} {incr iBracket}
+    if {$c == ")"} {incr iBracket -1}
+  }
+
+  return [expr ($iBracket==0 && $i==$nExpr)]
+}
+
+proc get_near_results {tbl expr arrayvar {nullvar ""}} {
+  upvar $arrayvar aMatchinfo
+  if {$nullvar != ""} { upvar $nullvar nullentry }
+
+  set expr [string trim $expr]
+  while { [matching_brackets $expr] } {
+    set expr [string trim [string range $expr 1 end-1]]
+  }
+
+  set prec(NOT) 1
+  set prec(AND) 2
+  set prec(OR)  3
+
+  set currentprec 0
+  set iBracket 0
+  set expr_length [llength $expr]
+  for {set i 0} {$i < $expr_length} {incr i} {
+    set op [lindex $expr $i]
+    if {$iBracket==0 && [info exists prec($op)] && $prec($op)>=$currentprec } {
+      set opidx $i
+      set currentprec $prec($op)
+    } else {
+      for {set j 0} {$j < [string length $op]} {incr j} {
+        set c [string range $op $j $j]
+        if {$c == "("} { incr iBracket +1 }
+        if {$c == ")"} { incr iBracket -1 }
+      }
+    }
+  }
+  if {$iBracket!=0} { error "mismatched brackets in: $expr" }
+
+  if {[info exists opidx]==0} {
+    get_single_near_results $tbl $expr aMatchinfo nullentry
+  } else {
+    set eLeft  [lrange $expr 0 [expr $opidx-1]]
+    set eRight [lrange $expr [expr $opidx+1] end]
+
+    get_near_results $tbl $eLeft  aLeft  nullleft
+    get_near_results $tbl $eRight aRight nullright
+
+    switch -- [lindex $expr $opidx] {
+      "NOT" {
+        foreach hit [array names aLeft] {
+          if {0==[info exists aRight($hit)]} {
+            set aMatchinfo($hit) $aLeft($hit)
+          }
+        }
+        set nullentry $nullleft
+      }
+
+      "AND" {
+        foreach hit [array names aLeft] {
+          if {[info exists aRight($hit)]} {
+            set aMatchinfo($hit) [concat $aLeft($hit) $aRight($hit)]
+          }
+        }
+        set nullentry [concat $nullleft $nullright]
+      }
+
+      "OR" {
+        foreach hit [array names aLeft] {
+          if {[info exists aRight($hit)]} {
+            set aMatchinfo($hit) [concat $aLeft($hit) $aRight($hit)]
+            unset aRight($hit)
+          } else {
+            set aMatchinfo($hit) [concat $aLeft($hit) $nullright]
+          }
+        }
+        foreach hit [array names aRight] {
+          set aMatchinfo($hit) [concat $nullleft $aRight($hit)]
+        }
+
+        set nullentry [concat $nullleft $nullright]
+      }
+    }
+  }
+}
+
+proc do_near_test {tn tbl expr} {
+
+  get_near_results $tbl $expr aMatchinfo
+  set match $expr
+
   set matchinfo_asc [list]
   foreach docid [lsort -integer -incr [array names aMatchinfo]] {
     lappend matchinfo_asc $docid $aMatchinfo($docid)
@@ -153,15 +233,15 @@ proc do_near_test {tn tbl expr} {
     lappend matchinfo_desc $docid $aMatchinfo($docid)
   }
 
-  set title "(\"$match\" -> [llength [array names hits]] rows)"
+  set title "(\"$match\" -> [llength [array names aMatchinfo]] rows)"
 
   do_execsql_test $tn$title.1 "
     SELECT docid FROM $tbl WHERE $tbl MATCH '$match' ORDER BY docid ASC
-  " [lsort -integer -incr [array names hits]] 
+  " [lsort -integer -incr [array names aMatchinfo]] 
 
   do_execsql_test $tn$title.2 "
     SELECT docid FROM $tbl WHERE $tbl MATCH '$match' ORDER BY docid DESC
-  " [lsort -integer -decr [array names hits]] 
+  " [lsort -integer -decr [array names aMatchinfo]] 
 
   do_execsql_test $tn$title.3 "
     SELECT docid, mit(matchinfo($tbl, 'x')) FROM $tbl 
@@ -174,28 +254,83 @@ proc do_near_test {tn tbl expr} {
   " $matchinfo_asc
 } 
 
-do_test 2.1 {
-  execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b) }
-  for {set i 0} {$i<32} {incr i} {
-    set doc [list]
-    if {$i&0x01} {lappend doc one}
-    if {$i&0x02} {lappend doc two}
-    if {$i&0x04} {lappend doc three}
-    if {$i&0x08} {lappend doc four}
-    if {$i&0x10} {lappend doc five}
-    execsql { INSERT INTO t1 VALUES($doc, null) }
-  }
-} {}
-foreach {tn expr} {
-  1     {one}
-  2     {one NEAR/1 five}
-  3     {t*}
-  4     {t* NEAR/0 five}
-  5     {o* NEAR/1 f*}
-  6     {one NEAR five NEAR two NEAR four NEAR three}
+
+# End of test procs. Actual tests are below this line.
+#--------------------------------------------------------------------------
+
+#--------------------------------------------------------------------------
+# The following test cases - fts3auto-1.* - focus on testing the Tcl 
+# command [fts3_near_match], which is used by other tests in this file.
+#
+proc test_fts3_near_match {tn doc expr res} {
+  fts3_near_match $doc $expr -phrasecountvar p
+  uplevel do_test [list $tn] [list [list set {} $p]] [list $res]
+}
+
+test_fts3_near_match 1.1.1 {a b c a b} a                   {2}
+test_fts3_near_match 1.1.2 {a b c a b} {a 5 b 6 c}         {2 2 1}
+test_fts3_near_match 1.1.3 {a b c a b} {"a b"}             {2}
+test_fts3_near_match 1.1.4 {a b c a b} {"b c"}             {1}
+test_fts3_near_match 1.1.5 {a b c a b} {"c c"}             {0}
+
+test_fts3_near_match 1.2.1 "a b c d e f g" {b 2 f}         {0 0}
+test_fts3_near_match 1.2.2 "a b c d e f g" {b 3 f}         {1 1}
+test_fts3_near_match 1.2.3 "a b c d e f g" {f 2 b}         {0 0}
+test_fts3_near_match 1.2.4 "a b c d e f g" {f 3 b}         {1 1}
+test_fts3_near_match 1.2.5 "a b c d e f g" {"a b" 2 "f g"} {0 0}
+test_fts3_near_match 1.2.6 "a b c d e f g" {"a b" 3 "f g"} {1 1}
+
+set A "a b c d e f g h i j k l m n o p q r s t u v w x y z"
+test_fts3_near_match 1.3.1 $A {"c d" 5 "i j" 1 "e f"}      {0 0 0}
+test_fts3_near_match 1.3.2 $A {"c d" 5 "i j" 2 "e f"}      {1 1 1}
+
+#--------------------------------------------------------------------------
+# Test cases fts3auto-2.* run some simple tests using the 
+# [do_near_test] proc.
+#
+foreach {tn create} {
+  1    "CREATE VIRTUAL TABLE t1 USING fts4(a, b)"
+  2    "CREATE VIRTUAL TABLE t1 USING fts4(a, b, order=DESC)"
+  3    "CREATE VIRTUAL TABLE t1 USING fts4(a, b, order=ASC)"
+  4    "CREATE VIRTUAL TABLE t1 USING fts4(a, b, prefix=1)"
+  5    "CREATE VIRTUAL TABLE t1 USING fts4(a, b, order=DESC, prefix=1)"
+  6    "CREATE VIRTUAL TABLE t1 USING fts4(a, b, order=ASC, prefix=1)"
 } {
-  do_near_test 2.2.$tn t1 $expr
+  do_test 2.$tn.1 {
+    catchsql { DROP TABLE t1 }
+    execsql  $create
+    for {set i 0} {$i<32} {incr i} {
+      set doc [list]
+      if {$i&0x01} {lappend doc one}
+      if {$i&0x02} {lappend doc two}
+      if {$i&0x04} {lappend doc three}
+      if {$i&0x08} {lappend doc four}
+      if {$i&0x10} {lappend doc five}
+      execsql { INSERT INTO t1 VALUES($doc, null) }
+    }
+  } {}
+  foreach {tn2 expr} {
+    1     {one}
+    2     {one NEAR/1 five}
+    3     {t*}
+    4     {t* NEAR/0 five}
+    5     {o* NEAR/1 f*}
+    6     {one NEAR five NEAR two NEAR four NEAR three}
+    7     {one NEAR xyz}
+    8     {one OR two}
+    9     {one AND two}
+    10    {one NOT two}
+    11    {one AND two OR three}
+    12    {three OR one AND two}
+    13    {(three OR one) AND two}
+    14    {(three OR one) AND two NOT (five NOT four)}
+    15    {"one two"}
+    16    {"one two" NOT "three four"}
+  } {
+    do_near_test 2.$tn.2.$tn2 t1 $expr
+  }
 }
 
+set sqlite_fts3_enable_parentheses $sfep
 finish_test