os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test5.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
** 2001 September 15
sl@0
     3
**
sl@0
     4
** The author disclaims copyright to this source code.  In place of
sl@0
     5
** a legal notice, here is a blessing:
sl@0
     6
**
sl@0
     7
**    May you do good and not evil.
sl@0
     8
**    May you find forgiveness for yourself and forgive others.
sl@0
     9
**    May you share freely, never taking more than you give.
sl@0
    10
**
sl@0
    11
*************************************************************************
sl@0
    12
** Code for testing the utf.c module in SQLite.  This code
sl@0
    13
** is not included in the SQLite library.  It is used for automated
sl@0
    14
** testing of the SQLite library. Specifically, the code in this file
sl@0
    15
** is used for testing the SQLite routines for converting between
sl@0
    16
** the various supported unicode encodings.
sl@0
    17
**
sl@0
    18
** $Id: test5.c,v 1.22 2008/08/12 15:04:59 danielk1977 Exp $
sl@0
    19
*/
sl@0
    20
#include "sqliteInt.h"
sl@0
    21
#include "vdbeInt.h"
sl@0
    22
#include "tcl.h"
sl@0
    23
#include <stdlib.h>
sl@0
    24
#include <string.h>
sl@0
    25
sl@0
    26
/*
sl@0
    27
** The first argument is a TCL UTF-8 string. Return the byte array
sl@0
    28
** object with the encoded representation of the string, including
sl@0
    29
** the NULL terminator.
sl@0
    30
*/
sl@0
    31
static int binarize(
sl@0
    32
  void * clientData,
sl@0
    33
  Tcl_Interp *interp,
sl@0
    34
  int objc,
sl@0
    35
  Tcl_Obj *CONST objv[]
sl@0
    36
){
sl@0
    37
  int len;
sl@0
    38
  char *bytes;
sl@0
    39
  Tcl_Obj *pRet;
sl@0
    40
  assert(objc==2);
sl@0
    41
sl@0
    42
  bytes = Tcl_GetStringFromObj(objv[1], &len);
sl@0
    43
  pRet = Tcl_NewByteArrayObj((u8*)bytes, len+1);
sl@0
    44
  Tcl_SetObjResult(interp, pRet);
sl@0
    45
  return TCL_OK;
sl@0
    46
}
sl@0
    47
sl@0
    48
/*
sl@0
    49
** Usage: test_value_overhead <repeat-count> <do-calls>.
sl@0
    50
**
sl@0
    51
** This routine is used to test the overhead of calls to
sl@0
    52
** sqlite3_value_text(), on a value that contains a UTF-8 string. The idea
sl@0
    53
** is to figure out whether or not it is a problem to use sqlite3_value
sl@0
    54
** structures with collation sequence functions.
sl@0
    55
**
sl@0
    56
** If <do-calls> is 0, then the calls to sqlite3_value_text() are not
sl@0
    57
** actually made.
sl@0
    58
*/
sl@0
    59
static int test_value_overhead(
sl@0
    60
  void * clientData,
sl@0
    61
  Tcl_Interp *interp,
sl@0
    62
  int objc,
sl@0
    63
  Tcl_Obj *CONST objv[]
sl@0
    64
){
sl@0
    65
  int do_calls;
sl@0
    66
  int repeat_count;
sl@0
    67
  int i;
sl@0
    68
  Mem val;
sl@0
    69
  const char *zVal;
sl@0
    70
sl@0
    71
  if( objc!=3 ){
sl@0
    72
    Tcl_AppendResult(interp, "wrong # args: should be \"",
sl@0
    73
        Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", 0);
sl@0
    74
    return TCL_ERROR;
sl@0
    75
  }
sl@0
    76
sl@0
    77
  if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR;
sl@0
    78
  if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR;
sl@0
    79
sl@0
    80
  val.flags = MEM_Str|MEM_Term|MEM_Static;
sl@0
    81
  val.z = "hello world";
sl@0
    82
  val.type = SQLITE_TEXT;
sl@0
    83
  val.enc = SQLITE_UTF8;
sl@0
    84
sl@0
    85
  for(i=0; i<repeat_count; i++){
sl@0
    86
    if( do_calls ){
sl@0
    87
      zVal = (char*)sqlite3_value_text(&val);
sl@0
    88
    }
sl@0
    89
  }
sl@0
    90
sl@0
    91
  return TCL_OK;
sl@0
    92
}
sl@0
    93
sl@0
    94
static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){
sl@0
    95
  struct EncName {
sl@0
    96
    char *zName;
sl@0
    97
    u8 enc;
sl@0
    98
  } encnames[] = {
sl@0
    99
    { "UTF8", SQLITE_UTF8 },
sl@0
   100
    { "UTF16LE", SQLITE_UTF16LE },
sl@0
   101
    { "UTF16BE", SQLITE_UTF16BE },
sl@0
   102
    { "UTF16", SQLITE_UTF16 },
sl@0
   103
    { 0, 0 }
sl@0
   104
  };
sl@0
   105
  struct EncName *pEnc;
sl@0
   106
  char *z = Tcl_GetString(pObj);
sl@0
   107
  for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
sl@0
   108
    if( 0==sqlite3StrICmp(z, pEnc->zName) ){
sl@0
   109
      break;
sl@0
   110
    }
sl@0
   111
  }
sl@0
   112
  if( !pEnc->enc ){
sl@0
   113
    Tcl_AppendResult(interp, "No such encoding: ", z, 0);
sl@0
   114
  }
sl@0
   115
  if( pEnc->enc==SQLITE_UTF16 ){
sl@0
   116
    return SQLITE_UTF16NATIVE;
sl@0
   117
  }
sl@0
   118
  return pEnc->enc;
sl@0
   119
}
sl@0
   120
sl@0
   121
/*
sl@0
   122
** Usage:   test_translate <string/blob> <from enc> <to enc> ?<transient>?
sl@0
   123
**
sl@0
   124
*/
sl@0
   125
static int test_translate(
sl@0
   126
  void * clientData,
sl@0
   127
  Tcl_Interp *interp,
sl@0
   128
  int objc,
sl@0
   129
  Tcl_Obj *CONST objv[]
sl@0
   130
){
sl@0
   131
  u8 enc_from;
sl@0
   132
  u8 enc_to;
sl@0
   133
  sqlite3_value *pVal;
sl@0
   134
sl@0
   135
  char *z;
sl@0
   136
  int len;
sl@0
   137
  void (*xDel)(void *p) = SQLITE_STATIC;
sl@0
   138
sl@0
   139
  if( objc!=4 && objc!=5 ){
sl@0
   140
    Tcl_AppendResult(interp, "wrong # args: should be \"",
sl@0
   141
        Tcl_GetStringFromObj(objv[0], 0), 
sl@0
   142
        " <string/blob> <from enc> <to enc>", 0
sl@0
   143
    );
sl@0
   144
    return TCL_ERROR;
sl@0
   145
  }
sl@0
   146
  if( objc==5 ){
sl@0
   147
    xDel = sqlite3_free;
sl@0
   148
  }
sl@0
   149
sl@0
   150
  enc_from = name_to_enc(interp, objv[2]);
sl@0
   151
  if( !enc_from ) return TCL_ERROR;
sl@0
   152
  enc_to = name_to_enc(interp, objv[3]);
sl@0
   153
  if( !enc_to ) return TCL_ERROR;
sl@0
   154
sl@0
   155
  pVal = sqlite3ValueNew(0);
sl@0
   156
sl@0
   157
  if( enc_from==SQLITE_UTF8 ){
sl@0
   158
    z = Tcl_GetString(objv[1]);
sl@0
   159
    if( objc==5 ){
sl@0
   160
      z = sqlite3DbStrDup(0, z);
sl@0
   161
    }
sl@0
   162
    sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
sl@0
   163
  }else{
sl@0
   164
    z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len);
sl@0
   165
    if( objc==5 ){
sl@0
   166
      char *zTmp = z;
sl@0
   167
      z = sqlite3_malloc(len);
sl@0
   168
      memcpy(z, zTmp, len);
sl@0
   169
    }
sl@0
   170
    sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel);
sl@0
   171
  }
sl@0
   172
sl@0
   173
  z = (char *)sqlite3ValueText(pVal, enc_to);
sl@0
   174
  len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2);
sl@0
   175
  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((u8*)z, len));
sl@0
   176
sl@0
   177
  sqlite3ValueFree(pVal);
sl@0
   178
sl@0
   179
  return TCL_OK;
sl@0
   180
}
sl@0
   181
sl@0
   182
/*
sl@0
   183
** Usage: translate_selftest
sl@0
   184
**
sl@0
   185
** Call sqlite3UtfSelfTest() to run the internal tests for unicode
sl@0
   186
** translation. If there is a problem an assert() will fail.
sl@0
   187
**/
sl@0
   188
void sqlite3UtfSelfTest(void);
sl@0
   189
static int test_translate_selftest(
sl@0
   190
  void * clientData,
sl@0
   191
  Tcl_Interp *interp,
sl@0
   192
  int objc,
sl@0
   193
  Tcl_Obj *CONST objv[]
sl@0
   194
){
sl@0
   195
#ifndef SQLITE_OMIT_UTF16
sl@0
   196
  sqlite3UtfSelfTest();
sl@0
   197
#endif
sl@0
   198
  return SQLITE_OK;
sl@0
   199
}
sl@0
   200
sl@0
   201
sl@0
   202
/*
sl@0
   203
** Register commands with the TCL interpreter.
sl@0
   204
*/
sl@0
   205
int Sqlitetest5_Init(Tcl_Interp *interp){
sl@0
   206
  static struct {
sl@0
   207
    char *zName;
sl@0
   208
    Tcl_ObjCmdProc *xProc;
sl@0
   209
  } aCmd[] = {
sl@0
   210
    { "binarize",                (Tcl_ObjCmdProc*)binarize },
sl@0
   211
    { "test_value_overhead",     (Tcl_ObjCmdProc*)test_value_overhead },
sl@0
   212
    { "test_translate",          (Tcl_ObjCmdProc*)test_translate     },
sl@0
   213
    { "translate_selftest",      (Tcl_ObjCmdProc*)test_translate_selftest},
sl@0
   214
  };
sl@0
   215
  int i;
sl@0
   216
  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
sl@0
   217
    Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
sl@0
   218
  }
sl@0
   219
  return SQLITE_OK;
sl@0
   220
}