First public contribution.
3 # Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
5 # The author disclaims copyright to this source code. In place of
6 # a legal notice, here is a blessing:
8 # May you do good and not evil.
9 # May you find forgiveness for yourself and forgive others.
10 # May you share freely, never taking more than you give.
12 #***********************************************************************
13 # This file implements regression tests for SQLite library. The
14 # focus of this script is page cache subsystem.
16 # $Id: pager.test,v 1.31 2008/08/20 14:49:25 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 if {[info commands pager_open]!=""} {
25 # Basic sanity check. Open and close a pager.
28 catch {file delete -force ptf1.db}
29 catch {file delete -force ptf1.db-journal}
31 set ::p1 [pager_open ptf1.db 10]
36 } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
42 } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
47 # Try to write a few pages.
51 set ::p1 [pager_open ptf1.db 10]
56 # set ::g1 [page_get $::p1 0]
61 set ::gx [page_lookup $::p1 1]
65 } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
68 set ::g1 [page_get $::p1 1]
70 if {$v} {lappend v $msg}
75 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
77 set ::gx [page_lookup $::p1 1]
83 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
89 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
92 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
98 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
109 # Update 24/03/2007: Even though the ref-count has dropped to zero, the
110 # pager-cache still contains some pages. Previously, it was always true
111 # that if there were no references to a pager it was empty.
114 } {ref 0 page 1 max 10 size -1 state 0 err 0 hit 0 miss 1 ovfl 0}
116 set ::g1 [page_get $::p1 1]
124 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 1 miss 1 ovfl 0}
127 page_write $::g1 "Page-One"
133 } {ref 1 page 1 max 10 size 1 state 2 err 0 hit 1 miss 1 ovfl 0}
145 } {ref 1 page 1 max 10 size -1 state 1 err 0 hit 2 miss 1 ovfl 0}
147 pager_pagecount $::p1
151 } {ref 1 page 1 max 10 size 1 state 1 err 0 hit 2 miss 1 ovfl 0}
157 } {ref 0 page 1 max 10 size -1 state 0 err 0 hit 2 miss 1 ovfl 0}
162 if {$v} {lappend v $::g1}
170 page_write $::g1 {page-one}
185 set ::g1 [page_get $::p1 1]
195 set ::p1 [pager_open ptf1.db 15]
197 if {$v} {lappend v $msg}
201 pager_pagecount $::p1
205 set ::g(1) [page_get $::p1 1]
207 if {$v} {lappend v $msg}
214 for {set i 2} {$i<=20} {incr i} {
215 set gx [page_get $::p1 $i]
216 page_write $gx "Page-$i"
222 for {set i 2} {$i<=20} {incr i} {
223 do_test pager-3.6.[expr {$i-1}] [subst {
224 set gx \[page_get $::p1 $i\]
225 set v \[page_read \$gx\]
230 for {set i 1} {$i<=20} {incr i} {
232 set ::g1 [page_get $::p1 CNT]
233 set ::g2 [page_get $::p1 CNT]
234 set ::vx [page_read $::g2]
237 do_test pager-3.7.$i.1 $body {1}
240 set vy [page_read $::g1]
243 do_test pager-3.7.$i.2 $body {1}
246 set gx [page_get $::p1 CNT]
247 set vy [page_read $gx]
251 do_test pager-3.7.$i.3 $body {1}
257 # tests of the checkpoint mechanism and api
261 file delete -force ptf1.db
262 set ::p1 [pager_open ptf1.db 15]
264 if {$v} {lappend v $msg}
268 set g1 [page_get $::p1 1]
269 page_write $g1 "Page-1 v0"
270 for {set i 2} {$i<=20} {incr i} {
271 set gx [page_get $::p1 $i]
272 page_write $gx "Page-$i v0"
277 for {set i 1} {$i<=20} {incr i} {
278 do_test pager-4.2.$i {
279 set gx [page_get $p1 $i]
280 set v [page_read $gx]
286 lrange [pager_stats $::p1] 0 1
289 lrange [pager_stats $::p1] 8 9
292 for {set i 1} {$i<20} {incr i} {
293 do_test pager-4.5.$i.0 {
295 for {set j 2} {$j<=20} {incr j} {
296 set gx [page_get $p1 $j]
297 set value [page_read $gx]
299 set shouldbe "Page-$j v[expr {$i-1}]"
300 if {$value!=$shouldbe} {
301 lappend res $value $shouldbe
306 do_test pager-4.5.$i.1 {
307 page_write $g1 "Page-1 v$i"
308 lrange [pager_stats $p1] 8 9
310 do_test pager-4.5.$i.2 {
311 for {set j 2} {$j<=20} {incr j} {
312 set gx [page_get $p1 $j]
313 page_write $gx "Page-$j v$i"
320 do_test pager-4.5.$i.3 {
322 for {set j 2} {$j<=20} {incr j} {
323 set gx [page_get $p1 $j]
324 set value [page_read $gx]
326 set shouldbe "Page-$j v$i"
327 if {$value!=$shouldbe} {
328 lappend res $value $shouldbe
333 do_test pager-4.5.$i.4 {
336 for {set j 2} {$j<=20} {incr j} {
337 set gx [page_get $p1 $j]
338 set value [page_read $gx]
340 set shouldbe "Page-$j v[expr {$i-1}]"
341 if {$value!=$shouldbe} {
342 lappend res $value $shouldbe
347 do_test pager-4.5.$i.5 {
348 page_write $g1 "Page-1 v$i"
349 lrange [pager_stats $p1] 8 9
351 do_test pager-4.5.$i.6 {
352 for {set j 2} {$j<=20} {incr j} {
353 set gx [page_get $p1 $j]
354 page_write $gx "Page-$j v$i"
361 do_test pager-4.5.$i.7 {
362 pager_stmt_rollback $p1
363 for {set j 2} {$j<=20} {incr j} {
364 set gx [page_get $p1 $j]
365 set value [page_read $gx]
367 if {$j<=$i || $i==1} {
368 set shouldbe "Page-$j v$i"
370 set shouldbe "Page-$j v[expr {$i-1}]"
372 if {$value!=$shouldbe} {
373 lappend res $value $shouldbe
378 do_test pager-4.5.$i.8 {
379 for {set j 2} {$j<=20} {incr j} {
380 set gx [page_get $p1 $j]
381 page_write $gx "Page-$j v$i"
388 do_test pager-4.5.$i.9 {
389 pager_stmt_commit $p1
390 for {set j 2} {$j<=20} {incr j} {
391 set gx [page_get $p1 $j]
392 set value [page_read $gx]
394 set shouldbe "Page-$j v$i"
395 if {$value!=$shouldbe} {
396 lappend res $value $shouldbe
401 do_test pager-4.5.$i.10 {
403 lrange [pager_stats $p1] 8 9
407 # Test that nothing bad happens when sqlite3pager_set_cachesize() is
408 # called with a negative argument.
409 do_test pager-4.6.1 {
410 pager_close [pager_open ptf2.db -15]
413 # Test truncate on an in-memory database is Ok.
415 do_test pager-4.6.2 {
416 set ::p2 [pager_open :memory: 10]
417 pager_truncate $::p2 5
419 do_test pager-4.6.3 {
420 for {set i 1} {$i<5} {incr i} {
421 set p [page_get $::p2 $i]
422 page_write $p "Page $i"
426 # pager_truncate $::p2 3
428 do_test pager-4.6.4 {
440 file delete -force ptf1.db
442 } ;# end if( not mem: and has pager_open command );
445 # Ticket #615: an assertion fault inside the pager. It is a benign
446 # fault, but we might as well test for it.
453 PRAGMA synchronous=off;
459 # The following tests cover rolling back hot journal files.
460 # They can't be run on windows because the windows version of
461 # SQLite holds a mandatory exclusive lock on journal files it has open.
463 if {$tcl_platform(platform)!="windows" && $tcl_platform(platform)!="symbian"} {
465 file delete -force test2.db
466 file delete -force test2.db-journal
469 PRAGMA synchronous = 0;
470 CREATE TABLE abc(a, b, c);
471 INSERT INTO abc VALUES(1, 2, randstr(200,200));
472 INSERT INTO abc VALUES(1, 2, randstr(200,200));
473 INSERT INTO abc VALUES(1, 2, randstr(200,200));
474 INSERT INTO abc VALUES(1, 2, randstr(200,200));
475 INSERT INTO abc VALUES(1, 2, randstr(200,200));
476 INSERT INTO abc VALUES(1, 2, randstr(200,200));
477 INSERT INTO abc VALUES(1, 2, randstr(200,200));
478 INSERT INTO abc VALUES(1, 2, randstr(200,200));
479 INSERT INTO abc VALUES(1, 2, randstr(200,200));
481 UPDATE abc SET c = randstr(200,200);
483 copy_file test2.db test.db
484 copy_file test2.db-journal test.db-journal
486 set f [open test.db-journal a]
487 fconfigure $f -encoding binary
488 seek $f [expr [file size test.db-journal] - 1032] start
489 puts -nonewline $f "\00\00\00\00"
494 SELECT sql FROM sqlite_master
496 } {{CREATE TABLE abc(a, b, c)}}
499 copy_file test2.db test.db
500 copy_file test2.db-journal test.db-journal
502 set f [open test.db-journal a]
503 fconfigure $f -encoding binary
504 seek $f [expr [file size test.db-journal] - 1032] start
505 puts -nonewline $f "\00\00\00\FF"
510 SELECT sql FROM sqlite_master
512 } {{CREATE TABLE abc(a, b, c)}}
515 copy_file test2.db test.db
516 copy_file test2.db-journal test.db-journal
518 set f [open test.db-journal a]
519 fconfigure $f -encoding binary
520 seek $f [expr [file size test.db-journal] - 4] start
521 puts -nonewline $f "\00\00\00\00"
526 SELECT sql FROM sqlite_master
528 } {{CREATE TABLE abc(a, b, c)}}
530 do_test pager-6.4.1 {
533 SELECT sql FROM sqlite_master;
535 copy_file test2.db-journal test.db-journal;
539 SELECT sql FROM sqlite_master;
541 } {1 {database is locked}}
542 do_test pager-6.4.2 {
543 file delete -force test.db-journal
545 SELECT sql FROM sqlite_master;
547 } {0 {{CREATE TABLE abc(a, b, c)}}}
548 do_test pager-6.4.3 {
556 copy_file test2.db test.db
557 copy_file test2.db-journal test.db-journal
559 set f [open test.db-journal a]
560 fconfigure $f -encoding binary
561 puts -nonewline $f "hello"
562 puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04"
563 puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
568 SELECT sql FROM sqlite_master
570 } {{CREATE TABLE abc(a, b, c)}}