descidx3.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # 2006 January 02
  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: descidx3.test,v 1.6 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. ifcapable !bloblit {
  23. finish_test
  24. return
  25. }
  26. db eval {PRAGMA legacy_file_format=OFF}
  27. # This procedure sets the value of the file-format in file 'test.db'
  28. # to $newval. Also, the schema cookie is incremented.
  29. #
  30. proc set_file_format {newval} {
  31. hexio_write test.db 44 [hexio_render_int32 $newval]
  32. set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  33. incr schemacookie
  34. hexio_write test.db 40 [hexio_render_int32 $schemacookie]
  35. return {}
  36. }
  37. # This procedure returns the value of the file-format in file 'test.db'.
  38. #
  39. proc get_file_format {{fname test.db}} {
  40. return [hexio_get_int [hexio_read $fname 44 4]]
  41. }
  42. # Verify that the file format starts as 4.
  43. #
  44. do_test descidx3-1.1 {
  45. execsql {
  46. CREATE TABLE t1(i INTEGER PRIMARY KEY,a,b,c,d);
  47. CREATE INDEX t1i1 ON t1(a DESC, b ASC, c DESC);
  48. CREATE INDEX t1i2 ON t1(b DESC, c ASC, d DESC);
  49. }
  50. get_file_format
  51. } {4}
  52. # Put some information in the table and verify that the descending
  53. # index actually works.
  54. #
  55. do_test descidx3-2.1 {
  56. execsql {
  57. INSERT INTO t1 VALUES(1, NULL, NULL, NULL, NULL);
  58. INSERT INTO t1 VALUES(2, 2, 2, 2, 2);
  59. INSERT INTO t1 VALUES(3, 3, 3, 3, 3);
  60. INSERT INTO t1 VALUES(4, 2.5, 2.5, 2.5, 2.5);
  61. INSERT INTO t1 VALUES(5, -5, -5, -5, -5);
  62. INSERT INTO t1 VALUES(6, 'six', 'six', 'six', 'six');
  63. INSERT INTO t1 VALUES(7, x'77', x'77', x'77', x'77');
  64. INSERT INTO t1 VALUES(8, 'eight', 'eight', 'eight', 'eight');
  65. INSERT INTO t1 VALUES(9, x'7979', x'7979', x'7979', x'7979');
  66. SELECT count(*) FROM t1;
  67. }
  68. } 9
  69. do_test descidx3-2.2 {
  70. execsql {
  71. SELECT i FROM t1 ORDER BY a;
  72. }
  73. } {1 5 2 4 3 8 6 7 9}
  74. do_test descidx3-2.3 {
  75. execsql {
  76. SELECT i FROM t1 ORDER BY a DESC;
  77. }
  78. } {9 7 6 8 3 4 2 5 1}
  79. # The "natural" order for the index is decreasing
  80. do_test descidx3-2.4 {
  81. execsql {
  82. SELECT i FROM t1 WHERE a<=x'7979';
  83. }
  84. } {9 7 6 8 3 4 2 5}
  85. do_test descidx3-2.5 {
  86. execsql {
  87. SELECT i FROM t1 WHERE a>-99;
  88. }
  89. } {9 7 6 8 3 4 2 5}
  90. # Even when all values of t1.a are the same, sorting by A returns
  91. # the rows in reverse order because this the natural order of the
  92. # index.
  93. #
  94. do_test descidx3-3.1 {
  95. execsql {
  96. UPDATE t1 SET a=1;
  97. SELECT i FROM t1 ORDER BY a;
  98. }
  99. } {9 7 6 8 3 4 2 5 1}
  100. do_test descidx3-3.2 {
  101. execsql {
  102. SELECT i FROM t1 WHERE a=1 AND b>0 AND b<'zzz'
  103. }
  104. } {2 4 3 8 6}
  105. do_test descidx3-3.3 {
  106. execsql {
  107. SELECT i FROM t1 WHERE b>0 AND b<'zzz'
  108. }
  109. } {6 8 3 4 2}
  110. do_test descidx3-3.4 {
  111. execsql {
  112. SELECT i FROM t1 WHERE a=1 AND b>-9999 AND b<x'ffffffff'
  113. }
  114. } {5 2 4 3 8 6 7 9}
  115. do_test descidx3-3.5 {
  116. execsql {
  117. SELECT i FROM t1 WHERE b>-9999 AND b<x'ffffffff'
  118. }
  119. } {9 7 6 8 3 4 2 5}
  120. ifcapable subquery {
  121. # If the subquery capability is not compiled in to the binary, then
  122. # the IN(...) operator is not available. Hence these tests cannot be
  123. # run.
  124. do_test descidx3-4.1 {
  125. lsort [execsql {
  126. UPDATE t1 SET a=2 WHERE i<6;
  127. SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
  128. }]
  129. } {2 3 4 6 8}
  130. do_test descidx3-4.2 {
  131. execsql {
  132. UPDATE t1 SET a=1;
  133. SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
  134. }
  135. } {2 4 3 8 6}
  136. do_test descidx3-4.3 {
  137. execsql {
  138. UPDATE t1 SET b=2;
  139. SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
  140. }
  141. } {9 7 6 8 3 4 2 5 1}
  142. }
  143. finish_test