/ Check-in [319bb4a9]
Login

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

Overview
Comment:Tests to improve coverage of vdbemem.c. (CVS 2200)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 319bb4a9064deb062a888fdc31067619c9b749bb
User & Date: danielk1977 2005-01-12 07:15:05
Context
2005-01-12
09:10
Tests to improve coverage of vdbeaux.c. (CVS 2201) check-in: 2b3e21ce user: danielk1977 tags: trunk
07:15
Tests to improve coverage of vdbemem.c. (CVS 2200) check-in: 319bb4a9 user: danielk1977 tags: trunk
00:08
Fix a bug in tclsqlite.c. (CVS 2199) check-in: 50f1e229 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.230 2005/01/11 10:25:07 danielk1977 Exp $
           12  +** $Id: btree.c,v 1.231 2005/01/12 07:15:05 danielk1977 Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
  4739   4739   ** is currently pointing to.
  4740   4740   */
  4741   4741   int sqlite3BtreeFlags(BtCursor *pCur){
  4742   4742     MemPage *pPage = pCur->pPage;
  4743   4743     return pPage ? pPage->aData[pPage->hdrOffset] : 0;
  4744   4744   }
  4745   4745   
         4746  +#ifdef SQLITE_DEBUG
  4746   4747   /*
  4747   4748   ** Print a disassembly of the given page on standard output.  This routine
  4748   4749   ** is used for debugging and testing only.
  4749   4750   */
  4750         -#ifdef SQLITE_TEST
  4751         -#ifndef SQLITE_OMIT_BTREEPAGEDUMP
  4752   4751   static int btreePageDump(Btree *pBt, int pgno, int recursive, MemPage *pParent){
  4753   4752     int rc;
  4754   4753     MemPage *pPage;
  4755   4754     int i, j, c;
  4756   4755     int nFree;
  4757   4756     u16 idx;
  4758   4757     int hdr;
................................................................................
  4841   4840       btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
  4842   4841     }
  4843   4842     pPage->isInit = isInit;
  4844   4843     sqlite3pager_unref(data);
  4845   4844     fflush(stdout);
  4846   4845     return SQLITE_OK;
  4847   4846   }
  4848         -#endif
  4849   4847   int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){
  4850         -#ifndef SQLITE_OMIT_BTREEPAGEDUMP
  4851   4848     return btreePageDump(pBt, pgno, recursive, 0);
  4852         -#endif
  4853   4849   }
  4854   4850   #endif
  4855   4851   
  4856   4852   #ifdef SQLITE_TEST
  4857   4853   /*
  4858   4854   ** Fill aResult[] with information about the entry and page that the
  4859   4855   ** cursor is pointing to.

Changes to src/btree.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite B-Tree file
    13     13   ** subsystem.  See comments in the source code for a detailed description
    14     14   ** of what each interface routine does.
    15     15   **
    16         -** @(#) $Id: btree.h,v 1.60 2004/11/05 15:45:10 danielk1977 Exp $
           16  +** @(#) $Id: btree.h,v 1.61 2005/01/12 07:15:05 danielk1977 Exp $
    17     17   */
    18     18   #ifndef _BTREE_H_
    19     19   #define _BTREE_H_
    20     20   
    21     21   /* TODO: This definition is just included so other modules compile. It
    22     22   ** needs to be revisited.
    23     23   */
................................................................................
   123    123   char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot);
   124    124   struct Pager *sqlite3BtreePager(Btree*);
   125    125   
   126    126   
   127    127   #ifdef SQLITE_TEST
   128    128   int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
   129    129   void sqlite3BtreeCursorList(Btree*);
   130         -int sqlite3BtreePageDump(Btree*, int, int recursive);
   131    130   #endif
   132    131   
          132  +#ifdef SQLITE_DEBUG
          133  +int sqlite3BtreePageDump(Btree*, int, int recursive);
          134  +#else
          135  +#define sqlite3BtreePageDump(X,Y,Z) SQLITE_OK
          136  +#endif
   133    137   
   134    138   #endif /* _BTREE_H_ */

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.351 2005/01/08 18:42:28 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.352 2005/01/12 07:15:05 danielk1977 Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** These #defines should enable >2GB file support on Posix if the
    21     21   ** underlying operating system supports it.  If the OS lacks
................................................................................
   227    227   **
   228    228   **      -DSQLITE_DEBUG=2
   229    229   **
   230    230   ** and a line of text will be written to standard error for
   231    231   ** each malloc() and free().  This output can be analyzed
   232    232   ** by an AWK script to determine if there are any leaks.
   233    233   */
   234         -#ifdef SQLITE_DEBUG
          234  +#ifdef SQLITE_TEST
   235    235   # define sqliteMalloc(X)    sqlite3Malloc_(X,1,__FILE__,__LINE__)
   236    236   # define sqliteMallocRaw(X) sqlite3Malloc_(X,0,__FILE__,__LINE__)
   237    237   # define sqliteFree(X)      sqlite3Free_(X,__FILE__,__LINE__)
   238    238   # define sqliteRealloc(X,Y) sqlite3Realloc_(X,Y,__FILE__,__LINE__)
   239    239   # define sqliteStrDup(X)    sqlite3StrDup_(X,__FILE__,__LINE__)
   240    240   # define sqliteStrNDup(X,Y) sqlite3StrNDup_(X,Y,__FILE__,__LINE__)
   241    241   #else
................................................................................
   253    253   */
   254    254   extern int sqlite3_malloc_failed;
   255    255   
   256    256   /*
   257    257   ** The following global variables are used for testing and debugging
   258    258   ** only.  They only work if SQLITE_DEBUG is defined.
   259    259   */
   260         -#ifdef SQLITE_DEBUG
          260  +#ifdef SQLITE_TEST
   261    261   extern int sqlite3_nMalloc;      /* Number of sqliteMalloc() calls */
   262    262   extern int sqlite3_nFree;        /* Number of sqliteFree() calls */
   263    263   extern int sqlite3_iMallocFail;  /* Fail sqliteMalloc() after this many calls */
   264    264   extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */
   265    265   #endif
   266    266   
   267    267   /*
................................................................................
  1276   1276   int sqlite3StrICmp(const char *, const char *);
  1277   1277   int sqlite3StrNICmp(const char *, const char *, int);
  1278   1278   int sqlite3HashNoCase(const char *, int);
  1279   1279   int sqlite3IsNumber(const char*, int*, u8);
  1280   1280   int sqlite3Compare(const char *, const char *);
  1281   1281   int sqlite3SortCompare(const char *, const char *);
  1282   1282   void sqlite3RealToSortable(double r, char *);
  1283         -#ifdef SQLITE_DEBUG
         1283  +#ifdef SQLITE_TEST
  1284   1284     void *sqlite3Malloc_(int,int,char*,int);
  1285   1285     void sqlite3Free_(void*,char*,int);
  1286   1286     void *sqlite3Realloc_(void*,int,char*,int);
  1287   1287     char *sqlite3StrDup_(const char*,char*,int);
  1288   1288     char *sqlite3StrNDup_(const char*, int,char*,int);
  1289   1289     void sqlite3CheckMemory(void*,int);
  1290   1290   #else

Changes to src/test1.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Code for testing the printf() interface to SQLite.  This code
    13     13   ** is not included in the SQLite library.  It is used for automated
    14     14   ** testing of the SQLite library.
    15     15   **
    16         -** $Id: test1.c,v 1.119 2005/01/11 16:54:15 drh Exp $
           16  +** $Id: test1.c,v 1.120 2005/01/12 07:15:05 danielk1977 Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include "os.h"
    21     21   #include <stdlib.h>
    22     22   #include <string.h>
    23     23   
................................................................................
   752    752   ** after that.  If REPEAT-INTERVAL is 0 or is omitted, then only a single
   753    753   ** malloc will fail.  If REPEAT-INTERVAL is 1 then all mallocs after the
   754    754   ** first failure will continue to fail on every call.  If REPEAT-INTERVAL is
   755    755   ** 2 then every other malloc will fail.  And so forth.
   756    756   **
   757    757   ** Turn off this mechanism and reset the sqlite3_malloc_failed variable is N==0.
   758    758   */
   759         -#ifdef SQLITE_DEBUG
          759  +#ifdef SQLITE_TEST
   760    760   static int sqlite_malloc_fail(
   761    761     void *NotUsed,
   762    762     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   763    763     int argc,              /* Number of arguments */
   764    764     char **argv            /* Text of each argument */
   765    765   ){
   766    766     int n;
................................................................................
   783    783   #endif
   784    784   
   785    785   /*
   786    786   ** Usage: sqlite_malloc_stat
   787    787   **
   788    788   ** Return the number of prior calls to sqliteMalloc() and sqliteFree().
   789    789   */
   790         -#ifdef SQLITE_DEBUG
          790  +#ifdef SQLITE_TEST
   791    791   static int sqlite_malloc_stat(
   792    792     void *NotUsed,
   793    793     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   794    794     int argc,              /* Number of arguments */
   795    795     char **argv            /* Text of each argument */
   796    796   ){
   797    797     char zBuf[200];
................................................................................
  2745   2745        { "sqlite3_exec_printf",           (Tcl_CmdProc*)test_exec_printf      },
  2746   2746        { "sqlite3_get_table_printf",      (Tcl_CmdProc*)test_get_table_printf },
  2747   2747        { "sqlite3_close",                 (Tcl_CmdProc*)sqlite_test_close     },
  2748   2748        { "sqlite3_create_function",       (Tcl_CmdProc*)test_create_function  },
  2749   2749        { "sqlite3_create_aggregate",      (Tcl_CmdProc*)test_create_aggregate },
  2750   2750        { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func    },
  2751   2751        { "sqlite_abort",                  (Tcl_CmdProc*)sqlite_abort          },
  2752         -#ifdef SQLITE_DEBUG
         2752  +#ifdef SQLITE_TEST
  2753   2753        { "sqlite_malloc_fail",            (Tcl_CmdProc*)sqlite_malloc_fail    },
  2754   2754        { "sqlite_malloc_stat",            (Tcl_CmdProc*)sqlite_malloc_stat    },
  2755   2755   #endif
  2756   2756        { "sqlite_bind",                   (Tcl_CmdProc*)test_bind             },
  2757   2757        { "breakpoint",                    (Tcl_CmdProc*)test_breakpoint       },
  2758   2758        { "sqlite3_key",                   (Tcl_CmdProc*)test_key              },
  2759   2759        { "sqlite3_rekey",                 (Tcl_CmdProc*)test_rekey            },

Changes to src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.30 2004/11/14 21:56:30 drh Exp $
           15  +** $Id: utf.c,v 1.31 2005/01/12 07:15:06 danielk1977 Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
   248    248     int c;
   249    249   
   250    250     assert( pMem->flags&MEM_Str );
   251    251     assert( pMem->enc!=desiredEnc );
   252    252     assert( pMem->enc!=0 );
   253    253     assert( pMem->n>=0 );
   254    254   
   255         -#ifdef TRANSLATE_TRACE
          255  +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   256    256     {
   257    257       char zBuf[100];
   258    258       sqlite3VdbeMemPrettyPrint(pMem, zBuf, 100);
   259    259       fprintf(stderr, "INPUT:  %s\n", zBuf);
   260    260     }
   261    261   #endif
   262    262   
................................................................................
   364    364       pMem->flags |= (MEM_Term|MEM_Short);
   365    365     }else{
   366    366       pMem->flags |= (MEM_Term|MEM_Dyn);
   367    367     }
   368    368     pMem->z = zOut;
   369    369   
   370    370   translate_out:
   371         -#ifdef TRANSLATE_TRACE
          371  +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   372    372     {
   373    373       char zBuf[100];
   374    374       sqlite3VdbeMemPrettyPrint(pMem, zBuf, 100);
   375    375       fprintf(stderr, "OUTPUT: %s\n", zBuf);
   376    376     }
   377    377   #endif
   378    378     return SQLITE_OK;

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.124 2005/01/11 15:28:33 drh Exp $
           17  +** $Id: util.c,v 1.125 2005/01/12 07:15:06 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdarg.h>
    21     21   #include <ctype.h>
    22     22   
    23     23   #if SQLITE_DEBUG>2 && defined(__GLIBC__)
    24     24   #include <execinfo.h>
................................................................................
    40     40   /*
    41     41   ** If malloc() ever fails, this global variable gets set to 1.
    42     42   ** This causes the library to abort and never again function.
    43     43   */
    44     44   int sqlite3_malloc_failed = 0;
    45     45   
    46     46   /*
    47         -** If SQLITE_DEBUG is defined, then use versions of malloc() and
           47  +** If SQLITE_TEST is defined, then use versions of malloc() and
    48     48   ** free() that track memory usage and check for buffer overruns.
    49     49   */
    50         -#ifdef SQLITE_DEBUG
           50  +#ifdef SQLITE_TEST
    51     51   
    52     52   /*
    53     53   ** For keeping track of the number of mallocs and frees.   This
    54     54   ** is used to check for memory leaks.  The iMallocFail and iMallocReset
    55     55   ** values are used to simulate malloc() failures during testing in 
    56     56   ** order to verify that the library correctly handles an out-of-memory
    57     57   ** condition.
................................................................................
   247    247   }
   248    248   #endif /* SQLITE_DEBUG */
   249    249   
   250    250   /*
   251    251   ** The following versions of malloc() and free() are for use in a
   252    252   ** normal build.
   253    253   */
   254         -#if !defined(SQLITE_DEBUG)
          254  +#if !defined(SQLITE_TEST)
   255    255   
   256    256   /*
   257    257   ** Allocate new memory and set it to zero.  Return NULL if
   258    258   ** no memory is available.  See also sqliteMallocRaw().
   259    259   */
   260    260   void *sqlite3Malloc(int n){
   261    261     void *p;
................................................................................
   325    325     zNew = sqliteMallocRaw(n+1);
   326    326     if( zNew ){
   327    327       memcpy(zNew, z, n);
   328    328       zNew[n] = 0;
   329    329     }
   330    330     return zNew;
   331    331   }
   332         -#endif /* !defined(SQLITE_DEBUG) */
          332  +#endif /* !defined(SQLITE_TEST) */
   333    333   
   334    334   /*
   335    335   ** Create a string from the 2nd and subsequent arguments (up to the
   336    336   ** first NULL argument), store the string in memory obtained from
   337    337   ** sqliteMalloc() and make the pointer indicated by the 1st argument
   338    338   ** point to that string.  The 1st argument must either be NULL or 
   339    339   ** point to memory obtained from sqliteMalloc().

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.440 2005/01/11 13:02:34 danielk1977 Exp $
           46  +** $Id: vdbe.c,v 1.441 2005/01/12 07:15:06 danielk1977 Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   299    299             pRec->flags |= MEM_Int;
   300    300           }
   301    301         }
   302    302       }
   303    303     }
   304    304   }
   305    305   
   306         -#ifndef NDEBUG
          306  +#ifdef SQLITE_DEBUG
   307    307   /*
   308    308   ** Write a nice string representation of the contents of cell pMem
   309    309   ** into buffer zBuf, length nBuf.
   310    310   */
   311    311   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf, int nBuf){
   312    312     char *zCsr = zBuf;
   313    313     int f = pMem->flags;
................................................................................
  4515   4515       if( pTos>=p->aStack ){
  4516   4516         sqlite3VdbeMemSanity(pTos, db->enc);
  4517   4517       }
  4518   4518       if( pc<-1 || pc>=p->nOp ){
  4519   4519         sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0);
  4520   4520         rc = SQLITE_INTERNAL;
  4521   4521       }
         4522  +#ifdef SQLITE_DEBUG
         4523  +    /* Code for tracing the vdbe stack. */
  4522   4524       if( p->trace && pTos>=p->aStack ){
  4523   4525         int i;
  4524   4526         fprintf(p->trace, "Stack:");
  4525   4527         for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
  4526   4528           if( pTos[i].flags & MEM_Null ){
  4527   4529             fprintf(p->trace, " NULL");
  4528   4530           }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
................................................................................
  4537   4539             fprintf(p->trace, " ");
  4538   4540             fprintf(p->trace, "%s", zBuf);
  4539   4541           }
  4540   4542         }
  4541   4543         if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
  4542   4544         fprintf(p->trace,"\n");
  4543   4545       }
  4544         -#endif
         4546  +#endif  /* SQLITE_DEBUG */
         4547  +#endif  /* NDEBUG */
  4545   4548     }  /* The end of the for(;;) loop the loops through opcodes */
  4546   4549   
  4547   4550     /* If we reach this point, it means that execution is finished.
  4548   4551     */
  4549   4552   vdbe_halt:
  4550   4553     if( rc ){
  4551   4554       p->rc = rc;

Changes to src/vdbemem.c.

   390    390       pMem->xDel = xDel;
   391    391     }
   392    392   
   393    393     pMem->enc = enc;
   394    394     pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
   395    395     pMem->n = n;
   396    396   
          397  +  assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE 
          398  +      || enc==SQLITE_UTF16BE );
   397    399     switch( enc ){
   398    400       case 0:
   399    401         pMem->flags |= MEM_Blob;
   400    402         break;
   401    403   
   402    404       case SQLITE_UTF8:
   403    405         pMem->flags |= MEM_Str;
................................................................................
   414    416         if( pMem->n<0 ){
   415    417           pMem->n = sqlite3utf16ByteLen(pMem->z,-1);
   416    418           pMem->flags |= MEM_Term;
   417    419         }
   418    420         if( sqlite3VdbeMemHandleBom(pMem) ){
   419    421           return SQLITE_NOMEM;
   420    422         }
   421         -      break;
   422    423   #endif /* SQLITE_OMIT_UTF16 */
   423         -
   424         -    default:
   425         -      assert(0);
   426    424     }
   427    425     if( pMem->flags&MEM_Ephem ){
   428    426       return sqlite3VdbeMemMakeWriteable(pMem);
   429    427     }
   430    428     return SQLITE_OK;
   431    429   }
   432    430   

Changes to test/all.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file runs all tests.
    12     12   #
    13         -# $Id: all.test,v 1.25 2005/01/08 02:35:44 danielk1977 Exp $
           13  +# $Id: all.test,v 1.26 2005/01/12 07:15:07 danielk1977 Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   rename finish_test really_finish_test
    18     18   proc finish_test {} {memleak_check}
    19     19   
    20     20   if {[file exists ./sqlite_test_count]} {
................................................................................
    51     51     all.test
    52     52     crash.test
    53     53     autovacuum_crash.test
    54     54     quick.test
    55     55     malloc.test
    56     56     misuse.test
    57     57     memleak.test
    58         -  corrupt.test
    59     58   }
    60     59   
    61     60   # Test files btree2.test and btree4.test don't work if the 
    62     61   # SQLITE_DEFAULT_AUTOVACUUM macro is defined to true (because they depend
    63     62   # on tables being allocated starting at page 2).
    64     63   #
    65     64   ifcapable default_autovacuum {

Changes to test/malloc.test.

    10     10   #***********************************************************************
    11     11   # This file attempts to check the library in an out-of-memory situation.
    12     12   # When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special
    13     13   # command (sqlite_malloc_fail N) which causes the N-th malloc to fail.  This
    14     14   # special feature is used to see what happens in the library if a malloc
    15     15   # were to really fail due to an out-of-memory situation.
    16     16   #
    17         -# $Id: malloc.test,v 1.13 2005/01/11 13:02:34 danielk1977 Exp $
           17  +# $Id: malloc.test,v 1.14 2005/01/12 07:15:07 danielk1977 Exp $
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   
    22     22   # Only run these tests if memory debugging is turned on.
    23     23   #
    24     24   if {[info command sqlite_malloc_stat]==""} {
................................................................................
   312    312        catch {file delete -force test.db-journal}
   313    313        sqlite3 db test.db
   314    314        execsql {
   315    315          CREATE TABLE t1(a, b);
   316    316          INSERT INTO t1 VALUES(1, 2);
   317    317          INSERT INTO t1 VALUES(3, 4);
   318    318          INSERT INTO t1 VALUES(5, 6);
          319  +       INSERT INTO t1 VALUES(7, randstr(1200,1200));
   319    320        }
   320    321        sqlite_malloc_fail $i
   321    322        set v [catch {execsql {
   322         -       SELECT min(a) FROM t1 GROUP BY b;
          323  +       SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
          324  +       SELECT a FROM t1 WHERE a<6 ORDER BY a;
          325  +       SELECT b FROM t1 WHERE a>6;
   323    326        }} msg]
   324    327        set leftover [lindex [sqlite_malloc_stat] 2]
   325    328        if {$leftover>0} {
   326    329          if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
   327    330          set ::go 0
   328    331          set v {1 1}
   329    332        } else {
   330    333          set v2 [expr {$msg=="" || $msg=="out of memory"}]
   331    334          if {!$v2} {puts "\nError message returned: $msg"}
   332    335          lappend v $v2
   333    336        }
   334    337     } {1 1}
   335    338   }
          339  +
          340  +# This block is designed to test that some malloc failures that may
          341  +# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
          342  +# when converting UTF-16 text to integers and real numbers is handled
          343  +# correctly. 
          344  +#
          345  +# This doesn't actually return an error to the user. That could be 
          346  +# viewed as a bug.
          347  +#
          348  +for {set go 1; set i 1} {$go && $::sqlite_options(utf16)} {incr i} {
          349  +  do_test malloc-8.$i {
          350  +     sqlite_malloc_fail 0
          351  +     catch {db close}
          352  +     catch {file delete -force test.db}
          353  +     catch {file delete -force test.db-journal}
          354  +
          355  +     set ::DB [sqlite3 db test.db]
          356  +     set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
          357  +     set ::STMT [sqlite3_prepare $::DB $sql -1 X]
          358  +     sqlite3_step $::STMT
          359  +
          360  +     if { $::tcl_platform(byteOrder)=="littleEndian" } {
          361  +       set ::bomstr "\xFF\xFE"
          362  +     } else {
          363  +       set ::bomstr "\xFE\xFF"
          364  +     }
          365  +     append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
          366  +
          367  +     sqlite_malloc_fail $i
          368  +     catch {
          369  +       sqlite3_column_text16 $::STMT 0
          370  +       sqlite3_column_int $::STMT 0
          371  +       sqlite3_column_text16 $::STMT 1
          372  +       sqlite3_column_double $::STMT 1
          373  +       sqlite3_reset $::STMT
          374  +       sqlite3_bind_text16 $::STMT 1 $::bomstr 60
          375  +       
          376  +     } msg
          377  +     sqlite3_finalize $::STMT
          378  +     if {[lindex [sqlite_malloc_stat] 2]>0} {
          379  +       set ::go 0
          380  +     }
          381  +     expr 0
          382  +  } {0}
          383  +}
   336    384   
   337    385   # Ensure that no file descriptors were leaked.
   338         -do_test malloc-6.X {
          386  +do_test malloc-99.X {
   339    387     catch {db close}
   340    388     set sqlite_open_file_count
   341    389   } {0}
   342    390   
   343    391   sqlite_malloc_fail 0
   344    392   finish_test