os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/pager2.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 # 2001 September 15
     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.  The
    12 # focus of this script is page cache subsystem.
    13 #
    14 # $Id: pager2.test,v 1.7 2008/08/20 14:49:25 danielk1977 Exp $
    15 
    16 
    17 set testdir [file dirname $argv0]
    18 source $testdir/tester.tcl
    19 
    20 # Don't run this test file if the pager test interface [pager_open] is not
    21 # available, or the library was compiled without in-memory database support.
    22 #
    23 if {[info commands pager_open]!=""} {
    24 ifcapable memorydb {
    25 db close
    26 
    27 # Basic sanity check.  Open and close a pager.
    28 #
    29 do_test pager2-1.0 {
    30   set v [catch {
    31     set ::p1 [pager_open :memory: 10]
    32   } msg]
    33 } {0}
    34 do_test pager2-1.1 {
    35   pager_stats $::p1
    36 } {ref 0 page 0 max 10 size 0 state 0 err 0 hit 0 miss 0 ovfl 0}
    37 do_test pager2-1.2 {
    38   pager_pagecount $::p1
    39 } {0}
    40 do_test pager2-1.3 {
    41   pager_stats $::p1
    42 } {ref 0 page 0 max 10 size 0 state 0 err 0 hit 0 miss 0 ovfl 0}
    43 do_test pager2-1.4 {
    44   pager_close $::p1
    45 } {}
    46 
    47 # Try to write a few pages.
    48 #
    49 do_test pager2-2.1 {
    50   set v [catch {
    51     set ::p1 [pager_open :memory: 10]
    52   } msg]
    53 } {0}
    54 #do_test pager2-2.2 {
    55 #  set v [catch {
    56 #    set ::g1 [page_get $::p1 0]
    57 #  } msg]
    58 #  lappend v $msg
    59 #} {1 SQLITE_ERROR}
    60 do_test pager2-2.3.1 {
    61   set ::gx [page_lookup $::p1 1]
    62 } {}
    63 do_test pager2-2.3.2 {
    64   pager_stats $::p1
    65 } {ref 0 page 0 max 10 size 0 state 0 err 0 hit 0 miss 0 ovfl 0}
    66 do_test pager2-2.3.3 {
    67   set v [catch {
    68     set ::g1 [page_get $::p1 1]
    69   } msg]
    70   if {$v} {lappend v $msg}
    71   set v
    72 } {0}
    73 do_test pager2-2.3.3 {
    74   pager_stats $::p1
    75 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
    76 do_test pager2-2.3.4 {
    77   set ::gx [page_lookup $::p1 1]
    78   page_unref $::gx
    79   expr {$::gx!=""}
    80 } {1}
    81 do_test pager2-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}
    84 do_test pager2-2.3.6 {
    85   expr {$::g1==$::gx}
    86 } {1}
    87 do_test pager2-2.3.7 {
    88   pager_stats $::p1
    89 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
    90 do_test pager2-2.4 {
    91   pager_stats $::p1
    92 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
    93 do_test pager2-2.5 {
    94   pager_pagecount $::p1
    95 } {0}
    96 do_test pager2-2.6 {
    97   pager_stats $::p1
    98 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
    99 do_test pager2-2.7 {
   100   page_number $::g1
   101 } {1}
   102 do_test pager2-2.8 {
   103   page_read $::g1
   104 } {}
   105 do_test pager2-2.9 {
   106   page_unref $::g1
   107 } {}
   108 do_test pager2-2.10 {
   109   pager_stats $::p1
   110 } {ref 0 page 1 max 10 size 0 state 0 err 0 hit 0 miss 1 ovfl 0}
   111 do_test pager2-2.11 {
   112   set ::g1 [page_get $::p1 1]
   113   expr {$::g1!=0}
   114 } {1}
   115 do_test pager2-2.12 {
   116   page_number $::g1
   117 } {1}
   118 do_test pager2-2.13 {
   119   pager_stats $::p1
   120 } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 1 miss 1 ovfl 0}
   121 do_test pager2-2.14 {
   122   set v [catch {
   123     page_write $::g1 "Page-One"
   124   } msg]
   125   lappend v $msg
   126 } {0 {}}
   127 do_test pager2-2.15 {
   128   pager_stats $::p1
   129 } {ref 1 page 1 max 10 size 1 state 4 err 0 hit 1 miss 1 ovfl 0}
   130 do_test pager2-2.16 {
   131   page_read $::g1
   132 } {Page-One}
   133 do_test pager2-2.17 {
   134   set v [catch {
   135     pager_commit $::p1
   136   } msg]
   137   lappend v $msg
   138 } {0 {}}
   139 do_test pager2-2.20 {
   140   pager_stats $::p1
   141 } {ref 1 page 1 max 10 size 1 state 1 err 0 hit 1 miss 1 ovfl 0}
   142 do_test pager2-2.19 {
   143   pager_pagecount $::p1
   144 } {1}
   145 do_test pager2-2.21 {
   146   pager_stats $::p1
   147 } {ref 1 page 1 max 10 size 1 state 1 err 0 hit 1 miss 1 ovfl 0}
   148 do_test pager2-2.22 {
   149   page_unref $::g1
   150 } {}
   151 do_test pager2-2.23 {
   152   pager_stats $::p1
   153 } {ref 0 page 1 max 10 size 1 state 0 err 0 hit 1 miss 1 ovfl 0}
   154 do_test pager2-2.24 {
   155   set v [catch {
   156     page_get $::p1 1
   157   } ::g1]
   158   if {$v} {lappend v $::g1}
   159   set v
   160 } {0}
   161 do_test pager2-2.25 {
   162   page_read $::g1
   163 } {Page-One}
   164 do_test pager2-2.26 {
   165   set v [catch {
   166     page_write $::g1 {page-one}
   167   } msg]
   168   lappend v $msg
   169 } {0 {}}
   170 do_test pager2-2.27 {
   171   page_read $::g1
   172 } {page-one}
   173 do_test pager2-2.28 {
   174   set v [catch {
   175     pager_rollback $::p1
   176   } msg]
   177   lappend v $msg
   178 } {0 {}}
   179 do_test pager2-2.29 {
   180   page_unref $::g1
   181   set ::g1 [page_get $::p1 1]
   182   page_read $::g1
   183 } {Page-One}
   184 do_test pager2-2.99 {
   185   page_unref $::g1
   186 } {}
   187 
   188 #do_test pager2-3.1 {
   189 #  set v [catch {
   190 #    set ::p1 [pager_open :memory: 15]
   191 #  } msg]
   192 #  if {$v} {lappend v $msg}
   193 #  set v
   194 #} {0}
   195 do_test pager2-3.2 {
   196   pager_pagecount $::p1
   197 } {1}
   198 do_test pager2-3.3 {
   199   set v [catch {
   200     set ::g(1) [page_get $::p1 1]
   201   } msg]
   202   if {$v} {lappend v $msg}
   203   set v
   204 } {0}
   205 do_test pager2-3.4 {
   206   page_read $::g(1)
   207 } {Page-One}
   208 do_test pager2-3.5 {
   209   for {set i 2} {$i<=20} {incr i} {
   210     set gx [page_get $::p1 $i]
   211     page_write $gx "Page-$i"
   212     page_unref $gx
   213   }
   214   pager_commit $::p1
   215   page_unref $::g(1)
   216 } {}
   217 for {set i 2} {$i<=20} {incr i} {
   218   do_test pager2-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 }
   225 for {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 pager2-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 pager2-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 pager2-3.7.$i.3 $body {1}
   247 }
   248 do_test pager2-3.99 {
   249   pager_close $::p1
   250 } {}
   251 
   252 # tests of the checkpoint mechanism and api
   253 #
   254 do_test pager2-4.0 {
   255   set v [catch {
   256     set ::p1 [pager_open :memory: 15]
   257   } msg]
   258   if {$v} {lappend v $msg}
   259   set v
   260 } {0}
   261 do_test pager2-4.1 {
   262   set g1 [page_get $::p1 1]
   263   page_write $g1 "Page-1 v0"
   264   for {set i 2} {$i<=20} {incr i} {
   265     set gx [page_get $::p1 $i]
   266     page_write $gx "Page-$i v0"
   267     page_unref $gx
   268   }
   269   pager_commit $::p1
   270 } {}
   271 for {set i 1} {$i<=20} {incr i} {
   272   do_test pager2-4.2.$i {
   273     set gx [page_get $p1 $i]
   274     set v [page_read $gx]
   275     page_unref $gx
   276     set v
   277   } "Page-$i v0"
   278 }
   279 do_test pager2-4.3 {
   280   lrange [pager_stats $::p1] 0 1
   281 } {ref 1}
   282 do_test pager2-4.4 {
   283   lrange [pager_stats $::p1] 8 9
   284 } {state 1}
   285 
   286 for {set i 1} {$i<20} {incr i} {
   287   do_test pager2-4.5.$i.0 {
   288     set res {}
   289     for {set j 2} {$j<=20} {incr j} {
   290       set gx [page_get $p1 $j]
   291       set value [page_read $gx]
   292       page_unref $gx
   293       set shouldbe "Page-$j v[expr {$i-1}]"
   294       if {$value!=$shouldbe} {
   295         lappend res $value $shouldbe
   296       }
   297     }
   298     set res
   299   } {}
   300   do_test pager2-4.5.$i.1 {
   301     page_write $g1 "Page-1 v$i"
   302     lrange [pager_stats $p1] 8 9
   303   } {state 4}
   304   do_test pager2-4.5.$i.2 {
   305     for {set j 2} {$j<=20} {incr j} {
   306       set gx [page_get $p1 $j]
   307       page_write $gx "Page-$j v$i"
   308       page_unref $gx
   309       if {$j==$i} {
   310         pager_stmt_begin $p1
   311       }
   312     }
   313   } {}
   314   do_test pager2-4.5.$i.3 {
   315     set res {}
   316     for {set j 2} {$j<=20} {incr j} {
   317       set gx [page_get $p1 $j]
   318       set value [page_read $gx]
   319       page_unref $gx
   320       set shouldbe "Page-$j v$i"
   321       if {$value!=$shouldbe} {
   322         lappend res $value $shouldbe
   323       }
   324     }
   325     set res
   326   } {}
   327   do_test pager2-4.5.$i.4 {
   328     pager_rollback $p1
   329     set res {}
   330     for {set j 2} {$j<=20} {incr j} {
   331       set gx [page_get $p1 $j]
   332       set value [page_read $gx]
   333       page_unref $gx
   334       set shouldbe "Page-$j v[expr {$i-1}]"
   335       if {$value!=$shouldbe} {
   336         lappend res $value $shouldbe
   337       }
   338     }
   339     set res
   340   } {}
   341 breakpoint
   342   do_test pager2-4.5.$i.5 {
   343     page_write $g1 "Page-1 v$i"
   344     lrange [pager_stats $p1] 8 9
   345   } {state 4}
   346   do_test pager2-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_stmt_begin $p1
   353       }
   354     }
   355   } {}
   356   do_test pager2-4.5.$i.7 {
   357     pager_stmt_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 pager2-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_stmt_begin $p1
   380       }
   381     }
   382   } {}
   383   do_test pager2-4.5.$i.9 {
   384     pager_stmt_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 pager2-4.5.$i.10 {
   397     pager_commit $p1
   398     lrange [pager_stats $p1] 8 9
   399   } {state 1}
   400 }
   401 
   402 do_test pager2-4.99 {
   403   page_unref $::g1
   404   pager_close $::p1
   405 } {}
   406 
   407 } ;# ifcapable inmemory
   408 } ;# end if( has pager_open command );
   409 
   410 
   411 finish_test