modernc.org/cc@v1.0.1/v2/testdata/_sqlite/test/checkfreelist.test (about)

     1  # 2017-10-11
     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 file is testing the checkfreelist extension.
    13  #
    14  
    15  set testdir [file dirname $argv0]
    16  source $testdir/tester.tcl
    17  set testprefix checkfreelist
    18  
    19  ifcapable !vtab||!compound {
    20    finish_test
    21    return
    22  }
    23  
    24  if {[file exists ../checkfreelist.so]==0} {
    25    finish_test
    26    return
    27  }
    28  
    29  do_execsql_test 1.0 {
    30    CREATE TABLE t1(a, b);
    31  }
    32  
    33  db enable_load_extension 1
    34  do_execsql_test 1.1 {
    35    SELECT load_extension('../checkfreelist.so');
    36  } {{}}
    37  
    38  do_execsql_test 1.2 { SELECT checkfreelist('main') } {ok}
    39  do_execsql_test 1.3 {
    40    WITH s(i) AS (
    41      SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
    42    )
    43    INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
    44    DELETE FROM t1 WHERE rowid%3;
    45    PRAGMA freelist_count;
    46  } {6726}
    47  
    48  do_execsql_test 1.4 { SELECT checkfreelist('main') } {ok}
    49  do_execsql_test 1.5 {
    50    WITH freelist_trunk(i, d, n) AS (
    51      SELECT 1, NULL, sqlite_readint32(data, 32) FROM sqlite_dbpage WHERE pgno=1
    52        UNION ALL
    53      SELECT n, data, sqlite_readint32(data) 
    54      FROM freelist_trunk, sqlite_dbpage WHERE pgno=n
    55    )
    56    SELECT i FROM freelist_trunk WHERE i!=1;
    57  } {
    58    10010 9716 9344 8970 8596 8223 7848 7475 7103 6728 6355 5983 5609 5235
    59    4861 4488 4113 3741 3368 2993 2620 2248 1873 1500 1126 753 378 5
    60  }
    61  
    62  do_execsql_test 1.6 { SELECT checkfreelist('main') } {ok}
    63  
    64  proc set_int {blob idx newval} {
    65    binary scan $blob I* ints
    66    lset ints $idx $newval
    67    binary format I* $ints
    68  }
    69  db func set_int set_int
    70  
    71  proc get_int {blob idx} {
    72    binary scan $blob I* ints
    73    lindex $ints $idx
    74  }
    75  db func get_int get_int
    76  
    77  do_execsql_test 1.7 {
    78    BEGIN;
    79      UPDATE sqlite_dbpage 
    80        SET data = set_int(data, 1, get_int(data, 1)-1) 
    81        WHERE pgno=4861;
    82      SELECT checkfreelist('main');
    83    ROLLBACK;
    84  } {{free-list count mismatch: actual=6725 header=6726}}
    85  
    86  do_execsql_test 1.8 {
    87    BEGIN;
    88      UPDATE sqlite_dbpage 
    89        SET data = set_int(data, 5, (SELECT * FROM pragma_page_count)+1)
    90        WHERE pgno=4861;
    91      SELECT checkfreelist('main');
    92    ROLLBACK;
    93  } {{leaf page 10093 is out of range (child 3 of trunk page 4861)}}
    94  
    95  do_execsql_test 1.9 {
    96    BEGIN;
    97      UPDATE sqlite_dbpage 
    98        SET data = set_int(data, 5, 0)
    99        WHERE pgno=4861;
   100      SELECT checkfreelist('main');
   101    ROLLBACK;
   102  } {{leaf page 0 is out of range (child 3 of trunk page 4861)}}
   103  
   104  do_execsql_test 1.10 {
   105    BEGIN;
   106      UPDATE sqlite_dbpage 
   107        SET data = set_int(data, get_int(data, 1)+1, 0)
   108        WHERE pgno=5;
   109      SELECT checkfreelist('main');
   110    ROLLBACK;
   111  } {{leaf page 0 is out of range (child 247 of trunk page 5)}}
   112  
   113  do_execsql_test 1.11 {
   114    BEGIN;
   115      UPDATE sqlite_dbpage 
   116        SET data = set_int(data, 1, 249)
   117        WHERE pgno=5;
   118      SELECT checkfreelist('main');
   119    ROLLBACK;
   120  } {{leaf count out of range (249) on trunk page 5}}
   121  
   122  finish_test
   123