danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 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 |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 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. |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 17 | ** |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame^] | 18 | ** $Id: test5.c,v 1.14 2004/11/14 21:56:30 drh Exp $ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include "sqliteInt.h" |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 21 | #include "vdbeInt.h" |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 22 | #include "os.h" /* to get SQLITE_BIGENDIAN */ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 23 | #include "tcl.h" |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | /* |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 28 | ** The first argument is a TCL UTF-8 string. Return the byte array |
| 29 | ** object with the encoded representation of the string, including |
| 30 | ** the NULL terminator. |
| 31 | */ |
| 32 | static int binarize( |
| 33 | void * clientData, |
| 34 | Tcl_Interp *interp, |
| 35 | int objc, |
| 36 | Tcl_Obj *CONST objv[] |
| 37 | ){ |
| 38 | int len; |
| 39 | char *bytes; |
| 40 | Tcl_Obj *pRet; |
| 41 | assert(objc==2); |
| 42 | |
| 43 | bytes = Tcl_GetStringFromObj(objv[1], &len); |
| 44 | pRet = Tcl_NewByteArrayObj(bytes, len+1); |
| 45 | Tcl_SetObjResult(interp, pRet); |
| 46 | return TCL_OK; |
| 47 | } |
| 48 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 49 | /* |
| 50 | ** Usage: test_value_overhead <repeat-count> <do-calls>. |
| 51 | ** |
| 52 | ** This routine is used to test the overhead of calls to |
| 53 | ** sqlite3_value_text(), on a value that contains a UTF-8 string. The idea |
| 54 | ** is to figure out whether or not it is a problem to use sqlite3_value |
| 55 | ** structures with collation sequence functions. |
| 56 | ** |
| 57 | ** If <do-calls> is 0, then the calls to sqlite3_value_text() are not |
| 58 | ** actually made. |
| 59 | */ |
| 60 | static int test_value_overhead( |
| 61 | void * clientData, |
| 62 | Tcl_Interp *interp, |
| 63 | int objc, |
| 64 | Tcl_Obj *CONST objv[] |
| 65 | ){ |
| 66 | int do_calls; |
| 67 | int repeat_count; |
| 68 | int i; |
| 69 | Mem val; |
| 70 | const char *zVal; |
| 71 | |
| 72 | if( objc!=3 ){ |
| 73 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 74 | Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", 0); |
| 75 | return TCL_ERROR; |
| 76 | } |
| 77 | |
| 78 | if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR; |
| 79 | if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR; |
| 80 | |
| 81 | val.flags = MEM_Str|MEM_Term|MEM_Static; |
| 82 | val.z = "hello world"; |
| 83 | val.type = SQLITE_TEXT; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 84 | val.enc = SQLITE_UTF8; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 85 | |
| 86 | for(i=0; i<repeat_count; i++){ |
| 87 | if( do_calls ){ |
| 88 | zVal = sqlite3_value_text(&val); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return TCL_OK; |
| 93 | } |
| 94 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 95 | static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){ |
| 96 | struct EncName { |
| 97 | char *zName; |
| 98 | u8 enc; |
| 99 | } encnames[] = { |
| 100 | { "UTF8", SQLITE_UTF8 }, |
| 101 | { "UTF16LE", SQLITE_UTF16LE }, |
| 102 | { "UTF16BE", SQLITE_UTF16BE }, |
| 103 | { "UTF16", SQLITE_UTF16NATIVE }, |
| 104 | { 0, 0 } |
| 105 | }; |
| 106 | struct EncName *pEnc; |
| 107 | char *z = Tcl_GetString(pObj); |
| 108 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 109 | if( 0==sqlite3StrICmp(z, pEnc->zName) ){ |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if( !pEnc->enc ){ |
| 114 | Tcl_AppendResult(interp, "No such encoding: ", z, 0); |
| 115 | } |
| 116 | return pEnc->enc; |
| 117 | } |
| 118 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 119 | /* |
| 120 | ** Usage: test_translate <string/blob> <from enc> <to enc> ?<transient>? |
| 121 | ** |
| 122 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 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 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 133 | char *z; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 134 | int len; |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 135 | void (*xDel)(void *p) = SQLITE_STATIC; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 136 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 137 | if( objc!=4 && objc!=5 ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 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 | } |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 144 | if( objc==5 ){ |
| 145 | xDel = sqlite3FreeX; |
| 146 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 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(); |
| 154 | |
| 155 | if( enc_from==SQLITE_UTF8 ){ |
| 156 | z = Tcl_GetString(objv[1]); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 157 | if( objc==5 ){ |
| 158 | z = sqliteStrDup(z); |
| 159 | } |
| 160 | sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 161 | }else{ |
| 162 | z = Tcl_GetByteArrayFromObj(objv[1], &len); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 163 | if( objc==5 ){ |
| 164 | char *zTmp = z; |
| 165 | z = sqliteMalloc(len); |
| 166 | memcpy(z, zTmp, len); |
| 167 | } |
| 168 | sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 169 | } |
| 170 | |
danielk1977 | 7657240 | 2004-06-25 02:38:54 +0000 | [diff] [blame] | 171 | z = (char *)sqlite3ValueText(pVal, enc_to); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 172 | len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2); |
| 173 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(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(); |
| 187 | static int test_translate_selftest( |
| 188 | void * clientData, |
| 189 | Tcl_Interp *interp, |
| 190 | int objc, |
| 191 | Tcl_Obj *CONST objv[] |
| 192 | ){ |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame^] | 193 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 194 | sqlite3utfSelfTest(); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame^] | 195 | #endif |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 196 | return SQLITE_OK; |
| 197 | } |
| 198 | |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | ** Register commands with the TCL interpreter. |
| 202 | */ |
| 203 | int Sqlitetest5_Init(Tcl_Interp *interp){ |
| 204 | static struct { |
| 205 | char *zName; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 206 | Tcl_ObjCmdProc *xProc; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 207 | } aCmd[] = { |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 208 | { "binarize", (Tcl_ObjCmdProc*)binarize }, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 209 | { "test_value_overhead", (Tcl_ObjCmdProc*)test_value_overhead }, |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 210 | { "test_translate", (Tcl_ObjCmdProc*)test_translate }, |
| 211 | { "translate_selftest", (Tcl_ObjCmdProc*)test_translate_selftest}, |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 212 | }; |
| 213 | int i; |
| 214 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 215 | Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 216 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 217 | return SQLITE_OK; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 218 | } |