xref: /sqlite-3.40.0/test/func4.test (revision bc3ec28b)
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
17
18set i 0
19do_execsql_test func4-1.[incr i] {
20  SELECT tointeger(NULL);
21} {{}}
22do_execsql_test func4-1.[incr i] {
23  SELECT tointeger('');
24} {{}}
25do_execsql_test func4-1.[incr i] {
26  SELECT tointeger('   ');
27} {{}}
28do_execsql_test func4-1.[incr i] {
29  SELECT tointeger('1234');
30} {1234}
31do_execsql_test func4-1.[incr i] {
32  SELECT tointeger('   1234');
33} {1234}
34do_execsql_test func4-1.[incr i] {
35  SELECT tointeger('bad');
36} {{}}
37do_execsql_test func4-1.[incr i] {
38  SELECT tointeger('0xBAD');
39} {{}}
40do_execsql_test func4-1.[incr i] {
41  SELECT tointeger('123BAD');
42} {{}}
43do_execsql_test func4-1.[incr i] {
44  SELECT tointeger('0x123BAD');
45} {{}}
46do_execsql_test func4-1.[incr i] {
47  SELECT tointeger('123NO');
48} {{}}
49do_execsql_test func4-1.[incr i] {
50  SELECT tointeger('0x123NO');
51} {{}}
52do_execsql_test func4-1.[incr i] {
53  SELECT tointeger('-0x1');
54} {{}}
55do_execsql_test func4-1.[incr i] {
56  SELECT tointeger('-0x0');
57} {{}}
58do_execsql_test func4-1.[incr i] {
59  SELECT tointeger('0x0');
60} {{}}
61do_execsql_test func4-1.[incr i] {
62  SELECT tointeger('0x1');
63} {{}}
64do_execsql_test func4-1.[incr i] {
65  SELECT tointeger(-1);
66} {-1}
67do_execsql_test func4-1.[incr i] {
68  SELECT tointeger(-0);
69} {0}
70do_execsql_test func4-1.[incr i] {
71  SELECT tointeger(0);
72} {0}
73do_execsql_test func4-1.[incr i] {
74  SELECT tointeger(1);
75} {1}
76do_execsql_test func4-1.[incr i] {
77  SELECT tointeger(-1.79769313486232e308 - 1);
78} {{}}
79do_execsql_test func4-1.[incr i] {
80  SELECT tointeger(-1.79769313486232e308);
81} {{}}
82do_execsql_test func4-1.[incr i] {
83  SELECT tointeger(-1.79769313486232e308 + 1);
84} {{}}
85do_execsql_test func4-1.[incr i] {
86  SELECT tointeger(-9223372036854775808 - 1);
87} {-9223372036854775808}
88do_execsql_test func4-1.[incr i] {
89  SELECT tointeger(-9223372036854775808);
90} {-9223372036854775808}
91do_execsql_test func4-1.[incr i] {
92  SELECT tointeger(-9223372036854775808 + 1);
93} {-9223372036854775807}
94do_execsql_test func4-1.[incr i] {
95  SELECT tointeger(-2147483648 - 1);
96} {-2147483649}
97do_execsql_test func4-1.[incr i] {
98  SELECT tointeger(-2147483648);
99} {-2147483648}
100do_execsql_test func4-1.[incr i] {
101  SELECT tointeger(-2147483648 + 1);
102} {-2147483647}
103do_execsql_test func4-1.[incr i] {
104  SELECT tointeger(2147483647 - 1);
105} {2147483646}
106do_execsql_test func4-1.[incr i] {
107  SELECT tointeger(2147483647);
108} {2147483647}
109do_execsql_test func4-1.[incr i] {
110  SELECT tointeger(2147483647 + 1);
111} {2147483648}
112do_execsql_test func4-1.[incr i] {
113  SELECT tointeger(9223372036854775807 - 1);
114} {9223372036854775806}
115do_execsql_test func4-1.[incr i] {
116  SELECT tointeger(9223372036854775807);
117} {9223372036854775807}
118do_execsql_test func4-1.[incr i] {
119  SELECT tointeger(9223372036854775807 + 1);
120} {-9223372036854775808}
121do_execsql_test func4-1.[incr i] {
122  SELECT tointeger(1.79769313486232e308 - 1);
123} {{}}
124do_execsql_test func4-1.[incr i] {
125  SELECT tointeger(1.79769313486232e308);
126} {{}}
127do_execsql_test func4-1.[incr i] {
128  SELECT tointeger(1.79769313486232e308 + 1);
129} {{}}
130do_execsql_test func4-1.[incr i] {
131  SELECT tointeger(4503599627370496 - 1);
132} {4503599627370495}
133do_execsql_test func4-1.[incr i] {
134  SELECT tointeger(4503599627370496);
135} {4503599627370496}
136do_execsql_test func4-1.[incr i] {
137  SELECT tointeger(4503599627370496 + 1);
138} {4503599627370497}
139do_execsql_test func4-1.[incr i] {
140  SELECT tointeger(9007199254740992 - 1);
141} {9007199254740991}
142do_execsql_test func4-1.[incr i] {
143  SELECT tointeger(9007199254740992);
144} {9007199254740992}
145do_execsql_test func4-1.[incr i] {
146  SELECT tointeger(9007199254740992 + 1);
147} {9007199254740993}
148do_execsql_test func4-1.[incr i] {
149  SELECT tointeger(9223372036854775808 - 1);
150} {-9223372036854775808}
151do_execsql_test func4-1.[incr i] {
152  SELECT tointeger(9223372036854775808);
153} {-9223372036854775808}
154do_execsql_test func4-1.[incr i] {
155  SELECT tointeger(9223372036854775808 + 1);
156} {-9223372036854775808}
157do_execsql_test func4-1.[incr i] {
158  SELECT tointeger(18446744073709551616 - 1);
159} {{}}
160do_execsql_test func4-1.[incr i] {
161  SELECT tointeger(18446744073709551616);
162} {{}}
163do_execsql_test func4-1.[incr i] {
164  SELECT tointeger(18446744073709551616 + 1);
165} {{}}
166
167ifcapable floatingpoint {
168  set i 0
169  do_execsql_test func4-2.[incr i] {
170    SELECT toreal(NULL);
171  } {{}}
172  do_execsql_test func4-2.[incr i] {
173    SELECT toreal('');
174  } {{}}
175  do_execsql_test func4-2.[incr i] {
176    SELECT toreal('   ');
177  } {{}}
178  do_execsql_test func4-2.[incr i] {
179    SELECT toreal('1234');
180  } {1234.0}
181  do_execsql_test func4-2.[incr i] {
182    SELECT toreal('   1234');
183  } {1234.0}
184  do_execsql_test func4-2.[incr i] {
185    SELECT toreal('bad');
186  } {{}}
187  do_execsql_test func4-2.[incr i] {
188    SELECT toreal('0xBAD');
189  } {{}}
190  do_execsql_test func4-2.[incr i] {
191    SELECT toreal('123BAD');
192  } {{}}
193  do_execsql_test func4-2.[incr i] {
194    SELECT toreal('0x123BAD');
195  } {{}}
196  do_execsql_test func4-2.[incr i] {
197    SELECT toreal('123NO');
198  } {{}}
199  do_execsql_test func4-2.[incr i] {
200    SELECT toreal('0x123NO');
201  } {{}}
202  do_execsql_test func4-2.[incr i] {
203    SELECT toreal('-0x1');
204  } {{}}
205  do_execsql_test func4-2.[incr i] {
206    SELECT toreal('-0x0');
207  } {{}}
208  do_execsql_test func4-2.[incr i] {
209    SELECT toreal('0x0');
210  } {{}}
211  do_execsql_test func4-2.[incr i] {
212    SELECT toreal('0x1');
213  } {{}}
214  do_execsql_test func4-2.[incr i] {
215    SELECT toreal(-1);
216  } {-1.0}
217  do_execsql_test func4-2.[incr i] {
218    SELECT toreal(-0);
219  } {0.0}
220  do_execsql_test func4-2.[incr i] {
221    SELECT toreal(0);
222  } {0.0}
223  do_execsql_test func4-2.[incr i] {
224    SELECT toreal(1);
225  } {1.0}
226  do_execsql_test func4-2.[incr i] {
227    SELECT toreal(-1.79769313486232e308 - 1);
228  } {-Inf}
229  do_execsql_test func4-2.[incr i] {
230    SELECT toreal(-1.79769313486232e308);
231  } {-Inf}
232  do_execsql_test func4-2.[incr i] {
233    SELECT toreal(-1.79769313486232e308 + 1);
234  } {-Inf}
235  do_execsql_test func4-2.[incr i] {
236    SELECT toreal(-9223372036854775808 - 1);
237  } {-9.22337203685478e+18}
238  do_execsql_test func4-2.[incr i] {
239    SELECT toreal(-9223372036854775808);
240  } {-9.22337203685478e+18}
241  do_execsql_test func4-2.[incr i] {
242    SELECT toreal(-9223372036854775808 + 1);
243  } {-9.22337203685478e+18}
244  do_execsql_test func4-2.[incr i] {
245    SELECT toreal(-2147483648 - 1);
246  } {-2147483649.0}
247  do_execsql_test func4-2.[incr i] {
248    SELECT toreal(-2147483648);
249  } {-2147483648.0}
250  do_execsql_test func4-2.[incr i] {
251    SELECT toreal(-2147483648 + 1);
252  } {-2147483647.0}
253  do_execsql_test func4-2.[incr i] {
254    SELECT toreal(2147483647 - 1);
255  } {2147483646.0}
256  do_execsql_test func4-2.[incr i] {
257    SELECT toreal(2147483647);
258  } {2147483647.0}
259  do_execsql_test func4-2.[incr i] {
260    SELECT toreal(2147483647 + 1);
261  } {2147483648.0}
262  do_execsql_test func4-2.[incr i] {
263    SELECT toreal(9223372036854775807 - 1);
264  } {9.22337203685478e+18}
265  do_execsql_test func4-2.[incr i] {
266    SELECT toreal(9223372036854775807);
267  } {9.22337203685478e+18}
268  do_execsql_test func4-2.[incr i] {
269    SELECT toreal(9223372036854775807 + 1);
270  } {9.22337203685478e+18}
271  do_execsql_test func4-2.[incr i] {
272    SELECT toreal(1.79769313486232e308 - 1);
273  } {Inf}
274  do_execsql_test func4-2.[incr i] {
275    SELECT toreal(1.79769313486232e308);
276  } {Inf}
277  do_execsql_test func4-2.[incr i] {
278    SELECT toreal(1.79769313486232e308 + 1);
279  } {Inf}
280  do_execsql_test func4-2.[incr i] {
281    SELECT toreal(4503599627370496 - 1);
282  } {4503599627370500.0}
283  do_execsql_test func4-2.[incr i] {
284    SELECT toreal(4503599627370496);
285  } {4503599627370500.0}
286  do_execsql_test func4-2.[incr i] {
287    SELECT toreal(4503599627370496 + 1);
288  } {4503599627370500.0}
289  do_execsql_test func4-2.[incr i] {
290    SELECT toreal(9007199254740992 - 1);
291  } {9007199254740990.0}
292  do_execsql_test func4-2.[incr i] {
293    SELECT toreal(9007199254740992);
294  } {9007199254740990.0}
295  do_execsql_test func4-2.[incr i] {
296    SELECT toreal(9007199254740992 + 1);
297  } {9007199254740990.0}
298  do_execsql_test func4-2.[incr i] {
299    SELECT toreal(9223372036854775808 - 1);
300  } {9.22337203685478e+18}
301  do_execsql_test func4-2.[incr i] {
302    SELECT toreal(9223372036854775808);
303  } {9.22337203685478e+18}
304  do_execsql_test func4-2.[incr i] {
305    SELECT toreal(9223372036854775808 + 1);
306  } {9.22337203685478e+18}
307  do_execsql_test func4-2.[incr i] {
308    SELECT toreal(18446744073709551616 - 1);
309  } {1.84467440737096e+19}
310  do_execsql_test func4-2.[incr i] {
311    SELECT toreal(18446744073709551616);
312  } {1.84467440737096e+19}
313  do_execsql_test func4-2.[incr i] {
314    SELECT toreal(18446744073709551616 + 1);
315  } {1.84467440737096e+19}
316}
317
318ifcapable check {
319  set i 0
320  do_execsql_test func4-3.[incr i] {
321    CREATE TABLE t1(
322      x INTEGER CHECK(tointeger(x) IS NOT NULL AND x = CAST(x AS INTEGER))
323    );
324  } {}
325  do_test func4-3.[incr i] {
326    catchsql {
327      INSERT INTO t1 (x) VALUES (NULL);
328    }
329  } {1 {constraint failed}}
330  do_test func4-3.[incr i] {
331    catchsql {
332      INSERT INTO t1 (x) VALUES (NULL);
333    }
334  } {1 {constraint failed}}
335  do_test func4-3.[incr i] {
336    catchsql {
337      INSERT INTO t1 (x) VALUES ('');
338    }
339  } {1 {constraint failed}}
340  do_test func4-3.[incr i] {
341    catchsql {
342      INSERT INTO t1 (x) VALUES ('bad');
343    }
344  } {1 {constraint failed}}
345  do_test func4-3.[incr i] {
346    catchsql {
347      INSERT INTO t1 (x) VALUES ('1234bad');
348    }
349  } {1 {constraint failed}}
350  do_test func4-3.[incr i] {
351    catchsql {
352      INSERT INTO t1 (x) VALUES ('1234.56bad');
353    }
354  } {1 {constraint failed}}
355  do_test func4-3.[incr i] {
356    catchsql {
357      INSERT INTO t1 (x) VALUES (1234);
358    }
359  } {0 {}}
360  do_test func4-3.[incr i] {
361    catchsql {
362      INSERT INTO t1 (x) VALUES (1234.56);
363    }
364  } {1 {constraint failed}}
365  do_test func4-3.[incr i] {
366    catchsql {
367      INSERT INTO t1 (x) VALUES ('1234');
368    }
369  } {0 {}}
370  do_test func4-3.[incr i] {
371    catchsql {
372      INSERT INTO t1 (x) VALUES ('1234.56');
373    }
374  } {1 {constraint failed}}
375  do_test func4-3.[incr i] {
376    catchsql {
377      INSERT INTO t1 (x) VALUES (ZEROBLOB(4));
378    }
379  } {1 {constraint failed}}
380  do_test func4-3.[incr i] {
381    catchsql {
382      INSERT INTO t1 (x) VALUES (X'');
383    }
384  } {1 {constraint failed}}
385  do_test func4-3.[incr i] {
386    catchsql {
387      INSERT INTO t1 (x) VALUES (X'1234');
388    }
389  } {1 {constraint failed}}
390  do_test func4-3.[incr i] {
391    catchsql {
392      INSERT INTO t1 (x) VALUES (X'12345678');
393    }
394  } {1 {constraint failed}}
395  do_execsql_test func4-3.[incr i] {
396    SELECT x FROM t1 ORDER BY x;
397  } {1234 1234}
398
399  ifcapable floatingpoint {
400    set i 0
401    do_execsql_test func4-4.[incr i] {
402      CREATE TABLE t2(
403        x REAL CHECK(toreal(x) IS NOT NULL)
404      );
405    } {}
406    do_test func4-4.[incr i] {
407      catchsql {
408        INSERT INTO t2 (x) VALUES (NULL);
409      }
410    } {1 {constraint failed}}
411    do_test func4-4.[incr i] {
412      catchsql {
413        INSERT INTO t2 (x) VALUES (NULL);
414      }
415    } {1 {constraint failed}}
416    do_test func4-4.[incr i] {
417      catchsql {
418        INSERT INTO t2 (x) VALUES ('');
419      }
420    } {1 {constraint failed}}
421    do_test func4-4.[incr i] {
422      catchsql {
423        INSERT INTO t2 (x) VALUES ('bad');
424      }
425    } {1 {constraint failed}}
426    do_test func4-4.[incr i] {
427      catchsql {
428        INSERT INTO t2 (x) VALUES ('1234bad');
429      }
430    } {1 {constraint failed}}
431    do_test func4-4.[incr i] {
432      catchsql {
433        INSERT INTO t2 (x) VALUES ('1234.56bad');
434      }
435    } {1 {constraint failed}}
436    do_test func4-4.[incr i] {
437      catchsql {
438        INSERT INTO t2 (x) VALUES (1234);
439      }
440    } {0 {}}
441    do_test func4-4.[incr i] {
442      catchsql {
443        INSERT INTO t2 (x) VALUES (1234.56);
444      }
445    } {0 {}}
446    do_test func4-4.[incr i] {
447      catchsql {
448        INSERT INTO t2 (x) VALUES ('1234');
449      }
450    } {0 {}}
451    do_test func4-4.[incr i] {
452      catchsql {
453        INSERT INTO t2 (x) VALUES ('1234.56');
454      }
455    } {0 {}}
456    do_test func4-4.[incr i] {
457      catchsql {
458        INSERT INTO t2 (x) VALUES (ZEROBLOB(4));
459      }
460    } {1 {constraint failed}}
461    do_test func4-4.[incr i] {
462      catchsql {
463        INSERT INTO t2 (x) VALUES (X'');
464      }
465    } {1 {constraint failed}}
466    do_test func4-4.[incr i] {
467      catchsql {
468        INSERT INTO t2 (x) VALUES (X'1234');
469      }
470    } {1 {constraint failed}}
471    do_test func4-4.[incr i] {
472      catchsql {
473        INSERT INTO t2 (x) VALUES (X'12345678');
474      }
475    } {1 {constraint failed}}
476    do_execsql_test func4-4.[incr i] {
477      SELECT x FROM t2 ORDER BY x;
478    } {1234.0 1234.0 1234.56 1234.56}
479  }
480}
481
482finish_test
483