# 2001 November 22
#
# 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
#
# In particular, this file tests a small part of the Delete logic
# for the BTree backend.  When a row is deleted from a table, the
# cursor is suppose to be left pointing at either the previous or
# next entry in that table.  If the cursor is left pointing at the
# next entry, then the next Next operation is ignored.  So the 
# sequence of operations (Delete, Next) should always leave the
# cursor pointing at the first entry past the one that was deleted.
# This test is designed to verify that behavior.
#
# $Id: btree3rb.test,v 1.1 2003/04/20 23:45:23 drh Exp $


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

if {[info commands btree_open]!=""} {

# Open a test database.
#
set b1 [btree_open :memory:]
btree_begin_transaction $::b1

# Insert a few one records
#
set data {abcdefghijklmnopqrstuvwxyz0123456789}
append data $data
append data $data
append data $data
append data $data
for {set k 2} {$k<=20} {incr k} {
  for {set j 1} {$j<=$k} {incr j} {
    set jkey [format %02d $j]
    btree_clear_table $::b1 2
    set ::c1 [btree_cursor $::b1 2 1]
    for {set i 1} {$i<=$k} {incr i} {
      set key [format %02d $i]
      do_test btree3rb-$k.$j.1.$i {
        btree_insert $::c1 $::key $::data
      } {}
      # btree_tree_dump $::b1 2
    }
    do_test btree3rb-$k.$j.2 {
      btree_move_to $::c1 $::jkey
      btree_key $::c1
    } $::jkey
    do_test btree3rb-$k.$j.3 {
      btree_delete $::c1
    } {}
    if {$j<$k} {
      do_test btree3rb-$k.$j.4 {
        btree_next $::c1
        btree_key $::c1
      } [format %02d [expr $j+1]]
    }
    if {$j>1} {
      do_test btree3rb-$k.$j.5 {
        btree_prev $::c1
        btree_key $::c1
      } [format %02d [expr $j-1]]
    }
    btree_close_cursor $::c1
  }
}

btree_rollback $::b1    
#btree_pager_ref_dump $::b1
btree_close $::b1

} ;# end if( not mem: and has pager_open command );

finish_test
