cast.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # 2005 June 25
  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 CAST operator.
  13. #
  14. # $Id: cast.test,v 1.10 2008/11/06 15:33:04 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Only run these tests if the build includes the CAST operator
  18. ifcapable !cast {
  19. finish_test
  20. return
  21. }
  22. # Tests for the CAST( AS blob), CAST( AS text) and CAST( AS numeric) built-ins
  23. #
  24. ifcapable bloblit {
  25. do_test cast-1.1 {
  26. execsql {SELECT x'616263'}
  27. } abc
  28. do_test cast-1.2 {
  29. execsql {SELECT typeof(x'616263')}
  30. } blob
  31. do_test cast-1.3 {
  32. execsql {SELECT CAST(x'616263' AS text)}
  33. } abc
  34. do_test cast-1.4 {
  35. execsql {SELECT typeof(CAST(x'616263' AS text))}
  36. } text
  37. do_test cast-1.5 {
  38. execsql {SELECT CAST(x'616263' AS numeric)}
  39. } 0
  40. do_test cast-1.6 {
  41. execsql {SELECT typeof(CAST(x'616263' AS numeric))}
  42. } integer
  43. do_test cast-1.7 {
  44. execsql {SELECT CAST(x'616263' AS blob)}
  45. } abc
  46. do_test cast-1.8 {
  47. execsql {SELECT typeof(CAST(x'616263' AS blob))}
  48. } blob
  49. do_test cast-1.9 {
  50. execsql {SELECT CAST(x'616263' AS integer)}
  51. } 0
  52. do_test cast-1.10 {
  53. execsql {SELECT typeof(CAST(x'616263' AS integer))}
  54. } integer
  55. }
  56. do_test cast-1.11 {
  57. execsql {SELECT null}
  58. } {{}}
  59. do_test cast-1.12 {
  60. execsql {SELECT typeof(NULL)}
  61. } null
  62. do_test cast-1.13 {
  63. execsql {SELECT CAST(NULL AS text)}
  64. } {{}}
  65. do_test cast-1.14 {
  66. execsql {SELECT typeof(CAST(NULL AS text))}
  67. } null
  68. do_test cast-1.15 {
  69. execsql {SELECT CAST(NULL AS numeric)}
  70. } {{}}
  71. do_test cast-1.16 {
  72. execsql {SELECT typeof(CAST(NULL AS numeric))}
  73. } null
  74. do_test cast-1.17 {
  75. execsql {SELECT CAST(NULL AS blob)}
  76. } {{}}
  77. do_test cast-1.18 {
  78. execsql {SELECT typeof(CAST(NULL AS blob))}
  79. } null
  80. do_test cast-1.19 {
  81. execsql {SELECT CAST(NULL AS integer)}
  82. } {{}}
  83. do_test cast-1.20 {
  84. execsql {SELECT typeof(CAST(NULL AS integer))}
  85. } null
  86. do_test cast-1.21 {
  87. execsql {SELECT 123}
  88. } {123}
  89. do_test cast-1.22 {
  90. execsql {SELECT typeof(123)}
  91. } integer
  92. do_test cast-1.23 {
  93. execsql {SELECT CAST(123 AS text)}
  94. } {123}
  95. do_test cast-1.24 {
  96. execsql {SELECT typeof(CAST(123 AS text))}
  97. } text
  98. do_test cast-1.25 {
  99. execsql {SELECT CAST(123 AS numeric)}
  100. } 123
  101. do_test cast-1.26 {
  102. execsql {SELECT typeof(CAST(123 AS numeric))}
  103. } integer
  104. do_test cast-1.27 {
  105. execsql {SELECT CAST(123 AS blob)}
  106. } {123}
  107. do_test cast-1.28 {
  108. execsql {SELECT typeof(CAST(123 AS blob))}
  109. } blob
  110. do_test cast-1.29 {
  111. execsql {SELECT CAST(123 AS integer)}
  112. } {123}
  113. do_test cast-1.30 {
  114. execsql {SELECT typeof(CAST(123 AS integer))}
  115. } integer
  116. do_test cast-1.31 {
  117. execsql {SELECT 123.456}
  118. } {123.456}
  119. do_test cast-1.32 {
  120. execsql {SELECT typeof(123.456)}
  121. } real
  122. do_test cast-1.33 {
  123. execsql {SELECT CAST(123.456 AS text)}
  124. } {123.456}
  125. do_test cast-1.34 {
  126. execsql {SELECT typeof(CAST(123.456 AS text))}
  127. } text
  128. do_test cast-1.35 {
  129. execsql {SELECT CAST(123.456 AS numeric)}
  130. } 123.456
  131. do_test cast-1.36 {
  132. execsql {SELECT typeof(CAST(123.456 AS numeric))}
  133. } real
  134. do_test cast-1.37 {
  135. execsql {SELECT CAST(123.456 AS blob)}
  136. } {123.456}
  137. do_test cast-1.38 {
  138. execsql {SELECT typeof(CAST(123.456 AS blob))}
  139. } blob
  140. do_test cast-1.39 {
  141. execsql {SELECT CAST(123.456 AS integer)}
  142. } {123}
  143. do_test cast-1.38 {
  144. execsql {SELECT typeof(CAST(123.456 AS integer))}
  145. } integer
  146. do_test cast-1.41 {
  147. execsql {SELECT '123abc'}
  148. } {123abc}
  149. do_test cast-1.42 {
  150. execsql {SELECT typeof('123abc')}
  151. } text
  152. do_test cast-1.43 {
  153. execsql {SELECT CAST('123abc' AS text)}
  154. } {123abc}
  155. do_test cast-1.44 {
  156. execsql {SELECT typeof(CAST('123abc' AS text))}
  157. } text
  158. do_test cast-1.45 {
  159. execsql {SELECT CAST('123abc' AS numeric)}
  160. } 123
  161. do_test cast-1.46 {
  162. execsql {SELECT typeof(CAST('123abc' AS numeric))}
  163. } integer
  164. do_test cast-1.47 {
  165. execsql {SELECT CAST('123abc' AS blob)}
  166. } {123abc}
  167. do_test cast-1.48 {
  168. execsql {SELECT typeof(CAST('123abc' AS blob))}
  169. } blob
  170. do_test cast-1.49 {
  171. execsql {SELECT CAST('123abc' AS integer)}
  172. } 123
  173. do_test cast-1.50 {
  174. execsql {SELECT typeof(CAST('123abc' AS integer))}
  175. } integer
  176. do_test cast-1.51 {
  177. execsql {SELECT CAST('123.5abc' AS numeric)}
  178. } 123.5
  179. do_test cast-1.53 {
  180. execsql {SELECT CAST('123.5abc' AS integer)}
  181. } 123
  182. do_test case-1.60 {
  183. execsql {SELECT CAST(null AS REAL)}
  184. } {{}}
  185. do_test case-1.61 {
  186. execsql {SELECT typeof(CAST(null AS REAL))}
  187. } {null}
  188. do_test case-1.62 {
  189. execsql {SELECT CAST(1 AS REAL)}
  190. } {1.0}
  191. do_test case-1.63 {
  192. execsql {SELECT typeof(CAST(1 AS REAL))}
  193. } {real}
  194. do_test case-1.64 {
  195. execsql {SELECT CAST('1' AS REAL)}
  196. } {1.0}
  197. do_test case-1.65 {
  198. execsql {SELECT typeof(CAST('1' AS REAL))}
  199. } {real}
  200. do_test case-1.66 {
  201. execsql {SELECT CAST('abc' AS REAL)}
  202. } {0.0}
  203. do_test case-1.67 {
  204. execsql {SELECT typeof(CAST('abc' AS REAL))}
  205. } {real}
  206. do_test case-1.68 {
  207. execsql {SELECT CAST(x'31' AS REAL)}
  208. } {1.0}
  209. do_test case-1.69 {
  210. execsql {SELECT typeof(CAST(x'31' AS REAL))}
  211. } {real}
  212. # Ticket #1662. Ignore leading spaces in numbers when casting.
  213. #
  214. do_test cast-2.1 {
  215. execsql {SELECT CAST(' 123' AS integer)}
  216. } 123
  217. do_test cast-2.2 {
  218. execsql {SELECT CAST(' -123.456' AS real)}
  219. } -123.456
  220. # ticket #2364. Use full percision integers if possible when casting
  221. # to numeric. Do not fallback to real (and the corresponding 48-bit
  222. # mantissa) unless absolutely necessary.
  223. #
  224. do_test cast-3.1 {
  225. execsql {SELECT CAST(9223372036854774800 AS integer)}
  226. } 9223372036854774800
  227. do_test cast-3.2 {
  228. execsql {SELECT CAST(9223372036854774800 AS numeric)}
  229. } 9223372036854774800
  230. do_realnum_test cast-3.3 {
  231. execsql {SELECT CAST(9223372036854774800 AS real)}
  232. } 9.22337203685477e+18
  233. do_test cast-3.4 {
  234. execsql {SELECT CAST(CAST(9223372036854774800 AS real) AS integer)}
  235. } 9223372036854774784
  236. do_test cast-3.5 {
  237. execsql {SELECT CAST(-9223372036854774800 AS integer)}
  238. } -9223372036854774800
  239. do_test cast-3.6 {
  240. execsql {SELECT CAST(-9223372036854774800 AS numeric)}
  241. } -9223372036854774800
  242. do_realnum_test cast-3.7 {
  243. execsql {SELECT CAST(-9223372036854774800 AS real)}
  244. } -9.22337203685477e+18
  245. do_test cast-3.8 {
  246. execsql {SELECT CAST(CAST(-9223372036854774800 AS real) AS integer)}
  247. } -9223372036854774784
  248. do_test cast-3.11 {
  249. execsql {SELECT CAST('9223372036854774800' AS integer)}
  250. } 9223372036854774800
  251. do_test cast-3.12 {
  252. execsql {SELECT CAST('9223372036854774800' AS numeric)}
  253. } 9223372036854774800
  254. do_realnum_test cast-3.13 {
  255. execsql {SELECT CAST('9223372036854774800' AS real)}
  256. } 9.22337203685477e+18
  257. ifcapable long_double {
  258. do_test cast-3.14 {
  259. execsql {SELECT CAST(CAST('9223372036854774800' AS real) AS integer)}
  260. } 9223372036854774784
  261. }
  262. do_test cast-3.15 {
  263. execsql {SELECT CAST('-9223372036854774800' AS integer)}
  264. } -9223372036854774800
  265. do_test cast-3.16 {
  266. execsql {SELECT CAST('-9223372036854774800' AS numeric)}
  267. } -9223372036854774800
  268. do_realnum_test cast-3.17 {
  269. execsql {SELECT CAST('-9223372036854774800' AS real)}
  270. } -9.22337203685477e+18
  271. ifcapable long_double {
  272. do_test cast-3.18 {
  273. execsql {SELECT CAST(CAST('-9223372036854774800' AS real) AS integer)}
  274. } -9223372036854774784
  275. }
  276. if {[db eval {PRAGMA encoding}]=="UTF-8"} {
  277. do_test cast-3.21 {
  278. execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS integer)}
  279. } 9223372036854774800
  280. do_test cast-3.22 {
  281. execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS numeric)}
  282. } 9223372036854774800
  283. do_realnum_test cast-3.23 {
  284. execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS real)}
  285. } 9.22337203685477e+18
  286. ifcapable long_double {
  287. do_test cast-3.24 {
  288. execsql {
  289. SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real)
  290. AS integer)
  291. }
  292. } 9223372036854774784
  293. }
  294. }
  295. do_test case-3.31 {
  296. execsql {SELECT CAST(NULL AS numeric)}
  297. } {{}}
  298. # Test to see if it is possible to trick SQLite into reading past
  299. # the end of a blob when converting it to a number.
  300. do_test cast-3.32.1 {
  301. set blob "1234567890"
  302. set DB [sqlite3_connection_pointer db]
  303. set ::STMT [sqlite3_prepare $DB {SELECT CAST(? AS real)} -1 TAIL]
  304. sqlite3_bind_blob -static $::STMT 1 $blob 5
  305. sqlite3_step $::STMT
  306. } {SQLITE_ROW}
  307. do_test cast-3.32.2 {
  308. sqlite3_column_int $::STMT 0
  309. } {12345}
  310. do_test cast-3.32.3 {
  311. sqlite3_finalize $::STMT
  312. } {SQLITE_OK}
  313. do_test cast-4.1 {
  314. db eval {
  315. CREATE TABLE t1(a);
  316. INSERT INTO t1 VALUES('abc');
  317. SELECT a, CAST(a AS integer) FROM t1;
  318. }
  319. } {abc 0}
  320. do_test cast-4.2 {
  321. db eval {
  322. SELECT CAST(a AS integer), a FROM t1;
  323. }
  324. } {0 abc}
  325. do_test cast-4.3 {
  326. db eval {
  327. SELECT a, CAST(a AS integer), a FROM t1;
  328. }
  329. } {abc 0 abc}
  330. do_test cast-4.4 {
  331. db eval {
  332. SELECT CAST(a AS integer), a, CAST(a AS real), a FROM t1;
  333. }
  334. } {0 abc 0.0 abc}
  335. finish_test