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