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.6 2007/05/16 11:55:57 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19# Only run these tests if the build includes the CAST operator 20ifcapable !cast { 21 finish_test 22 return 23} 24 25# Tests for the CAST( AS blob), CAST( AS text) and CAST( AS numeric) built-ins 26# 27ifcapable bloblit { 28 do_test cast-1.1 { 29 execsql {SELECT x'616263'} 30 } abc 31 do_test cast-1.2 { 32 execsql {SELECT typeof(x'616263')} 33 } blob 34 do_test cast-1.3 { 35 execsql {SELECT CAST(x'616263' AS text)} 36 } abc 37 do_test cast-1.4 { 38 execsql {SELECT typeof(CAST(x'616263' AS text))} 39 } text 40 do_test cast-1.5 { 41 execsql {SELECT CAST(x'616263' AS numeric)} 42 } 0 43 do_test cast-1.6 { 44 execsql {SELECT typeof(CAST(x'616263' AS numeric))} 45 } integer 46 do_test cast-1.7 { 47 execsql {SELECT CAST(x'616263' AS blob)} 48 } abc 49 do_test cast-1.8 { 50 execsql {SELECT typeof(CAST(x'616263' AS blob))} 51 } blob 52 do_test cast-1.9 { 53 execsql {SELECT CAST(x'616263' AS integer)} 54 } 0 55 do_test cast-1.10 { 56 execsql {SELECT typeof(CAST(x'616263' AS integer))} 57 } integer 58} 59do_test cast-1.11 { 60 execsql {SELECT null} 61} {{}} 62do_test cast-1.12 { 63 execsql {SELECT typeof(NULL)} 64} null 65do_test cast-1.13 { 66 execsql {SELECT CAST(NULL AS text)} 67} {{}} 68do_test cast-1.14 { 69 execsql {SELECT typeof(CAST(NULL AS text))} 70} null 71do_test cast-1.15 { 72 execsql {SELECT CAST(NULL AS numeric)} 73} {{}} 74do_test cast-1.16 { 75 execsql {SELECT typeof(CAST(NULL AS numeric))} 76} null 77do_test cast-1.17 { 78 execsql {SELECT CAST(NULL AS blob)} 79} {{}} 80do_test cast-1.18 { 81 execsql {SELECT typeof(CAST(NULL AS blob))} 82} null 83do_test cast-1.19 { 84 execsql {SELECT CAST(NULL AS integer)} 85} {{}} 86do_test cast-1.20 { 87 execsql {SELECT typeof(CAST(NULL AS integer))} 88} null 89do_test cast-1.21 { 90 execsql {SELECT 123} 91} {123} 92do_test cast-1.22 { 93 execsql {SELECT typeof(123)} 94} integer 95do_test cast-1.23 { 96 execsql {SELECT CAST(123 AS text)} 97} {123} 98do_test cast-1.24 { 99 execsql {SELECT typeof(CAST(123 AS text))} 100} text 101do_test cast-1.25 { 102 execsql {SELECT CAST(123 AS numeric)} 103} 123 104do_test cast-1.26 { 105 execsql {SELECT typeof(CAST(123 AS numeric))} 106} integer 107do_test cast-1.27 { 108 execsql {SELECT CAST(123 AS blob)} 109} {123} 110do_test cast-1.28 { 111 execsql {SELECT typeof(CAST(123 AS blob))} 112} blob 113do_test cast-1.29 { 114 execsql {SELECT CAST(123 AS integer)} 115} {123} 116do_test cast-1.30 { 117 execsql {SELECT typeof(CAST(123 AS integer))} 118} integer 119do_test cast-1.31 { 120 execsql {SELECT 123.456} 121} {123.456} 122do_test cast-1.32 { 123 execsql {SELECT typeof(123.456)} 124} real 125do_test cast-1.33 { 126 execsql {SELECT CAST(123.456 AS text)} 127} {123.456} 128do_test cast-1.34 { 129 execsql {SELECT typeof(CAST(123.456 AS text))} 130} text 131do_test cast-1.35 { 132 execsql {SELECT CAST(123.456 AS numeric)} 133} 123.456 134do_test cast-1.36 { 135 execsql {SELECT typeof(CAST(123.456 AS numeric))} 136} real 137do_test cast-1.37 { 138 execsql {SELECT CAST(123.456 AS blob)} 139} {123.456} 140do_test cast-1.38 { 141 execsql {SELECT typeof(CAST(123.456 AS blob))} 142} blob 143do_test cast-1.39 { 144 execsql {SELECT CAST(123.456 AS integer)} 145} {123} 146do_test cast-1.38 { 147 execsql {SELECT typeof(CAST(123.456 AS integer))} 148} integer 149do_test cast-1.41 { 150 execsql {SELECT '123abc'} 151} {123abc} 152do_test cast-1.42 { 153 execsql {SELECT typeof('123abc')} 154} text 155do_test cast-1.43 { 156 execsql {SELECT CAST('123abc' AS text)} 157} {123abc} 158do_test cast-1.44 { 159 execsql {SELECT typeof(CAST('123abc' AS text))} 160} text 161do_test cast-1.45 { 162 execsql {SELECT CAST('123abc' AS numeric)} 163} 123 164do_test cast-1.46 { 165 execsql {SELECT typeof(CAST('123abc' AS numeric))} 166} integer 167do_test cast-1.47 { 168 execsql {SELECT CAST('123abc' AS blob)} 169} {123abc} 170do_test cast-1.48 { 171 execsql {SELECT typeof(CAST('123abc' AS blob))} 172} blob 173do_test cast-1.49 { 174 execsql {SELECT CAST('123abc' AS integer)} 175} 123 176do_test cast-1.50 { 177 execsql {SELECT typeof(CAST('123abc' AS integer))} 178} integer 179do_test cast-1.51 { 180 execsql {SELECT CAST('123.5abc' AS numeric)} 181} 123.5 182do_test cast-1.53 { 183 execsql {SELECT CAST('123.5abc' AS integer)} 184} 123 185 186# Ticket #1662. Ignore leading spaces in numbers when casting. 187# 188do_test cast-2.1 { 189 execsql {SELECT CAST(' 123' AS integer)} 190} 123 191do_test cast-2.2 { 192 execsql {SELECT CAST(' -123.456' AS real)} 193} -123.456 194 195# ticket #2364. Use full percision integers if possible when casting 196# to numeric. Do not fallback to real (and the corresponding 48-bit 197# mantissa) unless absolutely necessary. 198# 199do_test cast-3.1 { 200 execsql {SELECT CAST(9223372036854774800 AS integer)} 201} 9223372036854774800 202do_test cast-3.2 { 203 execsql {SELECT CAST(9223372036854774800 AS numeric)} 204} 9223372036854774800 205do_test cast-3.3 { 206 execsql {SELECT CAST(9223372036854774800 AS real)} 207} 9.22337203685477e+18 208do_test cast-3.4 { 209 execsql {SELECT CAST(CAST(9223372036854774800 AS real) AS integer)} 210} 9223372036854774784 211do_test cast-3.5 { 212 execsql {SELECT CAST(-9223372036854774800 AS integer)} 213} -9223372036854774800 214do_test cast-3.6 { 215 execsql {SELECT CAST(-9223372036854774800 AS numeric)} 216} -9223372036854774800 217do_test cast-3.7 { 218 execsql {SELECT CAST(-9223372036854774800 AS real)} 219} -9.22337203685477e+18 220do_test cast-3.8 { 221 execsql {SELECT CAST(CAST(-9223372036854774800 AS real) AS integer)} 222} -9223372036854774784 223do_test cast-3.11 { 224 execsql {SELECT CAST('9223372036854774800' AS integer)} 225} 9223372036854774800 226do_test cast-3.12 { 227 execsql {SELECT CAST('9223372036854774800' AS numeric)} 228} 9223372036854774800 229do_test cast-3.13 { 230 execsql {SELECT CAST('9223372036854774800' AS real)} 231} 9.22337203685477e+18 232do_test cast-3.14 { 233 execsql {SELECT CAST(CAST('9223372036854774800' AS real) AS integer)} 234} 9223372036854774784 235do_test cast-3.15 { 236 execsql {SELECT CAST('-9223372036854774800' AS integer)} 237} -9223372036854774800 238do_test cast-3.16 { 239 execsql {SELECT CAST('-9223372036854774800' AS numeric)} 240} -9223372036854774800 241do_test cast-3.17 { 242 execsql {SELECT CAST('-9223372036854774800' AS real)} 243} -9.22337203685477e+18 244do_test cast-3.18 { 245 execsql {SELECT CAST(CAST('-9223372036854774800' AS real) AS integer)} 246} -9223372036854774784 247if {[db eval {PRAGMA encoding}]=="UTF-8"} { 248 do_test cast-3.21 { 249 execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS integer)} 250 } 9223372036854774800 251 do_test cast-3.22 { 252 execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS numeric)} 253 } 9223372036854774800 254 do_test cast-3.23 { 255 execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS real)} 256 } 9.22337203685477e+18 257 do_test cast-3.24 { 258 execsql {SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real) AS integer)} 259 } 9223372036854774784 260} 261do_test case-3.31 { 262 execsql {SELECT CAST(NULL AS numeric)} 263} {{}} 264 265 266finish_test 267