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