Debian Codegraph @

test/func3.test

raw
1 # 2010 August 27
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 that destructor functions associated
13 # with functions created using sqlite3_create_function_v2() is
14 # correctly invoked.
15 #
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18
19
20 ifcapable utf16 {
21 do_test func3-1.1 {
22 set destroyed 0
23 proc destroy {} { set ::destroyed 1 }
24 sqlite3_create_function_v2 db f2 -1 any -func f2 -destroy destroy
25 set destroyed
26 } 0
27 do_test func3-1.2 {
28 sqlite3_create_function_v2 db f2 -1 utf8 -func f2
29 set destroyed
30 } 0
31 do_test func3-1.3 {
32 sqlite3_create_function_v2 db f2 -1 utf16le -func f2
33 set destroyed
34 } 0
35 do_test func3-1.4 {
36 sqlite3_create_function_v2 db f2 -1 utf16be -func f2
37 set destroyed
38 } 1
39 }
40
41 do_test func3-2.1 {
42 set destroyed 0
43 proc destroy {} { set ::destroyed 1 }
44 sqlite3_create_function_v2 db f3 -1 utf8 -func f3 -destroy destroy
45 set destroyed
46 } 0
47 do_test func3-2.2 {
48 sqlite3_create_function_v2 db f3 -1 utf8 -func f3
49 set destroyed
50 } 1
51
52 do_test func3-3.1 {
53 set destroyed 0
54 proc destroy {} { set ::destroyed 1 }
55 sqlite3_create_function_v2 db f3 -1 any -func f3 -destroy destroy
56 set destroyed
57 } 0
58 do_test func3-3.2 {
59 db close
60 set destroyed
61 } 1
62
63 sqlite3 db test.db
64 do_test func3-4.1 {
65 set destroyed 0
66 set rc [catch {
67 sqlite3_create_function_v2 db f3 -1 any -func f3 -step f3 -destroy destroy
68 } msg]
69 list $rc $msg
70 } {1 SQLITE_MISUSE}
71 do_test func3-4.2 { set destroyed } 1
72
73 # EVIDENCE-OF: R-41921-05214 The likelihood(X,Y) function returns
74 # argument X unchanged.
75 #
76 do_execsql_test func3-5.1 {
77 SELECT likelihood(9223372036854775807, 0.5);
78 } {9223372036854775807}
79 do_execsql_test func3-5.2 {
80 SELECT likelihood(-9223372036854775808, 0.5);
81 } {-9223372036854775808}
82 do_execsql_test func3-5.3 {
83 SELECT likelihood(14.125, 0.5);
84 } {14.125}
85 do_execsql_test func3-5.4 {
86 SELECT likelihood(NULL, 0.5);
87 } {{}}
88 do_execsql_test func3-5.5 {
89 SELECT likelihood('test-string', 0.5);
90 } {test-string}
91 do_execsql_test func3-5.6 {
92 SELECT quote(likelihood(x'010203000405', 0.5));
93 } {X'010203000405'}
94
95 # EVIDENCE-OF: R-44133-61651 The value Y in likelihood(X,Y) must be a
96 # floating point constant between 0.0 and 1.0, inclusive.
97 #
98 do_execsql_test func3-5.7 {
99 SELECT likelihood(123, 1.0), likelihood(456, 0.0);
100 } {123 456}
101 do_test func3-5.8 {
102 catchsql {
103 SELECT likelihood(123, 1.000001);
104 }
105 } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
106 do_test func3-5.9 {
107 catchsql {
108 SELECT likelihood(123, -0.000001);
109 }
110 } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
111 do_test func3-5.10 {
112 catchsql {
113 SELECT likelihood(123, 0.5+0.3);
114 }
115 } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
116
117 # EVIDENCE-OF: R-28535-44631 The likelihood(X) function is a no-op that
118 # the code generator optimizes away so that it consumes no CPU cycles
119 # during run-time (that is, during calls to sqlite3_step()).
120 #
121 do_test func3-5.20 {
122 db eval {EXPLAIN SELECT likelihood(min(1.0+'2.0',4*11), 0.5)}
123 } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]
124
125
126 # EVIDENCE-OF: R-11152-23456 The unlikely(X) function returns the
127 # argument X unchanged.
128 #
129 do_execsql_test func3-5.30 {
130 SELECT unlikely(9223372036854775807);
131 } {9223372036854775807}
132 do_execsql_test func3-5.31 {
133 SELECT unlikely(-9223372036854775808);
134 } {-9223372036854775808}
135 do_execsql_test func3-5.32 {
136 SELECT unlikely(14.125);
137 } {14.125}
138 do_execsql_test func3-5.33 {
139 SELECT unlikely(NULL);
140 } {{}}
141 do_execsql_test func3-5.34 {
142 SELECT unlikely('test-string');
143 } {test-string}
144 do_execsql_test func3-5.35 {
145 SELECT quote(unlikely(x'010203000405'));
146 } {X'010203000405'}
147
148 # EVIDENCE-OF: R-22887-63324 The unlikely(X) function is a no-op that
149 # the code generator optimizes away so that it consumes no CPU cycles at
150 # run-time (that is, during calls to sqlite3_step()).
151 #
152 do_test func3-5.39 {
153 db eval {EXPLAIN SELECT unlikely(min(1.0+'2.0',4*11))}
154 } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]
155
156 # Unlikely() does not preserve the affinity of X.
157 # ticket https://sqlite.org/src/tktview/0c620df60b
158 #
159 do_execsql_test func3-5.40 {
160 SELECT likely(CAST(1 AS INT))=='1';
161 } 0
162 do_execsql_test func3-5.41 {
163 SELECT unlikely(CAST(1 AS INT))=='1';
164 } 0
165 do_execsql_test func3-5.41 {
166 SELECT likelihood(CAST(1 AS INT),0.5)=='1';
167 } 0
168
169
170 # EVIDENCE-OF: R-23735-03107 The likely(X) function returns the argument
171 # X unchanged.
172 #
173 do_execsql_test func3-5.50 {
174 SELECT likely(9223372036854775807);
175 } {9223372036854775807}
176 do_execsql_test func3-5.51 {
177 SELECT likely(-9223372036854775808);
178 } {-9223372036854775808}
179 do_execsql_test func3-5.52 {
180 SELECT likely(14.125);
181 } {14.125}
182 do_execsql_test func3-5.53 {
183 SELECT likely(NULL);
184 } {{}}
185 do_execsql_test func3-5.54 {
186 SELECT likely('test-string');
187 } {test-string}
188 do_execsql_test func3-5.55 {
189 SELECT quote(likely(x'010203000405'));
190 } {X'010203000405'}
191
192 # EVIDENCE-OF: R-43464-09689 The likely(X) function is a no-op that the
193 # code generator optimizes away so that it consumes no CPU cycles at
194 # run-time (that is, during calls to sqlite3_step()).
195 #
196 do_test func3-5.59 {
197 db eval {EXPLAIN SELECT likely(min(1.0+'2.0',4*11))}
198 } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]
199
200
201 # Test the outcome of specifying NULL xStep and xFinal pointers (normally
202 # used to delete any existing function) and a non-NULL xDestroy when there
203 # is no existing function to destroy.
204 #
205 do_test func3-6.0 {
206 sqlite3_create_function_v2 db nofunc 1 utf8
207 } {}
208
209
210
211 finish_test