xref: /titanic_41/usr/src/lib/libsqlite/test/pager.test (revision 5bbb4db2c3f208d12bf0fd11769728f9e5ba66a2)
1
2#pragma ident	"%Z%%M%	%I%	%E% SMI"
3
4# 2001 September 15
5#
6# The author disclaims copyright to this source code.  In place of
7# a legal notice, here is a blessing:
8#
9#    May you do good and not evil.
10#    May you find forgiveness for yourself and forgive others.
11#    May you share freely, never taking more than you give.
12#
13#***********************************************************************
14# This file implements regression tests for SQLite library.  The
15# focus of this script is page cache subsystem.
16#
17# $Id: pager.test,v 1.14 2004/02/25 02:20:42 drh Exp $
18
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23if {[info commands pager_open]!=""} {
24db close
25
26# Basic sanity check.  Open and close a pager.
27#
28do_test pager-1.0 {
29  catch {file delete -force ptf1.db}
30  catch {file delete -force ptf1.db-journal}
31  set v [catch {
32    set ::p1 [pager_open ptf1.db 10]
33  } msg]
34} {0}
35do_test pager-1.1 {
36  pager_stats $::p1
37} {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
38do_test pager-1.2 {
39  pager_pagecount $::p1
40} {0}
41do_test pager-1.3 {
42  pager_stats $::p1
43} {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
44do_test pager-1.4 {
45  pager_close $::p1
46} {}
47
48# Try to write a few pages.
49#
50do_test pager-2.1 {
51  set v [catch {
52    set ::p1 [pager_open ptf1.db 10]
53  } msg]
54} {0}
55#do_test pager-2.2 {
56#  set v [catch {
57#    set ::g1 [page_get $::p1 0]
58#  } msg]
59#  lappend v $msg
60#} {1 SQLITE_ERROR}
61do_test pager-2.3.1 {
62  set ::gx [page_lookup $::p1 1]
63} {}
64do_test pager-2.3.2 {
65  pager_stats $::p1
66} {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
67do_test pager-2.3.3 {
68  set v [catch {
69    set ::g1 [page_get $::p1 1]
70  } msg]
71  if {$v} {lappend v $msg}
72  set v
73} {0}
74do_test pager-2.3.3 {
75  pager_stats $::p1
76} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
77do_test pager-2.3.4 {
78  set ::gx [page_lookup $::p1 1]
79  expr {$::gx!=""}
80} {1}
81do_test pager-2.3.5 {
82  pager_stats $::p1
83} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
84do_test pager-2.3.6 {
85  expr $::g1==$::gx
86} {1}
87do_test pager-2.3.7 {
88  page_unref $::gx
89  pager_stats $::p1
90} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
91do_test pager-2.4 {
92  pager_stats $::p1
93} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
94do_test pager-2.5 {
95  pager_pagecount $::p1
96} {0}
97do_test pager-2.6 {
98  pager_stats $::p1
99} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
100do_test pager-2.7 {
101  page_number $::g1
102} {1}
103do_test pager-2.8 {
104  page_read $::g1
105} {}
106do_test pager-2.9 {
107  page_unref $::g1
108} {}
109do_test pager-2.10 {
110  pager_stats $::p1
111} {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 1 ovfl 0}
112do_test pager-2.11 {
113  set ::g1 [page_get $::p1 1]
114  expr {$::g1!=0}
115} {1}
116do_test pager-2.12 {
117  page_number $::g1
118} {1}
119do_test pager-2.13 {
120  pager_stats $::p1
121} {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 2 ovfl 0}
122do_test pager-2.14 {
123  set v [catch {
124    page_write $::g1 "Page-One"
125  } msg]
126  lappend v $msg
127} {0 {}}
128do_test pager-2.15 {
129  pager_stats $::p1
130} {ref 1 page 1 max 10 size 1 state 2 err 0 hit 0 miss 2 ovfl 0}
131do_test pager-2.16 {
132  page_read $::g1
133} {Page-One}
134do_test pager-2.17 {
135  set v [catch {
136    pager_commit $::p1
137  } msg]
138  lappend v $msg
139} {0 {}}
140do_test pager-2.20 {
141  pager_stats $::p1
142} {ref 1 page 1 max 10 size -1 state 1 err 0 hit 0 miss 2 ovfl 0}
143do_test pager-2.19 {
144  pager_pagecount $::p1
145} {1}
146do_test pager-2.21 {
147  pager_stats $::p1
148} {ref 1 page 1 max 10 size 1 state 1 err 0 hit 0 miss 2 ovfl 0}
149do_test pager-2.22 {
150  page_unref $::g1
151} {}
152do_test pager-2.23 {
153  pager_stats $::p1
154} {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 2 ovfl 0}
155do_test pager-2.24 {
156  set v [catch {
157    page_get $::p1 1
158  } ::g1]
159  if {$v} {lappend v $::g1}
160  set v
161} {0}
162do_test pager-2.25 {
163  page_read $::g1
164} {Page-One}
165do_test pager-2.26 {
166  set v [catch {
167    page_write $::g1 {page-one}
168  } msg]
169  lappend v $msg
170} {0 {}}
171do_test pager-2.27 {
172  page_read $::g1
173} {page-one}
174do_test pager-2.28 {
175  set v [catch {
176    pager_rollback $::p1
177  } msg]
178  lappend v $msg
179} {0 {}}
180do_test pager-2.29 {
181  page_unref $::g1
182  set ::g1 [page_get $::p1 1]
183  page_read $::g1
184} {Page-One}
185do_test pager-2.99 {
186  pager_close $::p1
187} {}
188
189do_test pager-3.1 {
190  set v [catch {
191    set ::p1 [pager_open ptf1.db 15]
192  } msg]
193  if {$v} {lappend v $msg}
194  set v
195} {0}
196do_test pager-3.2 {
197  pager_pagecount $::p1
198} {1}
199do_test pager-3.3 {
200  set v [catch {
201    set ::g(1) [page_get $::p1 1]
202  } msg]
203  if {$v} {lappend v $msg}
204  set v
205} {0}
206do_test pager-3.4 {
207  page_read $::g(1)
208} {Page-One}
209do_test pager-3.5 {
210  for {set i 2} {$i<=20} {incr i} {
211    set gx [page_get $::p1 $i]
212    page_write $gx "Page-$i"
213    page_unref $gx
214  }
215  pager_commit $::p1
216} {}
217for {set i 2} {$i<=20} {incr i} {
218  do_test pager-3.6.[expr {$i-1}] [subst {
219    set gx \[page_get $::p1 $i\]
220    set v \[page_read \$gx\]
221    page_unref \$gx
222    set v
223  }] "Page-$i"
224}
225for {set i 1} {$i<=20} {incr i} {
226  regsub -all CNT {
227    set ::g1 [page_get $::p1 CNT]
228    set ::g2 [page_get $::p1 CNT]
229    set ::vx [page_read $::g2]
230    expr {$::g1==$::g2}
231  } $i body;
232  do_test pager-3.7.$i.1 $body {1}
233  regsub -all CNT {
234    page_unref $::g2
235    set vy [page_read $::g1]
236    expr {$vy==$::vx}
237  } $i body;
238  do_test pager-3.7.$i.2 $body {1}
239  regsub -all CNT {
240    page_unref $::g1
241    set gx [page_get $::p1 CNT]
242    set vy [page_read $gx]
243    page_unref $gx
244    expr {$vy==$::vx}
245  } $i body;
246  do_test pager-3.7.$i.3 $body {1}
247}
248do_test pager-3.99 {
249  pager_close $::p1
250} {}
251
252# tests of the checkpoint mechanism and api
253#
254do_test pager-4.0 {
255  set v [catch {
256    file delete -force ptf1.db
257    set ::p1 [pager_open ptf1.db 15]
258  } msg]
259  if {$v} {lappend v $msg}
260  set v
261} {0}
262do_test pager-4.1 {
263  set g1 [page_get $::p1 1]
264  page_write $g1 "Page-1 v0"
265  for {set i 2} {$i<=20} {incr i} {
266    set gx [page_get $::p1 $i]
267    page_write $gx "Page-$i v0"
268    page_unref $gx
269  }
270  pager_commit $::p1
271} {}
272for {set i 1} {$i<=20} {incr i} {
273  do_test pager-4.2.$i {
274    set gx [page_get $p1 $i]
275    set v [page_read $gx]
276    page_unref $gx
277    set v
278  } "Page-$i v0"
279}
280do_test pager-4.3 {
281  lrange [pager_stats $::p1] 0 1
282} {ref 1}
283do_test pager-4.4 {
284  lrange [pager_stats $::p1] 8 9
285} {state 1}
286
287for {set i 1} {$i<20} {incr i} {
288  do_test pager-4.5.$i.0 {
289    set res {}
290    for {set j 2} {$j<=20} {incr j} {
291      set gx [page_get $p1 $j]
292      set value [page_read $gx]
293      page_unref $gx
294      set shouldbe "Page-$j v[expr {$i-1}]"
295      if {$value!=$shouldbe} {
296        lappend res $value $shouldbe
297      }
298    }
299    set res
300  } {}
301  do_test pager-4.5.$i.1 {
302    page_write $g1 "Page-1 v$i"
303    lrange [pager_stats $p1] 8 9
304  } {state 2}
305  do_test pager-4.5.$i.2 {
306    for {set j 2} {$j<=20} {incr j} {
307      set gx [page_get $p1 $j]
308      page_write $gx "Page-$j v$i"
309      page_unref $gx
310      if {$j==$i} {
311        pager_ckpt_begin $p1
312      }
313    }
314  } {}
315  do_test pager-4.5.$i.3 {
316    set res {}
317    for {set j 2} {$j<=20} {incr j} {
318      set gx [page_get $p1 $j]
319      set value [page_read $gx]
320      page_unref $gx
321      set shouldbe "Page-$j v$i"
322      if {$value!=$shouldbe} {
323        lappend res $value $shouldbe
324      }
325    }
326    set res
327  } {}
328  do_test pager-4.5.$i.4 {
329    pager_rollback $p1
330    set res {}
331    for {set j 2} {$j<=20} {incr j} {
332      set gx [page_get $p1 $j]
333      set value [page_read $gx]
334      page_unref $gx
335      set shouldbe "Page-$j v[expr {$i-1}]"
336      if {$value!=$shouldbe} {
337        lappend res $value $shouldbe
338      }
339    }
340    set res
341  } {}
342  do_test pager-4.5.$i.5 {
343    page_write $g1 "Page-1 v$i"
344    lrange [pager_stats $p1] 8 9
345  } {state 2}
346  do_test pager-4.5.$i.6 {
347    for {set j 2} {$j<=20} {incr j} {
348      set gx [page_get $p1 $j]
349      page_write $gx "Page-$j v$i"
350      page_unref $gx
351      if {$j==$i} {
352        pager_ckpt_begin $p1
353      }
354    }
355  } {}
356  do_test pager-4.5.$i.7 {
357    pager_ckpt_rollback $p1
358    for {set j 2} {$j<=20} {incr j} {
359      set gx [page_get $p1 $j]
360      set value [page_read $gx]
361      page_unref $gx
362      if {$j<=$i || $i==1} {
363        set shouldbe "Page-$j v$i"
364      } else {
365        set shouldbe "Page-$j v[expr {$i-1}]"
366      }
367      if {$value!=$shouldbe} {
368        lappend res $value $shouldbe
369      }
370    }
371    set res
372  } {}
373  do_test pager-4.5.$i.8 {
374    for {set j 2} {$j<=20} {incr j} {
375      set gx [page_get $p1 $j]
376      page_write $gx "Page-$j v$i"
377      page_unref $gx
378      if {$j==$i} {
379        pager_ckpt_begin $p1
380      }
381    }
382  } {}
383  do_test pager-4.5.$i.9 {
384    pager_ckpt_commit $p1
385    for {set j 2} {$j<=20} {incr j} {
386      set gx [page_get $p1 $j]
387      set value [page_read $gx]
388      page_unref $gx
389      set shouldbe "Page-$j v$i"
390      if {$value!=$shouldbe} {
391        lappend res $value $shouldbe
392      }
393    }
394    set res
395  } {}
396  do_test pager-4.5.$i.10 {
397    pager_commit $p1
398    lrange [pager_stats $p1] 8 9
399  } {state 1}
400}
401
402do_test pager-4.99 {
403  pager_close $::p1
404} {}
405
406
407
408  file delete -force ptf1.db
409
410} ;# end if( not mem: and has pager_open command );
411
412# Ticket #615: an assertion fault inside the pager.  It is a benign
413# fault, but we might as well test for it.
414#
415do_test pager-5.1 {
416  sqlite db test.db
417  execsql {
418    BEGIN;
419    CREATE TABLE t1(x);
420    PRAGMA synchronous=off;
421    COMMIT;
422  }
423} {}
424
425
426finish_test
427