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