File: numcast.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (254 lines) | stat: -rwxr-xr-x 6,213 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(31)

--!./tcltestrunner.lua
-- 2013 March 20
--
-- The author disclaims copyright to this source code.  In place of
-- a legal notice, here is a blessing:
--
--    May you do good and not evil.
--    May you find forgiveness for yourself and forgive others.
--    May you share freely, never taking more than you give.
--
-------------------------------------------------------------------------
-- This file implements regression tests for sql library.
-- This particular file does testing of casting strings into numeric
-- values.
--
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]

-- MUST_WORK_TEST should we use
-- for _, enc in ipairs({"utf8", }) do
for _, enc in ipairs({"utf8"}) do
    test:do_test(
        "numcast-"..enc..".0",
        function()
            --db("close")
            --sql("db", ":memory:")
            --test:execsql("PRAGMA encoding='"..enc.."'")
            local x = "utf8"--test:execsql("PRAGMA encoding")[1]
            x = string.lower(x)
            x = string.gsub(x, "-", "")
            return x
        end, enc)
    local data = {
        {"1", "12345.0", 12345.0, 12345},
        {"2", "12345.0e0", 12345.0, 12345},
        {"3", "-12345.0e0", -12345.0, -12345},
        {"4", "-12345.25", -12345.25, -12345},
        {"5", "-12345.0", -12345.0, -12345},
    }
    for _, val in ipairs(data) do
        local idx = val[1]
        local str = val[2]
        local rval = val[3]
        local ival = val[4]
        test:do_test(
            string.format("numcast-%s.%s.1", enc, idx),
            function()
                return test:execsql("SELECT CAST("..str.." AS NUMBER)")
            end, {
                rval
            })

        test:do_test(
            string.format("numcast-%s.%s.2", enc, idx),
            function()
                return test:execsql("SELECT CAST("..str.." AS integer)")
            end, {
                ival
            })

    end
end

--
-- gh-4526: make sure that DOUBLE values that more than
-- 9223372036854775296 and less than 18446744073709551616 can be
-- converted to INTEGER or UNSIGNED.
--
test:do_execsql_test(
    "cast-2.1",
    [[
        SELECT CAST((9223372036854775297.01) AS INTEGER);
    ]], {
        9223372036854775808ULL
    })

test:do_execsql_test(
    "cast-2.2",
    [[
        SELECT CAST((18000000000000000000.) AS INTEGER);
    ]], {
        18000000000000000000ULL
    })

test:do_execsql_test(
    "cast-2.3",
    [[
        SELECT CAST((9223372036854775297.01) AS UNSIGNED);
    ]], {
        9223372036854775808ULL
    })

test:do_execsql_test(
    "cast-2.4",
    [[
        SELECT CAST((18000000000000000000.) AS UNSIGNED);
    ]], {
        18000000000000000000ULL
    })

test:do_catchsql_test(
    "cast-2.5",
    [[
        SELECT CAST((20000000000000000000.) AS UNSIGNED);
    ]], {
        1,"Type mismatch: can not convert 2.0e+19 to unsigned"
    })

test:do_execsql_test(
    "cast-2.6",
    [[
        CREATE TABLE t (i INTEGER PRIMARY KEY);
        INSERT INTO t VALUES(9223372036854775297.01);
        SELECT * FROM t;
    ]], {
        9223372036854775808ULL
    })

test:do_execsql_test(
    "cast-2.7",
    [[
        INSERT INTO t VALUES(18000000000000000000.01);
        SELECT * FROM t;
    ]], {
        9223372036854775808ULL,18000000000000000000ULL
    })

test:do_catchsql_test(
    "cast-2.8",
    [[
        INSERT INTO t VALUES(20000000000000000000.01);
        SELECT * FROM t;
    ]], {
        1,"Type mismatch: can not convert 2.0e+19 to integer"
    })

test:do_execsql_test(
    "cast-2.9",
    [[
        INSERT INTO t VALUES(2.1);
        SELECT * FROM t;
    ]], {
        2, 9223372036854775808ULL, 18000000000000000000ULL
    })

--
-- gh-4233: Make sure that NUMBER can contain UNSIGNED, INTEGER
-- and DOUBLE and is not automatically converted to DOUBLE.
--
test:do_execsql_test(
    "numcast-3.1",
    [[
        SELECT CAST(x'3131313131313131313131313131313131313131' AS NUMBER);
    ]], {
        11111111111111111111ULL
    })

test:do_execsql_test(
    "numcast-3.2",
    [[
        SELECT CAST(x'31313131313131313131313131313131313131312E' AS NUMBER);
    ]], {
        11111111111111110656
    })

test:do_execsql_test(
    "numcast-3.3",
    [[
        SELECT CAST('11111111111111111111' AS NUMBER);
    ]], {
        11111111111111111111ULL
    })

test:do_execsql_test(
    "numcast-3.4",
    [[
        SELECT CAST('101' AS NUMBER) / 10, CAST('101.' AS NUMBER) / 10;
    ]], {
        10, 10.1
    })

test:do_execsql_test(
    "numcast-3.5",
    [[
        SELECT CAST('101     ' AS NUMBER) / 10, CAST('      101' AS NUMBER) / 10;
    ]], {
        10, 10
    })

test:do_execsql_test(
    "numcast-3.6",
    [[
        CREATE TABLE t1 (id INT PRIMARY KEY, n NUMBER);
        INSERT INTO t1 VALUES (1, 9223372036854775807);
        INSERT INTO t1 VALUES (2, -9223372036854775807);
        INSERT INTO t1 VALUES (3, 9007199254740992.0);
        SELECT n, n/100 FROM t1;
    ]], {
        9223372036854775807ULL, 92233720368547758ULL,
        -9223372036854775807LL, -92233720368547758LL,
        9007199254740992, 90071992547409.92
    })

test:do_execsql_test(
    "numcast-3.7",
    [[
        CREATE TABLE t2(a NUMBER primary key);
        INSERT INTO t2 VALUES(-56);
        INSERT INTO t2 VALUES(44.0);
        INSERT INTO t2 VALUES(46);
        INSERT INTO t2 VALUES(56.0);
        SELECT (a + 25) / 50 FROM t2;
    ]], {
        0,1.38,1,1.62
})


test:do_execsql_test(
    "numcast-3.8",
    [[
        SELECT (1 + 0) / 3, (1 + 0.) / 3, (1 + 0) / 3.;
    ]], {
        0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
    "numcast-3.9",
    [[
        SELECT (1 - 0) / 3, (1 - 0.) / 3, (1 - 0) / 3.;
    ]], {
        0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
    "numcast-3.10",
    [[
        SELECT (1 * 1) / 3, (1 * 1.) / 3, (1 * 1) / 3.;
    ]], {
        0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
    "numcast-3.11",
    [[
        SELECT (1 / 1) / 3, (1 / 1.) / 3, (1 / 1) / 3.;
    ]], {
        0 , 0.33333333333333, 0.33333333333333
})

test:finish_test()