sl@0: # 2007 June 21 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #************************************************************************* sl@0: # This file implements regression tests for SQLite library. The focus sl@0: # of this script is testing the pluggable tokeniser feature of the sl@0: # FTS2 module. sl@0: # sl@0: # $Id: fts2token.test,v 1.3 2007/06/25 12:05:40 danielk1977 Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # If SQLITE_ENABLE_FTS2 is defined, omit this file. sl@0: ifcapable !fts2 { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: proc escape_string {str} { sl@0: set out "" sl@0: foreach char [split $str ""] { sl@0: scan $char %c i sl@0: if {$i<=127} { sl@0: append out $char sl@0: } else { sl@0: append out [format {\x%.4x} $i] sl@0: } sl@0: } sl@0: set out sl@0: } sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # Test cases fts2token-1.* are the warm-body test for the SQL scalar sl@0: # function fts2_tokenizer(). The procedure is as follows: sl@0: # sl@0: # 1: Verify that there is no such fts2 tokenizer as 'blah'. sl@0: # sl@0: # 2: Query for the built-in tokenizer 'simple'. Insert a copy of the sl@0: # retrieved value as tokenizer 'blah'. sl@0: # sl@0: # 3: Test that the value returned for tokenizer 'blah' is now the sl@0: # same as that retrieved for 'simple'. sl@0: # sl@0: # 4: Test that it is now possible to create an fts2 table using sl@0: # tokenizer 'blah' (it was not possible in step 1). sl@0: # sl@0: # 5: Test that the table created to use tokenizer 'blah' is usable. sl@0: # sl@0: do_test fts2token-1.1 { sl@0: catchsql { sl@0: CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah); sl@0: } sl@0: } {1 {unknown tokenizer: blah}} sl@0: do_test fts2token-1.2 { sl@0: execsql { sl@0: SELECT fts2_tokenizer('blah', fts2_tokenizer('simple')) IS NULL; sl@0: } sl@0: } {0} sl@0: do_test fts2token-1.3 { sl@0: execsql { sl@0: SELECT fts2_tokenizer('blah') == fts2_tokenizer('simple'); sl@0: } sl@0: } {1} sl@0: do_test fts2token-1.4 { sl@0: catchsql { sl@0: CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah); sl@0: } sl@0: } {0 {}} sl@0: do_test fts2token-1.5 { sl@0: execsql { sl@0: INSERT INTO t1(content) VALUES('There was movement at the station'); sl@0: INSERT INTO t1(content) VALUES('For the word has passed around'); sl@0: INSERT INTO t1(content) VALUES('That the colt from ol regret had got away'); sl@0: SELECT content FROM t1 WHERE content MATCH 'movement' sl@0: } sl@0: } {{There was movement at the station}} sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # Test cases fts2token-2.* test error cases in the scalar function based sl@0: # API for getting and setting tokenizers. sl@0: # sl@0: do_test fts2token-2.1 { sl@0: catchsql { sl@0: SELECT fts2_tokenizer('nosuchtokenizer'); sl@0: } sl@0: } {1 {unknown tokenizer: nosuchtokenizer}} sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # Test cases fts2token-3.* test the three built-in tokenizers with a sl@0: # simple input string via the built-in test function. This is as much sl@0: # to test the test function as the tokenizer implementations. sl@0: # sl@0: do_test fts2token-3.1 { sl@0: execsql { sl@0: SELECT fts2_tokenizer_test('simple', 'I don''t see how'); sl@0: } sl@0: } {{0 i I 1 don don 2 t t 3 see see 4 how how}} sl@0: do_test fts2token-3.2 { sl@0: execsql { sl@0: SELECT fts2_tokenizer_test('porter', 'I don''t see how'); sl@0: } sl@0: } {{0 i I 1 don don 2 t t 3 see see 4 how how}} sl@0: ifcapable icu { sl@0: do_test fts2token-3.3 { sl@0: execsql { sl@0: SELECT fts2_tokenizer_test('icu', 'I don''t see how'); sl@0: } sl@0: } {{0 i I 1 don't don't 2 see see 3 how how}} sl@0: } sl@0: sl@0: #-------------------------------------------------------------------------- sl@0: # Test cases fts2token-4.* test the ICU tokenizer. In practice, this sl@0: # tokenizer only has two modes - "thai" and "everybody else". Some other sl@0: # Asian languages (Lao, Khmer etc.) require the same special treatment as sl@0: # Thai, but ICU doesn't support them yet. sl@0: # sl@0: ifcapable icu { sl@0: sl@0: proc do_icu_test {name locale input output} { sl@0: set ::out [db eval { SELECT fts2_tokenizer_test('icu', $locale, $input) }] sl@0: do_test $name { sl@0: lindex $::out 0 sl@0: } $output sl@0: } sl@0: sl@0: do_icu_test fts2token-4.1 en_US {} {} sl@0: do_icu_test fts2token-4.2 en_US {Test cases fts2} [list \ sl@0: 0 test Test 1 cases cases 2 fts2 fts2 sl@0: ] sl@0: sl@0: # The following test shows that ICU is smart enough to recognise sl@0: # Thai chararacters, even when the locale is set to English/United sl@0: # States. sl@0: # sl@0: set input "\u0e2d\u0e30\u0e44\u0e23\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a" sl@0: set output "0 \u0e2d\u0e30\u0e44\u0e23 \u0e2d\u0e30\u0e44\u0e23 " sl@0: append output "1 \u0e19\u0e30 \u0e19\u0e30 " sl@0: append output "2 \u0e04\u0e23\u0e31\u0e1a \u0e04\u0e23\u0e31\u0e1a" sl@0: sl@0: do_icu_test fts2token-4.3 th_TH $input $output sl@0: do_icu_test fts2token-4.4 en_US $input $output sl@0: sl@0: # ICU handles an unknown locale by falling back to the default. sl@0: # So this is not an error. sl@0: do_icu_test fts2token-4.5 MiddleOfTheOcean $input $output sl@0: sl@0: set longtoken "AReallyReallyLongTokenOneThatWillSurelyRequire" sl@0: append longtoken "AReallocInTheIcuTokenizerCode" sl@0: sl@0: set input "short tokens then " sl@0: append input $longtoken sl@0: set output "0 short short " sl@0: append output "1 tokens tokens " sl@0: append output "2 then then " sl@0: append output "3 [string tolower $longtoken] $longtoken" sl@0: sl@0: do_icu_test fts2token-4.6 MiddleOfTheOcean $input $output sl@0: do_icu_test fts2token-4.7 th_TH $input $output sl@0: do_icu_test fts2token-4.8 en_US $input $output sl@0: } sl@0: sl@0: do_test fts2token-internal { sl@0: execsql { SELECT fts2_tokenizer_internal_test() } sl@0: } {ok} sl@0: sl@0: finish_test