xref: /sqlite-3.40.0/src/test9.c (revision 8a29dfde)
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.5 2008/01/23 12:52:41 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_ERROR ){
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   int rc;
118 
119   if( objc!=1 ){
120     Tcl_WrongNumArgs(interp, 1, objv, "");
121     return TCL_ERROR;
122   }
123 
124   /* Open a database. Then close it again. We need to do this so that
125   ** we have a "closed database handle" to pass to various API functions.
126   */
127   rc = sqlite3_open(":memory:", &db);
128   if( rc!=SQLITE_OK ){
129     zErrFunction = "sqlite3_open";
130     goto error_out;
131   }
132   sqlite3_close(db);
133 
134 
135   rc = sqlite3_errcode(db);
136   if( rc!=SQLITE_MISUSE ){
137     zErrFunction = "sqlite3_errcode";
138     goto error_out;
139   }
140 
141   rc = sqlite3_prepare(db, 0, 0, 0, 0);
142   if( rc!=SQLITE_MISUSE ){
143     zErrFunction = "sqlite3_prepare";
144     goto error_out;
145   }
146 
147   rc = sqlite3_prepare_v2(db, 0, 0, 0, 0);
148   if( rc!=SQLITE_MISUSE ){
149     zErrFunction = "sqlite3_prepare_v2";
150     goto error_out;
151   }
152 
153 #ifndef SQLITE_OMIT_UTF16
154   rc = sqlite3_prepare16(db, 0, 0, 0, 0);
155   if( rc!=SQLITE_MISUSE ){
156     zErrFunction = "sqlite3_prepare16";
157     goto error_out;
158   }
159   rc = sqlite3_prepare16_v2(db, 0, 0, 0, 0);
160   if( rc!=SQLITE_MISUSE ){
161     zErrFunction = "sqlite3_prepare16_v2";
162     goto error_out;
163   }
164 #endif
165 
166   return TCL_OK;
167 
168 error_out:
169   Tcl_ResetResult(interp);
170   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
171   return TCL_ERROR;
172 }
173 
174 /*
175 ** Register commands with the TCL interpreter.
176 */
177 int Sqlitetest9_Init(Tcl_Interp *interp){
178   static struct {
179      char *zName;
180      Tcl_ObjCmdProc *xProc;
181      void *clientData;
182   } aObjCmd[] = {
183      { "c_misuse_test",    c_misuse_test, 0 },
184      { "c_realloc_test",   c_realloc_test, 0 },
185      { "c_collation_test", c_collation_test, 0 },
186   };
187   int i;
188   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
189     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
190         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
191   }
192   return TCL_OK;
193 }
194