fts1b.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # 2006 September 13
  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
  12. # focus of this script is testing the FTS1 module.
  13. #
  14. # $Id: fts1b.test,v 1.4 2006/09/18 02:12:48 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # If SQLITE_ENABLE_FTS1 is defined, omit this file.
  19. ifcapable !fts1 {
  20. finish_test
  21. return
  22. }
  23. # Fill the full-text index "t1" with phrases in english, spanish,
  24. # and german. For the i-th row, fill in the names for the bits
  25. # that are set in the value of i. The least significant bit is
  26. # 1. For example, the value 5 is 101 in binary which will be
  27. # converted to "one three" in english.
  28. #
  29. proc fill_multilanguage_fulltext_t1 {} {
  30. set english {one two three four five}
  31. set spanish {un dos tres cuatro cinco}
  32. set german {eine zwei drei vier funf}
  33. for {set i 1} {$i<=31} {incr i} {
  34. set cmd "INSERT INTO t1 VALUES"
  35. set vset {}
  36. foreach lang {english spanish german} {
  37. set words {}
  38. for {set j 0; set k 1} {$j<5} {incr j; incr k $k} {
  39. if {$k&$i} {lappend words [lindex [set $lang] $j]}
  40. }
  41. lappend vset "'$words'"
  42. }
  43. set sql "INSERT INTO t1(english,spanish,german) VALUES([join $vset ,])"
  44. # puts $sql
  45. db eval $sql
  46. }
  47. }
  48. # Construct a full-text search table containing five keywords:
  49. # one, two, three, four, and five, in various combinations. The
  50. # rowid for each will be a bitmask for the elements it contains.
  51. #
  52. db eval {
  53. CREATE VIRTUAL TABLE t1 USING fts1(english,spanish,german);
  54. }
  55. fill_multilanguage_fulltext_t1
  56. do_test fts1b-1.1 {
  57. execsql {SELECT rowid FROM t1 WHERE english MATCH 'one'}
  58. } {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31}
  59. do_test fts1b-1.2 {
  60. execsql {SELECT rowid FROM t1 WHERE spanish MATCH 'one'}
  61. } {}
  62. do_test fts1b-1.3 {
  63. execsql {SELECT rowid FROM t1 WHERE german MATCH 'one'}
  64. } {}
  65. do_test fts1b-1.4 {
  66. execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one'}
  67. } {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31}
  68. do_test fts1b-1.5 {
  69. execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one dos drei'}
  70. } {7 15 23 31}
  71. do_test fts1b-1.6 {
  72. execsql {SELECT english, spanish, german FROM t1 WHERE rowid=1}
  73. } {one un eine}
  74. do_test fts1b-1.7 {
  75. execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"one un"'}
  76. } {}
  77. do_test fts1b-2.1 {
  78. execsql {
  79. CREATE VIRTUAL TABLE t2 USING fts1(from,to);
  80. INSERT INTO t2([from],[to]) VALUES ('one two three', 'four five six');
  81. SELECT [from], [to] FROM t2
  82. }
  83. } {{one two three} {four five six}}
  84. # Compute an SQL string that contains the words one, two, three,... to
  85. # describe bits set in the value $i. Only the lower 5 bits are examined.
  86. #
  87. proc wordset {i} {
  88. set x {}
  89. for {set j 0; set k 1} {$j<5} {incr j; incr k $k} {
  90. if {$k&$i} {lappend x [lindex {one two three four five} $j]}
  91. }
  92. return '$x'
  93. }
  94. # Create a new FTS table with three columns:
  95. #
  96. # norm: words for the bits of rowid
  97. # plusone: words for the bits of rowid+1
  98. # invert: words for the bits of ~rowid
  99. #
  100. db eval {
  101. CREATE VIRTUAL TABLE t4 USING fts1([norm],'plusone',"invert");
  102. }
  103. for {set i 1} {$i<=15} {incr i} {
  104. set vset [list [wordset $i] [wordset [expr {$i+1}]] [wordset [expr {~$i}]]]
  105. db eval "INSERT INTO t4(norm,plusone,invert) VALUES([join $vset ,]);"
  106. }
  107. do_test fts1b-4.1 {
  108. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one'}
  109. } {1 3 5 7 9 11 13 15}
  110. do_test fts1b-4.2 {
  111. execsql {SELECT rowid FROM t4 WHERE norm MATCH 'one'}
  112. } {1 3 5 7 9 11 13 15}
  113. do_test fts1b-4.3 {
  114. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'one'}
  115. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
  116. do_test fts1b-4.4 {
  117. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:one'}
  118. } {2 4 6 8 10 12 14}
  119. do_test fts1b-4.5 {
  120. execsql {SELECT rowid FROM t4 WHERE plusone MATCH 'one'}
  121. } {2 4 6 8 10 12 14}
  122. do_test fts1b-4.6 {
  123. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one plusone:two'}
  124. } {1 5 9 13}
  125. do_test fts1b-4.7 {
  126. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one two'}
  127. } {1 3 5 7 9 11 13 15}
  128. do_test fts1b-4.8 {
  129. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:two norm:one'}
  130. } {1 5 9 13}
  131. do_test fts1b-4.9 {
  132. execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'two norm:one'}
  133. } {1 3 5 7 9 11 13 15}
  134. finish_test