1 # 2009 November 11 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 15 set testdir [file dirname $argv0] 16 source $testdir/tester.tcl 17 18 # Test plan: 19 # 20 # func2-1.*: substr implementation (ascii) 21 # func2-2.*: substr implementation (utf8) 22 # func2-3.*: substr implementation (blob) 23 # 24 25 proc bin_to_hex {blob} { 26 set bytes {} 27 binary scan $blob \c* bytes 28 set bytes2 [list] 29 foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]} 30 join $bytes2 {} 31 } 32 33 #---------------------------------------------------------------------------- 34 # Test cases func2-1.*: substr implementation (ascii) 35 # 36 37 do_test func2-1.1 { 38 execsql {SELECT 'Supercalifragilisticexpialidocious'} 39 } {Supercalifragilisticexpialidocious} 40 41 # substr(x,y), substr(x,y,z) 42 do_test func2-1.2.1 { 43 catchsql {SELECT SUBSTR()} 44 } {1 {wrong number of arguments to function SUBSTR()}} 45 do_test func2-1.2.2 { 46 catchsql {SELECT SUBSTR('Supercalifragilisticexpialidocious')} 47 } {1 {wrong number of arguments to function SUBSTR()}} 48 do_test func2-1.2.3 { 49 catchsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1,1,1)} 50 } {1 {wrong number of arguments to function SUBSTR()}} 51 52 # p1 is 1-indexed 53 do_test func2-1.3 { 54 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0)} 55 } {Supercalifragilisticexpialidocious} 56 do_test func2-1.4 { 57 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1)} 58 } {Supercalifragilisticexpialidocious} 59 do_test func2-1.5 { 60 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2)} 61 } {upercalifragilisticexpialidocious} 62 do_test func2-1.6 { 63 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30)} 64 } {cious} 65 do_test func2-1.7 { 66 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34)} 67 } {s} 68 do_test func2-1.8 { 69 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35)} 70 } {{}} 71 do_test func2-1.9 { 72 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36)} 73 } {{}} 74 75 # if p1<0, start from right 76 do_test func2-1.10 { 77 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -0)} 78 } {Supercalifragilisticexpialidocious} 79 do_test func2-1.11 { 80 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1)} 81 } {s} 82 do_test func2-1.12 { 83 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -2)} 84 } {us} 85 do_test func2-1.13 { 86 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -30)} 87 } {rcalifragilisticexpialidocious} 88 do_test func2-1.14 { 89 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34)} 90 } {Supercalifragilisticexpialidocious} 91 do_test func2-1.15 { 92 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35)} 93 } {Supercalifragilisticexpialidocious} 94 do_test func2-1.16 { 95 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36)} 96 } {Supercalifragilisticexpialidocious} 97 98 # p1 is 1-indexed, p2 length to return 99 do_test func2-1.17.1 { 100 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 1)} 101 } {{}} 102 do_test func2-1.17.2 { 103 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 2)} 104 } {S} 105 do_test func2-1.18 { 106 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, 1)} 107 } {S} 108 do_test func2-1.19.0 { 109 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 0)} 110 } {{}} 111 do_test func2-1.19.1 { 112 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 1)} 113 } {u} 114 do_test func2-1.19.2 { 115 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 2)} 116 } {up} 117 do_test func2-1.20 { 118 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, 1)} 119 } {c} 120 do_test func2-1.21 { 121 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34, 1)} 122 } {s} 123 do_test func2-1.22 { 124 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35, 1)} 125 } {{}} 126 do_test func2-1.23 { 127 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, 1)} 128 } {{}} 129 130 # if p1<0, start from right, p2 length to return 131 do_test func2-1.24 { 132 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -0, 1)} 133 } {{}} 134 do_test func2-1.25.0 { 135 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 0)} 136 } {{}} 137 do_test func2-1.25.1 { 138 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 1)} 139 } {s} 140 do_test func2-1.25.2 { 141 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 2)} 142 } {s} 143 do_test func2-1.26 { 144 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -2, 1)} 145 } {u} 146 do_test func2-1.27 { 147 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -30, 1)} 148 } {r} 149 do_test func2-1.28.0 { 150 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 0)} 151 } {{}} 152 do_test func2-1.28.1 { 153 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 1)} 154 } {S} 155 do_test func2-1.28.2 { 156 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 2)} 157 } {Su} 158 do_test func2-1.29.1 { 159 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35, 1)} 160 } {{}} 161 do_test func2-1.29.2 { 162 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35, 2)} 163 } {S} 164 do_test func2-1.30.0 { 165 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 0)} 166 } {{}} 167 do_test func2-1.30.1 { 168 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 1)} 169 } {{}} 170 do_test func2-1.30.2 { 171 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 2)} 172 } {{}} 173 do_test func2-1.30.3 { 174 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 3)} 175 } {S} 176 177 # p1 is 1-indexed, p2 length to return, p2<0 return p2 chars before p1 178 do_test func2-1.31.0 { 179 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 0)} 180 } {{}} 181 do_test func2-1.31.1 { 182 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, -1)} 183 } {{}} 184 do_test func2-1.31.2 { 185 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, -2)} 186 } {{}} 187 do_test func2-1.32.0 { 188 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, 0)} 189 } {{}} 190 do_test func2-1.32.1 { 191 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, -1)} 192 } {{}} 193 do_test func2-1.33.0 { 194 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 0)} 195 } {{}} 196 do_test func2-1.33.1 { 197 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, -1)} 198 } {S} 199 do_test func2-1.33.2 { 200 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, -2)} 201 } {S} 202 do_test func2-1.34.0 { 203 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, 0)} 204 } {{}} 205 do_test func2-1.34.1 { 206 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, -1)} 207 } {u} 208 do_test func2-1.34.2 { 209 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, -2)} 210 } {Su} 211 do_test func2-1.35.1 { 212 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, -1)} 213 } {o} 214 do_test func2-1.35.2 { 215 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, -2)} 216 } {do} 217 do_test func2-1.36 { 218 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34, -1)} 219 } {u} 220 do_test func2-1.37 { 221 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35, -1)} 222 } {s} 223 do_test func2-1.38.0 { 224 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, 0)} 225 } {{}} 226 do_test func2-1.38.1 { 227 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, -1)} 228 } {{}} 229 do_test func2-1.38.2 { 230 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, -2)} 231 } {s} 232 233 234 #---------------------------------------------------------------------------- 235 # Test cases func2-2.*: substr implementation (utf8) 236 # 237 238 # Only do the following tests if TCL has UTF-8 capabilities 239 # 240 if {"\u1234"!="u1234"} { 241 242 do_test func2-2.1.1 { 243 execsql "SELECT 'hi\u1234ho'" 244 } "hi\u1234ho" 245 246 # substr(x,y), substr(x,y,z) 247 do_test func2-2.1.2 { 248 catchsql "SELECT SUBSTR()" 249 } {1 {wrong number of arguments to function SUBSTR()}} 250 do_test func2-2.1.3 { 251 catchsql "SELECT SUBSTR('hi\u1234ho')" 252 } {1 {wrong number of arguments to function SUBSTR()}} 253 do_test func2-2.1.4 { 254 catchsql "SELECT SUBSTR('hi\u1234ho', 1,1,1)" 255 } {1 {wrong number of arguments to function SUBSTR()}} 256 257 do_test func2-2.2.0 { 258 execsql "SELECT SUBSTR('hi\u1234ho', 0, 0)" 259 } {{}} 260 do_test func2-2.2.1 { 261 execsql "SELECT SUBSTR('hi\u1234ho', 0, 1)" 262 } {{}} 263 do_test func2-2.2.2 { 264 execsql "SELECT SUBSTR('hi\u1234ho', 0, 2)" 265 } "h" 266 do_test func2-2.2.3 { 267 execsql "SELECT SUBSTR('hi\u1234ho', 0, 3)" 268 } "hi" 269 do_test func2-2.2.4 { 270 execsql "SELECT SUBSTR('hi\u1234ho', 0, 4)" 271 } "hi\u1234" 272 do_test func2-2.2.5 { 273 execsql "SELECT SUBSTR('hi\u1234ho', 0, 5)" 274 } "hi\u1234h" 275 do_test func2-2.2.6 { 276 execsql "SELECT SUBSTR('hi\u1234ho', 0, 6)" 277 } "hi\u1234ho" 278 279 do_test func2-2.3.0 { 280 execsql "SELECT SUBSTR('hi\u1234ho', 1, 0)" 281 } {{}} 282 do_test func2-2.3.1 { 283 execsql "SELECT SUBSTR('hi\u1234ho', 1, 1)" 284 } "h" 285 do_test func2-2.3.2 { 286 execsql "SELECT SUBSTR('hi\u1234ho', 1, 2)" 287 } "hi" 288 do_test func2-2.3.3 { 289 execsql "SELECT SUBSTR('hi\u1234ho', 1, 3)" 290 } "hi\u1234" 291 do_test func2-2.3.4 { 292 execsql "SELECT SUBSTR('hi\u1234ho', 1, 4)" 293 } "hi\u1234h" 294 do_test func2-2.3.5 { 295 execsql "SELECT SUBSTR('hi\u1234ho', 1, 5)" 296 } "hi\u1234ho" 297 do_test func2-2.3.6 { 298 execsql "SELECT SUBSTR('hi\u1234ho', 1, 6)" 299 } "hi\u1234ho" 300 301 do_test func2-2.4.0 { 302 execsql "SELECT SUBSTR('hi\u1234ho', 3, 0)" 303 } {{}} 304 do_test func2-2.4.1 { 305 execsql "SELECT SUBSTR('hi\u1234ho', 3, 1)" 306 } "\u1234" 307 do_test func2-2.4.2 { 308 execsql "SELECT SUBSTR('hi\u1234ho', 3, 2)" 309 } "\u1234h" 310 311 do_test func2-2.5.0 { 312 execsql "SELECT SUBSTR('\u1234', 0, 0)" 313 } {{}} 314 do_test func2-2.5.1 { 315 execsql "SELECT SUBSTR('\u1234', 0, 1)" 316 } {{}} 317 do_test func2-2.5.2 { 318 execsql "SELECT SUBSTR('\u1234', 0, 2)" 319 } "\u1234" 320 do_test func2-2.5.3 { 321 execsql "SELECT SUBSTR('\u1234', 0, 3)" 322 } "\u1234" 323 324 do_test func2-2.6.0 { 325 execsql "SELECT SUBSTR('\u1234', 1, 0)" 326 } {{}} 327 do_test func2-2.6.1 { 328 execsql "SELECT SUBSTR('\u1234', 1, 1)" 329 } "\u1234" 330 do_test func2-2.6.2 { 331 execsql "SELECT SUBSTR('\u1234', 1, 2)" 332 } "\u1234" 333 do_test func2-2.6.3 { 334 execsql "SELECT SUBSTR('\u1234', 1, 3)" 335 } "\u1234" 336 337 do_test func2-2.7.0 { 338 execsql "SELECT SUBSTR('\u1234', 2, 0)" 339 } {{}} 340 do_test func2-2.7.1 { 341 execsql "SELECT SUBSTR('\u1234', 2, 1)" 342 } {{}} 343 do_test func2-2.7.2 { 344 execsql "SELECT SUBSTR('\u1234', 2, 2)" 345 } {{}} 346 347 do_test func2-2.8.0 { 348 execsql "SELECT SUBSTR('\u1234', -1, 0)" 349 } {{}} 350 do_test func2-2.8.1 { 351 execsql "SELECT SUBSTR('\u1234', -1, 1)" 352 } "\u1234" 353 do_test func2-2.8.2 { 354 execsql "SELECT SUBSTR('\u1234', -1, 2)" 355 } "\u1234" 356 do_test func2-2.8.3 { 357 execsql "SELECT SUBSTR('\u1234', -1, 3)" 358 } "\u1234" 359 360 } ;# End \u1234!=u1234 361 362 #---------------------------------------------------------------------------- 363 # Test cases func2-3.*: substr implementation (blob) 364 # 365 366 ifcapable {!bloblit} { 367 finish_test 368 return 369 } 370 371 do_test func2-3.1.1 { 372 set blob [execsql "SELECT x'1234'"] 373 bin_to_hex [lindex $blob 0] 374 } "1234" 375 376 # substr(x,y), substr(x,y,z) 377 do_test func2-3.1.2 { 378 catchsql {SELECT SUBSTR()} 379 } {1 {wrong number of arguments to function SUBSTR()}} 380 do_test func2-3.1.3 { 381 catchsql {SELECT SUBSTR(x'1234')} 382 } {1 {wrong number of arguments to function SUBSTR()}} 383 do_test func2-3.1.4 { 384 catchsql {SELECT SUBSTR(x'1234', 1,1,1)} 385 } {1 {wrong number of arguments to function SUBSTR()}} 386 387 do_test func2-3.2.0 { 388 set blob [execsql "SELECT SUBSTR(x'1234', 0, 0)"] 389 bin_to_hex [lindex $blob 0] 390 } {} 391 do_test func2-3.2.1 { 392 set blob [execsql "SELECT SUBSTR(x'1234', 0, 1)"] 393 bin_to_hex [lindex $blob 0] 394 } {} 395 do_test func2-3.2.2 { 396 set blob [execsql "SELECT SUBSTR(x'1234', 0, 2)"] 397 bin_to_hex [lindex $blob 0] 398 } "12" 399 do_test func2-3.2.3 { 400 set blob [execsql "SELECT SUBSTR(x'1234', 0, 3)"] 401 bin_to_hex [lindex $blob 0] 402 } "1234" 403 404 do_test func2-3.3.0 { 405 set blob [execsql "SELECT SUBSTR(x'1234', 1, 0)"] 406 bin_to_hex [lindex $blob 0] 407 } {} 408 do_test func2-3.3.1 { 409 set blob [execsql "SELECT SUBSTR(x'1234', 1, 1)"] 410 bin_to_hex [lindex $blob 0] 411 } "12" 412 do_test func2-3.3.2 { 413 set blob [execsql "SELECT SUBSTR(x'1234', 1, 2)"] 414 bin_to_hex [lindex $blob 0] 415 } "1234" 416 do_test func2-3.3.3 { 417 set blob [execsql "SELECT SUBSTR(x'1234', 1, 3)"] 418 bin_to_hex [lindex $blob 0] 419 } "1234" 420 421 do_test func2-3.4.0 { 422 set blob [execsql "SELECT SUBSTR(x'1234', -1, 0)"] 423 bin_to_hex [lindex $blob 0] 424 } {} 425 do_test func2-3.4.1 { 426 set blob [execsql "SELECT SUBSTR(x'1234', -1, 1)"] 427 bin_to_hex [lindex $blob 0] 428 } "34" 429 do_test func2-3.4.2 { 430 set blob [execsql "SELECT SUBSTR(x'1234', -1, 2)"] 431 bin_to_hex [lindex $blob 0] 432 } "34" 433 do_test func2-3.4.3 { 434 set blob [execsql "SELECT SUBSTR(x'1234', -1, 3)"] 435 bin_to_hex [lindex $blob 0] 436 } "34" 437 438 do_test func2-3.5.0 { 439 set blob [execsql "SELECT SUBSTR(x'1234', -2, 0)"] 440 bin_to_hex [lindex $blob 0] 441 } {} 442 do_test func2-3.5.1 { 443 set blob [execsql "SELECT SUBSTR(x'1234', -2, 1)"] 444 bin_to_hex [lindex $blob 0] 445 } "12" 446 do_test func2-3.5.2 { 447 set blob [execsql "SELECT SUBSTR(x'1234', -2, 2)"] 448 bin_to_hex [lindex $blob 0] 449 } "1234" 450 do_test func2-3.5.3 { 451 set blob [execsql "SELECT SUBSTR(x'1234', -2, 3)"] 452 bin_to_hex [lindex $blob 0] 453 } "1234" 454 455 do_test func2-3.6.0 { 456 set blob [execsql "SELECT SUBSTR(x'1234', -1, 0)"] 457 bin_to_hex [lindex $blob 0] 458 } {} 459 do_test func2-3.6.1 { 460 set blob [execsql "SELECT SUBSTR(x'1234', -1, -1)"] 461 bin_to_hex [lindex $blob 0] 462 } "12" 463 do_test func2-3.6.2 { 464 set blob [execsql "SELECT SUBSTR(x'1234', -1, -2)"] 465 bin_to_hex [lindex $blob 0] 466 } "12" 467 do_test func2-3.6.3 { 468 set blob [execsql "SELECT SUBSTR(x'1234', -1, -3)"] 469 bin_to_hex [lindex $blob 0] 470 } "12" 471 472 do_test func2-3.7.0 { 473 set blob [execsql "SELECT SUBSTR(x'1234', -2, 0)"] 474 bin_to_hex [lindex $blob 0] 475 } {} 476 do_test func2-3.7.1 { 477 set blob [execsql "SELECT SUBSTR(x'1234', -2, -1)"] 478 bin_to_hex [lindex $blob 0] 479 } {} 480 do_test func2-3.7.2 { 481 set blob [execsql "SELECT SUBSTR(x'1234', -2, -2)"] 482 bin_to_hex [lindex $blob 0] 483 } {} 484 485 do_test func2-3.8.0 { 486 set blob [execsql "SELECT SUBSTR(x'1234', 1, 0)"] 487 bin_to_hex [lindex $blob 0] 488 } {} 489 do_test func2-3.8.1 { 490 set blob [execsql "SELECT SUBSTR(x'1234', 1, -1)"] 491 bin_to_hex [lindex $blob 0] 492 } {} 493 do_test func2-3.8.2 { 494 set blob [execsql "SELECT SUBSTR(x'1234', 1, -2)"] 495 bin_to_hex [lindex $blob 0] 496 } {} 497 498 do_test func2-3.9.0 { 499 set blob [execsql "SELECT SUBSTR(x'1234', 2, 0)"] 500 bin_to_hex [lindex $blob 0] 501 } {} 502 do_test func2-3.9.1 { 503 set blob [execsql "SELECT SUBSTR(x'1234', 2, -1)"] 504 bin_to_hex [lindex $blob 0] 505 } "12" 506 do_test func2-3.9.2 { 507 set blob [execsql "SELECT SUBSTR(x'1234', 2, -2)"] 508 bin_to_hex [lindex $blob 0] 509 } "12" 510 511 finish_test 512