xref: /sqlite-3.40.0/test/func4.test (revision a17713ff)
1# 2013 March 10
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 focus of
12# this file is testing the tointeger() and toreal() functions.
13#
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.
17#
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20set saved_tcl_precision $tcl_precision
21set tcl_precision 0
22
23do_execsql_test func4-1.1 {
24  SELECT tointeger(NULL);
25} {{}}
26do_execsql_test func4-1.2 {
27  SELECT tointeger('');
28} {{}}
29do_execsql_test func4-1.3 {
30  SELECT tointeger('   ');
31} {{}}
32do_execsql_test func4-1.4 {
33  SELECT tointeger('1234');
34} {1234}
35do_execsql_test func4-1.5 {
36  SELECT tointeger('   1234');
37} {{}}
38do_execsql_test func4-1.6 {
39  SELECT tointeger('bad');
40} {{}}
41do_execsql_test func4-1.7 {
42  SELECT tointeger('0xBAD');
43} {{}}
44do_execsql_test func4-1.8 {
45  SELECT tointeger('123BAD');
46} {{}}
47do_execsql_test func4-1.9 {
48  SELECT tointeger('0x123BAD');
49} {{}}
50do_execsql_test func4-1.10 {
51  SELECT tointeger('123NO');
52} {{}}
53do_execsql_test func4-1.11 {
54  SELECT tointeger('0x123NO');
55} {{}}
56do_execsql_test func4-1.12 {
57  SELECT tointeger('-0x1');
58} {{}}
59do_execsql_test func4-1.13 {
60  SELECT tointeger('-0x0');
61} {{}}
62do_execsql_test func4-1.14 {
63  SELECT tointeger('0x0');
64} {{}}
65do_execsql_test func4-1.15 {
66  SELECT tointeger('0x1');
67} {{}}
68do_execsql_test func4-1.16 {
69  SELECT tointeger(-1);
70} {-1}
71do_execsql_test func4-1.17 {
72  SELECT tointeger(-0);
73} {0}
74do_execsql_test func4-1.18 {
75  SELECT tointeger(0);
76} {0}
77do_execsql_test func4-1.19 {
78  SELECT tointeger(1);
79} {1}
80do_execsql_test func4-1.20 {
81  SELECT tointeger(-1.79769313486232e308 - 1);
82} {{}}
83do_execsql_test func4-1.21 {
84  SELECT tointeger(-1.79769313486232e308);
85} {{}}
86do_execsql_test func4-1.22 {
87  SELECT tointeger(-1.79769313486232e308 + 1);
88} {{}}
89do_execsql_test func4-1.23 {
90  SELECT tointeger(-9223372036854775808 - 1);
91} {-9223372036854775808}
92do_execsql_test func4-1.24 {
93  SELECT tointeger(-9223372036854775808);
94} {-9223372036854775808}
95do_execsql_test func4-1.25 {
96  SELECT tointeger(-9223372036854775808 + 1);
97} {-9223372036854775807}
98do_execsql_test func4-1.26 {
99  SELECT tointeger(-9223372036854775807 - 1);
100} {-9223372036854775808}
101do_execsql_test func4-1.27 {
102  SELECT tointeger(-9223372036854775807);
103} {-9223372036854775807}
104do_execsql_test func4-1.28 {
105  SELECT tointeger(-9223372036854775807 + 1);
106} {-9223372036854775806}
107do_execsql_test func4-1.29 {
108  SELECT tointeger(-2147483648 - 1);
109} {-2147483649}
110do_execsql_test func4-1.30 {
111  SELECT tointeger(-2147483648);
112} {-2147483648}
113do_execsql_test func4-1.31 {
114  SELECT tointeger(-2147483648 + 1);
115} {-2147483647}
116do_execsql_test func4-1.32 {
117  SELECT tointeger(2147483647 - 1);
118} {2147483646}
119do_execsql_test func4-1.33 {
120  SELECT tointeger(2147483647);
121} {2147483647}
122do_execsql_test func4-1.34 {
123  SELECT tointeger(2147483647 + 1);
124} {2147483648}
125do_execsql_test func4-1.35 {
126  SELECT tointeger(9223372036854775807 - 1);
127} {9223372036854775806}
128do_execsql_test func4-1.36 {
129  SELECT tointeger(9223372036854775807);
130} {9223372036854775807}
131do_execsql_test func4-1.37 {
132  SELECT tointeger(9223372036854775807 + 1);
133} {{}}
134do_execsql_test func4-1.38 {
135  SELECT tointeger(1.79769313486232e308 - 1);
136} {{}}
137do_execsql_test func4-1.39 {
138  SELECT tointeger(1.79769313486232e308);
139} {{}}
140do_execsql_test func4-1.40 {
141  SELECT tointeger(1.79769313486232e308 + 1);
142} {{}}
143do_execsql_test func4-1.41 {
144  SELECT tointeger(4503599627370496 - 1);
145} {4503599627370495}
146do_execsql_test func4-1.42 {
147  SELECT tointeger(4503599627370496);
148} {4503599627370496}
149do_execsql_test func4-1.43 {
150  SELECT tointeger(4503599627370496 + 1);
151} {4503599627370497}
152do_execsql_test func4-1.44 {
153  SELECT tointeger(9007199254740992 - 1);
154} {9007199254740991}
155do_execsql_test func4-1.45 {
156  SELECT tointeger(9007199254740992);
157} {9007199254740992}
158do_execsql_test func4-1.46 {
159  SELECT tointeger(9007199254740992 + 1);
160} {9007199254740993}
161do_execsql_test func4-1.47 {
162  SELECT tointeger(9223372036854775807 - 1);
163} {9223372036854775806}
164do_execsql_test func4-1.48 {
165  SELECT tointeger(9223372036854775807);
166} {9223372036854775807}
167do_execsql_test func4-1.49 {
168  SELECT tointeger(9223372036854775807 + 1);
169} {{}}
170do_execsql_test func4-1.50 {
171  SELECT tointeger(9223372036854775808 - 1);
172} {{}}
173do_execsql_test func4-1.51 {
174  SELECT tointeger(9223372036854775808);
175} {{}}
176do_execsql_test func4-1.52 {
177  SELECT tointeger(9223372036854775808 + 1);
178} {{}}
179do_execsql_test func4-1.53 {
180  SELECT tointeger(18446744073709551616 - 1);
181} {{}}
182do_execsql_test func4-1.54 {
183  SELECT tointeger(18446744073709551616);
184} {{}}
185do_execsql_test func4-1.55 {
186  SELECT tointeger(18446744073709551616 + 1);
187} {{}}
188
189ifcapable floatingpoint {
190  set highPrecision [expr \
191      {[memdbsql {SELECT toreal(-9223372036854775808 + 1);}] eq {{}}}]
192
193  do_execsql_test func4-2.1 {
194    SELECT toreal(NULL);
195  } {{}}
196  do_execsql_test func4-2.2 {
197    SELECT toreal('');
198  } {{}}
199  do_execsql_test func4-2.3 {
200    SELECT toreal('   ');
201  } {{}}
202  do_execsql_test func4-2.4 {
203    SELECT toreal('1234');
204  } {1234.0}
205  do_execsql_test func4-2.5 {
206    SELECT toreal('   1234');
207  } {{}}
208  do_execsql_test func4-2.6 {
209    SELECT toreal('bad');
210  } {{}}
211  do_execsql_test func4-2.7 {
212    SELECT toreal('0xBAD');
213  } {{}}
214  do_execsql_test func4-2.8 {
215    SELECT toreal('123BAD');
216  } {{}}
217  do_execsql_test func4-2.9 {
218    SELECT toreal('0x123BAD');
219  } {{}}
220  do_execsql_test func4-2.10 {
221    SELECT toreal('123NO');
222  } {{}}
223  do_execsql_test func4-2.11 {
224    SELECT toreal('0x123NO');
225  } {{}}
226  do_execsql_test func4-2.12 {
227    SELECT toreal('-0x1');
228  } {{}}
229  do_execsql_test func4-2.13 {
230    SELECT toreal('-0x0');
231  } {{}}
232  do_execsql_test func4-2.14 {
233    SELECT toreal('0x0');
234  } {{}}
235  do_execsql_test func4-2.15 {
236    SELECT toreal('0x1');
237  } {{}}
238  do_execsql_test func4-2.16 {
239    SELECT toreal(-1);
240  } {-1.0}
241  do_execsql_test func4-2.17 {
242    SELECT toreal(-0);
243  } {0.0}
244  do_execsql_test func4-2.18 {
245    SELECT toreal(0);
246  } {0.0}
247  do_execsql_test func4-2.19 {
248    SELECT toreal(1);
249  } {1.0}
250  do_execsql_test func4-2.20 {
251    SELECT toreal(-1.79769313486232e308 - 1);
252  } {-Inf}
253  do_execsql_test func4-2.21 {
254    SELECT toreal(-1.79769313486232e308);
255  } {-Inf}
256  do_execsql_test func4-2.22 {
257    SELECT toreal(-1.79769313486232e308 + 1);
258  } {-Inf}
259  do_execsql_test func4-2.23 {
260    SELECT toreal(-9223372036854775808 - 1);
261  } {-9.223372036854776e+18}
262  do_execsql_test func4-2.24 {
263    SELECT toreal(-9223372036854775808);
264  } {-9.223372036854776e+18}
265  if {$highPrecision} {
266    do_execsql_test func4-2.25 {
267      SELECT toreal(-9223372036854775808 + 1);
268    } {{}}
269  }
270  do_execsql_test func4-2.26 {
271    SELECT toreal(-9223372036854775807 - 1);
272  } {-9.223372036854776e+18}
273  if {$highPrecision} {
274    do_execsql_test func4-2.27 {
275      SELECT toreal(-9223372036854775807);
276    } {{}}
277    do_execsql_test func4-2.28 {
278      SELECT toreal(-9223372036854775807 + 1);
279    } {{}}
280  }
281  do_execsql_test func4-2.29 {
282    SELECT toreal(-2147483648 - 1);
283  } {-2147483649.0}
284  do_execsql_test func4-2.30 {
285    SELECT toreal(-2147483648);
286  } {-2147483648.0}
287  do_execsql_test func4-2.31 {
288    SELECT toreal(-2147483648 + 1);
289  } {-2147483647.0}
290  do_execsql_test func4-2.32 {
291    SELECT toreal(2147483647 - 1);
292  } {2147483646.0}
293  do_execsql_test func4-2.33 {
294    SELECT toreal(2147483647);
295  } {2147483647.0}
296  do_execsql_test func4-2.34 {
297    SELECT toreal(2147483647 + 1);
298  } {2147483648.0}
299  if {$highPrecision} {
300    do_execsql_test func4-2.35 {
301      SELECT toreal(9223372036854775807 - 1);
302    } {{}}
303    do_execsql_test func4-2.36 {
304      SELECT toreal(9223372036854775807);
305    } {{}}
306  }
307  do_execsql_test func4-2.37 {
308    SELECT toreal(9223372036854775807 + 1);
309  } {9.223372036854776e+18}
310  do_execsql_test func4-2.38 {
311    SELECT toreal(1.79769313486232e308 - 1);
312  } {Inf}
313  do_execsql_test func4-2.39 {
314    SELECT toreal(1.79769313486232e308);
315  } {Inf}
316  do_execsql_test func4-2.40 {
317    SELECT toreal(1.79769313486232e308 + 1);
318  } {Inf}
319  do_execsql_test func4-2.41 {
320    SELECT toreal(4503599627370496 - 1);
321  } {4503599627370495.0}
322  do_execsql_test func4-2.42 {
323    SELECT toreal(4503599627370496);
324  } {4503599627370496.0}
325  do_execsql_test func4-2.43 {
326    SELECT toreal(4503599627370496 + 1);
327  } {4503599627370497.0}
328  do_execsql_test func4-2.44 {
329    SELECT toreal(9007199254740992 - 1);
330  } {9007199254740991.0}
331  do_execsql_test func4-2.45 {
332    SELECT toreal(9007199254740992);
333  } {9007199254740992.0}
334  if {$highPrecision} {
335    do_execsql_test func4-2.46 {
336      SELECT toreal(9007199254740992 + 1);
337    } {{}}
338  }
339  do_execsql_test func4-2.47 {
340    SELECT toreal(9007199254740992 + 2);
341  } {9007199254740994.0}
342  do_execsql_test func4-2.48 {
343    SELECT toreal(tointeger(9223372036854775808) - 1);
344  } {{}}
345  do_execsql_test func4-2.49 {
346    SELECT toreal(tointeger(9223372036854775808));
347  } {{}}
348  do_execsql_test func4-2.50 {
349    SELECT toreal(tointeger(9223372036854775808) + 1);
350  } {{}}
351  do_execsql_test func4-2.51 {
352    SELECT toreal(tointeger(18446744073709551616) - 1);
353  } {{}}
354  do_execsql_test func4-2.52 {
355    SELECT toreal(tointeger(18446744073709551616));
356  } {{}}
357  do_execsql_test func4-2.53 {
358    SELECT toreal(tointeger(18446744073709551616) + 1);
359  } {{}}
360}
361
362ifcapable check {
363  do_execsql_test func4-3.1 {
364    CREATE TABLE t1(
365      x INTEGER CHECK(tointeger(x) IS NOT NULL)
366    );
367  } {}
368  do_test func4-3.2 {
369    catchsql {
370      INSERT INTO t1 (x) VALUES (NULL);
371    }
372  } {1 {constraint failed}}
373  do_test func4-3.3 {
374    catchsql {
375      INSERT INTO t1 (x) VALUES (NULL);
376    }
377  } {1 {constraint failed}}
378  do_test func4-3.4 {
379    catchsql {
380      INSERT INTO t1 (x) VALUES ('');
381    }
382  } {1 {constraint failed}}
383  do_test func4-3.5 {
384    catchsql {
385      INSERT INTO t1 (x) VALUES ('bad');
386    }
387  } {1 {constraint failed}}
388  do_test func4-3.6 {
389    catchsql {
390      INSERT INTO t1 (x) VALUES ('1234bad');
391    }
392  } {1 {constraint failed}}
393  do_test func4-3.7 {
394    catchsql {
395      INSERT INTO t1 (x) VALUES ('1234.56bad');
396    }
397  } {1 {constraint failed}}
398  do_test func4-3.8 {
399    catchsql {
400      INSERT INTO t1 (x) VALUES (1234);
401    }
402  } {0 {}}
403  do_test func4-3.9 {
404    catchsql {
405      INSERT INTO t1 (x) VALUES (1234.56);
406    }
407  } {1 {constraint failed}}
408  do_test func4-3.10 {
409    catchsql {
410      INSERT INTO t1 (x) VALUES ('1234');
411    }
412  } {0 {}}
413  do_test func4-3.11 {
414    catchsql {
415      INSERT INTO t1 (x) VALUES ('1234.56');
416    }
417  } {1 {constraint failed}}
418  do_test func4-3.12 {
419    catchsql {
420      INSERT INTO t1 (x) VALUES (ZEROBLOB(4));
421    }
422  } {1 {constraint failed}}
423  do_test func4-3.13 {
424    catchsql {
425      INSERT INTO t1 (x) VALUES (X'');
426    }
427  } {1 {constraint failed}}
428  do_test func4-3.14 {
429    catchsql {
430      INSERT INTO t1 (x) VALUES (X'1234');
431    }
432  } {1 {constraint failed}}
433  do_test func4-3.15 {
434    catchsql {
435      INSERT INTO t1 (x) VALUES (X'12345678');
436    }
437  } {1 {constraint failed}}
438  do_test func4-3.16 {
439    catchsql {
440      INSERT INTO t1 (x) VALUES ('1234.00');
441    }
442  } {1 {constraint failed}}
443  do_test func4-3.17 {
444    catchsql {
445      INSERT INTO t1 (x) VALUES (1234.00);
446    }
447  } {0 {}}
448  do_test func4-3.18 {
449    catchsql {
450      INSERT INTO t1 (x) VALUES ('-9223372036854775809');
451    }
452  } {1 {constraint failed}}
453  do_test func4-3.19 {
454    catchsql {
455      INSERT INTO t1 (x) VALUES (9223372036854775808);
456    }
457  } {1 {constraint failed}}
458  do_execsql_test func4-3.20 {
459    SELECT x FROM t1 ORDER BY x;
460  } {1234 1234 1234}
461
462  ifcapable floatingpoint {
463    do_execsql_test func4-4.1 {
464      CREATE TABLE t2(
465        x REAL CHECK(toreal(x) IS NOT NULL)
466      );
467    } {}
468    do_test func4-4.2 {
469      catchsql {
470        INSERT INTO t2 (x) VALUES (NULL);
471      }
472    } {1 {constraint failed}}
473    do_test func4-4.3 {
474      catchsql {
475        INSERT INTO t2 (x) VALUES (NULL);
476      }
477    } {1 {constraint failed}}
478    do_test func4-4.4 {
479      catchsql {
480        INSERT INTO t2 (x) VALUES ('');
481      }
482    } {1 {constraint failed}}
483    do_test func4-4.5 {
484      catchsql {
485        INSERT INTO t2 (x) VALUES ('bad');
486      }
487    } {1 {constraint failed}}
488    do_test func4-4.6 {
489      catchsql {
490        INSERT INTO t2 (x) VALUES ('1234bad');
491      }
492    } {1 {constraint failed}}
493    do_test func4-4.7 {
494      catchsql {
495        INSERT INTO t2 (x) VALUES ('1234.56bad');
496      }
497    } {1 {constraint failed}}
498    do_test func4-4.8 {
499      catchsql {
500        INSERT INTO t2 (x) VALUES (1234);
501      }
502    } {0 {}}
503    do_test func4-4.9 {
504      catchsql {
505        INSERT INTO t2 (x) VALUES (1234.56);
506      }
507    } {0 {}}
508    do_test func4-4.10 {
509      catchsql {
510        INSERT INTO t2 (x) VALUES ('1234');
511      }
512    } {0 {}}
513    do_test func4-4.11 {
514      catchsql {
515        INSERT INTO t2 (x) VALUES ('1234.56');
516      }
517    } {0 {}}
518    do_test func4-4.12 {
519      catchsql {
520        INSERT INTO t2 (x) VALUES (ZEROBLOB(4));
521      }
522    } {1 {constraint failed}}
523    do_test func4-4.13 {
524      catchsql {
525        INSERT INTO t2 (x) VALUES (X'');
526      }
527    } {1 {constraint failed}}
528    do_test func4-4.14 {
529      catchsql {
530        INSERT INTO t2 (x) VALUES (X'1234');
531      }
532    } {1 {constraint failed}}
533    do_test func4-4.15 {
534      catchsql {
535        INSERT INTO t2 (x) VALUES (X'12345678');
536      }
537    } {1 {constraint failed}}
538    do_execsql_test func4-4.16 {
539      SELECT x FROM t2 ORDER BY x;
540    } {1234.0 1234.0 1234.56 1234.56}
541  }
542}
543
544ifcapable floatingpoint {
545  do_execsql_test func4-5.1 {
546    SELECT tointeger(toreal('1234'));
547  } {1234}
548  do_execsql_test func4-5.2 {
549    SELECT tointeger(toreal(-1));
550  } {-1}
551  do_execsql_test func4-5.3 {
552    SELECT tointeger(toreal(-0));
553  } {0}
554  do_execsql_test func4-5.4 {
555    SELECT tointeger(toreal(0));
556  } {0}
557  do_execsql_test func4-5.5 {
558    SELECT tointeger(toreal(1));
559  } {1}
560  do_execsql_test func4-5.6 {
561    SELECT tointeger(toreal(-9223372036854775808 - 1));
562  } {-9223372036854775808}
563  do_execsql_test func4-5.7 {
564    SELECT tointeger(toreal(-9223372036854775808));
565  } {-9223372036854775808}
566  if {$highPrecision} {
567    do_execsql_test func4-5.8 {
568      SELECT tointeger(toreal(-9223372036854775808 + 1));
569    } {{}}
570  }
571  do_execsql_test func4-5.9 {
572    SELECT tointeger(toreal(-2147483648 - 1));
573  } {-2147483649}
574  do_execsql_test func4-5.10 {
575    SELECT tointeger(toreal(-2147483648));
576  } {-2147483648}
577  do_execsql_test func4-5.11 {
578    SELECT tointeger(toreal(-2147483648 + 1));
579  } {-2147483647}
580  do_execsql_test func4-5.12 {
581    SELECT tointeger(toreal(2147483647 - 1));
582  } {2147483646}
583  do_execsql_test func4-5.13 {
584    SELECT tointeger(toreal(2147483647));
585  } {2147483647}
586  do_execsql_test func4-5.14 {
587    SELECT tointeger(toreal(2147483647 + 1));
588  } {2147483648}
589  do_execsql_test func4-5.15 {
590    SELECT tointeger(toreal(9223372036854775807 - 1));
591  } {{}}
592  do_execsql_test func4-5.16 {
593    SELECT tointeger(toreal(9223372036854775807));
594  } {{}}
595  do_execsql_test func4-5.17 {
596    SELECT tointeger(toreal(9223372036854775807 + 1));
597  } {{}}
598  do_execsql_test func4-5.18 {
599    SELECT tointeger(toreal(4503599627370496 - 1));
600  } {4503599627370495}
601  do_execsql_test func4-5.19 {
602    SELECT tointeger(toreal(4503599627370496));
603  } {4503599627370496}
604  do_execsql_test func4-5.20 {
605    SELECT tointeger(toreal(4503599627370496 + 1));
606  } {4503599627370497}
607  do_execsql_test func4-5.21 {
608    SELECT tointeger(toreal(9007199254740992 - 1));
609  } {9007199254740991}
610  do_execsql_test func4-5.22 {
611    SELECT tointeger(toreal(9007199254740992));
612  } {9007199254740992}
613  if {$highPrecision} {
614    do_execsql_test func4-5.23 {
615      SELECT tointeger(toreal(9007199254740992 + 1));
616    } {{}}
617  }
618  do_execsql_test func4-5.24 {
619    SELECT tointeger(toreal(9007199254740992 + 2));
620  } {9007199254740994}
621  do_execsql_test func4-5.25 {
622    SELECT tointeger(toreal(9223372036854775808 - 1));
623  } {{}}
624  do_execsql_test func4-5.26 {
625    SELECT tointeger(toreal(9223372036854775808));
626  } {{}}
627  do_execsql_test func4-5.27 {
628    SELECT tointeger(toreal(9223372036854775808 + 1));
629  } {{}}
630  do_execsql_test func4-5.28 {
631    SELECT tointeger(toreal(18446744073709551616 - 1));
632  } {{}}
633  do_execsql_test func4-5.29 {
634    SELECT tointeger(toreal(18446744073709551616));
635  } {{}}
636  do_execsql_test func4-5.30 {
637    SELECT tointeger(toreal(18446744073709551616 + 1));
638  } {{}}
639}
640
641set tcl_precision $saved_tcl_precision
642unset saved_tcl_precision
643finish_test
644