xref: /sqlite-3.40.0/src/test9.c (revision 7152de8d)
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.2 2007/03/29 17:28:15 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   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   rc = sqlite3_collation_needed16(db, 0, 0);
136   if( rc!=SQLITE_MISUSE ){
137     zErrFunction = "sqlite3_collation_needed16";
138     goto error_out;
139   }
140 
141   rc = sqlite3_collation_needed(db, 0, 0);
142   if( rc!=SQLITE_MISUSE ){
143     zErrFunction = "sqlite3_collation_needed";
144     goto error_out;
145   }
146 
147   rc = sqlite3_create_collation(db, 0, 0, 0, 0);
148   if( rc!=SQLITE_MISUSE ){
149     zErrFunction = "sqlite3_create_collation";
150     goto error_out;
151   }
152 
153   rc = sqlite3_create_function(db, 0, 0, 0, 0, 0, 0, 0);
154   if( rc!=SQLITE_MISUSE ){
155     zErrFunction = "sqlite3_create_function";
156     goto error_out;
157   }
158 
159   rc = sqlite3_busy_handler(db, 0, 0);
160   if( rc!=SQLITE_MISUSE ){
161     zErrFunction = "sqlite3_busy_handler";
162     goto error_out;
163   }
164 
165   rc = sqlite3_errcode(db);
166   if( rc!=SQLITE_MISUSE ){
167     zErrFunction = "sqlite3_busy_handler";
168     goto error_out;
169   }
170 
171   rc = sqlite3_prepare16(db, 0, 0, 0, 0);
172   if( rc!=SQLITE_MISUSE ){
173     zErrFunction = "sqlite3_prepare16";
174     goto error_out;
175   }
176 
177   return TCL_OK;
178 
179 error_out:
180   Tcl_ResetResult(interp);
181   Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
182   return TCL_ERROR;
183 }
184 
185 /*
186 ** Register commands with the TCL interpreter.
187 */
188 int Sqlitetest9_Init(Tcl_Interp *interp){
189   static struct {
190      char *zName;
191      Tcl_ObjCmdProc *xProc;
192      void *clientData;
193   } aObjCmd[] = {
194      { "c_misuse_test",    c_misuse_test, 0 },
195      { "c_realloc_test",   c_realloc_test, 0 },
196      { "c_collation_test", c_collation_test, 0 },
197   };
198   int i;
199   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
200     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
201         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
202   }
203   return TCL_OK;
204 }
205 
206