3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus of
12 # this file is testing the tointeger() and toreal() functions.
14 # Several of the toreal() tests are disabled on platforms where floating
15 # point precision is not high enough to represent their constant integer
16 # expression arguments as double precision floating point values.
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set saved_tcl_precision $tcl_precision
22 load_static_extension db totype
24 set highPrecision(1) [expr \
25 {[db eval {SELECT tointeger(9223372036854775807 + 1);}] eq {{}}}]
27 do_execsql_test func4-1.1 {
28 SELECT tointeger(NULL);
30 do_execsql_test func4-1.2 {
33 do_execsql_test func4-1.3 {
34 SELECT tointeger(' ');
36 do_execsql_test func4-1.4 {
37 SELECT tointeger('1234');
39 do_execsql_test func4-1.5 {
40 SELECT tointeger(' 1234');
42 do_execsql_test func4-1.6 {
43 SELECT tointeger('bad');
45 do_execsql_test func4-1.7 {
46 SELECT tointeger('0xBAD');
48 do_execsql_test func4-1.8 {
49 SELECT tointeger('123BAD');
51 do_execsql_test func4-1.9 {
52 SELECT tointeger('0x123BAD');
54 do_execsql_test func4-1.10 {
55 SELECT tointeger('123NO');
57 do_execsql_test func4-1.11 {
58 SELECT tointeger('0x123NO');
60 do_execsql_test func4-1.12 {
61 SELECT tointeger('-0x1');
63 do_execsql_test func4-1.13 {
64 SELECT tointeger('-0x0');
66 do_execsql_test func4-1.14 {
67 SELECT tointeger('0x0');
69 do_execsql_test func4-1.15 {
70 SELECT tointeger('0x1');
72 do_execsql_test func4-1.16 {
75 do_execsql_test func4-1.17 {
78 do_execsql_test func4-1.18 {
81 do_execsql_test func4-1.19 {
84 do_execsql_test func4-1.20 {
85 SELECT tointeger(-1.79769313486232e308 - 1);
87 do_execsql_test func4-1.21 {
88 SELECT tointeger(-1.79769313486232e308);
90 do_execsql_test func4-1.22 {
91 SELECT tointeger(-1.79769313486232e308 + 1);
93 do_execsql_test func4-1.23 {
94 SELECT tointeger(-9223372036854775808 - 1);
95 } {-9223372036854775808}
96 do_execsql_test func4-1.24 {
97 SELECT tointeger(-9223372036854775808);
98 } {-9223372036854775808}
99 do_execsql_test func4-1.25 {
100 SELECT tointeger(-9223372036854775808 + 1);
101 } {-9223372036854775807}
102 do_execsql_test func4-1.26 {
103 SELECT tointeger(-9223372036854775807 - 1);
104 } {-9223372036854775808}
105 do_execsql_test func4-1.27 {
106 SELECT tointeger(-9223372036854775807);
107 } {-9223372036854775807}
108 do_execsql_test func4-1.28 {
109 SELECT tointeger(-9223372036854775807 + 1);
110 } {-9223372036854775806}
111 do_execsql_test func4-1.29 {
112 SELECT tointeger(-2147483648 - 1);
114 do_execsql_test func4-1.30 {
115 SELECT tointeger(-2147483648);
117 do_execsql_test func4-1.31 {
118 SELECT tointeger(-2147483648 + 1);
120 do_execsql_test func4-1.32 {
121 SELECT tointeger(2147483647 - 1);
123 do_execsql_test func4-1.33 {
124 SELECT tointeger(2147483647);
126 do_execsql_test func4-1.34 {
127 SELECT tointeger(2147483647 + 1);
129 do_execsql_test func4-1.35 {
130 SELECT tointeger(9223372036854775807 - 1);
131 } {9223372036854775806}
132 do_execsql_test func4-1.36 {
133 SELECT tointeger(9223372036854775807);
134 } {9223372036854775807}
135 if {$highPrecision(1)} {
136 do_execsql_test func4-1.37 {
137 SELECT tointeger(9223372036854775807 + 1);
140 do_execsql_test func4-1.38 {
141 SELECT tointeger(1.79769313486232e308 - 1);
143 do_execsql_test func4-1.39 {
144 SELECT tointeger(1.79769313486232e308);
146 do_execsql_test func4-1.40 {
147 SELECT tointeger(1.79769313486232e308 + 1);
149 do_execsql_test func4-1.41 {
150 SELECT tointeger(4503599627370496 - 1);
152 do_execsql_test func4-1.42 {
153 SELECT tointeger(4503599627370496);
155 do_execsql_test func4-1.43 {
156 SELECT tointeger(4503599627370496 + 1);
158 do_execsql_test func4-1.44 {
159 SELECT tointeger(9007199254740992 - 1);
161 do_execsql_test func4-1.45 {
162 SELECT tointeger(9007199254740992);
164 do_execsql_test func4-1.46 {
165 SELECT tointeger(9007199254740992 + 1);
167 do_execsql_test func4-1.47 {
168 SELECT tointeger(9223372036854775807 - 1);
169 } {9223372036854775806}
170 do_execsql_test func4-1.48 {
171 SELECT tointeger(9223372036854775807);
172 } {9223372036854775807}
173 if {$highPrecision(1)} {
174 do_execsql_test func4-1.49 {
175 SELECT tointeger(9223372036854775807 + 1);
177 do_execsql_test func4-1.50 {
178 SELECT tointeger(9223372036854775808 - 1);
180 do_execsql_test func4-1.51 {
181 SELECT tointeger(9223372036854775808);
183 do_execsql_test func4-1.52 {
184 SELECT tointeger(9223372036854775808 + 1);
187 do_execsql_test func4-1.53 {
188 SELECT tointeger(18446744073709551616 - 1);
190 do_execsql_test func4-1.54 {
191 SELECT tointeger(18446744073709551616);
193 do_execsql_test func4-1.55 {
194 SELECT tointeger(18446744073709551616 + 1);
197 ifcapable floatingpoint {
198 set highPrecision(2) [expr \
199 {[db eval {SELECT toreal(-9223372036854775808 + 1);}] eq {{}}}]
201 do_execsql_test func4-2.1 {
204 do_execsql_test func4-2.2 {
207 do_execsql_test func4-2.3 {
210 do_execsql_test func4-2.4 {
211 SELECT toreal('1234');
213 do_execsql_test func4-2.5 {
214 SELECT toreal(' 1234');
216 do_execsql_test func4-2.6 {
217 SELECT toreal('bad');
219 do_execsql_test func4-2.7 {
220 SELECT toreal('0xBAD');
222 do_execsql_test func4-2.8 {
223 SELECT toreal('123BAD');
225 do_execsql_test func4-2.9 {
226 SELECT toreal('0x123BAD');
228 do_execsql_test func4-2.10 {
229 SELECT toreal('123NO');
231 do_execsql_test func4-2.11 {
232 SELECT toreal('0x123NO');
234 do_execsql_test func4-2.12 {
235 SELECT toreal('-0x1');
237 do_execsql_test func4-2.13 {
238 SELECT toreal('-0x0');
240 do_execsql_test func4-2.14 {
241 SELECT toreal('0x0');
243 do_execsql_test func4-2.15 {
244 SELECT toreal('0x1');
246 do_execsql_test func4-2.16 {
249 do_execsql_test func4-2.17 {
252 do_execsql_test func4-2.18 {
255 do_execsql_test func4-2.19 {
258 do_execsql_test func4-2.20 {
259 SELECT toreal(-1.79769313486232e308 - 1);
261 do_execsql_test func4-2.21 {
262 SELECT toreal(-1.79769313486232e308);
264 do_execsql_test func4-2.22 {
265 SELECT toreal(-1.79769313486232e308 + 1);
267 do_execsql_test func4-2.23 {
268 SELECT toreal(-9223372036854775808 - 1);
269 } {-9.223372036854776e+18}
270 do_execsql_test func4-2.24 {
271 SELECT toreal(-9223372036854775808);
272 } {-9.223372036854776e+18}
273 if {$highPrecision(2)} {
274 do_execsql_test func4-2.25 {
275 SELECT toreal(-9223372036854775808 + 1);
278 do_execsql_test func4-2.26 {
279 SELECT toreal(-9223372036854775807 - 1);
280 } {-9.223372036854776e+18}
281 if {$highPrecision(2)} {
282 do_execsql_test func4-2.27 {
283 SELECT toreal(-9223372036854775807);
285 do_execsql_test func4-2.28 {
286 SELECT toreal(-9223372036854775807 + 1);
289 do_execsql_test func4-2.29 {
290 SELECT toreal(-2147483648 - 1);
292 do_execsql_test func4-2.30 {
293 SELECT toreal(-2147483648);
295 do_execsql_test func4-2.31 {
296 SELECT toreal(-2147483648 + 1);
298 do_execsql_test func4-2.32 {
299 SELECT toreal(2147483647 - 1);
301 do_execsql_test func4-2.33 {
302 SELECT toreal(2147483647);
304 do_execsql_test func4-2.34 {
305 SELECT toreal(2147483647 + 1);
307 if {$highPrecision(2)} {
308 do_execsql_test func4-2.35 {
309 SELECT toreal(9223372036854775807 - 1);
311 if {$highPrecision(1)} {
312 do_execsql_test func4-2.36 {
313 SELECT toreal(9223372036854775807);
317 do_execsql_test func4-2.37 {
318 SELECT toreal(9223372036854775807 + 1);
319 } {9.223372036854776e+18}
320 do_execsql_test func4-2.38 {
321 SELECT toreal(1.79769313486232e308 - 1);
323 do_execsql_test func4-2.39 {
324 SELECT toreal(1.79769313486232e308);
326 do_execsql_test func4-2.40 {
327 SELECT toreal(1.79769313486232e308 + 1);
329 do_execsql_test func4-2.41 {
330 SELECT toreal(4503599627370496 - 1);
331 } {4503599627370495.0}
332 do_execsql_test func4-2.42 {
333 SELECT toreal(4503599627370496);
334 } {4503599627370496.0}
335 do_execsql_test func4-2.43 {
336 SELECT toreal(4503599627370496 + 1);
337 } {4503599627370497.0}
338 do_execsql_test func4-2.44 {
339 SELECT toreal(9007199254740992 - 1);
340 } {9007199254740991.0}
341 do_execsql_test func4-2.45 {
342 SELECT toreal(9007199254740992);
343 } {9007199254740992.0}
344 if {$highPrecision(2)} {
345 do_execsql_test func4-2.46 {
346 SELECT toreal(9007199254740992 + 1);
349 do_execsql_test func4-2.47 {
350 SELECT toreal(9007199254740992 + 2);
351 } {9007199254740994.0}
352 do_execsql_test func4-2.48 {
353 SELECT toreal(tointeger(9223372036854775808) - 1);
355 if {$highPrecision(1)} {
356 do_execsql_test func4-2.49 {
357 SELECT toreal(tointeger(9223372036854775808));
359 do_execsql_test func4-2.50 {
360 SELECT toreal(tointeger(9223372036854775808) + 1);
363 do_execsql_test func4-2.51 {
364 SELECT toreal(tointeger(18446744073709551616) - 1);
366 do_execsql_test func4-2.52 {
367 SELECT toreal(tointeger(18446744073709551616));
369 do_execsql_test func4-2.53 {
370 SELECT toreal(tointeger(18446744073709551616) + 1);
375 do_execsql_test func4-3.1 {
377 x INTEGER CHECK(tointeger(x) IS NOT NULL)
382 INSERT INTO t1 (x) VALUES (NULL);
384 } {1 {CHECK constraint failed: t1}}
387 INSERT INTO t1 (x) VALUES (NULL);
389 } {1 {CHECK constraint failed: t1}}
392 INSERT INTO t1 (x) VALUES ('');
394 } {1 {CHECK constraint failed: t1}}
397 INSERT INTO t1 (x) VALUES ('bad');
399 } {1 {CHECK constraint failed: t1}}
402 INSERT INTO t1 (x) VALUES ('1234bad');
404 } {1 {CHECK constraint failed: t1}}
407 INSERT INTO t1 (x) VALUES ('1234.56bad');
409 } {1 {CHECK constraint failed: t1}}
412 INSERT INTO t1 (x) VALUES (1234);
417 INSERT INTO t1 (x) VALUES (1234.56);
419 } {1 {CHECK constraint failed: t1}}
422 INSERT INTO t1 (x) VALUES ('1234');
427 INSERT INTO t1 (x) VALUES ('1234.56');
429 } {1 {CHECK constraint failed: t1}}
432 INSERT INTO t1 (x) VALUES (ZEROBLOB(4));
434 } {1 {CHECK constraint failed: t1}}
437 INSERT INTO t1 (x) VALUES (X'');
439 } {1 {CHECK constraint failed: t1}}
442 INSERT INTO t1 (x) VALUES (X'1234');
444 } {1 {CHECK constraint failed: t1}}
447 INSERT INTO t1 (x) VALUES (X'12345678');
449 } {1 {CHECK constraint failed: t1}}
452 INSERT INTO t1 (x) VALUES ('1234.00');
454 } {1 {CHECK constraint failed: t1}}
457 INSERT INTO t1 (x) VALUES (1234.00);
462 INSERT INTO t1 (x) VALUES ('-9223372036854775809');
464 } {1 {CHECK constraint failed: t1}}
465 if {$highPrecision(1)} {
468 INSERT INTO t1 (x) VALUES (9223372036854775808);
470 } {1 {CHECK constraint failed: t1}}
472 do_execsql_test func4-3.20 {
473 SELECT x FROM t1 ORDER BY x;
476 ifcapable floatingpoint {
477 do_execsql_test func4-4.1 {
479 x REAL CHECK(toreal(x) IS NOT NULL)
484 INSERT INTO t2 (x) VALUES (NULL);
486 } {1 {CHECK constraint failed: t2}}
489 INSERT INTO t2 (x) VALUES (NULL);
491 } {1 {CHECK constraint failed: t2}}
494 INSERT INTO t2 (x) VALUES ('');
496 } {1 {CHECK constraint failed: t2}}
499 INSERT INTO t2 (x) VALUES ('bad');
501 } {1 {CHECK constraint failed: t2}}
504 INSERT INTO t2 (x) VALUES ('1234bad');
506 } {1 {CHECK constraint failed: t2}}
509 INSERT INTO t2 (x) VALUES ('1234.56bad');
511 } {1 {CHECK constraint failed: t2}}
514 INSERT INTO t2 (x) VALUES (1234);
519 INSERT INTO t2 (x) VALUES (1234.56);
524 INSERT INTO t2 (x) VALUES ('1234');
529 INSERT INTO t2 (x) VALUES ('1234.56');
534 INSERT INTO t2 (x) VALUES (ZEROBLOB(4));
536 } {1 {CHECK constraint failed: t2}}
539 INSERT INTO t2 (x) VALUES (X'');
541 } {1 {CHECK constraint failed: t2}}
544 INSERT INTO t2 (x) VALUES (X'1234');
546 } {1 {CHECK constraint failed: t2}}
549 INSERT INTO t2 (x) VALUES (X'12345678');
551 } {1 {CHECK constraint failed: t2}}
552 do_execsql_test func4-4.16 {
553 SELECT x FROM t2 ORDER BY x;
554 } {1234.0 1234.0 1234.56 1234.56}
558 ifcapable floatingpoint {
559 do_execsql_test func4-5.1 {
560 SELECT tointeger(toreal('1234'));
562 do_execsql_test func4-5.2 {
563 SELECT tointeger(toreal(-1));
565 do_execsql_test func4-5.3 {
566 SELECT tointeger(toreal(-0));
568 do_execsql_test func4-5.4 {
569 SELECT tointeger(toreal(0));
571 do_execsql_test func4-5.5 {
572 SELECT tointeger(toreal(1));
574 do_execsql_test func4-5.6 {
575 SELECT tointeger(toreal(-9223372036854775808 - 1));
576 } {-9223372036854775808}
577 do_execsql_test func4-5.7 {
578 SELECT tointeger(toreal(-9223372036854775808));
579 } {-9223372036854775808}
580 if {$highPrecision(2)} {
581 do_execsql_test func4-5.8 {
582 SELECT tointeger(toreal(-9223372036854775808 + 1));
585 do_execsql_test func4-5.9 {
586 SELECT tointeger(toreal(-2147483648 - 1));
588 do_execsql_test func4-5.10 {
589 SELECT tointeger(toreal(-2147483648));
591 do_execsql_test func4-5.11 {
592 SELECT tointeger(toreal(-2147483648 + 1));
594 do_execsql_test func4-5.12 {
595 SELECT tointeger(toreal(2147483647 - 1));
597 do_execsql_test func4-5.13 {
598 SELECT tointeger(toreal(2147483647));
600 do_execsql_test func4-5.14 {
601 SELECT tointeger(toreal(2147483647 + 1));
603 do_execsql_test func4-5.15 {
604 SELECT tointeger(toreal(9223372036854775807 - 1));
606 if {$highPrecision(1)} {
607 do_execsql_test func4-5.16 {
608 SELECT tointeger(toreal(9223372036854775807));
610 do_execsql_test func4-5.17 {
611 SELECT tointeger(toreal(9223372036854775807 + 1));
614 do_execsql_test func4-5.18 {
615 SELECT tointeger(toreal(4503599627370496 - 1));
617 do_execsql_test func4-5.19 {
618 SELECT tointeger(toreal(4503599627370496));
620 do_execsql_test func4-5.20 {
621 SELECT tointeger(toreal(4503599627370496 + 1));
623 do_execsql_test func4-5.21 {
624 SELECT tointeger(toreal(9007199254740992 - 1));
626 do_execsql_test func4-5.22 {
627 SELECT tointeger(toreal(9007199254740992));
629 if {$highPrecision(2)} {
630 do_execsql_test func4-5.23 {
631 SELECT tointeger(toreal(9007199254740992 + 1));
634 do_execsql_test func4-5.24 {
635 SELECT tointeger(toreal(9007199254740992 + 2));
637 if {$highPrecision(1)} {
638 do_execsql_test func4-5.25 {
639 SELECT tointeger(toreal(9223372036854775808 - 1));
641 do_execsql_test func4-5.26 {
642 SELECT tointeger(toreal(9223372036854775808));
644 do_execsql_test func4-5.27 {
645 SELECT tointeger(toreal(9223372036854775808 + 1));
648 do_execsql_test func4-5.28 {
649 SELECT tointeger(toreal(18446744073709551616 - 1));
651 do_execsql_test func4-5.29 {
652 SELECT tointeger(toreal(18446744073709551616));
654 do_execsql_test func4-5.30 {
655 SELECT tointeger(toreal(18446744073709551616 + 1));
659 for {set i 0} {$i < 10} {incr i} {
660 if {$i == 8} continue
661 do_execsql_test func4-6.1.$i.1 [subst {
662 SELECT tointeger(x'[string repeat 01 $i]');
664 ifcapable floatingpoint {
665 do_execsql_test func4-6.1.$i.2 [subst {
666 SELECT toreal(x'[string repeat 01 $i]');
671 do_execsql_test func4-6.2.1 {
672 SELECT tointeger(x'0102030405060708');
673 } {578437695752307201}
674 do_execsql_test func4-6.2.2 {
675 SELECT tointeger(x'0807060504030201');
676 } {72623859790382856}
678 ifcapable floatingpoint {
679 do_execsql_test func4-6.3.1 {
680 SELECT toreal(x'ffefffffffffffff');
681 } {-1.7976931348623157e+308}
682 do_execsql_test func4-6.3.2 {
683 SELECT toreal(x'8010000000000000');
684 } {-2.2250738585072014e-308}
685 do_execsql_test func4-6.3.3 {
686 SELECT toreal(x'c000000000000000');
688 do_execsql_test func4-6.3.4 {
689 SELECT toreal(x'bff0000000000000');
691 do_execsql_test func4-6.3.5 {
692 SELECT toreal(x'8000000000000000');
694 do_execsql_test func4-6.3.6 {
695 SELECT toreal(x'0000000000000000');
697 do_execsql_test func4-6.3.7 {
698 SELECT toreal(x'3ff0000000000000');
700 do_execsql_test func4-6.3.8 {
701 SELECT toreal(x'4000000000000000');
703 do_execsql_test func4-6.3.9 {
704 SELECT toreal(x'0010000000000000');
705 } {2.2250738585072014e-308}
706 do_execsql_test func4-6.3.10 {
707 SELECT toreal(x'7fefffffffffffff');
708 } {1.7976931348623157e+308}
709 do_execsql_test func4-6.3.11 {
710 SELECT toreal(x'8000000000000001');
712 do_execsql_test func4-6.3.12 {
713 SELECT toreal(x'800fffffffffffff');
714 } {-2.225073858507201e-308}
715 do_execsql_test func4-6.3.13 {
716 SELECT toreal(x'0000000000000001');
718 do_execsql_test func4-6.3.14 {
719 SELECT toreal(x'000fffffffffffff');
720 } {2.225073858507201e-308}
721 do_execsql_test func4-6.3.15 {
722 SELECT toreal(x'fff0000000000000');
724 do_execsql_test func4-6.3.16 {
725 SELECT toreal(x'7ff0000000000000');
727 do_execsql_test func4-6.3.17 {
728 SELECT toreal(x'fff8000000000000');
730 do_execsql_test func4-6.3.18 {
731 SELECT toreal(x'fff0000000000001');
733 do_execsql_test func4-6.3.19 {
734 SELECT toreal(x'fff7ffffffffffff');
736 do_execsql_test func4-6.3.20 {
737 SELECT toreal(x'7ff0000000000001');
739 do_execsql_test func4-6.3.21 {
740 SELECT toreal(x'7ff7ffffffffffff');
742 do_execsql_test func4-6.3.22 {
743 SELECT toreal(x'fff8000000000001');
745 do_execsql_test func4-6.3.23 {
746 SELECT toreal(x'ffffffffffffffff');
748 do_execsql_test func4-6.3.24 {
749 SELECT toreal(x'7ff8000000000000');
751 do_execsql_test func4-6.3.25 {
752 SELECT toreal(x'7fffffffffffffff');
756 set tcl_precision $saved_tcl_precision
757 unset saved_tcl_precision