mirror of
https://github.com/status-im/sqlcipher.git
synced 2025-02-23 09:18:11 +00:00
293 lines
8.3 KiB
Plaintext
293 lines
8.3 KiB
Plaintext
# 2009 December 03
|
|
#
|
|
# 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.
|
|
#
|
|
#***********************************************************************
|
|
#
|
|
# Brute force (random data) tests for FTS3.
|
|
#
|
|
|
|
set testdir [file dirname $argv0]
|
|
source $testdir/tester.tcl
|
|
|
|
# If this build does not include FTS3, skip the tests in this file.
|
|
#
|
|
ifcapable !fts3 { finish_test ; return }
|
|
source $testdir/fts3_common.tcl
|
|
|
|
set nVocab 100
|
|
set lVocab [list]
|
|
|
|
# Generate a vocabulary of nVocab words. Each word is 3 characters long.
|
|
#
|
|
set lChar {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}
|
|
for {set i 0} {$i < $nVocab} {incr i} {
|
|
set word [lindex $lChar [expr int(rand()*26)]]
|
|
append word [lindex $lChar [expr int(rand()*26)]]
|
|
append word [lindex $lChar [expr int(rand()*26)]]
|
|
lappend lVocab $word
|
|
}
|
|
|
|
proc random_term {} {
|
|
lindex $::lVocab [expr {int(rand()*$::nVocab)}]
|
|
}
|
|
|
|
# Return a document consisting of $nWord arbitrarily selected terms
|
|
# from the $::lVocab list.
|
|
#
|
|
proc generate_doc {nWord} {
|
|
set doc [list]
|
|
for {set i 0} {$i < $nWord} {incr i} {
|
|
lappend doc [random_term]
|
|
}
|
|
return $doc
|
|
}
|
|
|
|
|
|
|
|
# Primitives to update the table.
|
|
#
|
|
unset -nocomplain t1
|
|
proc insert_row {rowid} {
|
|
set a [generate_doc [expr int((rand()*100))]]
|
|
set b [generate_doc [expr int((rand()*100))]]
|
|
set c [generate_doc [expr int((rand()*100))]]
|
|
execsql { INSERT INTO t1(docid, a, b, c) VALUES($rowid, $a, $b, $c) }
|
|
set ::t1($rowid) [list $a $b $c]
|
|
}
|
|
proc delete_row {rowid} {
|
|
execsql { DELETE FROM t1 WHERE rowid = $rowid }
|
|
catch {unset ::t1($rowid)}
|
|
}
|
|
proc update_row {rowid} {
|
|
set cols {a b c}
|
|
set iCol [expr int(rand()*3)]
|
|
set doc [generate_doc [expr int((rand()*100))]]
|
|
lset ::t1($rowid) $iCol $doc
|
|
execsql "UPDATE t1 SET [lindex $cols $iCol] = \$doc WHERE rowid = \$rowid"
|
|
}
|
|
|
|
proc simple_phrase {zPrefix} {
|
|
set ret [list]
|
|
set pattern "*[string map {* \[a-z\]} $zPrefix]*"
|
|
foreach {key value} [array get ::t1] {
|
|
if {[string match $pattern $value]} { lappend ret $key }
|
|
}
|
|
lsort -integer $ret
|
|
}
|
|
proc simple_near {termlist nNear} {
|
|
set ret [list]
|
|
|
|
foreach {key value} [array get ::t1] {
|
|
foreach v $value {
|
|
|
|
set l [lsearch -exact -all $v [lindex $termlist 0]]
|
|
foreach T [lrange $termlist 1 end] {
|
|
set l2 [list]
|
|
foreach i $l {
|
|
set iStart [expr $i - $nNear - 1]
|
|
set iEnd [expr $i + $nNear + 1]
|
|
if {$iStart < 0} {set iStart 0}
|
|
foreach i2 [lsearch -exact -all [lrange $v $iStart $iEnd] $T] {
|
|
incr i2 $iStart
|
|
if {$i2 != $i} { lappend l2 $i2 }
|
|
}
|
|
}
|
|
set l [lsort -uniq -integer $l2]
|
|
}
|
|
|
|
if {[llength $l]} {
|
|
#puts "MATCH($key): $v"
|
|
lappend ret $key
|
|
}
|
|
}
|
|
}
|
|
|
|
lsort -unique -integer $ret
|
|
}
|
|
|
|
# The following three procs:
|
|
#
|
|
# setup_not A B
|
|
# setup_or A B
|
|
# setup_and A B
|
|
#
|
|
# each take two arguments. Both arguments must be lists of integer values
|
|
# sorted by value. The return value is the list produced by evaluating
|
|
# the equivalent of "A op B", where op is the FTS3 operator NOT, OR or
|
|
# AND.
|
|
#
|
|
proc setop_not {A B} {
|
|
foreach b $B { set n($b) {} }
|
|
set ret [list]
|
|
foreach a $A { if {![info exists n($a)]} {lappend ret $a} }
|
|
return $ret
|
|
}
|
|
proc setop_or {A B} {
|
|
lsort -integer -uniq [concat $A $B]
|
|
}
|
|
proc setop_and {A B} {
|
|
foreach b $B { set n($b) {} }
|
|
set ret [list]
|
|
foreach a $A { if {[info exists n($a)]} {lappend ret $a} }
|
|
return $ret
|
|
}
|
|
|
|
set sqlite_fts3_enable_parentheses 1
|
|
|
|
foreach nodesize {50 500 1000 2000} {
|
|
catch { array unset ::t1 }
|
|
|
|
# Create the FTS3 table. Populate it (and the Tcl array) with 100 rows.
|
|
#
|
|
db transaction {
|
|
catchsql { DROP TABLE t1 }
|
|
execsql "CREATE VIRTUAL TABLE t1 USING fts3(a, b, c, test:$nodesize)"
|
|
for {set i 0} {$i < 100} {incr i} { insert_row $i }
|
|
}
|
|
|
|
for {set iTest 1} {$iTest <= 100} {incr iTest} {
|
|
|
|
# Delete one row, update one row and insert one row.
|
|
#
|
|
set rows [array names ::t1]
|
|
set nRow [llength $rows]
|
|
set iUpdate [lindex $rows [expr {int(rand()*$nRow)}]]
|
|
set iDelete $iUpdate
|
|
while {$iDelete == $iUpdate} {
|
|
set iDelete [lindex $rows [expr {int(rand()*$nRow)}]]
|
|
}
|
|
set iInsert $iUpdate
|
|
while {[info exists ::t1($iInsert)]} {
|
|
set iInsert [expr {int(rand()*1000000)}]
|
|
}
|
|
db transaction {
|
|
insert_row $iInsert
|
|
update_row $iUpdate
|
|
delete_row $iDelete
|
|
}
|
|
|
|
# Pick 10 terms from the vocabulary. Check that the results of querying
|
|
# the database for the set of documents containing each of these terms
|
|
# is the same as the result obtained by scanning the contents of the Tcl
|
|
# array for each term.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set term [random_term]
|
|
do_test fts3rnd-1.$nodesize.$iTest.1.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $term }
|
|
} [simple_phrase $term]
|
|
}
|
|
|
|
# This time, use the first two characters of each term as a term prefix
|
|
# to query for. Test that querying the Tcl array produces the same results
|
|
# as querying the FTS3 table for the prefix.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set prefix [string range [random_term] 0 1]
|
|
set match "${prefix}*"
|
|
do_test fts3rnd-1.$nodesize.$iTest.2.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_phrase $match]
|
|
}
|
|
|
|
# Similar to the above, except for phrase queries.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set term [list [random_term] [random_term]]
|
|
set match "\"$term\""
|
|
do_test fts3rnd-1.$nodesize.$iTest.3.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_phrase $term]
|
|
}
|
|
|
|
# Three word phrases.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set term [list [random_term] [random_term] [random_term]]
|
|
set match "\"$term\""
|
|
do_test fts3rnd-1.$nodesize.$iTest.4.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_phrase $term]
|
|
}
|
|
|
|
# Three word phrases made up of term-prefixes.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set query "[string range [random_term] 0 1]* "
|
|
append query "[string range [random_term] 0 1]* "
|
|
append query "[string range [random_term] 0 1]*"
|
|
|
|
set match "\"$query\""
|
|
do_test fts3rnd-1.$nodesize.$iTest.5.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_phrase $query]
|
|
}
|
|
|
|
# A NEAR query with terms as the arguments.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set terms [list [random_term] [random_term]]
|
|
set match [join $terms " NEAR "]
|
|
do_test fts3rnd-1.$nodesize.$iTest.6.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_near $terms 10]
|
|
}
|
|
|
|
# A 3-way NEAR query with terms as the arguments.
|
|
#
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set terms [list [random_term] [random_term] [random_term]]
|
|
set nNear 11
|
|
set match [join $terms " NEAR/$nNear "]
|
|
set fts3 [execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }]
|
|
do_test fts3rnd-1.$nodesize.$iTest.7.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [simple_near $terms $nNear]
|
|
}
|
|
|
|
# Set operations on simple term queries.
|
|
#
|
|
foreach {tn op proc} {
|
|
8 OR setop_or
|
|
9 NOT setop_not
|
|
10 AND setop_and
|
|
} {
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set term1 [random_term]
|
|
set term2 [random_term]
|
|
set match "$term1 $op $term2"
|
|
do_test fts3rnd-1.$nodesize.$iTest.$tn.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [$proc [simple_phrase $term1] [simple_phrase $term2]]
|
|
}
|
|
}
|
|
|
|
# Set operations on NEAR queries.
|
|
#
|
|
foreach {tn op proc} {
|
|
8 OR setop_or
|
|
9 NOT setop_not
|
|
10 AND setop_and
|
|
} {
|
|
for {set i 0} {$i < 10} {incr i} {
|
|
set term1 [random_term]
|
|
set term2 [random_term]
|
|
set term3 [random_term]
|
|
set term4 [random_term]
|
|
set match "$term1 NEAR $term2 $op $term3 NEAR $term4"
|
|
do_test fts3rnd-1.$nodesize.$iTest.$tn.$i {
|
|
execsql { SELECT docid FROM t1 WHERE t1 MATCH $match }
|
|
} [$proc \
|
|
[simple_near [list $term1 $term2] 10] \
|
|
[simple_near [list $term3 $term4] 10]
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
finish_test
|