4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
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.
18 #include "sqliteInt.h"
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.
40 bytes
= Tcl_GetStringFromObj(objv
[1], &len
);
41 pRet
= Tcl_NewByteArrayObj((u8
*)bytes
, len
+1);
42 Tcl_SetObjResult(interp
, pRet
);
47 ** Usage: test_value_overhead <repeat-count> <do-calls>.
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.
54 ** If <do-calls> is 0, then the calls to sqlite3_value_text() are not
57 static int test_value_overhead(
69 Tcl_AppendResult(interp
, "wrong # args: should be \"",
70 Tcl_GetStringFromObj(objv
[0], 0), " <repeat-count> <do-calls>", 0);
74 if( Tcl_GetIntFromObj(interp
, objv
[1], &repeat_count
) ) return TCL_ERROR
;
75 if( Tcl_GetIntFromObj(interp
, objv
[2], &do_calls
) ) return TCL_ERROR
;
77 val
.flags
= MEM_Str
|MEM_Term
|MEM_Static
;
78 val
.z
= "hello world";
79 val
.type
= SQLITE_TEXT
;
80 val
.enc
= SQLITE_UTF8
;
82 for(i
=0; i
<repeat_count
; i
++){
84 sqlite3_value_text(&val
);
91 static u8
name_to_enc(Tcl_Interp
*interp
, Tcl_Obj
*pObj
){
96 { "UTF8", SQLITE_UTF8
},
97 { "UTF16LE", SQLITE_UTF16LE
},
98 { "UTF16BE", SQLITE_UTF16BE
},
99 { "UTF16", SQLITE_UTF16
},
102 struct EncName
*pEnc
;
103 char *z
= Tcl_GetString(pObj
);
104 for(pEnc
=&encnames
[0]; pEnc
->zName
; pEnc
++){
105 if( 0==sqlite3StrICmp(z
, pEnc
->zName
) ){
110 Tcl_AppendResult(interp
, "No such encoding: ", z
, 0);
112 if( pEnc
->enc
==SQLITE_UTF16
){
113 return SQLITE_UTF16NATIVE
;
119 ** Usage: test_translate <string/blob> <from enc> <to enc> ?<transient>?
122 static int test_translate(
126 Tcl_Obj
*CONST objv
[]
134 void (*xDel
)(void *p
) = SQLITE_STATIC
;
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
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
;
152 pVal
= sqlite3ValueNew(0);
154 if( enc_from
==SQLITE_UTF8
){
155 z
= Tcl_GetString(objv
[1]);
157 z
= sqlite3_mprintf("%s", z
);
159 sqlite3ValueSetStr(pVal
, -1, z
, enc_from
, xDel
);
161 z
= (char*)Tcl_GetByteArrayFromObj(objv
[1], &len
);
164 z
= sqlite3_malloc(len
);
165 memcpy(z
, zTmp
, len
);
167 sqlite3ValueSetStr(pVal
, -1, z
, enc_from
, xDel
);
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
));
174 sqlite3ValueFree(pVal
);
180 ** Usage: translate_selftest
182 ** Call sqlite3UtfSelfTest() to run the internal tests for unicode
183 ** translation. If there is a problem an assert() will fail.
185 void sqlite3UtfSelfTest(void);
186 static int test_translate_selftest(
190 Tcl_Obj
*CONST objv
[]
192 #ifndef SQLITE_OMIT_UTF16
193 sqlite3UtfSelfTest();
200 ** Register commands with the TCL interpreter.
202 int Sqlitetest5_Init(Tcl_Interp
*interp
){
205 Tcl_ObjCmdProc
*xProc
;
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
},
213 for(i
=0; i
<sizeof(aCmd
)/sizeof(aCmd
[0]); i
++){
214 Tcl_CreateObjCommand(interp
, aCmd
[i
].zName
, aCmd
[i
].xProc
, 0, 0);