func.test 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. # 2001 September 15
  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 built-in functions.
  13. #
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. set testprefix func
  17. # Create a table to work with.
  18. #
  19. do_test func-0.0 {
  20. execsql {CREATE TABLE tbl1(t1 text)}
  21. foreach word {this program is free software} {
  22. execsql "INSERT INTO tbl1 VALUES('$word')"
  23. }
  24. execsql {SELECT t1 FROM tbl1 ORDER BY t1}
  25. } {free is program software this}
  26. do_test func-0.1 {
  27. execsql {
  28. CREATE TABLE t2(a);
  29. INSERT INTO t2 VALUES(1);
  30. INSERT INTO t2 VALUES(NULL);
  31. INSERT INTO t2 VALUES(345);
  32. INSERT INTO t2 VALUES(NULL);
  33. INSERT INTO t2 VALUES(67890);
  34. SELECT * FROM t2;
  35. }
  36. } {1 {} 345 {} 67890}
  37. # Check out the length() function
  38. #
  39. do_test func-1.0 {
  40. execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
  41. } {4 2 7 8 4}
  42. do_test func-1.1 {
  43. set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
  44. lappend r $msg
  45. } {1 {wrong number of arguments to function length()}}
  46. do_test func-1.2 {
  47. set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
  48. lappend r $msg
  49. } {1 {wrong number of arguments to function length()}}
  50. do_test func-1.3 {
  51. execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
  52. ORDER BY length(t1)}
  53. } {2 1 4 2 7 1 8 1}
  54. do_test func-1.4 {
  55. execsql {SELECT coalesce(length(a),-1) FROM t2}
  56. } {1 -1 3 -1 5}
  57. # Check out the substr() function
  58. #
  59. do_test func-2.0 {
  60. execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
  61. } {fr is pr so th}
  62. do_test func-2.1 {
  63. execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
  64. } {r s r o h}
  65. do_test func-2.2 {
  66. execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
  67. } {ee {} ogr ftw is}
  68. do_test func-2.3 {
  69. execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
  70. } {e s m e s}
  71. do_test func-2.4 {
  72. execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
  73. } {e s m e s}
  74. do_test func-2.5 {
  75. execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
  76. } {e i a r i}
  77. do_test func-2.6 {
  78. execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
  79. } {ee is am re is}
  80. do_test func-2.7 {
  81. execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
  82. } {fr {} gr wa th}
  83. do_test func-2.8 {
  84. execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
  85. } {this software free program is}
  86. do_test func-2.9 {
  87. execsql {SELECT substr(a,1,1) FROM t2}
  88. } {1 {} 3 {} 6}
  89. do_test func-2.10 {
  90. execsql {SELECT substr(a,2,2) FROM t2}
  91. } {{} {} 45 {} 78}
  92. # Only do the following tests if TCL has UTF-8 capabilities
  93. #
  94. if {"\u1234"!="u1234"} {
  95. # Put some UTF-8 characters in the database
  96. #
  97. do_test func-3.0 {
  98. execsql {DELETE FROM tbl1}
  99. foreach word "contains UTF-8 characters hi\u1234ho" {
  100. execsql "INSERT INTO tbl1 VALUES('$word')"
  101. }
  102. execsql {SELECT t1 FROM tbl1 ORDER BY t1}
  103. } "UTF-8 characters contains hi\u1234ho"
  104. do_test func-3.1 {
  105. execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
  106. } {5 10 8 5}
  107. do_test func-3.2 {
  108. execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
  109. } {UT ch co hi}
  110. do_test func-3.3 {
  111. execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
  112. } "UTF cha con hi\u1234"
  113. do_test func-3.4 {
  114. execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
  115. } "TF ha on i\u1234"
  116. do_test func-3.5 {
  117. execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
  118. } "TF- har ont i\u1234h"
  119. do_test func-3.6 {
  120. execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
  121. } "F- ar nt \u1234h"
  122. do_test func-3.7 {
  123. execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
  124. } "-8 ra ta ho"
  125. do_test func-3.8 {
  126. execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
  127. } "8 s s o"
  128. do_test func-3.9 {
  129. execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
  130. } "F- er in \u1234h"
  131. do_test func-3.10 {
  132. execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
  133. } "TF- ter ain i\u1234h"
  134. do_test func-3.99 {
  135. execsql {DELETE FROM tbl1}
  136. foreach word {this program is free software} {
  137. execsql "INSERT INTO tbl1 VALUES('$word')"
  138. }
  139. execsql {SELECT t1 FROM tbl1}
  140. } {this program is free software}
  141. } ;# End \u1234!=u1234
  142. # Test the abs() and round() functions.
  143. #
  144. ifcapable !floatingpoint {
  145. do_test func-4.1 {
  146. execsql {
  147. CREATE TABLE t1(a,b,c);
  148. INSERT INTO t1 VALUES(1,2,3);
  149. INSERT INTO t1 VALUES(2,12345678901234,-1234567890);
  150. INSERT INTO t1 VALUES(3,-2,-5);
  151. }
  152. catchsql {SELECT abs(a,b) FROM t1}
  153. } {1 {wrong number of arguments to function abs()}}
  154. }
  155. ifcapable floatingpoint {
  156. do_test func-4.1 {
  157. execsql {
  158. CREATE TABLE t1(a,b,c);
  159. INSERT INTO t1 VALUES(1,2,3);
  160. INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
  161. INSERT INTO t1 VALUES(3,-2,-5);
  162. }
  163. catchsql {SELECT abs(a,b) FROM t1}
  164. } {1 {wrong number of arguments to function abs()}}
  165. }
  166. do_test func-4.2 {
  167. catchsql {SELECT abs() FROM t1}
  168. } {1 {wrong number of arguments to function abs()}}
  169. ifcapable floatingpoint {
  170. do_test func-4.3 {
  171. catchsql {SELECT abs(b) FROM t1 ORDER BY a}
  172. } {0 {2 1.2345678901234 2}}
  173. do_test func-4.4 {
  174. catchsql {SELECT abs(c) FROM t1 ORDER BY a}
  175. } {0 {3 12345.6789 5}}
  176. }
  177. ifcapable !floatingpoint {
  178. if {[working_64bit_int]} {
  179. do_test func-4.3 {
  180. catchsql {SELECT abs(b) FROM t1 ORDER BY a}
  181. } {0 {2 12345678901234 2}}
  182. }
  183. do_test func-4.4 {
  184. catchsql {SELECT abs(c) FROM t1 ORDER BY a}
  185. } {0 {3 1234567890 5}}
  186. }
  187. do_test func-4.4.1 {
  188. execsql {SELECT abs(a) FROM t2}
  189. } {1 {} 345 {} 67890}
  190. do_test func-4.4.2 {
  191. execsql {SELECT abs(t1) FROM tbl1}
  192. } {0.0 0.0 0.0 0.0 0.0}
  193. ifcapable floatingpoint {
  194. do_test func-4.5 {
  195. catchsql {SELECT round(a,b,c) FROM t1}
  196. } {1 {wrong number of arguments to function round()}}
  197. do_test func-4.6 {
  198. catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
  199. } {0 {-2.0 1.23 2.0}}
  200. do_test func-4.7 {
  201. catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
  202. } {0 {2.0 1.0 -2.0}}
  203. do_test func-4.8 {
  204. catchsql {SELECT round(c) FROM t1 ORDER BY a}
  205. } {0 {3.0 -12346.0 -5.0}}
  206. do_test func-4.9 {
  207. catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
  208. } {0 {3.0 -12345.68 -5.0}}
  209. do_test func-4.10 {
  210. catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
  211. } {0 {x3.0y x-12345.68y x-5.0y}}
  212. do_test func-4.11 {
  213. catchsql {SELECT round() FROM t1 ORDER BY a}
  214. } {1 {wrong number of arguments to function round()}}
  215. do_test func-4.12 {
  216. execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
  217. } {1.0 nil 345.0 nil 67890.0}
  218. do_test func-4.13 {
  219. execsql {SELECT round(t1,2) FROM tbl1}
  220. } {0.0 0.0 0.0 0.0 0.0}
  221. do_test func-4.14 {
  222. execsql {SELECT typeof(round(5.1,1));}
  223. } {real}
  224. do_test func-4.15 {
  225. execsql {SELECT typeof(round(5.1));}
  226. } {real}
  227. do_test func-4.16 {
  228. catchsql {SELECT round(b,2.0) FROM t1 ORDER BY b}
  229. } {0 {-2.0 1.23 2.0}}
  230. # Verify some values reported on the mailing list.
  231. # Some of these fail on MSVC builds with 64-bit
  232. # long doubles, but not on GCC builds with 80-bit
  233. # long doubles.
  234. for {set i 1} {$i<999} {incr i} {
  235. set x1 [expr 40222.5 + $i]
  236. set x2 [expr 40223.0 + $i]
  237. do_test func-4.17.$i {
  238. execsql {SELECT round($x1);}
  239. } $x2
  240. }
  241. for {set i 1} {$i<999} {incr i} {
  242. set x1 [expr 40222.05 + $i]
  243. set x2 [expr 40222.10 + $i]
  244. do_test func-4.18.$i {
  245. execsql {SELECT round($x1,1);}
  246. } $x2
  247. }
  248. do_test func-4.20 {
  249. execsql {SELECT round(40223.4999999999);}
  250. } {40223.0}
  251. do_test func-4.21 {
  252. execsql {SELECT round(40224.4999999999);}
  253. } {40224.0}
  254. do_test func-4.22 {
  255. execsql {SELECT round(40225.4999999999);}
  256. } {40225.0}
  257. for {set i 1} {$i<10} {incr i} {
  258. do_test func-4.23.$i {
  259. execsql {SELECT round(40223.4999999999,$i);}
  260. } {40223.5}
  261. do_test func-4.24.$i {
  262. execsql {SELECT round(40224.4999999999,$i);}
  263. } {40224.5}
  264. do_test func-4.25.$i {
  265. execsql {SELECT round(40225.4999999999,$i);}
  266. } {40225.5}
  267. }
  268. for {set i 10} {$i<32} {incr i} {
  269. do_test func-4.26.$i {
  270. execsql {SELECT round(40223.4999999999,$i);}
  271. } {40223.4999999999}
  272. do_test func-4.27.$i {
  273. execsql {SELECT round(40224.4999999999,$i);}
  274. } {40224.4999999999}
  275. do_test func-4.28.$i {
  276. execsql {SELECT round(40225.4999999999,$i);}
  277. } {40225.4999999999}
  278. }
  279. do_test func-4.29 {
  280. execsql {SELECT round(1234567890.5);}
  281. } {1234567891.0}
  282. do_test func-4.30 {
  283. execsql {SELECT round(12345678901.5);}
  284. } {12345678902.0}
  285. do_test func-4.31 {
  286. execsql {SELECT round(123456789012.5);}
  287. } {123456789013.0}
  288. do_test func-4.32 {
  289. execsql {SELECT round(1234567890123.5);}
  290. } {1234567890124.0}
  291. do_test func-4.33 {
  292. execsql {SELECT round(12345678901234.5);}
  293. } {12345678901235.0}
  294. do_test func-4.34 {
  295. execsql {SELECT round(1234567890123.35,1);}
  296. } {1234567890123.4}
  297. do_test func-4.35 {
  298. execsql {SELECT round(1234567890123.445,2);}
  299. } {1234567890123.45}
  300. do_test func-4.36 {
  301. execsql {SELECT round(99999999999994.5);}
  302. } {99999999999995.0}
  303. do_test func-4.37 {
  304. execsql {SELECT round(9999999999999.55,1);}
  305. } {9999999999999.6}
  306. do_test func-4.38 {
  307. execsql {SELECT round(9999999999999.556,2);}
  308. } {9999999999999.56}
  309. }
  310. # Test the upper() and lower() functions
  311. #
  312. do_test func-5.1 {
  313. execsql {SELECT upper(t1) FROM tbl1}
  314. } {THIS PROGRAM IS FREE SOFTWARE}
  315. do_test func-5.2 {
  316. execsql {SELECT lower(upper(t1)) FROM tbl1}
  317. } {this program is free software}
  318. do_test func-5.3 {
  319. execsql {SELECT upper(a), lower(a) FROM t2}
  320. } {1 1 {} {} 345 345 {} {} 67890 67890}
  321. ifcapable !icu {
  322. do_test func-5.4 {
  323. catchsql {SELECT upper(a,5) FROM t2}
  324. } {1 {wrong number of arguments to function upper()}}
  325. }
  326. do_test func-5.5 {
  327. catchsql {SELECT upper(*) FROM t2}
  328. } {1 {wrong number of arguments to function upper()}}
  329. # Test the coalesce() and nullif() functions
  330. #
  331. do_test func-6.1 {
  332. execsql {SELECT coalesce(a,'xyz') FROM t2}
  333. } {1 xyz 345 xyz 67890}
  334. do_test func-6.2 {
  335. execsql {SELECT coalesce(upper(a),'nil') FROM t2}
  336. } {1 nil 345 nil 67890}
  337. do_test func-6.3 {
  338. execsql {SELECT coalesce(nullif(1,1),'nil')}
  339. } {nil}
  340. do_test func-6.4 {
  341. execsql {SELECT coalesce(nullif(1,2),'nil')}
  342. } {1}
  343. do_test func-6.5 {
  344. execsql {SELECT coalesce(nullif(1,NULL),'nil')}
  345. } {1}
  346. # Test the last_insert_rowid() function
  347. #
  348. do_test func-7.1 {
  349. execsql {SELECT last_insert_rowid()}
  350. } [db last_insert_rowid]
  351. # Tests for aggregate functions and how they handle NULLs.
  352. #
  353. ifcapable floatingpoint {
  354. do_test func-8.1 {
  355. ifcapable explain {
  356. execsql {EXPLAIN SELECT sum(a) FROM t2;}
  357. }
  358. execsql {
  359. SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
  360. }
  361. } {68236 3 22745.33 1 67890 5}
  362. }
  363. ifcapable !floatingpoint {
  364. do_test func-8.1 {
  365. ifcapable explain {
  366. execsql {EXPLAIN SELECT sum(a) FROM t2;}
  367. }
  368. execsql {
  369. SELECT sum(a), count(a), avg(a), min(a), max(a), count(*) FROM t2;
  370. }
  371. } {68236 3 22745.0 1 67890 5}
  372. }
  373. do_test func-8.2 {
  374. execsql {
  375. SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
  376. }
  377. } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
  378. ifcapable tempdb {
  379. do_test func-8.3 {
  380. execsql {
  381. CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
  382. SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
  383. }
  384. } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
  385. } else {
  386. do_test func-8.3 {
  387. execsql {
  388. CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
  389. SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
  390. }
  391. } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
  392. }
  393. do_test func-8.4 {
  394. execsql {
  395. SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
  396. }
  397. } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
  398. ifcapable compound {
  399. do_test func-8.5 {
  400. execsql {
  401. SELECT sum(x) FROM (SELECT '9223372036' || '854775807' AS x
  402. UNION ALL SELECT -9223372036854775807)
  403. }
  404. } {0}
  405. do_test func-8.6 {
  406. execsql {
  407. SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775807' AS x
  408. UNION ALL SELECT -9223372036854775807)
  409. }
  410. } {integer}
  411. do_test func-8.7 {
  412. execsql {
  413. SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775808' AS x
  414. UNION ALL SELECT -9223372036854775807)
  415. }
  416. } {real}
  417. ifcapable floatingpoint {
  418. do_test func-8.8 {
  419. execsql {
  420. SELECT sum(x)>0.0 FROM (SELECT '9223372036' || '854775808' AS x
  421. UNION ALL SELECT -9223372036850000000)
  422. }
  423. } {1}
  424. }
  425. ifcapable !floatingpoint {
  426. do_test func-8.8 {
  427. execsql {
  428. SELECT sum(x)>0 FROM (SELECT '9223372036' || '854775808' AS x
  429. UNION ALL SELECT -9223372036850000000)
  430. }
  431. } {1}
  432. }
  433. }
  434. # How do you test the random() function in a meaningful, deterministic way?
  435. #
  436. do_test func-9.1 {
  437. execsql {
  438. SELECT random() is not null;
  439. }
  440. } {1}
  441. do_test func-9.2 {
  442. execsql {
  443. SELECT typeof(random());
  444. }
  445. } {integer}
  446. do_test func-9.3 {
  447. execsql {
  448. SELECT randomblob(32) is not null;
  449. }
  450. } {1}
  451. do_test func-9.4 {
  452. execsql {
  453. SELECT typeof(randomblob(32));
  454. }
  455. } {blob}
  456. do_test func-9.5 {
  457. execsql {
  458. SELECT length(randomblob(32)), length(randomblob(-5)),
  459. length(randomblob(2000))
  460. }
  461. } {32 1 2000}
  462. # The "hex()" function was added in order to be able to render blobs
  463. # generated by randomblob(). So this seems like a good place to test
  464. # hex().
  465. #
  466. ifcapable bloblit {
  467. do_test func-9.10 {
  468. execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
  469. } {00112233445566778899AABBCCDDEEFF}
  470. }
  471. set encoding [db one {PRAGMA encoding}]
  472. if {$encoding=="UTF-16le"} {
  473. do_test func-9.11-utf16le {
  474. execsql {SELECT hex(replace('abcdefg','ef','12'))}
  475. } {6100620063006400310032006700}
  476. do_test func-9.12-utf16le {
  477. execsql {SELECT hex(replace('abcdefg','','12'))}
  478. } {6100620063006400650066006700}
  479. do_test func-9.13-utf16le {
  480. execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
  481. } {610061006100610061006100620063006400650066006700}
  482. } elseif {$encoding=="UTF-8"} {
  483. do_test func-9.11-utf8 {
  484. execsql {SELECT hex(replace('abcdefg','ef','12'))}
  485. } {61626364313267}
  486. do_test func-9.12-utf8 {
  487. execsql {SELECT hex(replace('abcdefg','','12'))}
  488. } {61626364656667}
  489. do_test func-9.13-utf8 {
  490. execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
  491. } {616161616161626364656667}
  492. }
  493. # Use the "sqlite_register_test_function" TCL command which is part of
  494. # the text fixture in order to verify correct operation of some of
  495. # the user-defined SQL function APIs that are not used by the built-in
  496. # functions.
  497. #
  498. set ::DB [sqlite3_connection_pointer db]
  499. sqlite_register_test_function $::DB testfunc
  500. do_test func-10.1 {
  501. catchsql {
  502. SELECT testfunc(NULL,NULL);
  503. }
  504. } {1 {first argument should be one of: int int64 string double null value}}
  505. do_test func-10.2 {
  506. execsql {
  507. SELECT testfunc(
  508. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  509. 'int', 1234
  510. );
  511. }
  512. } {1234}
  513. do_test func-10.3 {
  514. execsql {
  515. SELECT testfunc(
  516. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  517. 'string', NULL
  518. );
  519. }
  520. } {{}}
  521. ifcapable floatingpoint {
  522. do_test func-10.4 {
  523. execsql {
  524. SELECT testfunc(
  525. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  526. 'double', 1.234
  527. );
  528. }
  529. } {1.234}
  530. do_test func-10.5 {
  531. execsql {
  532. SELECT testfunc(
  533. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  534. 'int', 1234,
  535. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  536. 'string', NULL,
  537. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  538. 'double', 1.234,
  539. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  540. 'int', 1234,
  541. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  542. 'string', NULL,
  543. 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  544. 'double', 1.234
  545. );
  546. }
  547. } {1.234}
  548. }
  549. # Test the built-in sqlite_version(*) SQL function.
  550. #
  551. do_test func-11.1 {
  552. execsql {
  553. SELECT sqlite_version(*);
  554. }
  555. } [sqlite3 -version]
  556. # Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
  557. # etc. are called. These tests use two special user-defined functions
  558. # (implemented in func.c) only available in test builds.
  559. #
  560. # Function test_destructor() takes one argument and returns a copy of the
  561. # text form of that argument. A destructor is associated with the return
  562. # value. Function test_destructor_count() returns the number of outstanding
  563. # destructor calls for values returned by test_destructor().
  564. #
  565. if {[db eval {PRAGMA encoding}]=="UTF-8"} {
  566. do_test func-12.1-utf8 {
  567. execsql {
  568. SELECT test_destructor('hello world'), test_destructor_count();
  569. }
  570. } {{hello world} 1}
  571. } else {
  572. ifcapable {utf16} {
  573. do_test func-12.1-utf16 {
  574. execsql {
  575. SELECT test_destructor16('hello world'), test_destructor_count();
  576. }
  577. } {{hello world} 1}
  578. }
  579. }
  580. do_test func-12.2 {
  581. execsql {
  582. SELECT test_destructor_count();
  583. }
  584. } {0}
  585. do_test func-12.3 {
  586. execsql {
  587. SELECT test_destructor('hello')||' world'
  588. }
  589. } {{hello world}}
  590. do_test func-12.4 {
  591. execsql {
  592. SELECT test_destructor_count();
  593. }
  594. } {0}
  595. do_test func-12.5 {
  596. execsql {
  597. CREATE TABLE t4(x);
  598. INSERT INTO t4 VALUES(test_destructor('hello'));
  599. INSERT INTO t4 VALUES(test_destructor('world'));
  600. SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
  601. }
  602. } {hello world}
  603. do_test func-12.6 {
  604. execsql {
  605. SELECT test_destructor_count();
  606. }
  607. } {0}
  608. do_test func-12.7 {
  609. execsql {
  610. DROP TABLE t4;
  611. }
  612. } {}
  613. # Test that the auxdata API for scalar functions works. This test uses
  614. # a special user-defined function only available in test builds,
  615. # test_auxdata(). Function test_auxdata() takes any number of arguments.
  616. do_test func-13.1 {
  617. execsql {
  618. SELECT test_auxdata('hello world');
  619. }
  620. } {0}
  621. do_test func-13.2 {
  622. execsql {
  623. CREATE TABLE t4(a, b);
  624. INSERT INTO t4 VALUES('abc', 'def');
  625. INSERT INTO t4 VALUES('ghi', 'jkl');
  626. }
  627. } {}
  628. do_test func-13.3 {
  629. execsql {
  630. SELECT test_auxdata('hello world') FROM t4;
  631. }
  632. } {0 1}
  633. do_test func-13.4 {
  634. execsql {
  635. SELECT test_auxdata('hello world', 123) FROM t4;
  636. }
  637. } {{0 0} {1 1}}
  638. do_test func-13.5 {
  639. execsql {
  640. SELECT test_auxdata('hello world', a) FROM t4;
  641. }
  642. } {{0 0} {1 0}}
  643. do_test func-13.6 {
  644. execsql {
  645. SELECT test_auxdata('hello'||'world', a) FROM t4;
  646. }
  647. } {{0 0} {1 0}}
  648. # Test that auxilary data is preserved between calls for SQL variables.
  649. do_test func-13.7 {
  650. set DB [sqlite3_connection_pointer db]
  651. set sql "SELECT test_auxdata( ? , a ) FROM t4;"
  652. set STMT [sqlite3_prepare $DB $sql -1 TAIL]
  653. sqlite3_bind_text $STMT 1 hello\000 -1
  654. set res [list]
  655. while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
  656. lappend res [sqlite3_column_text $STMT 0]
  657. }
  658. lappend res [sqlite3_finalize $STMT]
  659. } {{0 0} {1 0} SQLITE_OK}
  660. # Test that auxiliary data is discarded when a statement is reset.
  661. do_execsql_test 13.8.1 {
  662. SELECT test_auxdata('constant') FROM t4;
  663. } {0 1}
  664. do_execsql_test 13.8.2 {
  665. SELECT test_auxdata('constant') FROM t4;
  666. } {0 1}
  667. db cache flush
  668. do_execsql_test 13.8.3 {
  669. SELECT test_auxdata('constant') FROM t4;
  670. } {0 1}
  671. set V "one"
  672. do_execsql_test 13.8.4 {
  673. SELECT test_auxdata($V), $V FROM t4;
  674. } {0 one 1 one}
  675. set V "two"
  676. do_execsql_test 13.8.5 {
  677. SELECT test_auxdata($V), $V FROM t4;
  678. } {0 two 1 two}
  679. db cache flush
  680. set V "three"
  681. do_execsql_test 13.8.6 {
  682. SELECT test_auxdata($V), $V FROM t4;
  683. } {0 three 1 three}
  684. # Make sure that a function with a very long name is rejected
  685. do_test func-14.1 {
  686. catch {
  687. db function [string repeat X 254] {return "hello"}
  688. }
  689. } {0}
  690. do_test func-14.2 {
  691. catch {
  692. db function [string repeat X 256] {return "hello"}
  693. }
  694. } {1}
  695. do_test func-15.1 {
  696. catchsql {select test_error(NULL)}
  697. } {1 {}}
  698. do_test func-15.2 {
  699. catchsql {select test_error('this is the error message')}
  700. } {1 {this is the error message}}
  701. do_test func-15.3 {
  702. catchsql {select test_error('this is the error message',12)}
  703. } {1 {this is the error message}}
  704. do_test func-15.4 {
  705. db errorcode
  706. } {12}
  707. # Test the quote function for BLOB and NULL values.
  708. do_test func-16.1 {
  709. execsql {
  710. CREATE TABLE tbl2(a, b);
  711. }
  712. set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL]
  713. sqlite3_bind_blob $::STMT 1 abc 3
  714. sqlite3_step $::STMT
  715. sqlite3_finalize $::STMT
  716. execsql {
  717. SELECT quote(a), quote(b) FROM tbl2;
  718. }
  719. } {X'616263' NULL}
  720. # Correctly handle function error messages that include %. Ticket #1354
  721. #
  722. do_test func-17.1 {
  723. proc testfunc1 args {error "Error %d with %s percents %p"}
  724. db function testfunc1 ::testfunc1
  725. catchsql {
  726. SELECT testfunc1(1,2,3);
  727. }
  728. } {1 {Error %d with %s percents %p}}
  729. # The SUM function should return integer results when all inputs are integer.
  730. #
  731. do_test func-18.1 {
  732. execsql {
  733. CREATE TABLE t5(x);
  734. INSERT INTO t5 VALUES(1);
  735. INSERT INTO t5 VALUES(-99);
  736. INSERT INTO t5 VALUES(10000);
  737. SELECT sum(x) FROM t5;
  738. }
  739. } {9902}
  740. ifcapable floatingpoint {
  741. do_test func-18.2 {
  742. execsql {
  743. INSERT INTO t5 VALUES(0.0);
  744. SELECT sum(x) FROM t5;
  745. }
  746. } {9902.0}
  747. }
  748. # The sum of nothing is NULL. But the sum of all NULLs is NULL.
  749. #
  750. # The TOTAL of nothing is 0.0.
  751. #
  752. do_test func-18.3 {
  753. execsql {
  754. DELETE FROM t5;
  755. SELECT sum(x), total(x) FROM t5;
  756. }
  757. } {{} 0.0}
  758. do_test func-18.4 {
  759. execsql {
  760. INSERT INTO t5 VALUES(NULL);
  761. SELECT sum(x), total(x) FROM t5
  762. }
  763. } {{} 0.0}
  764. do_test func-18.5 {
  765. execsql {
  766. INSERT INTO t5 VALUES(NULL);
  767. SELECT sum(x), total(x) FROM t5
  768. }
  769. } {{} 0.0}
  770. do_test func-18.6 {
  771. execsql {
  772. INSERT INTO t5 VALUES(123);
  773. SELECT sum(x), total(x) FROM t5
  774. }
  775. } {123 123.0}
  776. # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes
  777. # an error. The non-standard TOTAL() function continues to give a helpful
  778. # result.
  779. #
  780. do_test func-18.10 {
  781. execsql {
  782. CREATE TABLE t6(x INTEGER);
  783. INSERT INTO t6 VALUES(1);
  784. INSERT INTO t6 VALUES(1<<62);
  785. SELECT sum(x) - ((1<<62)+1) from t6;
  786. }
  787. } 0
  788. do_test func-18.11 {
  789. execsql {
  790. SELECT typeof(sum(x)) FROM t6
  791. }
  792. } integer
  793. ifcapable floatingpoint {
  794. do_test func-18.12 {
  795. catchsql {
  796. INSERT INTO t6 VALUES(1<<62);
  797. SELECT sum(x) - ((1<<62)*2.0+1) from t6;
  798. }
  799. } {1 {integer overflow}}
  800. do_test func-18.13 {
  801. execsql {
  802. SELECT total(x) - ((1<<62)*2.0+1) FROM t6
  803. }
  804. } 0.0
  805. }
  806. ifcapable !floatingpoint {
  807. do_test func-18.12 {
  808. catchsql {
  809. INSERT INTO t6 VALUES(1<<62);
  810. SELECT sum(x) - ((1<<62)*2+1) from t6;
  811. }
  812. } {1 {integer overflow}}
  813. do_test func-18.13 {
  814. execsql {
  815. SELECT total(x) - ((1<<62)*2+1) FROM t6
  816. }
  817. } 0.0
  818. }
  819. if {[working_64bit_int]} {
  820. do_test func-18.14 {
  821. execsql {
  822. SELECT sum(-9223372036854775805);
  823. }
  824. } -9223372036854775805
  825. }
  826. ifcapable compound&&subquery {
  827. do_test func-18.15 {
  828. catchsql {
  829. SELECT sum(x) FROM
  830. (SELECT 9223372036854775807 AS x UNION ALL
  831. SELECT 10 AS x);
  832. }
  833. } {1 {integer overflow}}
  834. if {[working_64bit_int]} {
  835. do_test func-18.16 {
  836. catchsql {
  837. SELECT sum(x) FROM
  838. (SELECT 9223372036854775807 AS x UNION ALL
  839. SELECT -10 AS x);
  840. }
  841. } {0 9223372036854775797}
  842. do_test func-18.17 {
  843. catchsql {
  844. SELECT sum(x) FROM
  845. (SELECT -9223372036854775807 AS x UNION ALL
  846. SELECT 10 AS x);
  847. }
  848. } {0 -9223372036854775797}
  849. }
  850. do_test func-18.18 {
  851. catchsql {
  852. SELECT sum(x) FROM
  853. (SELECT -9223372036854775807 AS x UNION ALL
  854. SELECT -10 AS x);
  855. }
  856. } {1 {integer overflow}}
  857. do_test func-18.19 {
  858. catchsql {
  859. SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x);
  860. }
  861. } {0 -1}
  862. do_test func-18.20 {
  863. catchsql {
  864. SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x);
  865. }
  866. } {0 1}
  867. do_test func-18.21 {
  868. catchsql {
  869. SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x);
  870. }
  871. } {0 -1}
  872. do_test func-18.22 {
  873. catchsql {
  874. SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x);
  875. }
  876. } {0 1}
  877. } ;# ifcapable compound&&subquery
  878. # Integer overflow on abs()
  879. #
  880. if {[working_64bit_int]} {
  881. do_test func-18.31 {
  882. catchsql {
  883. SELECT abs(-9223372036854775807);
  884. }
  885. } {0 9223372036854775807}
  886. }
  887. do_test func-18.32 {
  888. catchsql {
  889. SELECT abs(-9223372036854775807-1);
  890. }
  891. } {1 {integer overflow}}
  892. # The MATCH function exists but is only a stub and always throws an error.
  893. #
  894. do_test func-19.1 {
  895. execsql {
  896. SELECT match(a,b) FROM t1 WHERE 0;
  897. }
  898. } {}
  899. do_test func-19.2 {
  900. catchsql {
  901. SELECT 'abc' MATCH 'xyz';
  902. }
  903. } {1 {unable to use function MATCH in the requested context}}
  904. do_test func-19.3 {
  905. catchsql {
  906. SELECT 'abc' NOT MATCH 'xyz';
  907. }
  908. } {1 {unable to use function MATCH in the requested context}}
  909. do_test func-19.4 {
  910. catchsql {
  911. SELECT match(1,2,3);
  912. }
  913. } {1 {wrong number of arguments to function match()}}
  914. # Soundex tests.
  915. #
  916. if {![catch {db eval {SELECT soundex('hello')}}]} {
  917. set i 0
  918. foreach {name sdx} {
  919. euler E460
  920. EULER E460
  921. Euler E460
  922. ellery E460
  923. gauss G200
  924. ghosh G200
  925. hilbert H416
  926. Heilbronn H416
  927. knuth K530
  928. kant K530
  929. Lloyd L300
  930. LADD L300
  931. Lukasiewicz L222
  932. Lissajous L222
  933. A A000
  934. 12345 ?000
  935. } {
  936. incr i
  937. do_test func-20.$i {
  938. execsql {SELECT soundex($name)}
  939. } $sdx
  940. }
  941. }
  942. # Tests of the REPLACE function.
  943. #
  944. do_test func-21.1 {
  945. catchsql {
  946. SELECT replace(1,2);
  947. }
  948. } {1 {wrong number of arguments to function replace()}}
  949. do_test func-21.2 {
  950. catchsql {
  951. SELECT replace(1,2,3,4);
  952. }
  953. } {1 {wrong number of arguments to function replace()}}
  954. do_test func-21.3 {
  955. execsql {
  956. SELECT typeof(replace("This is the main test string", NULL, "ALT"));
  957. }
  958. } {null}
  959. do_test func-21.4 {
  960. execsql {
  961. SELECT typeof(replace(NULL, "main", "ALT"));
  962. }
  963. } {null}
  964. do_test func-21.5 {
  965. execsql {
  966. SELECT typeof(replace("This is the main test string", "main", NULL));
  967. }
  968. } {null}
  969. do_test func-21.6 {
  970. execsql {
  971. SELECT replace("This is the main test string", "main", "ALT");
  972. }
  973. } {{This is the ALT test string}}
  974. do_test func-21.7 {
  975. execsql {
  976. SELECT replace("This is the main test string", "main", "larger-main");
  977. }
  978. } {{This is the larger-main test string}}
  979. do_test func-21.8 {
  980. execsql {
  981. SELECT replace("aaaaaaa", "a", "0123456789");
  982. }
  983. } {0123456789012345678901234567890123456789012345678901234567890123456789}
  984. ifcapable tclvar {
  985. do_test func-21.9 {
  986. # Attempt to exploit a buffer-overflow that at one time existed
  987. # in the REPLACE function.
  988. set ::str "[string repeat A 29998]CC[string repeat A 35537]"
  989. set ::rep [string repeat B 65536]
  990. execsql {
  991. SELECT LENGTH(REPLACE($::str, 'C', $::rep));
  992. }
  993. } [expr 29998 + 2*65536 + 35537]
  994. }
  995. # Tests for the TRIM, LTRIM and RTRIM functions.
  996. #
  997. do_test func-22.1 {
  998. catchsql {SELECT trim(1,2,3)}
  999. } {1 {wrong number of arguments to function trim()}}
  1000. do_test func-22.2 {
  1001. catchsql {SELECT ltrim(1,2,3)}
  1002. } {1 {wrong number of arguments to function ltrim()}}
  1003. do_test func-22.3 {
  1004. catchsql {SELECT rtrim(1,2,3)}
  1005. } {1 {wrong number of arguments to function rtrim()}}
  1006. do_test func-22.4 {
  1007. execsql {SELECT trim(' hi ');}
  1008. } {hi}
  1009. do_test func-22.5 {
  1010. execsql {SELECT ltrim(' hi ');}
  1011. } {{hi }}
  1012. do_test func-22.6 {
  1013. execsql {SELECT rtrim(' hi ');}
  1014. } {{ hi}}
  1015. do_test func-22.7 {
  1016. execsql {SELECT trim(' hi ','xyz');}
  1017. } {{ hi }}
  1018. do_test func-22.8 {
  1019. execsql {SELECT ltrim(' hi ','xyz');}
  1020. } {{ hi }}
  1021. do_test func-22.9 {
  1022. execsql {SELECT rtrim(' hi ','xyz');}
  1023. } {{ hi }}
  1024. do_test func-22.10 {
  1025. execsql {SELECT trim('xyxzy hi zzzy','xyz');}
  1026. } {{ hi }}
  1027. do_test func-22.11 {
  1028. execsql {SELECT ltrim('xyxzy hi zzzy','xyz');}
  1029. } {{ hi zzzy}}
  1030. do_test func-22.12 {
  1031. execsql {SELECT rtrim('xyxzy hi zzzy','xyz');}
  1032. } {{xyxzy hi }}
  1033. do_test func-22.13 {
  1034. execsql {SELECT trim(' hi ','');}
  1035. } {{ hi }}
  1036. if {[db one {PRAGMA encoding}]=="UTF-8"} {
  1037. do_test func-22.14 {
  1038. execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))}
  1039. } {F48FBFBF6869}
  1040. do_test func-22.15 {
  1041. execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61',
  1042. x'6162e1bfbfc280f48fbfbf'))}
  1043. } {6869}
  1044. do_test func-22.16 {
  1045. execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));}
  1046. } {CEB2CEB3}
  1047. }
  1048. do_test func-22.20 {
  1049. execsql {SELECT typeof(trim(NULL));}
  1050. } {null}
  1051. do_test func-22.21 {
  1052. execsql {SELECT typeof(trim(NULL,'xyz'));}
  1053. } {null}
  1054. do_test func-22.22 {
  1055. execsql {SELECT typeof(trim('hello',NULL));}
  1056. } {null}
  1057. # This is to test the deprecated sqlite3_aggregate_count() API.
  1058. #
  1059. ifcapable deprecated {
  1060. do_test func-23.1 {
  1061. sqlite3_create_aggregate db
  1062. execsql {
  1063. SELECT legacy_count() FROM t6;
  1064. }
  1065. } {3}
  1066. }
  1067. # The group_concat() function.
  1068. #
  1069. do_test func-24.1 {
  1070. execsql {
  1071. SELECT group_concat(t1) FROM tbl1
  1072. }
  1073. } {this,program,is,free,software}
  1074. do_test func-24.2 {
  1075. execsql {
  1076. SELECT group_concat(t1,' ') FROM tbl1
  1077. }
  1078. } {{this program is free software}}
  1079. do_test func-24.3 {
  1080. execsql {
  1081. SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1
  1082. }
  1083. } {{this 2 program 3 is 4 free 5 software}}
  1084. do_test func-24.4 {
  1085. execsql {
  1086. SELECT group_concat(NULL,t1) FROM tbl1
  1087. }
  1088. } {{}}
  1089. do_test func-24.5 {
  1090. execsql {
  1091. SELECT group_concat(t1,NULL) FROM tbl1
  1092. }
  1093. } {thisprogramisfreesoftware}
  1094. do_test func-24.6 {
  1095. execsql {
  1096. SELECT 'BEGIN-'||group_concat(t1) FROM tbl1
  1097. }
  1098. } {BEGIN-this,program,is,free,software}
  1099. # Ticket #3179: Make sure aggregate functions can take many arguments.
  1100. # None of the built-in aggregates do this, so use the md5sum() from the
  1101. # test extensions.
  1102. #
  1103. unset -nocomplain midargs
  1104. set midargs {}
  1105. unset -nocomplain midres
  1106. set midres {}
  1107. unset -nocomplain result
  1108. for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]} {incr i} {
  1109. append midargs ,'/$i'
  1110. append midres /$i
  1111. set result [md5 \
  1112. "this${midres}program${midres}is${midres}free${midres}software${midres}"]
  1113. set sql "SELECT md5sum(t1$midargs) FROM tbl1"
  1114. do_test func-24.7.$i {
  1115. db eval $::sql
  1116. } $result
  1117. }
  1118. # Ticket #3806. If the initial string in a group_concat is an empty
  1119. # string, the separator that follows should still be present.
  1120. #
  1121. do_test func-24.8 {
  1122. execsql {
  1123. SELECT group_concat(CASE t1 WHEN 'this' THEN '' ELSE t1 END) FROM tbl1
  1124. }
  1125. } {,program,is,free,software}
  1126. do_test func-24.9 {
  1127. execsql {
  1128. SELECT group_concat(CASE WHEN t1!='software' THEN '' ELSE t1 END) FROM tbl1
  1129. }
  1130. } {,,,,software}
  1131. # Ticket #3923. Initial empty strings have a separator. But initial
  1132. # NULLs do not.
  1133. #
  1134. do_test func-24.10 {
  1135. execsql {
  1136. SELECT group_concat(CASE t1 WHEN 'this' THEN null ELSE t1 END) FROM tbl1
  1137. }
  1138. } {program,is,free,software}
  1139. do_test func-24.11 {
  1140. execsql {
  1141. SELECT group_concat(CASE WHEN t1!='software' THEN null ELSE t1 END) FROM tbl1
  1142. }
  1143. } {software}
  1144. do_test func-24.12 {
  1145. execsql {
  1146. SELECT group_concat(CASE t1 WHEN 'this' THEN ''
  1147. WHEN 'program' THEN null ELSE t1 END) FROM tbl1
  1148. }
  1149. } {,is,free,software}
  1150. # Use the test_isolation function to make sure that type conversions
  1151. # on function arguments do not effect subsequent arguments.
  1152. #
  1153. do_test func-25.1 {
  1154. execsql {SELECT test_isolation(t1,t1) FROM tbl1}
  1155. } {this program is free software}
  1156. # Try to misuse the sqlite3_create_function() interface. Verify that
  1157. # errors are returned.
  1158. #
  1159. do_test func-26.1 {
  1160. abuse_create_function db
  1161. } {}
  1162. # The previous test (func-26.1) registered a function with a very long
  1163. # function name that takes many arguments and always returns NULL. Verify
  1164. # that this function works correctly.
  1165. #
  1166. do_test func-26.2 {
  1167. set a {}
  1168. for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} {
  1169. lappend a $i
  1170. }
  1171. db eval "
  1172. SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
  1173. "
  1174. } {{}}
  1175. do_test func-26.3 {
  1176. set a {}
  1177. for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG+1} {incr i} {
  1178. lappend a $i
  1179. }
  1180. catchsql "
  1181. SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
  1182. "
  1183. } {1 {too many arguments on function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789}}
  1184. do_test func-26.4 {
  1185. set a {}
  1186. for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} {
  1187. lappend a $i
  1188. }
  1189. catchsql "
  1190. SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
  1191. "
  1192. } {1 {wrong number of arguments to function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789()}}
  1193. do_test func-26.5 {
  1194. catchsql "
  1195. SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a(0);
  1196. "
  1197. } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a}}
  1198. do_test func-26.6 {
  1199. catchsql "
  1200. SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a(0);
  1201. "
  1202. } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a}}
  1203. do_test func-27.1 {
  1204. catchsql {SELECT coalesce()}
  1205. } {1 {wrong number of arguments to function coalesce()}}
  1206. do_test func-27.2 {
  1207. catchsql {SELECT coalesce(1)}
  1208. } {1 {wrong number of arguments to function coalesce()}}
  1209. do_test func-27.3 {
  1210. catchsql {SELECT coalesce(1,2)}
  1211. } {0 1}
  1212. # Ticket 2d401a94287b5
  1213. # Unknown function in a DEFAULT expression causes a segfault.
  1214. #
  1215. do_test func-28.1 {
  1216. db eval {
  1217. CREATE TABLE t28(x, y DEFAULT(nosuchfunc(1)));
  1218. }
  1219. catchsql {
  1220. INSERT INTO t28(x) VALUES(1);
  1221. }
  1222. } {1 {unknown function: nosuchfunc()}}
  1223. # Verify that the length() and typeof() functions do not actually load
  1224. # the content of their argument.
  1225. #
  1226. do_test func-29.1 {
  1227. db eval {
  1228. CREATE TABLE t29(id INTEGER PRIMARY KEY, x, y);
  1229. INSERT INTO t29 VALUES(1, 2, 3), (2, NULL, 4), (3, 4.5, 5);
  1230. INSERT INTO t29 VALUES(4, randomblob(1000000), 6);
  1231. INSERT INTO t29 VALUES(5, "hello", 7);
  1232. }
  1233. db close
  1234. sqlite3 db test.db
  1235. sqlite3_db_status db CACHE_MISS 1
  1236. db eval {SELECT typeof(x), length(x), typeof(y) FROM t29 ORDER BY id}
  1237. } {integer 1 integer null {} integer real 3 integer blob 1000000 integer text 5 integer}
  1238. do_test func-29.2 {
  1239. set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
  1240. if {$x<5} {set x 1}
  1241. set x
  1242. } {1}
  1243. do_test func-29.3 {
  1244. db close
  1245. sqlite3 db test.db
  1246. sqlite3_db_status db CACHE_MISS 1
  1247. db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
  1248. } {integer null real blob text}
  1249. if {[permutation] != "mmap"} {
  1250. do_test func-29.4 {
  1251. set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
  1252. if {$x>100} {set x many}
  1253. set x
  1254. } {many}
  1255. }
  1256. do_test func-29.5 {
  1257. db close
  1258. sqlite3 db test.db
  1259. sqlite3_db_status db CACHE_MISS 1
  1260. db eval {SELECT sum(length(x)) FROM t29}
  1261. } {1000009}
  1262. do_test func-29.6 {
  1263. set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
  1264. if {$x<5} {set x 1}
  1265. set x
  1266. } {1}
  1267. # EVIDENCE-OF: R-29701-50711 The unicode(X) function returns the numeric
  1268. # unicode code point corresponding to the first character of the string
  1269. # X.
  1270. #
  1271. # EVIDENCE-OF: R-55469-62130 The char(X1,X2,...,XN) function returns a
  1272. # string composed of characters having the unicode code point values of
  1273. # integers X1 through XN, respectively.
  1274. #
  1275. do_execsql_test func-30.1 {SELECT unicode('$');} 36
  1276. do_execsql_test func-30.2 [subst {SELECT unicode('\u00A2');}] 162
  1277. do_execsql_test func-30.3 [subst {SELECT unicode('\u20AC');}] 8364
  1278. do_execsql_test func-30.4 {SELECT char(36,162,8364);} [subst {$\u00A2\u20AC}]
  1279. for {set i 1} {$i<0xd800} {incr i 13} {
  1280. do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
  1281. }
  1282. for {set i 57344} {$i<=0xfffd} {incr i 17} {
  1283. if {$i==0xfeff} continue
  1284. do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
  1285. }
  1286. for {set i 65536} {$i<=0x10ffff} {incr i 139} {
  1287. do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
  1288. }
  1289. finish_test