xref: /sqlite-3.40.0/src/test9.c (revision a3f06598)
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.7 2009/04/02 18:32:27 drh Exp $
18 */
19 #include "sqliteInt.h"
20 #include "tcl.h"
21 #include <stdlib.h>
22 #include <string.h>
23 
24 /*
25 ** c_collation_test
26 */
27 static int c_collation_test(
28   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
29   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
30   int objc,              /* Number of arguments */
31   Tcl_Obj *CONST objv[]  /* Command arguments */
32 ){
33   const char *zErrFunction = "N/A";
34   sqlite3 *db;
35 
36   int rc;
37   if( objc!=1 ){
38     Tcl_WrongNumArgs(interp, 1, objv, "");
39     return TCL_ERROR;
40   }
41 
42   /* Open a database. */
43   rc = sqlite3_open(":memory:", &db);
44   if( rc!=SQLITE_OK ){
45     zErrFunction = "sqlite3_open";
46     goto error_out;
47   }
48 
49   rc = sqlite3_create_collation(db, "collate", 456, 0, 0);
50   if( rc!=SQLITE_MISUSE ){
51     sqlite3_close(db);
52     zErrFunction = "sqlite3_create_collation";
53     goto error_out;
54   }
55 
56   sqlite3_close(db);
57   return TCL_OK;
58 
59 error_out:
60   Tcl_ResetResult(interp);
61   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
62   return TCL_ERROR;
63 }
64 
65 /*
66 ** c_realloc_test
67 */
68 static int c_realloc_test(
69   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
70   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
71   int objc,              /* Number of arguments */
72   Tcl_Obj *CONST objv[]  /* Command arguments */
73 ){
74   void *p;
75   const char *zErrFunction = "N/A";
76 
77   if( objc!=1 ){
78     Tcl_WrongNumArgs(interp, 1, objv, "");
79     return TCL_ERROR;
80   }
81 
82   p = sqlite3_malloc(5);
83   if( !p ){
84     zErrFunction = "sqlite3_malloc";
85     goto error_out;
86   }
87 
88   /* Test that realloc()ing a block of memory to a negative size is
89   ** the same as free()ing that memory.
90   */
91   p = sqlite3_realloc(p, -1);
92   if( p ){
93     zErrFunction = "sqlite3_realloc";
94     goto error_out;
95   }
96 
97   return TCL_OK;
98 
99 error_out:
100   Tcl_ResetResult(interp);
101   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
102   return TCL_ERROR;
103 }
104 
105 
106 /*
107 ** c_misuse_test
108 */
109 static int c_misuse_test(
110   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
111   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
112   int objc,              /* Number of arguments */
113   Tcl_Obj *CONST objv[]  /* Command arguments */
114 ){
115   const char *zErrFunction = "N/A";
116   sqlite3 *db = 0;
117   sqlite3_stmt *pStmt;
118   int rc;
119 
120   if( objc!=1 ){
121     Tcl_WrongNumArgs(interp, 1, objv, "");
122     return TCL_ERROR;
123   }
124 
125   /* Open a database. Then close it again. We need to do this so that
126   ** we have a "closed database handle" to pass to various API functions.
127   */
128   rc = sqlite3_open(":memory:", &db);
129   if( rc!=SQLITE_OK ){
130     zErrFunction = "sqlite3_open";
131     goto error_out;
132   }
133   sqlite3_close(db);
134 
135 
136   rc = sqlite3_errcode(db);
137   if( rc!=SQLITE_MISUSE ){
138     zErrFunction = "sqlite3_errcode";
139     goto error_out;
140   }
141 
142   pStmt = (sqlite3_stmt*)1234;
143   rc = sqlite3_prepare(db, 0, 0, &pStmt, 0);
144   if( rc!=SQLITE_MISUSE ){
145     zErrFunction = "sqlite3_prepare";
146     goto error_out;
147   }
148   assert( pStmt==0 ); /* Verify that pStmt is zeroed even on a MISUSE error */
149 
150   pStmt = (sqlite3_stmt*)1234;
151   rc = sqlite3_prepare_v2(db, 0, 0, &pStmt, 0);
152   if( rc!=SQLITE_MISUSE ){
153     zErrFunction = "sqlite3_prepare_v2";
154     goto error_out;
155   }
156   assert( pStmt==0 );
157 
158 #ifndef SQLITE_OMIT_UTF16
159   pStmt = (sqlite3_stmt*)1234;
160   rc = sqlite3_prepare16(db, 0, 0, &pStmt, 0);
161   if( rc!=SQLITE_MISUSE ){
162     zErrFunction = "sqlite3_prepare16";
163     goto error_out;
164   }
165   assert( pStmt==0 );
166   pStmt = (sqlite3_stmt*)1234;
167   rc = sqlite3_prepare16_v2(db, 0, 0, &pStmt, 0);
168   if( rc!=SQLITE_MISUSE ){
169     zErrFunction = "sqlite3_prepare16_v2";
170     goto error_out;
171   }
172   assert( pStmt==0 );
173 #endif
174 
175   return TCL_OK;
176 
177 error_out:
178   Tcl_ResetResult(interp);
179   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
180   return TCL_ERROR;
181 }
182 
183 /*
184 ** Register commands with the TCL interpreter.
185 */
186 int Sqlitetest9_Init(Tcl_Interp *interp){
187   static struct {
188      char *zName;
189      Tcl_ObjCmdProc *xProc;
190      void *clientData;
191   } aObjCmd[] = {
192      { "c_misuse_test",    c_misuse_test, 0 },
193      { "c_realloc_test",   c_realloc_test, 0 },
194      { "c_collation_test", c_collation_test, 0 },
195   };
196   int i;
197   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
198     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
199         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
200   }
201   return TCL_OK;
202 }
203