# 2013 Jun 15
#
# 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.
#
# This file implements tests for the sqlite4_trace() and sqlite4_profile() 
# APIs. Specifically, it tests that destructor functions are invoked
# correctly.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !trace { finish_test ; return }
set ::testprefix trace3

#-------------------------------------------------------------------------
# The following tests verify that the profile destructor is
# invoked:
#
#   1. When it is overridden, and
#   2. When the database connection is closed.
#
# It would be good to test that if an error occurs within the 
# collation_needed() the destructor is invoked immediately (as the 
# documentation says). However there is currently no way to cause
# sqlite4_collation_needed() to fail. sqlite4_collation_needed() 
# currently *always* returns SQLITE4_OK.
#

foreach {tn cmd} {
  1 sqlite4_profile
  2 sqlite4_trace
} {

  reset_db

  do_test $tn.1 {
    set ::del 0
    $cmd db abc_profile {incr ::del}
    set ::del
  } {0}

  do_test $tn.2 {
    set ::del2 0
    $cmd db def_profile {incr ::del2}
    list $::del $::del2
  } {1 0}

  do_test $tn.3 {
    db close
    list $::del $::del2
  } {1 1}
}

finish_test
