SQLite4
Check-in [6cc07d19e4]
Not logged in

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

Overview
Comment:Add a few sqlite4_num tests.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | num_work
Files: files | file ages | folders
SHA1: 6cc07d19e4c2f520d14bd233303039d1f1bd57d2
User & Date: peterreid 2013-02-10 04:06:44
Context
2013-02-10
04:26
Add a failing test and supporting functions. When the buffer is ended by a passed-in byte count instead of a trailing 0, sqlite4_num_from_text returns NaN check-in: 2cff3b428a user: peterreid tags: num_work
04:06
Add a few sqlite4_num tests. check-in: 6cc07d19e4 user: peterreid tags: num_work
2013-02-09
05:36
Create sqlite4_num_isinf check-in: 555cdfbf52 user: peterreid tags: num_work
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added test/num.test.

            1  +# 2001 September 15
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is testing the sqlite_*_printf() interface.
           13  +#
           14  +# $Id: printf.test,v 1.31 2009/02/01 00:21:10 drh Exp $
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +
           19  +do_test num-equal-1.1.1 {
           20  +  sqlite4_num_compare 20 20 
           21  +} {equal}
           22  +do_test num-equal-1.1.2 {
           23  +  sqlite4_num_compare 20 2e1
           24  +} {equal}
           25  +do_test num-equal-1.1.3 {
           26  +  sqlite4_num_compare -00034 -3.4e1
           27  +} {equal}
           28  +# Is +0 > -0?
           29  +#do_test num-equal-1.1.4 {
           30  +#  sqlite4_num_compare +0 -0
           31  +#} {equal}
           32  +
           33  +finish_test

Changes to test/test_main.c.

  4345   4345         Tcl_AppendResult(interp, " ", aOpt[i].zOptName);
  4346   4346       }
  4347   4347       return TCL_ERROR;
  4348   4348     }
  4349   4349     sqlite4_test_control(SQLITE4_TESTCTRL_OPTIMIZATIONS, db, mask);
  4350   4350     return TCL_OK;
  4351   4351   }
         4352  +
         4353  +#define NUM_FORMAT "sign:%d approx:%d e:%d m:%lld"
         4354  +
         4355  +/* Append a return value representing a sqlite4_num.
         4356  +*/
         4357  +static void append_num_result( Tcl_Interp *interp, sqlite4_num A ){
         4358  +  char buf[100];
         4359  +  sprintf( buf, NUM_FORMAT, A.sign, A.approx, A.e, A.m );
         4360  +  Tcl_AppendResult(interp, buf, 0);
         4361  +}
         4362  +
         4363  +/* Convert a string either representing a sqlite4_num (listing its fields as
         4364  +** returned by append_num_result) or that can be parsed as one. Invalid
         4365  +** strings become NaN.
         4366  +*/
         4367  +static sqlite4_num test_parse_num( char *arg ){
         4368  +  sqlite4_num A;
         4369  +  int sign, approx, e;
         4370  +  if( sscanf( arg, NUM_FORMAT, &sign, &approx, &e, &A.m)==4 ){
         4371  +    A.sign = sign;
         4372  +    A.approx = approx;
         4373  +    A.e = e;
         4374  +    return A;
         4375  +  } else {
         4376  +    return sqlite4_num_from_text(arg, -1, 0);
         4377  +  }
         4378  +}
         4379  +
         4380  +/* Convert return values of sqlite4_num to strings that will be readable in
         4381  +** the tests.
         4382  +*/
         4383  +static char *describe_num_comparison( int code ){
         4384  +  switch( code ){
         4385  +    case 0: return "incomparable";
         4386  +    case 1: return "lesser";
         4387  +    case 2: return "equal";
         4388  +    case 3: return "greater";
         4389  +    default: return "error"; 
         4390  +  }
         4391  +}
         4392  +
         4393  +/* Compare two numbers A and B. Returns "incomparable", "lesser", "equal",
         4394  +** "greater", or "error".
         4395  +*/
         4396  +static int test_num_compare(
         4397  +  void *NotUsed,
         4398  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         4399  +  int argc,              /* Number of arguments */
         4400  +  char **argv            /* Text of each argument */
         4401  +){
         4402  +  sqlite4_num A, B;
         4403  +  int cmp;
         4404  +  if( argc!=3 ){
         4405  +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
         4406  +       " NUM NUM\"", 0);
         4407  +    return TCL_ERROR;
         4408  +  }
         4409  +  
         4410  +  A = test_parse_num( argv[1] );
         4411  +  B = test_parse_num( argv[2] );
         4412  +  cmp = sqlite4_num_compare(A, B);
         4413  +  Tcl_AppendResult( interp, describe_num_comparison( cmp ), 0);
         4414  +  return TCL_OK; 
         4415  +}
         4416  +
         4417  +/* Create a sqlite4_num from a string. The optional second argument specifies
         4418  +** how many bytes may be read.
         4419  +*/
         4420  +static int test_num_from_text(
         4421  +  void *NotUsed,
         4422  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         4423  +  int argc,              /* Number of arguments */
         4424  +  char **argv            /* Text of each argument */
         4425  +){
         4426  +  sqlite4_num A;
         4427  +  int len;
         4428  +  if( argc!=2 && argc!=3 ){
         4429  +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
         4430  +      " STRING\" or \"", argv[0], " STRING INTEGER\"", 0);
         4431  +    return TCL_ERROR;
         4432  +  }
         4433  +
         4434  +  if( argc==3 ){
         4435  +    if ( Tcl_GetInt(interp, argv[2], &len) ) return TCL_ERROR; 
         4436  +  }else{
         4437  +    len = -1;
         4438  +  }
         4439  +
         4440  +  A = sqlite4_num_from_text( argv[1], len, 0 );
         4441  +  append_num_result(interp, A);
         4442  +  return TCL_OK;
         4443  +}
  4352   4444   
  4353   4445   /*
  4354   4446   ** Register commands with the TCL interpreter.
  4355   4447   */
  4356   4448   int Sqlitetest1_Init(Tcl_Interp *interp){
  4357   4449     extern int sqlite4_search_count;
  4358   4450     extern int sqlite4_found_count;
................................................................................
  4398   4490        { "sqlite_set_magic",              (Tcl_CmdProc*)sqlite_set_magic      },
  4399   4491        { "sqlite4_interrupt",             (Tcl_CmdProc*)test_interrupt        },
  4400   4492        { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
  4401   4493        { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
  4402   4494        { "sqlite4_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
  4403   4495        { "sqlite4_stack_used",            (Tcl_CmdProc*)test_stack_used       },
  4404   4496        { "printf",                        (Tcl_CmdProc*)test_printf           },
  4405         -     { "sqlite4IoTrace",              (Tcl_CmdProc*)test_io_trace         },
         4497  +     { "sqlite4IoTrace",                (Tcl_CmdProc*)test_io_trace         },
         4498  +     { "sqlite4_num_compare",           (Tcl_CmdProc*)test_num_compare      }, 
         4499  +     { "sqlite4_num_from_text",         (Tcl_CmdProc*)test_num_from_text    }, 
  4406   4500     };
  4407   4501     static struct {
  4408   4502        char *zName;
  4409   4503        Tcl_ObjCmdProc *xProc;
  4410   4504        void *clientData;
  4411   4505     } aObjCmd[] = {
  4412   4506        { "sqlite4_connection_pointer",    get_sqlite_pointer, 0 },