Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/test5.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / test5.c` 216 lines ยท 5480 bytes Raw Blame History 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 "tclsqlite.h" 21 #include 22 #include 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 SQLITE_TCLAPI binarize( 30 void * clientData, 31 Tcl_Interp *interp, 32 int objc, 33 Tcl_Obj *CONST objv[] 34 ){ 35 Tcl_Size 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 . 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 is 0, then the calls to sqlite3_value_text() are not 55 ** actually made. 56 */ 57 static int SQLITE_TCLAPI 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 68 if( objc!=3 ){ 69 Tcl_AppendResult(interp, "wrong # args: should be \"", 70 Tcl_GetStringFromObj(objv[0], 0), " ", NULL); 71 return TCL_ERROR; 72 } 73 74 if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR; 75 if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR; 76 77 val.flags = MEM_Str|MEM_Term|MEM_Static; 78 val.z = "hello world"; 79 val.enc = SQLITE_UTF8; 80 81 for(i=0; izName; pEnc++){ 104 if( 0==sqlite3StrICmp(z, pEnc->zName) ){ 105 break; 106 } 107 } 108 if( !pEnc->enc ){ 109 Tcl_AppendResult(interp, "No such encoding: ", z, NULL); 110 } 111 if( pEnc->enc==SQLITE_UTF16 ){ 112 return SQLITE_UTF16NATIVE; 113 } 114 return pEnc->enc; 115 } 116 117 /* 118 ** Usage: test_translate ?? 119 ** 120 */ 121 static int SQLITE_TCLAPI test_translate( 122 void * clientData, 123 Tcl_Interp *interp, 124 int objc, 125 Tcl_Obj *CONST objv[] 126 ){ 127 u8 enc_from; 128 u8 enc_to; 129 sqlite3_value *pVal; 130 131 char *z; 132 Tcl_Size len; 133 void (*xDel)(void *p) = SQLITE_STATIC; 134 135 if( objc!=4 && objc!=5 ){ 136 Tcl_AppendResult(interp, "wrong # args: should be \"", 137 Tcl_GetStringFromObj(objv[0], 0), 138 " ", NULL 139 ); 140 return TCL_ERROR; 141 } 142 if( objc==5 ){ 143 xDel = sqlite3_free; 144 } 145 146 enc_from = name_to_enc(interp, objv[2]); 147 if( !enc_from ) return TCL_ERROR; 148 enc_to = name_to_enc(interp, objv[3]); 149 if( !enc_to ) return TCL_ERROR; 150 151 pVal = sqlite3ValueNew(0); 152 153 if( enc_from==SQLITE_UTF8 ){ 154 z = Tcl_GetString(objv[1]); 155 if( objc==5 ){ 156 z = sqlite3_mprintf("%s", z); 157 } 158 sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); 159 }else{ 160 z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len); 161 if( objc==5 ){ 162 char *zTmp = z; 163 z = sqlite3_malloc64(len); 164 memcpy(z, zTmp, len); 165 } 166 sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); 167 } 168 169 z = (char *)sqlite3ValueText(pVal, enc_to); 170 len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2); 171 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((u8*)z, len)); 172 173 sqlite3ValueFree(pVal); 174 175 return TCL_OK; 176 } 177 178 /* 179 ** Usage: translate_selftest 180 ** 181 ** Call sqlite3UtfSelfTest() to run the internal tests for unicode 182 ** translation. If there is a problem an assert() will fail. 183 **/ 184 void sqlite3UtfSelfTest(void); 185 static int SQLITE_TCLAPI test_translate_selftest( 186 void * clientData, 187 Tcl_Interp *interp, 188 int objc, 189 Tcl_Obj *CONST objv[] 190 ){ 191 #ifndef SQLITE_OMIT_UTF16 192 sqlite3UtfSelfTest(); 193 #endif 194 return SQLITE_OK; 195 } 196 197 198 /* 199 ** Register commands with the TCL interpreter. 200 */ 201 int Sqlitetest5_Init(Tcl_Interp *interp){ 202 static struct { 203 char *zName; 204 Tcl_ObjCmdProc *xProc; 205 } aCmd[] = { 206 { "binarize", (Tcl_ObjCmdProc*)binarize }, 207 { "test_value_overhead", (Tcl_ObjCmdProc*)test_value_overhead }, 208 { "test_translate", (Tcl_ObjCmdProc*)test_translate }, 209 { "translate_selftest", (Tcl_ObjCmdProc*)test_translate_selftest}, 210 }; 211 int i; 212 for(i=0; i