1
0

descidx2.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # 2005 December 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
  12. # focus of this script is descending indices.
  13. #
  14. # $Id: descidx2.test,v 1.5 2008/03/19 00:21:31 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Do not use a codec for tests in this file, as the database file is
  19. # manipulated directly using tcl scripts (using the [hexio_write] command).
  20. #
  21. do_not_use_codec
  22. db eval {PRAGMA legacy_file_format=OFF}
  23. # This procedure sets the value of the file-format in file 'test.db'
  24. # to $newval. Also, the schema cookie is incremented.
  25. #
  26. proc set_file_format {newval} {
  27. hexio_write test.db 44 [hexio_render_int32 $newval]
  28. set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  29. incr schemacookie
  30. hexio_write test.db 40 [hexio_render_int32 $schemacookie]
  31. return {}
  32. }
  33. # This procedure returns the value of the file-format in file 'test.db'.
  34. #
  35. proc get_file_format {{fname test.db}} {
  36. return [hexio_get_int [hexio_read $fname 44 4]]
  37. }
  38. # Verify that the file format starts as 4
  39. #
  40. do_test descidx2-1.1 {
  41. execsql {
  42. CREATE TABLE t1(a,b);
  43. CREATE INDEX i1 ON t1(b ASC);
  44. }
  45. get_file_format
  46. } {4}
  47. do_test descidx2-1.2 {
  48. execsql {
  49. CREATE INDEX i2 ON t1(a DESC);
  50. }
  51. get_file_format
  52. } {4}
  53. # Before adding any information to the database, set the file format
  54. # back to three. Then close and reopen the database. With the file
  55. # format set to three, SQLite should ignore the DESC argument on the
  56. # index.
  57. #
  58. do_test descidx2-2.0 {
  59. set_file_format 3
  60. db close
  61. sqlite3 db test.db
  62. get_file_format
  63. } {3}
  64. # Put some information in the table and verify that the DESC
  65. # on the index is ignored.
  66. #
  67. do_test descidx2-2.1 {
  68. execsql {
  69. INSERT INTO t1 VALUES(1,1);
  70. INSERT INTO t1 VALUES(2,2);
  71. INSERT INTO t1 SELECT a+2, a+2 FROM t1;
  72. INSERT INTO t1 SELECT a+4, a+4 FROM t1;
  73. SELECT b FROM t1 WHERE a>3 AND a<7;
  74. }
  75. } {4 5 6}
  76. do_test descidx2-2.2 {
  77. execsql {
  78. SELECT a FROM t1 WHERE b>3 AND b<7;
  79. }
  80. } {4 5 6}
  81. do_test descidx2-2.3 {
  82. execsql {
  83. SELECT b FROM t1 WHERE a>=3 AND a<7;
  84. }
  85. } {3 4 5 6}
  86. do_test descidx2-2.4 {
  87. execsql {
  88. SELECT b FROM t1 WHERE a>3 AND a<=7;
  89. }
  90. } {4 5 6 7}
  91. do_test descidx2-2.5 {
  92. execsql {
  93. SELECT b FROM t1 WHERE a>=3 AND a<=7;
  94. }
  95. } {3 4 5 6 7}
  96. do_test descidx2-2.6 {
  97. execsql {
  98. SELECT a FROM t1 WHERE b>=3 AND b<=7;
  99. }
  100. } {3 4 5 6 7}
  101. # This procedure executes the SQL. Then it checks to see if the OP_Sort
  102. # opcode was executed. If an OP_Sort did occur, then "sort" is appended
  103. # to the result. If no OP_Sort happened, then "nosort" is appended.
  104. #
  105. # This procedure is used to check to make sure sorting is or is not
  106. # occurring as expected.
  107. #
  108. proc cksort {sql} {
  109. set ::sqlite_sort_count 0
  110. set data [execsql $sql]
  111. if {$::sqlite_sort_count} {set x sort} {set x nosort}
  112. lappend data $x
  113. return $data
  114. }
  115. # Test sorting using a descending index.
  116. #
  117. do_test descidx2-3.1 {
  118. cksort {SELECT a FROM t1 ORDER BY a}
  119. } {1 2 3 4 5 6 7 8 nosort}
  120. do_test descidx2-3.2 {
  121. cksort {SELECT a FROM t1 ORDER BY a ASC}
  122. } {1 2 3 4 5 6 7 8 nosort}
  123. do_test descidx2-3.3 {
  124. cksort {SELECT a FROM t1 ORDER BY a DESC}
  125. } {8 7 6 5 4 3 2 1 nosort}
  126. do_test descidx2-3.4 {
  127. cksort {SELECT b FROM t1 ORDER BY a}
  128. } {1 2 3 4 5 6 7 8 nosort}
  129. do_test descidx2-3.5 {
  130. cksort {SELECT b FROM t1 ORDER BY a ASC}
  131. } {1 2 3 4 5 6 7 8 nosort}
  132. do_test descidx2-3.6 {
  133. cksort {SELECT b FROM t1 ORDER BY a DESC}
  134. } {8 7 6 5 4 3 2 1 nosort}
  135. do_test descidx2-3.7 {
  136. cksort {SELECT a FROM t1 ORDER BY b}
  137. } {1 2 3 4 5 6 7 8 nosort}
  138. do_test descidx2-3.8 {
  139. cksort {SELECT a FROM t1 ORDER BY b ASC}
  140. } {1 2 3 4 5 6 7 8 nosort}
  141. do_test descidx2-3.9 {
  142. cksort {SELECT a FROM t1 ORDER BY b DESC}
  143. } {8 7 6 5 4 3 2 1 nosort}
  144. do_test descidx2-3.10 {
  145. cksort {SELECT b FROM t1 ORDER BY b}
  146. } {1 2 3 4 5 6 7 8 nosort}
  147. do_test descidx2-3.11 {
  148. cksort {SELECT b FROM t1 ORDER BY b ASC}
  149. } {1 2 3 4 5 6 7 8 nosort}
  150. do_test descidx2-3.12 {
  151. cksort {SELECT b FROM t1 ORDER BY b DESC}
  152. } {8 7 6 5 4 3 2 1 nosort}
  153. do_test descidx2-3.21 {
  154. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
  155. } {4 5 6 7 nosort}
  156. do_test descidx2-3.22 {
  157. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
  158. } {4 5 6 7 nosort}
  159. do_test descidx2-3.23 {
  160. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
  161. } {7 6 5 4 nosort}
  162. do_test descidx2-3.24 {
  163. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
  164. } {4 5 6 7 nosort}
  165. do_test descidx2-3.25 {
  166. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
  167. } {4 5 6 7 nosort}
  168. do_test descidx2-3.26 {
  169. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
  170. } {7 6 5 4 nosort}
  171. finish_test