3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is percentile.c extension
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Basic test of the percentile() function.
20 do_test percentile-1.0 {
21 load_static_extension db percentile
24 INSERT INTO t1 VALUES(1),(4),(6),(7),(8),(9),(11),(11),(11);
26 execsql {SELECT percentile(x,0) FROM t1}
37 do_test percentile-1.1.$in {
38 execsql {SELECT percentile(x,$in) FROM t1}
42 # Add some NULL values.
44 do_test percentile-1.2 {
45 execsql {INSERT INTO t1 VALUES(NULL),(NULL);}
56 do_test percentile-1.3.$in {
57 execsql {SELECT percentile(x,$in) FROM t1}
61 # The second argument to percentile can change some, but not much.
63 do_test percentile-1.4 {
64 catchsql {SELECT round(percentile(x, 15+0.000001*rowid),1) FROM t1}
66 do_test percentile-1.5 {
67 catchsql {SELECT round(percentile(x, 15+0.1*rowid),1) FROM t1}
68 } {1 {2nd argument to percentile() is not the same for all input rows}}
70 # Input values in a random order
72 do_test percentile-1.6 {
75 INSERT INTO t2 SELECT x+0.0 FROM t1 ORDER BY random();
87 do_test percentile-1.7.$in {
88 execsql {SELECT percentile(x,$in) FROM t2}
92 # Wrong number of arguments
94 do_test percentile-1.8 {
95 catchsql {SELECT percentile(x,0,1) FROM t1}
96 } {1 {wrong number of arguments to function percentile()}}
97 do_test percentile-1.9 {
98 catchsql {SELECT percentile(x) FROM t1}
99 } {1 {wrong number of arguments to function percentile()}}
101 # Second argument must be numeric
103 do_test percentile-1.10 {
104 catchsql {SELECT percentile(x,null) FROM t1}
105 } {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
106 do_test percentile-1.11 {
107 catchsql {SELECT percentile(x,'fifty') FROM t1}
108 } {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
109 do_test percentile-1.12 {
110 catchsql {SELECT percentile(x,x'3530') FROM t1}
111 } {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
113 # Second argument is out of range
115 do_test percentile-1.13 {
116 catchsql {SELECT percentile(x,-0.0000001) FROM t1}
117 } {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
118 do_test percentile-1.14 {
119 catchsql {SELECT percentile(x,100.0000001) FROM t1}
120 } {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}}
122 # First argument is not NULL and is not NUMERIC
124 do_test percentile-1.15 {
127 UPDATE t1 SET x='50' WHERE x IS NULL;
128 SELECT percentile(x, 50) FROM t1;
130 } {1 {1st argument to percentile() is not numeric}}
131 do_test percentile-1.16 {
135 UPDATE t1 SET x=x'3530' WHERE x IS NULL;
136 SELECT percentile(x, 50) FROM t1;
138 } {1 {1st argument to percentile() is not numeric}}
139 do_test percentile-1.17 {
142 SELECT percentile(x, 50) FROM t1;
146 # No non-NULL entries.
148 do_test percentile-1.18 {
150 UPDATE t1 SET x=NULL;
151 SELECT ifnull(percentile(x, 50),'NULL') FROM t1
155 # Exactly one non-NULL entry
157 do_test percentile-1.19 {
159 UPDATE t1 SET x=12345 WHERE rowid=5;
160 SELECT percentile(x, 0), percentile(x, 50), percentile(x,100) FROM t1
162 } {12345.0 12345.0 12345.0}
164 # Infinity as an input
166 do_test percentile-1.20 {
169 INSERT INTO t1 SELECT x+0.0 FROM t2;
170 UPDATE t1 SET x=1.0e300*1.0e300 WHERE rowid=5;
171 SELECT percentile(x,50) from t1;
173 } {1 {Inf input to percentile()}}
174 do_test percentile-1.21 {
176 UPDATE t1 SET x=-1.0e300*1.0e300 WHERE rowid=5;
177 SELECT percentile(x,50) from t1;
179 } {1 {Inf input to percentile()}}
184 do_test percentile-2.0 {
185 load_static_extension db wholenumber
187 CREATE VIRTUAL TABLE nums USING wholenumber;
189 INSERT INTO t3 SELECT value-1 FROM nums WHERE value BETWEEN 1 AND 500000;
190 INSERT INTO t3 SELECT value*10 FROM nums
191 WHERE value BETWEEN 500000 AND 999999;
192 SELECT count(*) FROM t3;
201 do_test percentile-2.1.$in {
203 SELECT round(percentile(x, $in),1) from t3;