aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/test/btree5.test
blob: 2afcd8425ff6d557935524d64353651e34847b0e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# 2004 May 10
#
# 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.  The
# focus of this script is btree database backend
#
# $Id: btree5.test,v 1.5 2004/05/14 12:17:46 drh Exp $


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

# Attempting to read table 1 of an empty file gives an SQLITE_EMPTY
# error.
#
do_test btree5-1.1 {
  file delete -force test1.bt
  file delete -force test1.bt-journal
  set rc [catch {btree_open test1.bt 2000 0} ::b1]
} {0}
do_test btree5-1.2 {
  set rc [catch {btree_cursor $::b1 1 0} ::c1]
} {1}
do_test btree5-1.3 {
  set ::c1
} {SQLITE_EMPTY}
do_test btree5-1.4 {
  set rc [catch {btree_cursor $::b1 1 1} ::c1]
} {1}
do_test btree5-1.5 {
  set ::c1
} {SQLITE_EMPTY}

# Starting a transaction initializes the first page of the database
# and the error goes away.
#
do_test btree5-1.6 {
  btree_begin_transaction $b1
  set rc [catch {btree_cursor $b1 1 0} c1]
} {0}
do_test btree5-1.7 {
  btree_first $c1
} {1}
do_test btree5-1.8 {
  btree_close_cursor $c1
  btree_rollback $b1
  set rc [catch {btree_cursor $b1 1 0} c1]
} {1}
do_test btree5-1.9 {
  set c1
} {SQLITE_EMPTY}
do_test btree5-1.10 {
  btree_begin_transaction $b1
  set rc [catch {btree_cursor $b1 1 0} c1]
} {0}
do_test btree5-1.11 {
  btree_first $c1
} {1}
do_test btree5-1.12 {
  btree_close_cursor $c1
  btree_commit $b1
  set rc [catch {btree_cursor $b1 1 0} c1]
} {0}
do_test btree5-1.13 {
  btree_first $c1
} {1}
do_test btree5-1.14 {
  btree_close_cursor $c1
  btree_integrity_check $b1 1
} {}

# Insert many entries into table 1.  This is designed to test the
# virtual-root logic that comes into play for page one.  It is also
# a good test of INTKEY tables.
#
# Stagger the inserts.  After the inserts complete, go back and do
# deletes.  Stagger the deletes too.  Repeat this several times.
#

# Do N inserts into table 1 using random keys between 0 and 1000000
#
proc random_inserts {N} {
  global c1
  while {$N>0} {
    set k [expr {int(rand()*1000000)}]
    if {[btree_move_to $c1 $k]==0} continue;  # entry already exists
    btree_insert $c1 $k data-for-$k
    incr N -1
  }
}

# Do N delete from table 1
#
proc random_deletes {N} {
  global c1
  while {$N>0} {
    set k [expr {int(rand()*1000000)}]
    btree_move_to $c1 $k
    btree_delete $c1
    incr N -1
  }
}

# Make sure the table has exactly N entries.  Make sure the data for
# each entry agrees with its key.
#
proc check_table {N} {
  global c1
  btree_first $c1
  set cnt 0
  while {![btree_eof $c1]} {
    if {[set data [btree_data $c1]] ne "data-for-[btree_key $c1]"} {
      return "wrong data for entry $cnt"
    }
    set n [string length $data]
    set fdata1 [btree_fetch_data $c1 $n]
    set fdata2 [btree_fetch_data $c1 -1]
    if {$fdata1 ne "" && $fdata1 ne $data} {
      return "DataFetch returned the wrong value with amt=$n"
    }
    if {$fdata1 ne $fdata2} {
      return "DataFetch returned the wrong value when amt=-1"
    }
    if {$n>10} {
      set fdata3 [btree_fetch_data $c1 10]
      if {$fdata3 ne [string range $data 0 9]} {
        return "DataFetch returned the wrong value when amt=10"
      }
    }
    incr cnt
    btree_next $c1
  }
  if {$cnt!=$N} {
    return "wrong number of entries"
  }
  return {}
}

# Initialize the database
#
btree_begin_transaction $b1
set c1 [btree_cursor $b1 1 1]
set btree_trace 0

# Do the tests.
#
set cnt 0
for {set i 1} {$i<=100} {incr i} {
  do_test btree5-2.$i.1 {
    random_inserts 200
    incr cnt 200
    check_table $cnt
  } {}
  do_test btree5-2.$i.2 {
    btree_integrity_check $b1 1
  } {}
  do_test btree5-2.$i.3 {
    random_deletes 190
    incr cnt -190
    check_table $cnt
  } {}
  do_test btree5-2.$i.4 {
    btree_integrity_check $b1 1
  } {}
}

#btree_tree_dump $b1 1
btree_close_cursor $c1
btree_commit $b1
btree_begin_transaction $b1

# This procedure converts an integer into a variable-length text key.
# The conversion is reversible.
#
# The first two characters of the string are alphabetics derived from
# the least significant bits of the number.  Because they are derived
# from least significant bits, the sort order of the resulting string
# is different from numeric order.  After the alphabetic prefix comes
# the original number.  A variable-length suffix follows.  The length
# of the suffix is based on a hash of the original number.
# 
proc num_to_key {n} {
  global charset ncharset suffix
  set c1 [string index $charset [expr {$n%$ncharset}]]
  set c2 [string index $charset [expr {($n/$ncharset)%$ncharset}]]
  set nsuf [expr {($n*211)%593}]
  return $c1$c2-$n-[string range $suffix 0 $nsuf]
}
set charset {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
set ncharset [string length $charset]
set suffix $charset$charset
while {[string length $suffix]<1000} {append suffix $suffix}

# This procedures extracts the original integer used to create
# a key by num_to_key
#
proc key_to_num {key} {
  regexp {^..-([0-9]+)} $key all n
  return $n
}

# Insert into table $tab keys corresponding to all values between
# $start and $end, inclusive.
#
proc insert_range {tab start end} {
  for {set i $start} {$i<=$end} {incr i} {
    btree_insert $tab [num_to_key $i] {}
  }
}

# Delete from table $tab keys corresponding to all values between
# $start and $end, inclusive.
#
proc delete_range {tab start end} {
  for {set i $start} {$i<=$end} {incr i} {
    if {[btree_move_to $tab [num_to_key $i]]==0} {
      btree_delete $tab
    }
  }
}

# Make sure table $tab contains exactly those keys corresponding
# to values between $start and $end
#
proc check_range {tab start end} {
  btree_first $tab
  while {![btree_eof $tab]} {
    set key [btree_key $tab]
    set i [key_to_num $key]
    if {[num_to_key $i] ne $key} {
      return "malformed key: $key"
    }
    set got($i) 1
    btree_next $tab
  }
  set all [lsort -integer [array names got]]
  if {[llength $all]!=$end+1-$start} {
    return "table contains wrong number of values"
  }
  if {[lindex $all 0]!=$start} {
    return "wrong starting value"
  }
  if {[lindex $all end]!=$end} {
    return "wrong ending value"
  }
  return {}
}

# Create a zero-data table and test it out.
#
do_test btree5-3.1 {
  set rc [catch {btree_create_table $b1 2} t2]
} {0}
do_test btree5-3.2 {
  set rc [catch {btree_cursor $b1 $t2 1} c2]
} {0}
set start 1
set end 100
for {set i 1} {$i<=100} {incr i} {
  do_test btree5-3.3.$i.1 {
    insert_range $c2 $start $end
    btree_integrity_check $b1 1 $t2
  } {}
  do_test btree5-3.3.$i.2 {
    check_range $c2 $start $end
  } {}
  set nstart $start
  incr nstart 89
  do_test btree5-3.3.$i.3 {
    delete_range $c2 $start $nstart
    btree_integrity_check $b1 1 $t2
  } {}
  incr start 90
  do_test btree5-3.3.$i.4 {
    check_range $c2 $start $end
  } {}
  incr end 100
}


btree_close_cursor $c2
btree_commit $b1
btree_close $b1

finish_test