xref: /sqlite-3.40.0/src/test9.c (revision 5665b3ea)
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.3 2007/04/23 23:56:32 drh 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   const char *zErrFunction = "N/A";
35   sqlite3 *db;
36 
37   int rc;
38   if( objc!=1 ){
39     Tcl_WrongNumArgs(interp, 1, objv, "");
40     return TCL_ERROR;
41   }
42 
43   /* Open a database. */
44   rc = sqlite3_open(":memory:", &db);
45   if( rc!=SQLITE_OK ){
46     zErrFunction = "sqlite3_open";
47     goto error_out;
48   }
49 
50   rc = sqlite3_create_collation(db, "collate", 456, 0, 0);
51   if( rc!=SQLITE_ERROR ){
52     sqlite3_close(db);
53     zErrFunction = "sqlite3_create_collation";
54     goto error_out;
55   }
56 
57   sqlite3_close(db);
58   return TCL_OK;
59 
60 error_out:
61   Tcl_ResetResult(interp);
62   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
63   return TCL_ERROR;
64 }
65 
66 /*
67 ** c_realloc_test
68 */
69 static int c_realloc_test(
70   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
71   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
72   int objc,              /* Number of arguments */
73   Tcl_Obj *CONST objv[]  /* Command arguments */
74 ){
75   void *p;
76   const char *zErrFunction = "N/A";
77 
78   if( objc!=1 ){
79     Tcl_WrongNumArgs(interp, 1, objv, "");
80     return TCL_ERROR;
81   }
82 
83   p = sqlite3_malloc(5);
84   if( !p ){
85     zErrFunction = "sqlite3_malloc";
86     goto error_out;
87   }
88 
89   /* Test that realloc()ing a block of memory to a negative size is
90   ** the same as free()ing that memory.
91   */
92   p = sqlite3_realloc(p, -1);
93   if( p ){
94     zErrFunction = "sqlite3_realloc";
95     goto error_out;
96   }
97 
98   return TCL_OK;
99 
100 error_out:
101   Tcl_ResetResult(interp);
102   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
103   return TCL_ERROR;
104 }
105 
106 
107 /*
108 ** c_misuse_test
109 */
110 static int c_misuse_test(
111   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
112   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
113   int objc,              /* Number of arguments */
114   Tcl_Obj *CONST objv[]  /* Command arguments */
115 ){
116   const char *zErrFunction = "N/A";
117   sqlite3 *db = 0;
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 #ifndef SQLITE_OMIT_UTF16
136   rc = sqlite3_collation_needed16(db, 0, 0);
137   if( rc!=SQLITE_MISUSE ){
138     zErrFunction = "sqlite3_collation_needed16";
139     goto error_out;
140   }
141 #endif
142 
143   rc = sqlite3_collation_needed(db, 0, 0);
144   if( rc!=SQLITE_MISUSE ){
145     zErrFunction = "sqlite3_collation_needed";
146     goto error_out;
147   }
148 
149   rc = sqlite3_create_collation(db, 0, 0, 0, 0);
150   if( rc!=SQLITE_MISUSE ){
151     zErrFunction = "sqlite3_create_collation";
152     goto error_out;
153   }
154 
155   rc = sqlite3_create_function(db, 0, 0, 0, 0, 0, 0, 0);
156   if( rc!=SQLITE_MISUSE ){
157     zErrFunction = "sqlite3_create_function";
158     goto error_out;
159   }
160 
161   rc = sqlite3_busy_handler(db, 0, 0);
162   if( rc!=SQLITE_MISUSE ){
163     zErrFunction = "sqlite3_busy_handler";
164     goto error_out;
165   }
166 
167   rc = sqlite3_errcode(db);
168   if( rc!=SQLITE_MISUSE ){
169     zErrFunction = "sqlite3_busy_handler";
170     goto error_out;
171   }
172 
173 #ifndef SQLITE_OMIT_UTF16
174   rc = sqlite3_prepare16(db, 0, 0, 0, 0);
175   if( rc!=SQLITE_MISUSE ){
176     zErrFunction = "sqlite3_prepare16";
177     goto error_out;
178   }
179 #endif
180 
181   return TCL_OK;
182 
183 error_out:
184   Tcl_ResetResult(interp);
185   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
186   return TCL_ERROR;
187 }
188 
189 /*
190 ** Register commands with the TCL interpreter.
191 */
192 int Sqlitetest9_Init(Tcl_Interp *interp){
193   static struct {
194      char *zName;
195      Tcl_ObjCmdProc *xProc;
196      void *clientData;
197   } aObjCmd[] = {
198      { "c_misuse_test",    c_misuse_test, 0 },
199      { "c_realloc_test",   c_realloc_test, 0 },
200      { "c_collation_test", c_collation_test, 0 },
201   };
202   int i;
203   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
204     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
205         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
206   }
207   return TCL_OK;
208 }
209