summaryrefslogtreecommitdiff
path: root/test/capi3d.test
blob: 746ec20b25b09d181b5857ee94bc1cbcc6f90643 (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
# 2008 June 18
#
# 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.  
#
# This file is devoted to testing the sqlite3_next_stmt and
# sqlite3_stmt_readonly and sqlite3_stmt_busy interfaces.
#
# $Id: capi3d.test,v 1.2 2008/07/14 15:11:20 drh Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Create N prepared statements against database connection db
# and return a list of all the generated prepared statements.
#
proc make_prepared_statements {N} {
  set plist {}
  for {set i 0} {$i<$N} {incr i} {
    set sql "SELECT $i FROM sqlite_master WHERE name LIKE '%$i%'"
    if {rand()<0.33} {    
      set s [sqlite3_prepare_v2 db $sql -1 notused]
    } else {
      ifcapable utf16 {
        if {rand()<0.5} {
          set sql [encoding convertto unicode $sql]\x00\x00
          set s [sqlite3_prepare16 db $sql -1 notused]
        } else {
          set s [sqlite3_prepare db $sql -1 notused]
        }
      }
      ifcapable !utf16 {
        set s [sqlite3_prepare db $sql -1 notused]
      }
    }
    lappend plist $s
  }
  return $plist
}


# Scramble the $inlist into a random order.
#
proc scramble {inlist} {
  set y {}
  foreach x $inlist {
    lappend y [list [expr {rand()}] $x]
  }
  set y [lsort $y]
  set outlist {}
  foreach x $y {
    lappend outlist [lindex $x 1]
  }
  return $outlist
}

# Database initially has no prepared statements.
#
do_test capi3d-1.1 {
  db cache flush
  sqlite3_next_stmt db 0
} {}

# Run the following tests for between 1 and 100 prepared statements.
#
for {set i 1} {$i<=100} {incr i} {
  set stmtlist [make_prepared_statements $i]
  do_test capi3d-1.2.$i.1 {
    set p [sqlite3_next_stmt db 0]
    set x {}
    while {$p!=""} {
      lappend x $p
      set p [sqlite3_next_stmt db $p]
    }
    lsort $x
  } [lsort $stmtlist]
  do_test capi3-1.2.$i.2 {
    foreach p [scramble $::stmtlist] {
      sqlite3_finalize $p
    }
    sqlite3_next_stmt db 0
  } {}
}

# Tests for the is-read-only interface.
#
proc test_is_readonly {testname sql truth} {
  do_test $testname [format {
    set DB [sqlite3_connection_pointer db]
    set STMT [sqlite3_prepare $DB {%s} -1 TAIL]
    set rc [sqlite3_stmt_readonly $STMT]
    sqlite3_finalize $STMT
    set rc
  } $sql] $truth
}

test_is_readonly capi3d-2.1 {SELECT * FROM sqlite_master} 1
test_is_readonly capi3d-2.2 {CREATE TABLE t1(x)} 0
db eval {CREATE TABLE t1(x)}
test_is_readonly capi3d-2.3 {INSERT INTO t1 VALUES(5)} 0
test_is_readonly capi3d-2.4 {UPDATE t1 SET x=x+1 WHERE x<0} 0
test_is_readonly capi3d-2.5 {SELECT * FROM t1} 1
do_test capi3-2.99 {
  sqlite3_stmt_readonly 0
} 1

# Tests for sqlite3_stmt_busy
#
do_test capi3d-3.1 {
  db eval {INSERT INTO t1 VALUES(6); INSERT INTO t1 VALUES(7);}
  set STMT [sqlite3_prepare db {SELECT * FROM t1} -1 TAIL]
  sqlite3_stmt_busy $STMT
} {0}
do_test capi3d-3.2 {
  sqlite3_step $STMT
  sqlite3_stmt_busy $STMT
} {1}
do_test capi3d-3.3 {
  sqlite3_step $STMT
  sqlite3_stmt_busy $STMT
} {1}
do_test capi3d-3.4 {
  sqlite3_reset $STMT
  sqlite3_stmt_busy $STMT
} {0}

do_test capi3d-3.99 {
  sqlite3_finalize $STMT
  sqlite3_stmt_busy 0
} {0}

finish_test