sort.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # 2001 September 15
  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 file is testing the CREATE TABLE statement.
  13. #
  14. # $Id: sort.test,v 1.4 2002/01/22 14:11:30 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a bunch of data to sort against
  18. #
  19. do_test sort-1.0 {
  20. set fd [open data.txt w]
  21. puts $fd "1\tone\t0\tI\t3.141592653"
  22. puts $fd "2\ttwo\t1\tII\t2.15"
  23. puts $fd "3\tthree\t1\tIII\t4221.0"
  24. puts $fd "4\tfour\t2\tIV\t-0.0013442"
  25. puts $fd "5\tfive\t2\tV\t-11"
  26. puts $fd "6\tsix\t2\tVI\t0.123"
  27. puts $fd "7\tseven\t2\tVII\t123.0"
  28. puts $fd "8\teight\t3\tVIII\t-1.6"
  29. close $fd
  30. execsql {
  31. CREATE TABLE t1(
  32. n int,
  33. v varchar(10),
  34. log int,
  35. roman varchar(10),
  36. flt real
  37. );
  38. COPY t1 FROM 'data.txt'
  39. }
  40. file delete data.txt
  41. execsql {SELECT count(*) FROM t1}
  42. } {8}
  43. do_test sort-1.1 {
  44. execsql {SELECT n FROM t1 ORDER BY n}
  45. } {1 2 3 4 5 6 7 8}
  46. do_test sort-1.1.1 {
  47. execsql {SELECT n FROM t1 ORDER BY n ASC}
  48. } {1 2 3 4 5 6 7 8}
  49. do_test sort-1.1.1 {
  50. execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
  51. } {1 2 3 4 5 6 7 8}
  52. do_test sort-1.2 {
  53. execsql {SELECT n FROM t1 ORDER BY n DESC}
  54. } {8 7 6 5 4 3 2 1}
  55. do_test sort-1.3a {
  56. execsql {SELECT v FROM t1 ORDER BY v}
  57. } {eight five four one seven six three two}
  58. do_test sort-1.3b {
  59. execsql {SELECT n FROM t1 ORDER BY v}
  60. } {8 5 4 1 7 6 3 2}
  61. do_test sort-1.4 {
  62. execsql {SELECT n FROM t1 ORDER BY v DESC}
  63. } {2 3 6 7 1 4 5 8}
  64. do_test sort-1.5 {
  65. execsql {SELECT flt FROM t1 ORDER BY flt}
  66. } {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
  67. do_test sort-1.6 {
  68. execsql {SELECT flt FROM t1 ORDER BY flt DESC}
  69. } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
  70. do_test sort-1.7 {
  71. execsql {SELECT roman FROM t1 ORDER BY roman}
  72. } {I II III IV V VI VII VIII}
  73. do_test sort-1.8 {
  74. execsql {SELECT n FROM t1 ORDER BY log, flt}
  75. } {1 2 3 5 4 6 7 8}
  76. do_test sort-1.8.1 {
  77. execsql {SELECT n FROM t1 ORDER BY log asc, flt}
  78. } {1 2 3 5 4 6 7 8}
  79. do_test sort-1.8.2 {
  80. execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
  81. } {1 2 3 5 4 6 7 8}
  82. do_test sort-1.8.3 {
  83. execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
  84. } {1 2 3 5 4 6 7 8}
  85. do_test sort-1.9 {
  86. execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
  87. } {1 3 2 7 6 4 5 8}
  88. do_test sort-1.9.1 {
  89. execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
  90. } {1 3 2 7 6 4 5 8}
  91. do_test sort-1.10 {
  92. execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
  93. } {8 5 4 6 7 2 3 1}
  94. do_test sort-1.11 {
  95. execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
  96. } {8 7 6 4 5 3 2 1}
  97. # These tests are designed to reach some hard-to-reach places
  98. # inside the string comparison routines.
  99. #
  100. do_test sort-2.1 {
  101. execsql {
  102. UPDATE t1 SET v='x' || -flt;
  103. UPDATE t1 SET v='x-2b' where v=='x-0.123';
  104. SELECT v FROM t1 ORDER BY v;
  105. }
  106. } {x-2b x-2.15 x-3.141592653 x-123 x-4221 x0.0013442 x1.6 x11}
  107. do_test sort-2.2 {
  108. execsql {
  109. UPDATE t1 SET v='x-2_' where v=='x0.0013442';
  110. SELECT v FROM t1 ORDER BY v;
  111. }
  112. } {x-2_ x-2b x-2.15 x-3.141592653 x-123 x-4221 x1.6 x11}
  113. do_test sort-2.3 {
  114. execsql {
  115. UPDATE t1 SET v='x ' || (-1.3+0.01*n);
  116. SELECT v FROM t1 ORDER BY v;
  117. }
  118. } {{x -1.29} {x -1.28} {x -1.27} {x -1.26} {x -1.25} {x -1.24} {x -1.23} {x -1.22}}
  119. # This is a bug fix for 2.2.4.
  120. # Strings are normally mapped to upper-case for a caseless comparison.
  121. # But this can cause problems for characters in between 'Z' and 'a'.
  122. #
  123. do_test sort-3.1 {
  124. execsql {
  125. CREATE TABLE t2(a,b);
  126. INSERT INTO t2 VALUES('AGLIENTU',1);
  127. INSERT INTO t2 VALUES('AGLIE`',2);
  128. INSERT INTO t2 VALUES('AGNA',3);
  129. SELECT a, b FROM t2 ORDER BY a;
  130. }
  131. } {AGLIENTU 1 AGLIE` 2 AGNA 3}
  132. do_test sort-3.2 {
  133. execsql {
  134. SELECT a, b FROM t2 ORDER BY a DESC;
  135. }
  136. } {AGNA 3 AGLIE` 2 AGLIENTU 1}
  137. do_test sort-3.3 {
  138. execsql {
  139. DELETE FROM t2;
  140. INSERT INTO t2 VALUES('aglientu',1);
  141. INSERT INTO t2 VALUES('aglie`',2);
  142. INSERT INTO t2 VALUES('agna',3);
  143. SELECT a, b FROM t2 ORDER BY a;
  144. }
  145. } {aglie` 2 aglientu 1 agna 3}
  146. do_test sort-3.4 {
  147. execsql {
  148. SELECT a, b FROM t2 ORDER BY a DESC;
  149. }
  150. } {agna 3 aglientu 1 aglie` 2}
  151. finish_test