1 /* 2 ** 2007 March 29 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file contains obscure tests of the C-interface required 14 ** for completeness. Test code is written in C for these cases 15 ** as there is not much point in binding to Tcl. 16 ** 17 ** $Id: test9.c,v 1.1 2007/03/29 12:24:17 danielk1977 Exp $ 18 */ 19 #include "sqliteInt.h" 20 #include "tcl.h" 21 #include "os.h" 22 #include <stdlib.h> 23 #include <string.h> 24 25 /* 26 ** c_collation_test 27 */ 28 static int c_collation_test( 29 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 30 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 31 int objc, /* Number of arguments */ 32 Tcl_Obj *CONST objv[] /* Command arguments */ 33 ){ 34 void *p; 35 const char *zErrFunction = "N/A"; 36 sqlite3 *db; 37 38 int rc; 39 if( objc!=1 ){ 40 Tcl_WrongNumArgs(interp, 1, objv, ""); 41 return TCL_ERROR; 42 } 43 44 /* Open a database. */ 45 rc = sqlite3_open(":memory:", &db); 46 if( rc!=SQLITE_OK ){ 47 zErrFunction = "sqlite3_open"; 48 goto error_out; 49 } 50 51 rc = sqlite3_create_collation(db, "collate", 456, 0, 0); 52 if( rc!=SQLITE_ERROR ){ 53 sqlite3_close(db); 54 zErrFunction = "sqlite3_create_collation"; 55 goto error_out; 56 } 57 58 sqlite3_close(db); 59 return TCL_OK; 60 61 error_out: 62 Tcl_ResetResult(interp); 63 Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); 64 return TCL_ERROR; 65 } 66 67 /* 68 ** c_realloc_test 69 */ 70 static int c_realloc_test( 71 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 72 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 73 int objc, /* Number of arguments */ 74 Tcl_Obj *CONST objv[] /* Command arguments */ 75 ){ 76 void *p; 77 const char *zErrFunction = "N/A"; 78 79 sqlite3 *db; 80 int rc; 81 if( objc!=1 ){ 82 Tcl_WrongNumArgs(interp, 1, objv, ""); 83 return TCL_ERROR; 84 } 85 86 p = sqlite3_malloc(5); 87 if( !p ){ 88 zErrFunction = "sqlite3_malloc"; 89 goto error_out; 90 } 91 92 /* Test that realloc()ing a block of memory to a negative size is 93 ** the same as free()ing that memory. 94 */ 95 p = sqlite3_realloc(p, -1); 96 if( p ){ 97 zErrFunction = "sqlite3_realloc"; 98 goto error_out; 99 } 100 101 return TCL_OK; 102 103 error_out: 104 Tcl_ResetResult(interp); 105 Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); 106 return TCL_ERROR; 107 } 108 109 110 /* 111 ** c_misuse_test 112 */ 113 static int c_misuse_test( 114 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ 115 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ 116 int objc, /* Number of arguments */ 117 Tcl_Obj *CONST objv[] /* Command arguments */ 118 ){ 119 const char *zErrFunction = "N/A"; 120 sqlite3 *db = 0; 121 int rc; 122 123 if( objc!=1 ){ 124 Tcl_WrongNumArgs(interp, 1, objv, ""); 125 return TCL_ERROR; 126 } 127 128 /* Open a database. Then close it again. We need to do this so that 129 ** we have a "closed database handle" to pass to various API functions. 130 */ 131 rc = sqlite3_open(":memory:", &db); 132 if( rc!=SQLITE_OK ){ 133 zErrFunction = "sqlite3_open"; 134 goto error_out; 135 } 136 sqlite3_close(db); 137 138 rc = sqlite3_collation_needed16(db, 0, 0); 139 if( rc!=SQLITE_MISUSE ){ 140 zErrFunction = "sqlite3_collation_needed16"; 141 goto error_out; 142 } 143 144 rc = sqlite3_collation_needed(db, 0, 0); 145 if( rc!=SQLITE_MISUSE ){ 146 zErrFunction = "sqlite3_collation_needed"; 147 goto error_out; 148 } 149 150 rc = sqlite3_create_collation(db, 0, 0, 0, 0); 151 if( rc!=SQLITE_MISUSE ){ 152 zErrFunction = "sqlite3_create_collation"; 153 goto error_out; 154 } 155 156 rc = sqlite3_create_function(db, 0, 0, 0, 0, 0, 0, 0); 157 if( rc!=SQLITE_MISUSE ){ 158 zErrFunction = "sqlite3_create_function"; 159 goto error_out; 160 } 161 162 rc = sqlite3_busy_handler(db, 0, 0); 163 if( rc!=SQLITE_MISUSE ){ 164 zErrFunction = "sqlite3_busy_handler"; 165 goto error_out; 166 } 167 168 rc = sqlite3_errcode(db); 169 if( rc!=SQLITE_MISUSE ){ 170 zErrFunction = "sqlite3_busy_handler"; 171 goto error_out; 172 } 173 174 rc = sqlite3_prepare16(db, 0, 0, 0, 0); 175 if( rc!=SQLITE_MISUSE ){ 176 zErrFunction = "sqlite3_prepare16"; 177 goto error_out; 178 } 179 180 return TCL_OK; 181 182 error_out: 183 Tcl_ResetResult(interp); 184 Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); 185 return TCL_ERROR; 186 } 187 188 /* 189 ** Register commands with the TCL interpreter. 190 */ 191 int Sqlitetest9_Init(Tcl_Interp *interp){ 192 static struct { 193 char *zName; 194 Tcl_ObjCmdProc *xProc; 195 void *clientData; 196 } aObjCmd[] = { 197 { "c_misuse_test", c_misuse_test, 0 }, 198 { "c_realloc_test", c_realloc_test, 0 }, 199 { "c_collation_test", c_collation_test, 0 }, 200 }; 201 int i; 202 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 203 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 204 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); 205 } 206 return TCL_OK; 207 } 208 209