xref: /sqlite-3.40.0/src/test9.c (revision 999cc5d7)
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.4 2007/08/21 10:44:16 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 #ifndef SQLITE_OMIT_UTF16
135   rc = sqlite3_collation_needed16(db, 0, 0);
136   if( rc!=SQLITE_MISUSE ){
137     zErrFunction = "sqlite3_collation_needed16";
138     goto error_out;
139   }
140 #endif
141 
142   rc = sqlite3_collation_needed(db, 0, 0);
143   if( rc!=SQLITE_MISUSE ){
144     zErrFunction = "sqlite3_collation_needed";
145     goto error_out;
146   }
147 
148   rc = sqlite3_create_collation(db, 0, 0, 0, 0);
149   if( rc!=SQLITE_MISUSE ){
150     zErrFunction = "sqlite3_create_collation";
151     goto error_out;
152   }
153 
154   rc = sqlite3_create_function(db, 0, 0, 0, 0, 0, 0, 0);
155   if( rc!=SQLITE_MISUSE ){
156     zErrFunction = "sqlite3_create_function";
157     goto error_out;
158   }
159 
160   rc = sqlite3_busy_handler(db, 0, 0);
161   if( rc!=SQLITE_MISUSE ){
162     zErrFunction = "sqlite3_busy_handler";
163     goto error_out;
164   }
165 
166   rc = sqlite3_errcode(db);
167   if( rc!=SQLITE_MISUSE ){
168     zErrFunction = "sqlite3_busy_handler";
169     goto error_out;
170   }
171 
172 #ifndef SQLITE_OMIT_UTF16
173   rc = sqlite3_prepare16(db, 0, 0, 0, 0);
174   if( rc!=SQLITE_MISUSE ){
175     zErrFunction = "sqlite3_prepare16";
176     goto error_out;
177   }
178 #endif
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