func3.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. ifcapable utf16 {
  19. do_test func3-1.1 {
  20. set destroyed 0
  21. proc destroy {} { set ::destroyed 1 }
  22. sqlite3_create_function_v2 db f2 -1 any -func f2 -destroy destroy
  23. set destroyed
  24. } 0
  25. do_test func3-1.2 {
  26. sqlite3_create_function_v2 db f2 -1 utf8 -func f2
  27. set destroyed
  28. } 0
  29. do_test func3-1.3 {
  30. sqlite3_create_function_v2 db f2 -1 utf16le -func f2
  31. set destroyed
  32. } 0
  33. do_test func3-1.4 {
  34. sqlite3_create_function_v2 db f2 -1 utf16be -func f2
  35. set destroyed
  36. } 1
  37. }
  38. do_test func3-2.1 {
  39. set destroyed 0
  40. proc destroy {} { set ::destroyed 1 }
  41. sqlite3_create_function_v2 db f3 -1 utf8 -func f3 -destroy destroy
  42. set destroyed
  43. } 0
  44. do_test func3-2.2 {
  45. sqlite3_create_function_v2 db f3 -1 utf8 -func f3
  46. set destroyed
  47. } 1
  48. do_test func3-3.1 {
  49. set destroyed 0
  50. proc destroy {} { set ::destroyed 1 }
  51. sqlite3_create_function_v2 db f3 -1 any -func f3 -destroy destroy
  52. set destroyed
  53. } 0
  54. do_test func3-3.2 {
  55. db close
  56. set destroyed
  57. } 1
  58. sqlite3 db test.db
  59. do_test func3-4.1 {
  60. set destroyed 0
  61. set rc [catch {
  62. sqlite3_create_function_v2 db f3 -1 any -func f3 -step f3 -destroy destroy
  63. } msg]
  64. list $rc $msg
  65. } {1 SQLITE_MISUSE}
  66. do_test func3-4.2 { set destroyed } 1
  67. # EVIDENCE-OF: R-41921-05214 The likelihood(X,Y) function returns
  68. # argument X unchanged.
  69. #
  70. do_execsql_test func3-5.1 {
  71. SELECT likelihood(9223372036854775807, 0.5);
  72. } {9223372036854775807}
  73. do_execsql_test func3-5.2 {
  74. SELECT likelihood(-9223372036854775808, 0.5);
  75. } {-9223372036854775808}
  76. do_execsql_test func3-5.3 {
  77. SELECT likelihood(14.125, 0.5);
  78. } {14.125}
  79. do_execsql_test func3-5.4 {
  80. SELECT likelihood(NULL, 0.5);
  81. } {{}}
  82. do_execsql_test func3-5.5 {
  83. SELECT likelihood('test-string', 0.5);
  84. } {test-string}
  85. do_execsql_test func3-5.6 {
  86. SELECT quote(likelihood(x'010203000405', 0.5));
  87. } {X'010203000405'}
  88. # EVIDENCE-OF: R-44133-61651 The value Y in likelihood(X,Y) must be a
  89. # floating point constant between 0.0 and 1.0, inclusive.
  90. #
  91. do_execsql_test func3-5.7 {
  92. SELECT likelihood(123, 1.0), likelihood(456, 0.0);
  93. } {123 456}
  94. do_test func3-5.8 {
  95. catchsql {
  96. SELECT likelihood(123, 1.000001);
  97. }
  98. } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
  99. do_test func3-5.9 {
  100. catchsql {
  101. SELECT likelihood(123, -0.000001);
  102. }
  103. } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
  104. do_test func3-5.10 {
  105. catchsql {
  106. SELECT likelihood(123, 0.5+0.3);
  107. }
  108. } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
  109. # EVIDENCE-OF: R-28535-44631 The likelihood(X) function is a no-op that
  110. # the code generator optimizes away so that it consumes no CPU cycles
  111. # during run-time (that is, during calls to sqlite3_step()).
  112. #
  113. do_test func3-5.20 {
  114. db eval {EXPLAIN SELECT likelihood(min(1.0+'2.0',4*11), 0.5)}
  115. } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]
  116. # EVIDENCE-OF: R-11152-23456 The unlikely(X) function returns the
  117. # argument X unchanged.
  118. #
  119. do_execsql_test func3-5.30 {
  120. SELECT unlikely(9223372036854775807);
  121. } {9223372036854775807}
  122. do_execsql_test func3-5.31 {
  123. SELECT unlikely(-9223372036854775808);
  124. } {-9223372036854775808}
  125. do_execsql_test func3-5.32 {
  126. SELECT unlikely(14.125);
  127. } {14.125}
  128. do_execsql_test func3-5.33 {
  129. SELECT unlikely(NULL);
  130. } {{}}
  131. do_execsql_test func3-5.34 {
  132. SELECT unlikely('test-string');
  133. } {test-string}
  134. do_execsql_test func3-5.35 {
  135. SELECT quote(unlikely(x'010203000405'));
  136. } {X'010203000405'}
  137. # EVIDENCE-OF: R-22887-63324 The unlikely(X) function is a no-op that
  138. # the code generator optimizes away so that it consumes no CPU cycles at
  139. # run-time (that is, during calls to sqlite3_step()).
  140. #
  141. do_test func3-5.40 {
  142. db eval {EXPLAIN SELECT unlikely(min(1.0+'2.0',4*11))}
  143. } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}]
  144. finish_test