collate9.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #
  2. # 2007 November 12
  3. #
  4. # The author disclaims copyright to this source code. In place of
  5. # a legal notice, here is a blessing:
  6. #
  7. # May you do good and not evil.
  8. # May you find forgiveness for yourself and forgive others.
  9. # May you share freely, never taking more than you give.
  10. #
  11. #***********************************************************************
  12. # This file implements regression tests for SQLite library. The
  13. # focus of this script is making sure that the names of collation
  14. # sequences may be quoted using double quotes in SQL statements.
  15. #
  16. # $Id: collate9.test,v 1.2 2008/07/10 00:32:42 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. proc reverse_sort {lhs rhs} {
  20. return [string compare $rhs $lhs]
  21. }
  22. db collate "reverse sort" reverse_sort
  23. # This procedure executes the SQL. Then it checks to see if the OP_Sort
  24. # opcode was executed. If an OP_Sort did occur, then "sort" is appended
  25. # to the result. If no OP_Sort happened, then "nosort" is appended.
  26. #
  27. # This procedure is used to check to make sure sorting is or is not
  28. # occurring as expected.
  29. #
  30. proc cksort {sql} {
  31. set ::sqlite_sort_count 0
  32. set data [execsql $sql]
  33. if {$::sqlite_sort_count} {set x sort} {set x nosort}
  34. lappend data $x
  35. return $data
  36. }
  37. # Test plan:
  38. #
  39. # collate9-1.* - Test collation sequences attached to table columns
  40. # collate9-2.* - Test collation sequences attached to expressions
  41. # collate9-3.* - Test collation sequences attached to an index
  42. # collate9-4.* - Test collation sequences as an argument to REINDEX
  43. #
  44. do_test collate9-1.1 {
  45. execsql {
  46. CREATE TABLE xy(x COLLATE "reverse sort", y COLLATE binary);
  47. INSERT INTO xy VALUES('one', 'one');
  48. INSERT INTO xy VALUES('two', 'two');
  49. INSERT INTO xy VALUES('three', 'three');
  50. }
  51. } {}
  52. do_test collate9-1.2 {
  53. execsql {
  54. SELECT x FROM xy ORDER BY x
  55. }
  56. } {two three one}
  57. do_test collate9-1.3 {
  58. execsql {
  59. SELECT y FROM xy ORDER BY y
  60. }
  61. } {one three two}
  62. do_test collate9-1.4 {
  63. cksort {
  64. SELECT x FROM xy ORDER BY x
  65. }
  66. } {two three one sort}
  67. do_test collate9-1.5 {
  68. execsql {
  69. CREATE INDEX xy_i ON xy(x)
  70. }
  71. } {}
  72. do_test collate9-1.6 {
  73. cksort {
  74. SELECT x FROM xy ORDER BY x
  75. }
  76. } {two three one nosort}
  77. do_test collate9-2.1 {
  78. execsql {
  79. SELECT x, x < 'seven' FROM xy ORDER BY x
  80. }
  81. } {two 1 three 1 one 0}
  82. do_test collate9-2.2 {
  83. execsql {
  84. SELECT y, y < 'seven' FROM xy ORDER BY x
  85. }
  86. } {two 0 three 0 one 1}
  87. do_test collate9-2.3 {
  88. execsql {
  89. SELECT y, y COLLATE "reverse sort" < 'seven' FROM xy ORDER BY x
  90. }
  91. } {two 1 three 1 one 0}
  92. do_test collate9-2.4 {
  93. execsql {
  94. SELECT y FROM xy ORDER BY y
  95. }
  96. } {one three two}
  97. do_test collate9-2.5 {
  98. execsql {
  99. SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
  100. }
  101. } {two three one}
  102. do_test collate9-2.6 {
  103. execsql {
  104. SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
  105. }
  106. } {two three one}
  107. do_test collate9-3.1 {
  108. execsql {
  109. CREATE INDEX xy_i2 ON xy(y COLLATE "reverse sort");
  110. }
  111. } {}
  112. do_test collate9-3.2 {
  113. cksort {
  114. SELECT y FROM xy ORDER BY y
  115. }
  116. } {one three two sort}
  117. do_test collate9-3.3 {
  118. cksort {
  119. SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
  120. }
  121. } {two three one nosort}
  122. do_test collate9-3.4 {
  123. cksort {
  124. SELECT y AS aaa FROM xy ORDER BY aaa
  125. }
  126. } {one three two sort}
  127. do_test collate9-3.5 {
  128. cksort {
  129. SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
  130. }
  131. } {two three one nosort}
  132. ifcapable reindex {
  133. do_test collate9-4.1 {
  134. execsql {
  135. REINDEX "reverse sort"
  136. }
  137. } {}
  138. # Modify the "reverse sort" collation so that it now sorts in the same
  139. # order as binary.
  140. proc reverse_sort {lhs rhs} {
  141. return [string compare $lhs $rhs]
  142. }
  143. # The integrity check should now fail because the indexes created using
  144. # "reverse sort" are no longer in sync with the collation sequence
  145. # implementation.
  146. do_test collate9-4.2 {
  147. expr {"ok" eq [execsql { PRAGMA integrity_check }]}
  148. } {0}
  149. do_test collate9-4.3 {
  150. execsql {
  151. REINDEX "reverse sort"
  152. }
  153. } {}
  154. # Integrity check should now pass.
  155. do_test collate9-4.4 {
  156. expr {"ok" eq [execsql { PRAGMA integrity_check }]}
  157. } {1}
  158. do_test collate9-4.5 {
  159. cksort {
  160. SELECT x FROM xy ORDER BY x COLLATE "reverse sort"
  161. }
  162. } {one three two nosort}
  163. }
  164. finish_test