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 that are
13 # part of the "totype.c" extension. This file does not test the core
14 # SQLite library. Failures of tests in this file are related to the
15 # ext/misc/totype.c extension.
17 # Several of the toreal() tests are disabled on platforms where floating
18 # point precision is not high enough to represent their constant integer
19 # expression arguments as double precision floating point values.
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
23 set saved_tcl_precision $tcl_precision
25 load_static_extension db totype
27 set highPrecision(1) [expr \
28 {[db eval {SELECT tointeger(9223372036854775807 + 1);}] eq {{}}}]
29 set highPrecision(2) [expr \
30 {[db eval {SELECT toreal(-9223372036854775808 + 1);}] eq {{}}}]
32 # highPrecision(3) is only known to be false on i586 with gcc-13 and -O2.
33 # It is true on the exact same platform with -O0. Both results seem
34 # reasonable, so we'll just very the expectation accordingly.
36 set highPrecision(3) [expr \
37 {[db eval {SELECT toreal(9007199254740992 + 1);}] eq {{}}}]
39 if {!$highPrecision(1) || !$highPrecision(2) || !$highPrecision(3)} {
40 puts "NOTICE: use_long_double: [use_long_double] \
41 highPrecision: $highPrecision(1) $highPrecision(2) $highPrecision(3)"
44 do_execsql_test func4-1.1 {
45 SELECT tointeger(NULL);
47 do_execsql_test func4-1.2 {
50 do_execsql_test func4-1.3 {
51 SELECT tointeger(' ');
53 do_execsql_test func4-1.4 {
54 SELECT tointeger('1234');
56 do_execsql_test func4-1.5 {
57 SELECT tointeger(' 1234');
59 do_execsql_test func4-1.6 {
60 SELECT tointeger('bad');
62 do_execsql_test func4-1.7 {
63 SELECT tointeger('0xBAD');
65 do_execsql_test func4-1.8 {
66 SELECT tointeger('123BAD');
68 do_execsql_test func4-1.9 {
69 SELECT tointeger('0x123BAD');
71 do_execsql_test func4-1.10 {
72 SELECT tointeger('123NO');
74 do_execsql_test func4-1.11 {
75 SELECT tointeger('0x123NO');
77 do_execsql_test func4-1.12 {
78 SELECT tointeger('-0x1');
80 do_execsql_test func4-1.13 {
81 SELECT tointeger('-0x0');
83 do_execsql_test func4-1.14 {
84 SELECT tointeger('0x0');
86 do_execsql_test func4-1.15 {
87 SELECT tointeger('0x1');
89 do_execsql_test func4-1.16 {
92 do_execsql_test func4-1.17 {
95 do_execsql_test func4-1.18 {
98 do_execsql_test func4-1.19 {
101 do_execsql_test func4-1.20 {
102 SELECT tointeger(-1.79769313486232e308 - 1);
104 do_execsql_test func4-1.21 {
105 SELECT tointeger(-1.79769313486232e308);
107 do_execsql_test func4-1.22 {
108 SELECT tointeger(-1.79769313486232e308 + 1);
110 do_execsql_test func4-1.23 {
111 SELECT tointeger(-9223372036854775808 - 1);
113 do_execsql_test func4-1.24 {
114 SELECT tointeger(-9223372036854775808);
115 } {-9223372036854775808}
116 do_execsql_test func4-1.25 {
117 SELECT tointeger(-9223372036854775808 + 1);
118 } {-9223372036854775807}
119 do_execsql_test func4-1.26 {
120 SELECT tointeger(-9223372036854775807 - 1);
121 } {-9223372036854775808}
122 do_execsql_test func4-1.27 {
123 SELECT tointeger(-9223372036854775807);
124 } {-9223372036854775807}
125 do_execsql_test func4-1.28 {
126 SELECT tointeger(-9223372036854775807 + 1);
127 } {-9223372036854775806}
128 do_execsql_test func4-1.29 {
129 SELECT tointeger(-2147483648 - 1);
131 do_execsql_test func4-1.30 {
132 SELECT tointeger(-2147483648);
134 do_execsql_test func4-1.31 {
135 SELECT tointeger(-2147483648 + 1);
137 do_execsql_test func4-1.32 {
138 SELECT tointeger(2147483647 - 1);
140 do_execsql_test func4-1.33 {
141 SELECT tointeger(2147483647);
143 do_execsql_test func4-1.34 {
144 SELECT tointeger(2147483647 + 1);
146 do_execsql_test func4-1.35 {
147 SELECT tointeger(9223372036854775807 - 1);
148 } {9223372036854775806}
149 do_execsql_test func4-1.36 {
150 SELECT tointeger(9223372036854775807);
151 } {9223372036854775807}
152 if {$highPrecision(1)} {
153 do_execsql_test func4-1.37 {
154 SELECT tointeger(9223372036854775807 + 1);
157 do_execsql_test func4-1.38 {
158 SELECT tointeger(1.79769313486232e308 - 1);
160 do_execsql_test func4-1.39 {
161 SELECT tointeger(1.79769313486232e308);
163 do_execsql_test func4-1.40 {
164 SELECT tointeger(1.79769313486232e308 + 1);
166 do_execsql_test func4-1.41 {
167 SELECT tointeger(4503599627370496 - 1);
169 do_execsql_test func4-1.42 {
170 SELECT tointeger(4503599627370496);
172 do_execsql_test func4-1.43 {
173 SELECT tointeger(4503599627370496 + 1);
175 do_execsql_test func4-1.44 {
176 SELECT tointeger(9007199254740992 - 1);
178 do_execsql_test func4-1.45 {
179 SELECT tointeger(9007199254740992);
181 do_execsql_test func4-1.46 {
182 SELECT tointeger(9007199254740992 + 1);
184 do_execsql_test func4-1.47 {
185 SELECT tointeger(9223372036854775807 - 1);
186 } {9223372036854775806}
187 do_execsql_test func4-1.48 {
188 SELECT tointeger(9223372036854775807);
189 } {9223372036854775807}
190 if {$highPrecision(1)} {
191 do_execsql_test func4-1.49 {
192 SELECT tointeger(9223372036854775807 + 1);
194 do_execsql_test func4-1.50 {
195 SELECT tointeger(9223372036854775808 - 1);
197 do_execsql_test func4-1.51 {
198 SELECT tointeger(9223372036854775808);
200 do_execsql_test func4-1.52 {
201 SELECT tointeger(9223372036854775808 + 1);
204 do_execsql_test func4-1.53 {
205 SELECT tointeger(18446744073709551616 - 1);
207 do_execsql_test func4-1.54 {
208 SELECT tointeger(18446744073709551616);
210 do_execsql_test func4-1.55 {
211 SELECT tointeger(18446744073709551616 + 1);
214 ifcapable floatingpoint {
216 do_execsql_test func4-2.1 {
219 do_execsql_test func4-2.2 {
222 do_execsql_test func4-2.3 {
225 do_execsql_test func4-2.4 {
226 SELECT toreal('1234');
228 do_execsql_test func4-2.5 {
229 SELECT toreal(' 1234');
231 do_execsql_test func4-2.6 {
232 SELECT toreal('bad');
234 do_execsql_test func4-2.7 {
235 SELECT toreal('0xBAD');
237 do_execsql_test func4-2.8 {
238 SELECT toreal('123BAD');
240 do_execsql_test func4-2.9 {
241 SELECT toreal('0x123BAD');
243 do_execsql_test func4-2.10 {
244 SELECT toreal('123NO');
246 do_execsql_test func4-2.11 {
247 SELECT toreal('0x123NO');
249 do_execsql_test func4-2.12 {
250 SELECT toreal('-0x1');
252 do_execsql_test func4-2.13 {
253 SELECT toreal('-0x0');
255 do_execsql_test func4-2.14 {
256 SELECT toreal('0x0');
258 do_execsql_test func4-2.15 {
259 SELECT toreal('0x1');
261 do_execsql_test func4-2.16 {
264 do_execsql_test func4-2.17 {
267 do_execsql_test func4-2.18 {
270 do_execsql_test func4-2.19 {
273 do_execsql_test func4-2.20 {
274 SELECT toreal(-1.79769313486232e308 - 1);
276 do_execsql_test func4-2.21 {
277 SELECT toreal(-1.79769313486232e308);
279 do_execsql_test func4-2.22 {
280 SELECT toreal(-1.79769313486232e308 + 1);
282 do_execsql_test func4-2.23 {
283 SELECT toreal(-9223372036854775808 - 1);
284 } {-9.223372036854776e+18}
285 do_execsql_test func4-2.24 {
286 SELECT toreal(-9223372036854775808);
288 if {$highPrecision(2)} {
289 do_execsql_test func4-2.25 {
290 SELECT toreal(-9223372036854775808 + 1);
293 do_execsql_test func4-2.26 {
294 SELECT toreal(-9223372036854775807 - 1);
296 if {$highPrecision(2)} {
297 do_execsql_test func4-2.27 {
298 SELECT toreal(-9223372036854775807);
300 do_execsql_test func4-2.28 {
301 SELECT toreal(-9223372036854775807 + 1);
304 do_execsql_test func4-2.29 {
305 SELECT toreal(-2147483648 - 1);
307 do_execsql_test func4-2.30 {
308 SELECT toreal(-2147483648);
310 do_execsql_test func4-2.31 {
311 SELECT toreal(-2147483648 + 1);
313 do_execsql_test func4-2.32 {
314 SELECT toreal(2147483647 - 1);
316 do_execsql_test func4-2.33 {
317 SELECT toreal(2147483647);
319 do_execsql_test func4-2.34 {
320 SELECT toreal(2147483647 + 1);
322 if {$highPrecision(2)} {
323 do_execsql_test func4-2.35 {
324 SELECT toreal(9223372036854775807 - 1);
326 if {$highPrecision(1)} {
327 do_execsql_test func4-2.36 {
328 SELECT toreal(9223372036854775807);
332 do_execsql_test func4-2.37 {
333 SELECT toreal(9223372036854775807 + 1);
334 } {9.223372036854776e+18}
335 do_execsql_test func4-2.38 {
336 SELECT toreal(1.79769313486232e308 - 1);
338 do_execsql_test func4-2.39 {
339 SELECT toreal(1.79769313486232e308);
341 do_execsql_test func4-2.40 {
342 SELECT toreal(1.79769313486232e308 + 1);
344 do_execsql_test func4-2.41 {
345 SELECT toreal(4503599627370496 - 1);
346 } {4503599627370495.0}
347 do_execsql_test func4-2.42 {
348 SELECT toreal(4503599627370496);
349 } {4503599627370496.0}
350 do_execsql_test func4-2.43 {
351 SELECT toreal(4503599627370496 + 1);
352 } {4503599627370497.0}
353 do_execsql_test func4-2.44 {
354 SELECT toreal(9007199254740992 - 1);
355 } {9007199254740991.0}
356 do_execsql_test func4-2.45 {
357 SELECT toreal(9007199254740992);
358 } {9007199254740992.0}
359 if {$highPrecision(3)} {
360 do_execsql_test func4-2.46 {
361 SELECT toreal(9007199254740992 + 1);
364 do_execsql_test func4-2.46 {
365 SELECT toreal(9007199254740992 + 1);
366 } {9007199254740992.0}
368 do_execsql_test func4-2.47 {
369 SELECT toreal(9007199254740992 + 2);
370 } {9007199254740994.0}
371 do_execsql_test func4-2.48 {
372 SELECT toreal(tointeger(9223372036854775808) - 1);
374 if {$highPrecision(1)} {
375 do_execsql_test func4-2.49 {
376 SELECT toreal(tointeger(9223372036854775808));
378 do_execsql_test func4-2.50 {
379 SELECT toreal(tointeger(9223372036854775808) + 1);
382 do_execsql_test func4-2.51 {
383 SELECT toreal(tointeger(18446744073709551616) - 1);
385 do_execsql_test func4-2.52 {
386 SELECT toreal(tointeger(18446744073709551616));
388 do_execsql_test func4-2.53 {
389 SELECT toreal(tointeger(18446744073709551616) + 1);
394 do_execsql_test func4-3.1 {
396 x INTEGER CHECK(tointeger(x) IS NOT NULL)
401 INSERT INTO t1 (x) VALUES (NULL);
403 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
406 INSERT INTO t1 (x) VALUES (NULL);
408 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
411 INSERT INTO t1 (x) VALUES ('');
413 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
416 INSERT INTO t1 (x) VALUES ('bad');
418 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
421 INSERT INTO t1 (x) VALUES ('1234bad');
423 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
426 INSERT INTO t1 (x) VALUES ('1234.56bad');
428 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
431 INSERT INTO t1 (x) VALUES (1234);
436 INSERT INTO t1 (x) VALUES (1234.56);
438 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
441 INSERT INTO t1 (x) VALUES ('1234');
446 INSERT INTO t1 (x) VALUES ('1234.56');
448 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
451 INSERT INTO t1 (x) VALUES (ZEROBLOB(4));
453 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
456 INSERT INTO t1 (x) VALUES (X'');
458 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
461 INSERT INTO t1 (x) VALUES (X'1234');
463 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
466 INSERT INTO t1 (x) VALUES (X'12345678');
468 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
471 INSERT INTO t1 (x) VALUES ('1234.00');
476 INSERT INTO t1 (x) VALUES (1234.00);
481 INSERT INTO t1 (x) VALUES ('-9223372036854775809');
483 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
484 if {$highPrecision(1)} {
487 INSERT INTO t1 (x) VALUES (9223372036854775808);
489 } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
491 do_execsql_test func4-3.20 {
492 SELECT x FROM t1 WHERE x>0 ORDER BY x;
493 } {1234 1234 1234 1234}
495 ifcapable floatingpoint {
496 do_execsql_test func4-4.1 {
498 x REAL CHECK(toreal(x) IS NOT NULL)
503 INSERT INTO t2 (x) VALUES (NULL);
505 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
508 INSERT INTO t2 (x) VALUES (NULL);
510 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
513 INSERT INTO t2 (x) VALUES ('');
515 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
518 INSERT INTO t2 (x) VALUES ('bad');
520 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
523 INSERT INTO t2 (x) VALUES ('1234bad');
525 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
528 INSERT INTO t2 (x) VALUES ('1234.56bad');
530 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
533 INSERT INTO t2 (x) VALUES (1234);
538 INSERT INTO t2 (x) VALUES (1234.56);
543 INSERT INTO t2 (x) VALUES ('1234');
548 INSERT INTO t2 (x) VALUES ('1234.56');
553 INSERT INTO t2 (x) VALUES (ZEROBLOB(4));
555 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
558 INSERT INTO t2 (x) VALUES (X'');
560 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
563 INSERT INTO t2 (x) VALUES (X'1234');
565 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
568 INSERT INTO t2 (x) VALUES (X'12345678');
570 } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}}
571 do_execsql_test func4-4.16 {
572 SELECT x FROM t2 ORDER BY x;
573 } {1234.0 1234.0 1234.56 1234.56}
577 ifcapable floatingpoint {
578 do_execsql_test func4-5.1 {
579 SELECT tointeger(toreal('1234'));
581 do_execsql_test func4-5.2 {
582 SELECT tointeger(toreal(-1));
584 do_execsql_test func4-5.3 {
585 SELECT tointeger(toreal(-0));
587 do_execsql_test func4-5.4 {
588 SELECT tointeger(toreal(0));
590 do_execsql_test func4-5.5 {
591 SELECT tointeger(toreal(1));
593 do_execsql_test func4-5.6 {
594 SELECT tointeger(toreal(-9223372036854775808 - 1));
596 do_execsql_test func4-5.7 {
597 SELECT tointeger(toreal(-9223372036854775808));
599 if {$highPrecision(2)} {
600 do_execsql_test func4-5.8 {
601 SELECT tointeger(toreal(-9223372036854775808 + 1));
604 do_execsql_test func4-5.9 {
605 SELECT tointeger(toreal(-2147483648 - 1));
607 do_execsql_test func4-5.10 {
608 SELECT tointeger(toreal(-2147483648));
610 do_execsql_test func4-5.11 {
611 SELECT tointeger(toreal(-2147483648 + 1));
613 do_execsql_test func4-5.12 {
614 SELECT tointeger(toreal(2147483647 - 1));
616 do_execsql_test func4-5.13 {
617 SELECT tointeger(toreal(2147483647));
619 do_execsql_test func4-5.14 {
620 SELECT tointeger(toreal(2147483647 + 1));
622 do_execsql_test func4-5.15 {
623 SELECT tointeger(toreal(9223372036854775807 - 1));
625 if {$highPrecision(1)} {
626 do_execsql_test func4-5.16 {
627 SELECT tointeger(toreal(9223372036854775807));
629 do_execsql_test func4-5.17 {
630 SELECT tointeger(toreal(9223372036854775807 + 1));
633 do_execsql_test func4-5.18 {
634 SELECT tointeger(toreal(4503599627370496 - 1));
636 do_execsql_test func4-5.19 {
637 SELECT tointeger(toreal(4503599627370496));
639 do_execsql_test func4-5.20 {
640 SELECT tointeger(toreal(4503599627370496 + 1));
642 do_execsql_test func4-5.21 {
643 SELECT tointeger(toreal(9007199254740992 - 1));
645 do_execsql_test func4-5.22 {
646 SELECT tointeger(toreal(9007199254740992));
648 if {$highPrecision(3)} {
649 do_execsql_test func4-5.23 {
650 SELECT tointeger(toreal(9007199254740992 + 1));
653 do_execsql_test func4-5.23 {
654 SELECT tointeger(toreal(9007199254740992 + 1));
657 do_execsql_test func4-5.24 {
658 SELECT tointeger(toreal(9007199254740992 + 2));
660 if {$highPrecision(1)} {
661 do_execsql_test func4-5.25 {
662 SELECT tointeger(toreal(9223372036854775808 - 1));
664 do_execsql_test func4-5.26 {
665 SELECT tointeger(toreal(9223372036854775808));
667 do_execsql_test func4-5.27 {
668 SELECT tointeger(toreal(9223372036854775808 + 1));
671 do_execsql_test func4-5.28 {
672 SELECT tointeger(toreal(18446744073709551616 - 1));
674 do_execsql_test func4-5.29 {
675 SELECT tointeger(toreal(18446744073709551616));
677 do_execsql_test func4-5.30 {
678 SELECT tointeger(toreal(18446744073709551616 + 1));
682 for {set i 0} {$i < 10} {incr i} {
683 if {$i == 8} continue
684 do_execsql_test func4-6.1.$i.1 [subst {
685 SELECT tointeger(x'[string repeat 01 $i]');
687 ifcapable floatingpoint {
688 do_execsql_test func4-6.1.$i.2 [subst {
689 SELECT toreal(x'[string repeat 01 $i]');
694 do_execsql_test func4-6.2.1 {
695 SELECT tointeger(x'0102030405060708');
696 } {578437695752307201}
697 do_execsql_test func4-6.2.2 {
698 SELECT tointeger(x'0807060504030201');
699 } {72623859790382856}
701 ifcapable floatingpoint {
702 do_execsql_test func4-6.3.1 {
703 SELECT toreal(x'ffefffffffffffff');
704 } {-1.7976931348623157e+308}
705 do_execsql_test func4-6.3.2 {
706 SELECT toreal(x'8010000000000000');
707 } {-2.2250738585072014e-308}
708 do_execsql_test func4-6.3.3 {
709 SELECT toreal(x'c000000000000000');
711 do_execsql_test func4-6.3.4 {
712 SELECT toreal(x'bff0000000000000');
714 do_execsql_test func4-6.3.5 {
715 SELECT toreal(x'8000000000000000');
717 do_execsql_test func4-6.3.6 {
718 SELECT toreal(x'0000000000000000');
720 do_execsql_test func4-6.3.7 {
721 SELECT toreal(x'3ff0000000000000');
723 do_execsql_test func4-6.3.8 {
724 SELECT toreal(x'4000000000000000');
726 do_execsql_test func4-6.3.9 {
727 SELECT toreal(x'0010000000000000');
728 } {2.2250738585072014e-308}
729 do_execsql_test func4-6.3.10 {
730 SELECT toreal(x'7fefffffffffffff');
731 } {1.7976931348623157e+308}
732 do_execsql_test func4-6.3.11 {
733 SELECT toreal(x'8000000000000001');
735 do_execsql_test func4-6.3.12 {
736 SELECT toreal(x'800fffffffffffff');
737 } {-2.225073858507201e-308}
738 do_execsql_test func4-6.3.13 {
739 SELECT toreal(x'0000000000000001');
741 do_execsql_test func4-6.3.14 {
742 SELECT toreal(x'000fffffffffffff');
743 } {2.225073858507201e-308}
744 do_execsql_test func4-6.3.15 {
745 SELECT toreal(x'fff0000000000000');
747 do_execsql_test func4-6.3.16 {
748 SELECT toreal(x'7ff0000000000000');
750 do_execsql_test func4-6.3.17 {
751 SELECT toreal(x'fff8000000000000');
753 do_execsql_test func4-6.3.18 {
754 SELECT toreal(x'fff0000000000001');
756 do_execsql_test func4-6.3.19 {
757 SELECT toreal(x'fff7ffffffffffff');
759 do_execsql_test func4-6.3.20 {
760 SELECT toreal(x'7ff0000000000001');
762 do_execsql_test func4-6.3.21 {
763 SELECT toreal(x'7ff7ffffffffffff');
765 do_execsql_test func4-6.3.22 {
766 SELECT toreal(x'fff8000000000001');
768 do_execsql_test func4-6.3.23 {
769 SELECT toreal(x'ffffffffffffffff');
771 do_execsql_test func4-6.3.24 {
772 SELECT toreal(x'7ff8000000000000');
774 do_execsql_test func4-6.3.25 {
775 SELECT toreal(x'7fffffffffffffff');
779 set tcl_precision $saved_tcl_precision
780 unset saved_tcl_precision