Blame src/test9.c

Packit 87b942
/*
Packit 87b942
** 2007 March 29
Packit 87b942
**
Packit 87b942
** The author disclaims copyright to this source code.  In place of
Packit 87b942
** a legal notice, here is a blessing:
Packit 87b942
**
Packit 87b942
**    May you do good and not evil.
Packit 87b942
**    May you find forgiveness for yourself and forgive others.
Packit 87b942
**    May you share freely, never taking more than you give.
Packit 87b942
**
Packit 87b942
*************************************************************************
Packit 87b942
**
Packit 87b942
** This file contains obscure tests of the C-interface required
Packit 87b942
** for completeness. Test code is written in C for these cases
Packit 87b942
** as there is not much point in binding to Tcl.
Packit 87b942
*/
Packit 87b942
#include "sqliteInt.h"
Packit 87b942
#if defined(INCLUDE_SQLITE_TCL_H)
Packit 87b942
#  include "sqlite_tcl.h"
Packit 87b942
#else
Packit 87b942
#  include "tcl.h"
Packit 87b942
#endif
Packit 87b942
#include <stdlib.h>
Packit 87b942
#include <string.h>
Packit 87b942
Packit 87b942
/*
Packit 87b942
** c_collation_test
Packit 87b942
*/
Packit 87b942
static int SQLITE_TCLAPI c_collation_test(
Packit 87b942
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Packit 87b942
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
Packit 87b942
  int objc,              /* Number of arguments */
Packit 87b942
  Tcl_Obj *CONST objv[]  /* Command arguments */
Packit 87b942
){
Packit 87b942
  const char *zErrFunction = "N/A";
Packit 87b942
  sqlite3 *db;
Packit 87b942
Packit 87b942
  int rc;
Packit 87b942
  if( objc!=1 ){
Packit 87b942
    Tcl_WrongNumArgs(interp, 1, objv, "");
Packit 87b942
    return TCL_ERROR;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* Open a database. */
Packit 87b942
  rc = sqlite3_open(":memory:", &db);
Packit 87b942
  if( rc!=SQLITE_OK ){
Packit 87b942
    zErrFunction = "sqlite3_open";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  rc = sqlite3_create_collation(db, "collate", 456, 0, 0);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    sqlite3_close(db);
Packit 87b942
    zErrFunction = "sqlite3_create_collation";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  sqlite3_close(db);
Packit 87b942
  return TCL_OK;
Packit 87b942
Packit 87b942
error_out:
Packit 87b942
  Tcl_ResetResult(interp);
Packit 87b942
  Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
Packit 87b942
  return TCL_ERROR;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** c_realloc_test
Packit 87b942
*/
Packit 87b942
static int SQLITE_TCLAPI c_realloc_test(
Packit 87b942
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Packit 87b942
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
Packit 87b942
  int objc,              /* Number of arguments */
Packit 87b942
  Tcl_Obj *CONST objv[]  /* Command arguments */
Packit 87b942
){
Packit 87b942
  void *p;
Packit 87b942
  const char *zErrFunction = "N/A";
Packit 87b942
Packit 87b942
  if( objc!=1 ){
Packit 87b942
    Tcl_WrongNumArgs(interp, 1, objv, "");
Packit 87b942
    return TCL_ERROR;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  p = sqlite3_malloc(5);
Packit 87b942
  if( !p ){
Packit 87b942
    zErrFunction = "sqlite3_malloc";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* Test that realloc()ing a block of memory to a negative size is
Packit 87b942
  ** the same as free()ing that memory.
Packit 87b942
  */
Packit 87b942
  p = sqlite3_realloc(p, -1);
Packit 87b942
  if( p ){
Packit 87b942
    zErrFunction = "sqlite3_realloc";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  return TCL_OK;
Packit 87b942
Packit 87b942
error_out:
Packit 87b942
  Tcl_ResetResult(interp);
Packit 87b942
  Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
Packit 87b942
  return TCL_ERROR;
Packit 87b942
}
Packit 87b942
Packit 87b942
Packit 87b942
/*
Packit 87b942
** c_misuse_test
Packit 87b942
*/
Packit 87b942
static int SQLITE_TCLAPI c_misuse_test(
Packit 87b942
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Packit 87b942
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
Packit 87b942
  int objc,              /* Number of arguments */
Packit 87b942
  Tcl_Obj *CONST objv[]  /* Command arguments */
Packit 87b942
){
Packit 87b942
  const char *zErrFunction = "N/A";
Packit 87b942
  sqlite3 *db = 0;
Packit 87b942
  sqlite3_stmt *pStmt;
Packit 87b942
  int rc;
Packit 87b942
Packit 87b942
  if( objc!=1 ){
Packit 87b942
    Tcl_WrongNumArgs(interp, 1, objv, "");
Packit 87b942
    return TCL_ERROR;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  /* Open a database. Then close it again. We need to do this so that
Packit 87b942
  ** we have a "closed database handle" to pass to various API functions.
Packit 87b942
  */
Packit 87b942
  rc = sqlite3_open(":memory:", &db);
Packit 87b942
  if( rc!=SQLITE_OK ){
Packit 87b942
    zErrFunction = "sqlite3_open";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
  sqlite3_close(db);
Packit 87b942
Packit 87b942
Packit 87b942
  rc = sqlite3_errcode(db);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    zErrFunction = "sqlite3_errcode";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
Packit 87b942
  pStmt = (sqlite3_stmt*)1234;
Packit 87b942
  rc = sqlite3_prepare(db, 0, 0, &pStmt, 0);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    zErrFunction = "sqlite3_prepare";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
  assert( pStmt==0 ); /* Verify that pStmt is zeroed even on a MISUSE error */
Packit 87b942
Packit 87b942
  pStmt = (sqlite3_stmt*)1234;
Packit 87b942
  rc = sqlite3_prepare_v2(db, 0, 0, &pStmt, 0);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    zErrFunction = "sqlite3_prepare_v2";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
  assert( pStmt==0 );
Packit 87b942
Packit 87b942
#ifndef SQLITE_OMIT_UTF16
Packit 87b942
  pStmt = (sqlite3_stmt*)1234;
Packit 87b942
  rc = sqlite3_prepare16(db, 0, 0, &pStmt, 0);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    zErrFunction = "sqlite3_prepare16";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
  assert( pStmt==0 );
Packit 87b942
  pStmt = (sqlite3_stmt*)1234;
Packit 87b942
  rc = sqlite3_prepare16_v2(db, 0, 0, &pStmt, 0);
Packit 87b942
  if( rc!=SQLITE_MISUSE ){
Packit 87b942
    zErrFunction = "sqlite3_prepare16_v2";
Packit 87b942
    goto error_out;
Packit 87b942
  }
Packit 87b942
  assert( pStmt==0 );
Packit 87b942
#endif
Packit 87b942
Packit 87b942
  return TCL_OK;
Packit 87b942
Packit 87b942
error_out:
Packit 87b942
  Tcl_ResetResult(interp);
Packit 87b942
  Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
Packit 87b942
  return TCL_ERROR;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Register commands with the TCL interpreter.
Packit 87b942
*/
Packit 87b942
int Sqlitetest9_Init(Tcl_Interp *interp){
Packit 87b942
  static struct {
Packit 87b942
     char *zName;
Packit 87b942
     Tcl_ObjCmdProc *xProc;
Packit 87b942
     void *clientData;
Packit 87b942
  } aObjCmd[] = {
Packit 87b942
     { "c_misuse_test",    c_misuse_test, 0 },
Packit 87b942
     { "c_realloc_test",   c_realloc_test, 0 },
Packit 87b942
     { "c_collation_test", c_collation_test, 0 },
Packit 87b942
  };
Packit 87b942
  int i;
Packit 87b942
  for(i=0; i
Packit 87b942
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 
Packit 87b942
        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
Packit 87b942
  }
Packit 87b942
  return TCL_OK;
Packit 87b942
}