capi3d.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # 2008 June 18
  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.
  12. #
  13. # This file is devoted to testing the sqlite3_next_stmt and
  14. # sqlite3_stmt_readonly and sqlite3_stmt_busy interfaces.
  15. #
  16. # $Id: capi3d.test,v 1.2 2008/07/14 15:11:20 drh Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # Create N prepared statements against database connection db
  21. # and return a list of all the generated prepared statements.
  22. #
  23. proc make_prepared_statements {N} {
  24. set plist {}
  25. for {set i 0} {$i<$N} {incr i} {
  26. set sql "SELECT $i FROM sqlite_master WHERE name LIKE '%$i%'"
  27. if {rand()<0.33} {
  28. set s [sqlite3_prepare_v2 db $sql -1 notused]
  29. } else {
  30. ifcapable utf16 {
  31. if {rand()<0.5} {
  32. set sql [encoding convertto unicode $sql]\x00\x00
  33. set s [sqlite3_prepare16 db $sql -1 notused]
  34. } else {
  35. set s [sqlite3_prepare db $sql -1 notused]
  36. }
  37. }
  38. ifcapable !utf16 {
  39. set s [sqlite3_prepare db $sql -1 notused]
  40. }
  41. }
  42. lappend plist $s
  43. }
  44. return $plist
  45. }
  46. # Scramble the $inlist into a random order.
  47. #
  48. proc scramble {inlist} {
  49. set y {}
  50. foreach x $inlist {
  51. lappend y [list [expr {rand()}] $x]
  52. }
  53. set y [lsort $y]
  54. set outlist {}
  55. foreach x $y {
  56. lappend outlist [lindex $x 1]
  57. }
  58. return $outlist
  59. }
  60. # Database initially has no prepared statements.
  61. #
  62. do_test capi3d-1.1 {
  63. db cache flush
  64. sqlite3_next_stmt db 0
  65. } {}
  66. # Run the following tests for between 1 and 100 prepared statements.
  67. #
  68. for {set i 1} {$i<=100} {incr i} {
  69. set stmtlist [make_prepared_statements $i]
  70. do_test capi3d-1.2.$i.1 {
  71. set p [sqlite3_next_stmt db 0]
  72. set x {}
  73. while {$p!=""} {
  74. lappend x $p
  75. set p [sqlite3_next_stmt db $p]
  76. }
  77. lsort $x
  78. } [lsort $stmtlist]
  79. do_test capi3-1.2.$i.2 {
  80. foreach p [scramble $::stmtlist] {
  81. sqlite3_finalize $p
  82. }
  83. sqlite3_next_stmt db 0
  84. } {}
  85. }
  86. # Tests for the is-read-only interface.
  87. #
  88. proc test_is_readonly {testname sql truth} {
  89. do_test $testname [format {
  90. set DB [sqlite3_connection_pointer db]
  91. set STMT [sqlite3_prepare $DB {%s} -1 TAIL]
  92. set rc [sqlite3_stmt_readonly $STMT]
  93. sqlite3_finalize $STMT
  94. set rc
  95. } $sql] $truth
  96. }
  97. test_is_readonly capi3d-2.1 {SELECT * FROM sqlite_master} 1
  98. test_is_readonly capi3d-2.2 {CREATE TABLE t1(x)} 0
  99. db eval {CREATE TABLE t1(x)}
  100. test_is_readonly capi3d-2.3 {INSERT INTO t1 VALUES(5)} 0
  101. test_is_readonly capi3d-2.4 {UPDATE t1 SET x=x+1 WHERE x<0} 0
  102. test_is_readonly capi3d-2.5 {SELECT * FROM t1} 1
  103. ifcapable wal {
  104. test_is_readonly capi3d-2.6 {PRAGMA journal_mode=WAL} 0
  105. test_is_readonly capi3d-2.7 {PRAGMA wal_checkpoint} 0
  106. }
  107. test_is_readonly capi3d-2.8 {PRAGMA application_id=1234} 0
  108. test_is_readonly capi3d-2.9 {VACUUM} 0
  109. test_is_readonly capi3d-2.10 {PRAGMA integrity_check} 1
  110. do_test capi3-2.99 {
  111. sqlite3_stmt_readonly 0
  112. } 1
  113. # Tests for sqlite3_stmt_busy
  114. #
  115. do_test capi3d-3.1 {
  116. db eval {INSERT INTO t1 VALUES(6); INSERT INTO t1 VALUES(7);}
  117. set STMT [sqlite3_prepare db {SELECT * FROM t1} -1 TAIL]
  118. sqlite3_stmt_busy $STMT
  119. } {0}
  120. do_test capi3d-3.2 {
  121. sqlite3_step $STMT
  122. sqlite3_stmt_busy $STMT
  123. } {1}
  124. do_test capi3d-3.3 {
  125. sqlite3_step $STMT
  126. sqlite3_stmt_busy $STMT
  127. } {1}
  128. do_test capi3d-3.4 {
  129. sqlite3_reset $STMT
  130. sqlite3_stmt_busy $STMT
  131. } {0}
  132. do_test capi3d-3.99 {
  133. sqlite3_finalize $STMT
  134. sqlite3_stmt_busy 0
  135. } {0}
  136. finish_test