fts3atoken.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # 2007 June 21
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #*************************************************************************
  11. # This file implements regression tests for SQLite library. The focus
  12. # of this script is testing the pluggable tokeniser feature of the
  13. # FTS3 module.
  14. #
  15. # $Id: fts3atoken.test,v 1.1 2007/08/20 17:38:42 shess Exp $
  16. #
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # If SQLITE_ENABLE_FTS3 is defined, omit this file.
  20. ifcapable !fts3 {
  21. finish_test
  22. return
  23. }
  24. set ::testprefix fts3token
  25. proc escape_string {str} {
  26. set out ""
  27. foreach char [split $str ""] {
  28. scan $char %c i
  29. if {$i<=127} {
  30. append out $char
  31. } else {
  32. append out [format {\x%.4x} $i]
  33. }
  34. }
  35. set out
  36. }
  37. #--------------------------------------------------------------------------
  38. # Test cases fts3token-1.* are the warm-body test for the SQL scalar
  39. # function fts3_tokenizer(). The procedure is as follows:
  40. #
  41. # 1: Verify that there is no such fts3 tokenizer as 'blah'.
  42. #
  43. # 2: Query for the built-in tokenizer 'simple'. Insert a copy of the
  44. # retrieved value as tokenizer 'blah'.
  45. #
  46. # 3: Test that the value returned for tokenizer 'blah' is now the
  47. # same as that retrieved for 'simple'.
  48. #
  49. # 4: Test that it is now possible to create an fts3 table using
  50. # tokenizer 'blah' (it was not possible in step 1).
  51. #
  52. # 5: Test that the table created to use tokenizer 'blah' is usable.
  53. #
  54. do_test fts3token-1.1 {
  55. catchsql {
  56. CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah);
  57. }
  58. } {1 {unknown tokenizer: blah}}
  59. do_test fts3token-1.2 {
  60. execsql {
  61. SELECT fts3_tokenizer('blah', fts3_tokenizer('simple')) IS NULL;
  62. }
  63. } {0}
  64. do_test fts3token-1.3 {
  65. execsql {
  66. SELECT fts3_tokenizer('blah') == fts3_tokenizer('simple');
  67. }
  68. } {1}
  69. do_test fts3token-1.4 {
  70. catchsql {
  71. CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah);
  72. }
  73. } {0 {}}
  74. do_test fts3token-1.5 {
  75. execsql {
  76. INSERT INTO t1(content) VALUES('There was movement at the station');
  77. INSERT INTO t1(content) VALUES('For the word has passed around');
  78. INSERT INTO t1(content) VALUES('That the colt from ol regret had got away');
  79. SELECT content FROM t1 WHERE content MATCH 'movement'
  80. }
  81. } {{There was movement at the station}}
  82. #--------------------------------------------------------------------------
  83. # Test cases fts3token-2.* test error cases in the scalar function based
  84. # API for getting and setting tokenizers.
  85. #
  86. do_test fts3token-2.1 {
  87. catchsql {
  88. SELECT fts3_tokenizer('nosuchtokenizer');
  89. }
  90. } {1 {unknown tokenizer: nosuchtokenizer}}
  91. #--------------------------------------------------------------------------
  92. # Test cases fts3token-3.* test the three built-in tokenizers with a
  93. # simple input string via the built-in test function. This is as much
  94. # to test the test function as the tokenizer implementations.
  95. #
  96. do_test fts3token-3.1 {
  97. execsql {
  98. SELECT fts3_tokenizer_test('simple', 'I don''t see how');
  99. }
  100. } {{0 i I 1 don don 2 t t 3 see see 4 how how}}
  101. do_test fts3token-3.2 {
  102. execsql {
  103. SELECT fts3_tokenizer_test('porter', 'I don''t see how');
  104. }
  105. } {{0 i I 1 don don 2 t t 3 see see 4 how how}}
  106. ifcapable icu {
  107. do_test fts3token-3.3 {
  108. execsql {
  109. SELECT fts3_tokenizer_test('icu', 'I don''t see how');
  110. }
  111. } {{0 i I 1 don't don't 2 see see 3 how how}}
  112. }
  113. #--------------------------------------------------------------------------
  114. # Test cases fts3token-4.* test the ICU tokenizer. In practice, this
  115. # tokenizer only has two modes - "thai" and "everybody else". Some other
  116. # Asian languages (Lao, Khmer etc.) require the same special treatment as
  117. # Thai, but ICU doesn't support them yet.
  118. #
  119. ifcapable icu {
  120. proc do_icu_test {name locale input output} {
  121. set ::out [db eval { SELECT fts3_tokenizer_test('icu', $locale, $input) }]
  122. do_test $name {
  123. lindex $::out 0
  124. } $output
  125. }
  126. do_icu_test fts3token-4.1 en_US {} {}
  127. do_icu_test fts3token-4.2 en_US {Test cases fts3} [list \
  128. 0 test Test 1 cases cases 2 fts3 fts3
  129. ]
  130. # The following test shows that ICU is smart enough to recognise
  131. # Thai chararacters, even when the locale is set to English/United
  132. # States.
  133. #
  134. set input "\u0e2d\u0e30\u0e44\u0e23\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a"
  135. set output "0 \u0e2d\u0e30\u0e44\u0e23 \u0e2d\u0e30\u0e44\u0e23 "
  136. append output "1 \u0e19\u0e30 \u0e19\u0e30 "
  137. append output "2 \u0e04\u0e23\u0e31\u0e1a \u0e04\u0e23\u0e31\u0e1a"
  138. do_icu_test fts3token-4.3 th_TH $input $output
  139. do_icu_test fts3token-4.4 en_US $input $output
  140. # ICU handles an unknown locale by falling back to the default.
  141. # So this is not an error.
  142. do_icu_test fts3token-4.5 MiddleOfTheOcean $input $output
  143. set longtoken "AReallyReallyLongTokenOneThatWillSurelyRequire"
  144. append longtoken "AReallocInTheIcuTokenizerCode"
  145. set input "short tokens then "
  146. append input $longtoken
  147. set output "0 short short "
  148. append output "1 tokens tokens "
  149. append output "2 then then "
  150. append output "3 [string tolower $longtoken] $longtoken"
  151. do_icu_test fts3token-4.6 MiddleOfTheOcean $input $output
  152. do_icu_test fts3token-4.7 th_TH $input $output
  153. do_icu_test fts3token-4.8 en_US $input $output
  154. do_execsql_test 5.1 {
  155. CREATE VIRTUAL TABLE x1 USING fts3(name,TOKENIZE icu en_US);
  156. insert into x1 (name) values (NULL);
  157. insert into x1 (name) values (NULL);
  158. delete from x1;
  159. }
  160. proc cp_to_str {codepoint_list} {
  161. set fmt [string repeat %c [llength $codepoint_list]]
  162. eval [list format $fmt] $codepoint_list
  163. }
  164. do_test 5.2 {
  165. set str [cp_to_str {19968 26085 32822 32645 27874 23433 20986}]
  166. execsql { INSERT INTO x1 VALUES($str) }
  167. } {}
  168. }
  169. do_test fts3token-internal {
  170. execsql { SELECT fts3_tokenizer_internal_test() }
  171. } {ok}
  172. finish_test