SQLite4
Check-in [5ab129f59e]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add missing file test_num.c.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5ab129f59e23fa307283249b1d977ad8bb963628
User & Date: dan 2013-06-01 06:08:26
Context
2013-06-01
20:34
Use decimal arithmetic in the sum(), total() and avg() functions. check-in: 23ded9b859 user: dan tags: trunk
06:08
Add missing file test_num.c. check-in: 5ab129f59e user: dan tags: trunk
2013-05-31
19:57
Fix an issue in sqlite4_num_from_text() when parsing "inf" values. check-in: d1792cbf3c user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added test/test_num.c.



















































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
** 2013 May 24
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing the sqlite4_num_xxx() functions used by SQLite for
** base 10 arithmatic.
*/

#include "sqlite4.h"
#include <tcl.h>

#define NUM_FORMAT "sign:%d approx:%d e:%d m:%llu"

/* Append a return value representing a sqlite4_num.
*/
static void append_num_result( Tcl_Interp *interp, sqlite4_num A ){
  char buf[100];
  sprintf( buf, NUM_FORMAT, A.sign, A.approx, A.e, A.m );
  Tcl_AppendResult(interp, buf, 0);
}

/* Convert a string either representing a sqlite4_num (listing its fields as
** returned by append_num_result) or that can be parsed as one. Invalid
** strings become NaN.
*/
static sqlite4_num test_parse_num( char *arg ){
  sqlite4_num A;
  int sign, approx, e;
  if( sscanf( arg, NUM_FORMAT, &sign, &approx, &e, &A.m)==4 ){
    A.sign = sign;
    A.approx = approx;
    A.e = e;
    return A;
  } else {
    return sqlite4_num_from_text(arg, -1, 0, 0);
  }
}

/* Convert return values of sqlite4_num to strings that will be readable in
** the tests.
*/
static char *describe_num_comparison( int code ){
  switch( code ){
    case 0: return "incomparable";
    case 1: return "lesser";
    case 2: return "equal";
    case 3: return "greater";
    default: return "error"; 
  }
}

/* Compare two numbers A and B. Returns "incomparable", "lesser", "equal",
** "greater", or "error".
*/
static int test_num_compare(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  sqlite4_num A, B;
  int cmp;
  if( argc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
       " NUM NUM\"", 0);
    return TCL_ERROR;
  }
  
  A = test_parse_num( argv[1] );
  B = test_parse_num( argv[2] );
  cmp = sqlite4_num_compare(A, B);
  Tcl_AppendResult( interp, describe_num_comparison( cmp ), 0);
  return TCL_OK; 
}

/* Create a sqlite4_num from a string. The optional second argument specifies
** how many bytes may be read.
*/
static int test_num_from_text(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  sqlite4_num A;
  int len;
  if( argc!=2 && argc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
      " STRING\" or \"", argv[0], " STRING INTEGER\"", 0);
    return TCL_ERROR;
  }

  if( argc==3 ){
    if ( Tcl_GetInt(interp, argv[2], &len) ) return TCL_ERROR; 
  }else{
    len = -1;
  }

  A = sqlite4_num_from_text(argv[1], len, 0, 0);
  append_num_result(interp, A);
  return TCL_OK;
}

static int test_num_to_text(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  char text[30];
  if( argc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
      " NUM\"", 0);
    return TCL_ERROR;
  }
  sqlite4_num_to_text( test_parse_num( argv[1] ), text, 0 );
  Tcl_AppendResult( interp, text, 0 );
  return TCL_OK;
}

static int test_num_binary_op(
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv,            /* Text of each argument */
  sqlite4_num (*op) (sqlite4_num, sqlite4_num)
){
  sqlite4_num A, B, R;
  if( argc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
      " NUM NUM\"", 0);
    return TCL_ERROR;
  }
  A = test_parse_num(argv[1]);
  B = test_parse_num(argv[2]);
  R = op(A, B);
  append_num_result(interp, R);
  return TCL_OK;
}

static int test_num_add(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_binary_op( interp, argc, argv, sqlite4_num_add );
}

static int test_num_sub(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_binary_op( interp, argc, argv, sqlite4_num_sub );
}

static int test_num_mul(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_binary_op( interp, argc, argv, sqlite4_num_mul );
}

static int test_num_div(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_binary_op( interp, argc, argv, sqlite4_num_div );
}

static int test_num_predicate(
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv,            /* Text of each argument */
  int (*pred) (sqlite4_num)
){
  sqlite4_num A;
  if( argc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
      " NUM\"", 0);
    return TCL_ERROR;
  }
  A = test_parse_num(argv[1]);
  Tcl_AppendResult(interp, pred(A) ? "true" : "false", 0);  
  return TCL_OK;
}

static int test_num_isinf(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_predicate( interp, argc, argv, sqlite4_num_isinf );
}

static int test_num_isnan(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  return test_num_predicate( interp, argc, argv, sqlite4_num_isnan );
}


static int test_num_from_double(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_num ret;
  double val;
  
  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "NUMBER");
    return TCL_ERROR;
  }
  if( Tcl_GetDoubleFromObj(interp, objv[1], &val) ){
    return TCL_ERROR;
  }

  ret = sqlite4_num_from_double(val);

  Tcl_ResetResult(interp);
  append_num_result(interp, ret);
  return TCL_OK;
}

/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_num_init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_CmdProc *xProc;
  } aCmd[] = {
     { "sqlite4_num_compare",           (Tcl_CmdProc*)test_num_compare      }, 
     { "sqlite4_num_from_text",         (Tcl_CmdProc*)test_num_from_text    }, 
     { "sqlite4_num_to_text",           (Tcl_CmdProc*)test_num_to_text      },
     { "sqlite4_num_add",               (Tcl_CmdProc*)test_num_add          },
     { "sqlite4_num_sub",               (Tcl_CmdProc*)test_num_sub          },
     { "sqlite4_num_mul",               (Tcl_CmdProc*)test_num_mul          },
     { "sqlite4_num_div",               (Tcl_CmdProc*)test_num_div          },
     { "sqlite4_num_isinf",             (Tcl_CmdProc*)test_num_isinf        },
     { "sqlite4_num_isnan",             (Tcl_CmdProc*)test_num_isnan        },
  };

  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
     void *clientData;
  } aObjCmd[] = {
     { "sqlite4_num_from_double", test_num_from_double, 0 },
  };
  int i;

  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
    Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  }
  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
  }

  return TCL_OK;
}