xref: /sqlite-3.40.0/src/test5.c (revision 7fdb522c)
1 /*
2 ** 2001 September 15
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 ** Code for testing the utf.c module in SQLite.  This code
13 ** is not included in the SQLite library.  It is used for automated
14 ** testing of the SQLite library. Specifically, the code in this file
15 ** is used for testing the SQLite routines for converting between
16 ** the various supported unicode encodings.
17 **
18 ** $Id: test5.c,v 1.21 2008/07/28 19:34:54 drh Exp $
19 */
20 #include "sqliteInt.h"
21 #include "vdbeInt.h"
22 #include "tcl.h"
23 #include <stdlib.h>
24 #include <string.h>
25 
26 /*
27 ** The first argument is a TCL UTF-8 string. Return the byte array
28 ** object with the encoded representation of the string, including
29 ** the NULL terminator.
30 */
31 static int binarize(
32   void * clientData,
33   Tcl_Interp *interp,
34   int objc,
35   Tcl_Obj *CONST objv[]
36 ){
37   int len;
38   char *bytes;
39   Tcl_Obj *pRet;
40   assert(objc==2);
41 
42   bytes = Tcl_GetStringFromObj(objv[1], &len);
43   pRet = Tcl_NewByteArrayObj((u8*)bytes, len+1);
44   Tcl_SetObjResult(interp, pRet);
45   return TCL_OK;
46 }
47 
48 /*
49 ** Usage: test_value_overhead <repeat-count> <do-calls>.
50 **
51 ** This routine is used to test the overhead of calls to
52 ** sqlite3_value_text(), on a value that contains a UTF-8 string. The idea
53 ** is to figure out whether or not it is a problem to use sqlite3_value
54 ** structures with collation sequence functions.
55 **
56 ** If <do-calls> is 0, then the calls to sqlite3_value_text() are not
57 ** actually made.
58 */
59 static int test_value_overhead(
60   void * clientData,
61   Tcl_Interp *interp,
62   int objc,
63   Tcl_Obj *CONST objv[]
64 ){
65   int do_calls;
66   int repeat_count;
67   int i;
68   Mem val;
69   const char *zVal;
70 
71   if( objc!=3 ){
72     Tcl_AppendResult(interp, "wrong # args: should be \"",
73         Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", 0);
74     return TCL_ERROR;
75   }
76 
77   if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR;
78   if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR;
79 
80   val.flags = MEM_Str|MEM_Term|MEM_Static;
81   val.z = "hello world";
82   val.type = SQLITE_TEXT;
83   val.enc = SQLITE_UTF8;
84 
85   for(i=0; i<repeat_count; i++){
86     if( do_calls ){
87       zVal = (char*)sqlite3_value_text(&val);
88     }
89   }
90 
91   return TCL_OK;
92 }
93 
94 static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){
95   struct EncName {
96     char *zName;
97     u8 enc;
98   } encnames[] = {
99     { "UTF8", SQLITE_UTF8 },
100     { "UTF16LE", SQLITE_UTF16LE },
101     { "UTF16BE", SQLITE_UTF16BE },
102     { "UTF16", SQLITE_UTF16NATIVE },
103     { 0, 0 }
104   };
105   struct EncName *pEnc;
106   char *z = Tcl_GetString(pObj);
107   for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
108     if( 0==sqlite3StrICmp(z, pEnc->zName) ){
109       break;
110     }
111   }
112   if( !pEnc->enc ){
113     Tcl_AppendResult(interp, "No such encoding: ", z, 0);
114   }
115   return pEnc->enc;
116 }
117 
118 /*
119 ** Usage:   test_translate <string/blob> <from enc> <to enc> ?<transient>?
120 **
121 */
122 static int test_translate(
123   void * clientData,
124   Tcl_Interp *interp,
125   int objc,
126   Tcl_Obj *CONST objv[]
127 ){
128   u8 enc_from;
129   u8 enc_to;
130   sqlite3_value *pVal;
131 
132   char *z;
133   int len;
134   void (*xDel)(void *p) = SQLITE_STATIC;
135 
136   if( objc!=4 && objc!=5 ){
137     Tcl_AppendResult(interp, "wrong # args: should be \"",
138         Tcl_GetStringFromObj(objv[0], 0),
139         " <string/blob> <from enc> <to enc>", 0
140     );
141     return TCL_ERROR;
142   }
143   if( objc==5 ){
144     xDel = sqlite3_free;
145   }
146 
147   enc_from = name_to_enc(interp, objv[2]);
148   if( !enc_from ) return TCL_ERROR;
149   enc_to = name_to_enc(interp, objv[3]);
150   if( !enc_to ) return TCL_ERROR;
151 
152   pVal = sqlite3ValueNew(0);
153 
154   if( enc_from==SQLITE_UTF8 ){
155     z = Tcl_GetString(objv[1]);
156     if( objc==5 ){
157       z = sqlite3DbStrDup(0, z);
158     }
159     sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
160   }else{
161     z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len);
162     if( objc==5 ){
163       char *zTmp = z;
164       z = sqlite3_malloc(len);
165       memcpy(z, zTmp, len);
166     }
167     sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
168   }
169 
170   z = (char *)sqlite3ValueText(pVal, enc_to);
171   len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2);
172   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((u8*)z, len));
173 
174   sqlite3ValueFree(pVal);
175 
176   return TCL_OK;
177 }
178 
179 /*
180 ** Usage: translate_selftest
181 **
182 ** Call sqlite3UtfSelfTest() to run the internal tests for unicode
183 ** translation. If there is a problem an assert() will fail.
184 **/
185 void sqlite3UtfSelfTest();
186 static int test_translate_selftest(
187   void * clientData,
188   Tcl_Interp *interp,
189   int objc,
190   Tcl_Obj *CONST objv[]
191 ){
192 #ifndef SQLITE_OMIT_UTF16
193   sqlite3UtfSelfTest();
194 #endif
195   return SQLITE_OK;
196 }
197 
198 
199 /*
200 ** Register commands with the TCL interpreter.
201 */
202 int Sqlitetest5_Init(Tcl_Interp *interp){
203   static struct {
204     char *zName;
205     Tcl_ObjCmdProc *xProc;
206   } aCmd[] = {
207     { "binarize",                (Tcl_ObjCmdProc*)binarize },
208     { "test_value_overhead",     (Tcl_ObjCmdProc*)test_value_overhead },
209     { "test_translate",          (Tcl_ObjCmdProc*)test_translate     },
210     { "translate_selftest",      (Tcl_ObjCmdProc*)test_translate_selftest},
211   };
212   int i;
213   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
214     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
215   }
216   return SQLITE_OK;
217 }
218