summaryrefslogtreecommitdiffstats
path: root/test/func3.test
blob: 0221a0dfd6e97cf21d6f88c323dff3f7ef15f4f4 (plain)
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
# 2010 August 27
#
# 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 SQLite library. The
# focus of this file is testing that destructor functions associated
# with functions created using sqlite3_create_function_v2() is 
# correctly invoked.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl


ifcapable utf16 {
  do_test func3-1.1 {
    set destroyed 0
    proc destroy {} { set ::destroyed 1 }
    sqlite3_create_function_v2 db f2 -1 any -func f2 -destroy destroy
    set destroyed
  } 0
  do_test func3-1.2 {
    sqlite3_create_function_v2 db f2 -1 utf8 -func f2
    set destroyed
  } 0
  do_test func3-1.3 {
    sqlite3_create_function_v2 db f2 -1 utf16le -func f2
    set destroyed
  } 0
  do_test func3-1.4 {
    sqlite3_create_function_v2 db f2 -1 utf16be -func f2
    set destroyed
  } 1
}

do_test func3-2.1 {
  set destroyed 0
  proc destroy {} { set ::destroyed 1 }
  sqlite3_create_function_v2 db f3 -1 utf8 -func f3 -destroy destroy
  set destroyed
} 0
do_test func3-2.2 {
  sqlite3_create_function_v2 db f3 -1 utf8 -func f3
  set destroyed
} 1

do_test func3-3.1 {
  set destroyed 0
  proc destroy {} { set ::destroyed 1 }
  sqlite3_create_function_v2 db f3 -1 any -func f3 -destroy destroy
  set destroyed
} 0
do_test func3-3.2 {
  db close
  set destroyed
} 1

sqlite3 db test.db
do_test func3-4.1 {
  set destroyed 0
  set rc [catch { 
    sqlite3_create_function_v2 db f3 -1 any -func f3 -step f3 -destroy destroy
  } msg]
  list $rc $msg
} {1 SQLITE_MISUSE}
do_test func3-4.2 { set destroyed } 1

# EVIDENCE-OF: R-41921-05214 The likelihood(X,Y) function returns
# argument X unchanged.
#
do_execsql_test func3-5.1 {
  SELECT likelihood(9223372036854775807, 0.5);
} {9223372036854775807}
do_execsql_test func3-5.2 {
  SELECT likelihood(-9223372036854775808, 0.5);
} {-9223372036854775808}
do_execsql_test func3-5.3 {
  SELECT likelihood(14.125, 0.5);
} {14.125}
do_execsql_test func3-5.4 {
  SELECT likelihood(NULL, 0.5);
} {{}}
do_execsql_test func3-5.5 {
  SELECT likelihood('test-string', 0.5);
} {test-string}
do_execsql_test func3-5.6 {
  SELECT quote(likelihood(x'010203000405', 0.5));
} {X'010203000405'}

# EVIDENCE-OF: R-44133-61651 The value Y in likelihood(X,Y) must be a
# floating point constant between 0.0 and 1.0, inclusive.
#
do_execsql_test func3-5.7 {
  SELECT likelihood(123, 1.0), likelihood(456, 0.0);
} {123 456}
do_test func3-5.8 {
  catchsql {
    SELECT likelihood(123, 1.000001);
  }
} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
do_test func3-5.9 {
  catchsql {
    SELECT likelihood(123, -0.000001);
  }
} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
do_test func3-5.10 {
  catchsql {
    SELECT likelihood(123, 0.5+0.3);
  }
} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}

# EVIDENCE-OF: R-28535-44631 The likelihood(X) function is a no-op that
# the code generator optimizes away so that it consumes no CPU cycles
# during run-time (that is, during calls to sqlite3_step()).
#
do_test func3-5.20 {
  db eval {EXPLAIN SELECT likelihood(min(1.0+'2.0',4*11), 0.5)}
} [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]


# EVIDENCE-OF: R-11152-23456 The unlikely(X) function returns the
# argument X unchanged.
#
do_execsql_test func3-5.30 {
  SELECT unlikely(9223372036854775807);
} {9223372036854775807}
do_execsql_test func3-5.31 {
  SELECT unlikely(-9223372036854775808);
} {-9223372036854775808}
do_execsql_test func3-5.32 {
  SELECT unlikely(14.125);
} {14.125}
do_execsql_test func3-5.33 {
  SELECT unlikely(NULL);
} {{}}
do_execsql_test func3-5.34 {
  SELECT unlikely('test-string');
} {test-string}
do_execsql_test func3-5.35 {
  SELECT quote(unlikely(x'010203000405'));
} {X'010203000405'}

# EVIDENCE-OF: R-22887-63324 The unlikely(X) function is a no-op that
# the code generator optimizes away so that it consumes no CPU cycles at
# run-time (that is, during calls to sqlite3_step()).
#
do_test func3-5.39 {
  db eval {EXPLAIN SELECT unlikely(min(1.0+'2.0',4*11))}
} [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]

# Unlikely() does not preserve the affinity of X.
# ticket https://www.sqlite.org/src/tktview/0c620df60b
#
do_execsql_test func3-5.40 {
  SELECT likely(CAST(1 AS INT))=='1';
} 0
do_execsql_test func3-5.41 {
  SELECT unlikely(CAST(1 AS INT))=='1';
} 0
do_execsql_test func3-5.41 {
  SELECT likelihood(CAST(1 AS INT),0.5)=='1';
} 0


# EVIDENCE-OF: R-23735-03107 The likely(X) function returns the argument
# X unchanged.
#
do_execsql_test func3-5.50 {
  SELECT likely(9223372036854775807);
} {9223372036854775807}
do_execsql_test func3-5.51 {
  SELECT likely(-9223372036854775808);
} {-9223372036854775808}
do_execsql_test func3-5.52 {
  SELECT likely(14.125);
} {14.125}
do_execsql_test func3-5.53 {
  SELECT likely(NULL);
} {{}}
do_execsql_test func3-5.54 {
  SELECT likely('test-string');
} {test-string}
do_execsql_test func3-5.55 {
  SELECT quote(likely(x'010203000405'));
} {X'010203000405'}

# EVIDENCE-OF: R-43464-09689 The likely(X) function is a no-op that the
# code generator optimizes away so that it consumes no CPU cycles at
# run-time (that is, during calls to sqlite3_step()).
#
do_test func3-5.59 {
  db eval {EXPLAIN SELECT likely(min(1.0+'2.0',4*11))}
} [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]


# Test the outcome of specifying NULL xStep and xFinal pointers (normally
# used to delete any existing function) and a non-NULL xDestroy when there 
# is no existing function to destroy.
#
do_test func3-6.0 {
  sqlite3_create_function_v2 db nofunc 1 utf8
} {}



finish_test