Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 06d2de911d5b0d5c6b0fcd8ffad2870a7fdbb586 [file] [log] [blame]
danielk1977998b56c2004-05-06 23:37:521/*
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
danielk1977295ba552004-05-19 10:34:5114** 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.
danielk1977998b56c2004-05-06 23:37:5217*/
18#include "sqliteInt.h"
danielk19770202b292004-06-09 09:55:1619#include "vdbeInt.h"
drh064b6812024-07-30 15:49:0220#include "tclsqlite.h"
danielk1977998b56c2004-05-06 23:37:5221#include <stdlib.h>
22#include <string.h>
23
24/*
danielk1977295ba552004-05-19 10:34:5125** 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*/
mistachkin7617e4a2016-07-28 17:11:2029static int SQLITE_TCLAPI binarize(
danielk1977295ba552004-05-19 10:34:5130 void * clientData,
31 Tcl_Interp *interp,
32 int objc,
33 Tcl_Obj *CONST objv[]
34){
drh064b6812024-07-30 15:49:0235 Tcl_Size len;
danielk1977295ba552004-05-19 10:34:5136 char *bytes;
37 Tcl_Obj *pRet;
38 assert(objc==2);
39
40 bytes = Tcl_GetStringFromObj(objv[1], &len);
drh03d847e2005-12-09 20:21:5841 pRet = Tcl_NewByteArrayObj((u8*)bytes, len+1);
danielk1977295ba552004-05-19 10:34:5142 Tcl_SetObjResult(interp, pRet);
43 return TCL_OK;
44}
45
danielk19770202b292004-06-09 09:55:1646/*
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*/
mistachkin7617e4a2016-07-28 17:11:2057static int SQLITE_TCLAPI test_value_overhead(
danielk19770202b292004-06-09 09:55:1658 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;
danielk19770202b292004-06-09 09:55:1667
68 if( objc!=3 ){
69 Tcl_AppendResult(interp, "wrong # args: should be \"",
stephan065c0a62025-03-08 06:53:0670 Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", NULL);
danielk19770202b292004-06-09 09:55:1671 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";
danielk1977dc8453f2004-06-12 00:42:3479 val.enc = SQLITE_UTF8;
danielk19770202b292004-06-09 09:55:1680
81 for(i=0; i<repeat_count; i++){
82 if( do_calls ){
drhcaffb1a2012-01-30 18:00:3183 sqlite3_value_text(&val);
danielk19770202b292004-06-09 09:55:1684 }
85 }
86
87 return TCL_OK;
88}
89
danielk1977bfd6cce2004-06-18 04:24:5490static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){
91 struct EncName {
92 char *zName;
93 u8 enc;
94 } encnames[] = {
95 { "UTF8", SQLITE_UTF8 },
96 { "UTF16LE", SQLITE_UTF16LE },
97 { "UTF16BE", SQLITE_UTF16BE },
danielk197744a376f2008-08-12 15:04:5898 { "UTF16", SQLITE_UTF16 },
danielk1977bfd6cce2004-06-18 04:24:5499 { 0, 0 }
100 };
101 struct EncName *pEnc;
102 char *z = Tcl_GetString(pObj);
103 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
104 if( 0==sqlite3StrICmp(z, pEnc->zName) ){
105 break;
106 }
107 }
108 if( !pEnc->enc ){
stephanb6503f12025-03-06 13:38:07109 Tcl_AppendResult(interp, "No such encoding: ", z, NULL);
danielk1977bfd6cce2004-06-18 04:24:54110 }
danielk197744a376f2008-08-12 15:04:58111 if( pEnc->enc==SQLITE_UTF16 ){
112 return SQLITE_UTF16NATIVE;
113 }
danielk1977bfd6cce2004-06-18 04:24:54114 return pEnc->enc;
115}
116
danielk19771ba1b552004-06-23 13:46:32117/*
118** Usage: test_translate <string/blob> <from enc> <to enc> ?<transient>?
119**
120*/
mistachkin7617e4a2016-07-28 17:11:20121static int SQLITE_TCLAPI test_translate(
danielk1977bfd6cce2004-06-18 04:24:54122 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
danielk19771ba1b552004-06-23 13:46:32131 char *z;
drh064b6812024-07-30 15:49:02132 Tcl_Size len;
danielk19771ba1b552004-06-23 13:46:32133 void (*xDel)(void *p) = SQLITE_STATIC;
danielk1977bfd6cce2004-06-18 04:24:54134
danielk19771ba1b552004-06-23 13:46:32135 if( objc!=4 && objc!=5 ){
danielk1977bfd6cce2004-06-18 04:24:54136 Tcl_AppendResult(interp, "wrong # args: should be \"",
137 Tcl_GetStringFromObj(objv[0], 0),
stephan065c0a62025-03-08 06:53:06138 " <string/blob> <from enc> <to enc>", NULL
danielk1977bfd6cce2004-06-18 04:24:54139 );
140 return TCL_ERROR;
141 }
danielk19771ba1b552004-06-23 13:46:32142 if( objc==5 ){
danielk19771e536952007-08-16 10:09:01143 xDel = sqlite3_free;
danielk19771ba1b552004-06-23 13:46:32144 }
danielk1977bfd6cce2004-06-18 04:24:54145
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
danielk19771e536952007-08-16 10:09:01151 pVal = sqlite3ValueNew(0);
danielk1977bfd6cce2004-06-18 04:24:54152
153 if( enc_from==SQLITE_UTF8 ){
154 z = Tcl_GetString(objv[1]);
danielk19771ba1b552004-06-23 13:46:32155 if( objc==5 ){
drhb9755982010-07-24 16:34:37156 z = sqlite3_mprintf("%s", z);
danielk19771ba1b552004-06-23 13:46:32157 }
drhb21c8cd2007-08-21 19:33:56158 sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
danielk1977bfd6cce2004-06-18 04:24:54159 }else{
drh03d847e2005-12-09 20:21:58160 z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len);
danielk19771ba1b552004-06-23 13:46:32161 if( objc==5 ){
162 char *zTmp = z;
drh064b6812024-07-30 15:49:02163 z = sqlite3_malloc64(len);
danielk19771ba1b552004-06-23 13:46:32164 memcpy(z, zTmp, len);
165 }
drhb21c8cd2007-08-21 19:33:56166 sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
danielk1977bfd6cce2004-06-18 04:24:54167 }
168
drhb21c8cd2007-08-21 19:33:56169 z = (char *)sqlite3ValueText(pVal, enc_to);
170 len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2);
drh03d847e2005-12-09 20:21:58171 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((u8*)z, len));
danielk1977bfd6cce2004-06-18 04:24:54172
173 sqlite3ValueFree(pVal);
174
175 return TCL_OK;
176}
177
178/*
179** Usage: translate_selftest
180**
drhee858132007-05-08 20:37:38181** Call sqlite3UtfSelfTest() to run the internal tests for unicode
danielk1977bfd6cce2004-06-18 04:24:54182** translation. If there is a problem an assert() will fail.
183**/
danielk197744a376f2008-08-12 15:04:58184void sqlite3UtfSelfTest(void);
mistachkin7617e4a2016-07-28 17:11:20185static int SQLITE_TCLAPI test_translate_selftest(
danielk1977bfd6cce2004-06-18 04:24:54186 void * clientData,
187 Tcl_Interp *interp,
188 int objc,
189 Tcl_Obj *CONST objv[]
190){
drh6c626082004-11-14 21:56:29191#ifndef SQLITE_OMIT_UTF16
drhee858132007-05-08 20:37:38192 sqlite3UtfSelfTest();
drh6c626082004-11-14 21:56:29193#endif
danielk1977bfd6cce2004-06-18 04:24:54194 return SQLITE_OK;
195}
196
danielk1977998b56c2004-05-06 23:37:52197
198/*
199** Register commands with the TCL interpreter.
200*/
201int Sqlitetest5_Init(Tcl_Interp *interp){
202 static struct {
203 char *zName;
danielk1977295ba552004-05-19 10:34:51204 Tcl_ObjCmdProc *xProc;
danielk1977998b56c2004-05-06 23:37:52205 } aCmd[] = {
danielk197724162fe2004-06-04 06:22:00206 { "binarize", (Tcl_ObjCmdProc*)binarize },
danielk19770202b292004-06-09 09:55:16207 { "test_value_overhead", (Tcl_ObjCmdProc*)test_value_overhead },
danielk1977bfd6cce2004-06-18 04:24:54208 { "test_translate", (Tcl_ObjCmdProc*)test_translate },
209 { "translate_selftest", (Tcl_ObjCmdProc*)test_translate_selftest},
danielk1977998b56c2004-05-06 23:37:52210 };
211 int i;
212 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
danielk1977295ba552004-05-19 10:34:51213 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
danielk1977998b56c2004-05-06 23:37:52214 }
danielk19770202b292004-06-09 09:55:16215 return SQLITE_OK;
danielk1977998b56c2004-05-06 23:37:52216}