func.test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 built-in functions.
  13. #
  14. # $Id: func.test,v 1.12 2002/05/29 23:22:23 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a table to work with.
  18. #
  19. do_test func-0.0 {
  20. execsql {CREATE TABLE tbl1(t1 text)}
  21. foreach word {this program is free software} {
  22. execsql "INSERT INTO tbl1 VALUES('$word')"
  23. }
  24. execsql {SELECT t1 FROM tbl1 ORDER BY t1}
  25. } {free is program software this}
  26. do_test func-0.1 {
  27. execsql {
  28. CREATE TABLE t2(a);
  29. INSERT INTO t2 VALUES(1);
  30. INSERT INTO t2 VALUES(NULL);
  31. INSERT INTO t2 VALUES(345);
  32. INSERT INTO t2 VALUES(NULL);
  33. INSERT INTO t2 VALUES(67890);
  34. SELECT * FROM t2;
  35. }
  36. } {1 {} 345 {} 67890}
  37. # Check out the length() function
  38. #
  39. do_test func-1.0 {
  40. execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
  41. } {4 2 7 8 4}
  42. do_test func-1.1 {
  43. set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
  44. lappend r $msg
  45. } {1 {wrong number of arguments to function length()}}
  46. do_test func-1.2 {
  47. set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
  48. lappend r $msg
  49. } {1 {wrong number of arguments to function length()}}
  50. do_test func-1.3 {
  51. execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
  52. ORDER BY length(t1)}
  53. } {2 1 4 2 7 1 8 1}
  54. do_test func-1.4 {
  55. execsql {SELECT coalesce(length(a),-1) FROM t2}
  56. } {1 -1 3 -1 5}
  57. # Check out the substr() function
  58. #
  59. do_test func-2.0 {
  60. execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
  61. } {fr is pr so th}
  62. do_test func-2.1 {
  63. execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
  64. } {r s r o h}
  65. do_test func-2.2 {
  66. execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
  67. } {ee {} ogr ftw is}
  68. do_test func-2.3 {
  69. execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
  70. } {e s m e s}
  71. do_test func-2.4 {
  72. execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
  73. } {e s m e s}
  74. do_test func-2.5 {
  75. execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
  76. } {e i a r i}
  77. do_test func-2.6 {
  78. execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
  79. } {ee is am re is}
  80. do_test func-2.7 {
  81. execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
  82. } {fr {} gr wa th}
  83. do_test func-2.8 {
  84. execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
  85. } {this software free program is}
  86. do_test func-2.9 {
  87. execsql {SELECT substr(a,1,1) FROM t2}
  88. } {1 {} 3 {} 6}
  89. do_test func-2.10 {
  90. execsql {SELECT substr(a,2,2) FROM t2}
  91. } {{} {} 45 {} 78}
  92. # Only do the following tests if TCL has UTF-8 capabilities and
  93. # the UTF-8 encoding is turned on in the SQLite library.
  94. #
  95. if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
  96. # Put some UTF-8 characters in the database
  97. #
  98. do_test func-3.0 {
  99. execsql {DELETE FROM tbl1}
  100. foreach word "contains UTF-8 characters hi\u1234ho" {
  101. execsql "INSERT INTO tbl1 VALUES('$word')"
  102. }
  103. execsql {SELECT t1 FROM tbl1 ORDER BY t1}
  104. } "characters contains hi\u1234ho UTF-8"
  105. do_test func-3.1 {
  106. execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
  107. } {10 8 5 5}
  108. do_test func-3.2 {
  109. execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
  110. } {ch co hi UT}
  111. do_test func-3.3 {
  112. execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
  113. } "cha con hi\u1234 UTF"
  114. do_test func-3.4 {
  115. execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
  116. } "ha on i\u1234 TF"
  117. do_test func-3.5 {
  118. execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
  119. } "har ont i\u1234h TF-"
  120. do_test func-3.6 {
  121. execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
  122. } "ar nt \u1234h F-"
  123. do_test func-3.7 {
  124. execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
  125. } "ra ta ho -8"
  126. do_test func-3.8 {
  127. execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
  128. } "s s o 8"
  129. do_test func-3.9 {
  130. execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
  131. } "er in \u1234h F-"
  132. do_test func-3.10 {
  133. execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
  134. } "ter ain i\u1234h TF-"
  135. do_test func-3.99 {
  136. execsql {DELETE FROM tbl1}
  137. foreach word {this program is free software} {
  138. execsql "INSERT INTO tbl1 VALUES('$word')"
  139. }
  140. execsql {SELECT t1 FROM tbl1}
  141. } {this program is free software}
  142. } ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
  143. # Test the abs() and round() functions.
  144. #
  145. do_test func-4.1 {
  146. execsql {
  147. CREATE TABLE t1(a,b,c);
  148. INSERT INTO t1 VALUES(1,2,3);
  149. INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
  150. INSERT INTO t1 VALUES(3,-2,-5);
  151. }
  152. catchsql {SELECT abs(a,b) FROM t1}
  153. } {1 {wrong number of arguments to function abs()}}
  154. do_test func-4.2 {
  155. catchsql {SELECT abs() FROM t1}
  156. } {1 {wrong number of arguments to function abs()}}
  157. do_test func-4.3 {
  158. catchsql {SELECT abs(b) FROM t1 ORDER BY a}
  159. } {0 {2 1.2345678901234 2}}
  160. do_test func-4.4 {
  161. catchsql {SELECT abs(c) FROM t1 ORDER BY a}
  162. } {0 {3 12345.67890 5}}
  163. do_test func-4.4.1 {
  164. execsql {SELECT abs(a) FROM t2}
  165. } {1 {} 345 {} 67890}
  166. do_test func-4.4.2 {
  167. execsql {SELECT abs(t1) FROM tbl1}
  168. } {this program is free software}
  169. do_test func-4.5 {
  170. catchsql {SELECT round(a,b,c) FROM t1}
  171. } {1 {wrong number of arguments to function round()}}
  172. do_test func-4.6 {
  173. catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
  174. } {0 {-2.00 1.23 2.00}}
  175. do_test func-4.7 {
  176. catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
  177. } {0 {2 1 -2}}
  178. do_test func-4.8 {
  179. catchsql {SELECT round(c) FROM t1 ORDER BY a}
  180. } {0 {3 -12346 -5}}
  181. do_test func-4.9 {
  182. catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
  183. } {0 {3.0 -12345.68 -5.000}}
  184. do_test func-4.10 {
  185. catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
  186. } {0 {x3.0y x-12345.68y x-5.000y}}
  187. do_test func-4.11 {
  188. catchsql {SELECT round() FROM t1 ORDER BY a}
  189. } {1 {wrong number of arguments to function round()}}
  190. do_test func-4.12 {
  191. execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
  192. } {1.00 nil 345.00 nil 67890.00}
  193. do_test func-4.13 {
  194. execsql {SELECT round(t1,2) FROM tbl1}
  195. } {0.00 0.00 0.00 0.00 0.00}
  196. # Test the upper() and lower() functions
  197. #
  198. do_test func-5.1 {
  199. execsql {SELECT upper(t1) FROM tbl1}
  200. } {THIS PROGRAM IS FREE SOFTWARE}
  201. do_test func-5.2 {
  202. execsql {SELECT lower(upper(t1)) FROM tbl1}
  203. } {this program is free software}
  204. do_test func-5.3 {
  205. execsql {SELECT upper(a), lower(a) FROM t2}
  206. } {1 1 {} {} 345 345 {} {} 67890 67890}
  207. do_test func-5.4 {
  208. catchsql {SELECT upper(a,5) FROM t2}
  209. } {1 {wrong number of arguments to function upper()}}
  210. do_test func-5.5 {
  211. catchsql {SELECT upper(*) FROM t2}
  212. } {1 {wrong number of arguments to function upper()}}
  213. # Test the coalesce() function
  214. #
  215. do_test func-6.1 {
  216. execsql {SELECT coalesce(a,'xyz') FROM t2}
  217. } {1 xyz 345 xyz 67890}
  218. do_test func-6.2 {
  219. execsql {SELECT coalesce(upper(a),'nil') FROM t2}
  220. } {1 nil 345 nil 67890}
  221. # Test the last_insert_rowid() function
  222. #
  223. do_test func-7.1 {
  224. execsql {SELECT last_insert_rowid()}
  225. } [db last_insert_rowid]
  226. # Tests for aggregate functions and how they handle NULLs.
  227. #
  228. do_test func-8.1 {
  229. execsql {
  230. SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
  231. }
  232. } {68236 3 22745.33 1 67890 5}
  233. finish_test