os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/main.test
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
# 2001 September 15
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is exercising the code in main.c.
sl@0
    13
#
sl@0
    14
# $Id: main.test,v 1.29 2008/07/12 14:52:20 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# Only do the next group of tests if the sqlite3_complete API is available
sl@0
    20
#
sl@0
    21
ifcapable {complete} {
sl@0
    22
sl@0
    23
# Tests of the sqlite_complete() function.
sl@0
    24
#
sl@0
    25
do_test main-1.1 {
sl@0
    26
  db complete {This is a test}
sl@0
    27
} {0}
sl@0
    28
do_test main-1.2 {
sl@0
    29
  db complete {
sl@0
    30
  }
sl@0
    31
} {1}
sl@0
    32
do_test main-1.3 {
sl@0
    33
  db complete {
sl@0
    34
     -- a comment ;
sl@0
    35
  }
sl@0
    36
} {1}
sl@0
    37
do_test main-1.4 {
sl@0
    38
  db complete {
sl@0
    39
     -- a comment ;
sl@0
    40
     ;
sl@0
    41
  }
sl@0
    42
} {1}
sl@0
    43
do_test main-1.5 {
sl@0
    44
  db complete {DROP TABLE 'xyz;}
sl@0
    45
} {0}
sl@0
    46
do_test main-1.6 {
sl@0
    47
  db complete {DROP TABLE 'xyz';}
sl@0
    48
} {1}
sl@0
    49
do_test main-1.7 {
sl@0
    50
  db complete {DROP TABLE "xyz;}
sl@0
    51
} {0}
sl@0
    52
do_test main-1.8 {
sl@0
    53
  db complete {DROP TABLE "xyz';}
sl@0
    54
} {0}
sl@0
    55
do_test main-1.9 {
sl@0
    56
  db complete {DROP TABLE "xyz";}
sl@0
    57
} {1}
sl@0
    58
do_test main-1.10 {
sl@0
    59
  db complete {DROP TABLE xyz; hi}
sl@0
    60
} {0}
sl@0
    61
do_test main-1.11 {
sl@0
    62
  db complete {DROP TABLE xyz; }
sl@0
    63
} {1}
sl@0
    64
do_test main-1.12 {
sl@0
    65
  db complete {DROP TABLE xyz; -- hi }
sl@0
    66
} {1}
sl@0
    67
do_test main-1.13 {
sl@0
    68
  db complete {DROP TABLE xyz; -- hi
sl@0
    69
  }
sl@0
    70
} {1}
sl@0
    71
do_test main-1.14 {
sl@0
    72
  db complete {SELECT a-b FROM t1; }
sl@0
    73
} {1}
sl@0
    74
do_test main-1.15 {
sl@0
    75
  db complete {SELECT a/e FROM t1 }
sl@0
    76
} {0}
sl@0
    77
do_test main-1.16 {
sl@0
    78
  db complete {
sl@0
    79
    CREATE TABLE abc(x,y);
sl@0
    80
  }
sl@0
    81
} {1}
sl@0
    82
ifcapable {trigger} {
sl@0
    83
  do_test main-1.17 {
sl@0
    84
    db complete {
sl@0
    85
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr;
sl@0
    86
    }
sl@0
    87
  } {0}
sl@0
    88
  do_test main-1.18 {
sl@0
    89
    db complete {
sl@0
    90
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr; END;
sl@0
    91
    }
sl@0
    92
  } {1}
sl@0
    93
  do_test main-1.19 {
sl@0
    94
    db complete {
sl@0
    95
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN
sl@0
    96
         UPDATE pqr;
sl@0
    97
         unknown command;
sl@0
    98
    }
sl@0
    99
  } {0}
sl@0
   100
  do_test main-1.20 {
sl@0
   101
    db complete {
sl@0
   102
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   103
         UPDATE pqr;
sl@0
   104
    }
sl@0
   105
  } {0}
sl@0
   106
  do_test main-1.21 {
sl@0
   107
    db complete {
sl@0
   108
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
sl@0
   109
         SELECT a, b FROM end;
sl@0
   110
    }
sl@0
   111
  } {0}
sl@0
   112
  do_test main-1.22 {
sl@0
   113
    db complete {
sl@0
   114
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
sl@0
   115
         SELECT a, b FROM end;
sl@0
   116
      END;
sl@0
   117
    }
sl@0
   118
  } {1}
sl@0
   119
  do_test main-1.23 {
sl@0
   120
    db complete {
sl@0
   121
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
sl@0
   122
         SELECT a, b FROM end;
sl@0
   123
      END;
sl@0
   124
      SELECT a, b FROM end;
sl@0
   125
    }
sl@0
   126
  } {1}
sl@0
   127
  do_test main-1.24 {
sl@0
   128
    db complete {
sl@0
   129
      CREATE TRIGGER xyz AFTER DELETE [;end;] BEGIN
sl@0
   130
         UPDATE pqr;
sl@0
   131
    }
sl@0
   132
  } {0}
sl@0
   133
  do_test main-1.25 {
sl@0
   134
    db complete {
sl@0
   135
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   136
         UPDATE cantor SET a=[;end;];;;
sl@0
   137
    }
sl@0
   138
  } {0}
sl@0
   139
  do_test main-1.26 {
sl@0
   140
    db complete {
sl@0
   141
      CREATE -- a comment
sl@0
   142
      TRIGGER exy AFTER DELETE backend BEGIN
sl@0
   143
         UPDATE pqr SET a=5;
sl@0
   144
    }
sl@0
   145
  } {0}
sl@0
   146
  do_test main-1.27.1 {
sl@0
   147
    db complete {
sl@0
   148
      CREATE -- a comment
sl@0
   149
      TRIGGERX tangentxx AFTER DELETE backend BEGIN
sl@0
   150
         UPDATE pqr SET a=5;
sl@0
   151
    }
sl@0
   152
  } {1}
sl@0
   153
  do_test main-1.27.2 {
sl@0
   154
    db complete {
sl@0
   155
      CREATE/**/TRIGGER tiger00 AFTER DELETE backend BEGIN
sl@0
   156
         UPDATE pqr SET a=5;
sl@0
   157
    }
sl@0
   158
  } {0}
sl@0
   159
  ifcapable {explain} {
sl@0
   160
    do_test main-1.27.3 {
sl@0
   161
      db complete {
sl@0
   162
        /* */ EXPLAIN -- A comment
sl@0
   163
        CREATE/**/TRIGGER ezxyz12 AFTER DELETE backend BEGIN
sl@0
   164
           UPDATE pqr SET a=5;
sl@0
   165
      }
sl@0
   166
    } {0}
sl@0
   167
  }
sl@0
   168
  do_test main-1.27.4 {
sl@0
   169
    db complete {
sl@0
   170
      BOGUS token
sl@0
   171
      CREATE  TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   172
         UPDATE pqr SET a=5;
sl@0
   173
    }
sl@0
   174
  } {1}
sl@0
   175
  ifcapable {explain} {
sl@0
   176
    do_test main-1.27.5 {
sl@0
   177
      db complete {
sl@0
   178
        EXPLAIN 
sl@0
   179
        CREATE TEMP TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   180
           UPDATE pqr SET a=5;
sl@0
   181
      }
sl@0
   182
    } {0}
sl@0
   183
  }
sl@0
   184
  do_test main-1.28 {
sl@0
   185
    db complete {
sl@0
   186
      CREATE TEMPORARY TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   187
         UPDATE pqr SET a=5;
sl@0
   188
    }
sl@0
   189
  } {0}
sl@0
   190
  do_test main-1.29 {
sl@0
   191
    db complete {
sl@0
   192
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
sl@0
   193
         UPDATE pqr SET a=5;
sl@0
   194
         EXPLAIN select * from xyz;
sl@0
   195
    }
sl@0
   196
  } {0}
sl@0
   197
sl@0
   198
} ;# end ifcapable {complete}
sl@0
   199
sl@0
   200
}
sl@0
   201
do_test main-1.30 {
sl@0
   202
  db complete {
sl@0
   203
     CREATE TABLE /* In comment ; */
sl@0
   204
  }
sl@0
   205
} {0}
sl@0
   206
do_test main-1.31 {
sl@0
   207
  db complete {
sl@0
   208
     CREATE TABLE /* In comment ; */ hi;
sl@0
   209
  }
sl@0
   210
} {1}
sl@0
   211
do_test main-1.31 {
sl@0
   212
  db complete {
sl@0
   213
     CREATE TABLE /* In comment ; */;
sl@0
   214
  }
sl@0
   215
} {1}
sl@0
   216
do_test main-1.32 {
sl@0
   217
  db complete {
sl@0
   218
     stuff;
sl@0
   219
     /*
sl@0
   220
       CREATE TABLE
sl@0
   221
       multiple lines
sl@0
   222
       of text
sl@0
   223
     */
sl@0
   224
  }
sl@0
   225
} {1}
sl@0
   226
do_test main-1.33 {
sl@0
   227
  db complete {
sl@0
   228
     /*
sl@0
   229
       CREATE TABLE
sl@0
   230
       multiple lines
sl@0
   231
       of text;
sl@0
   232
  }
sl@0
   233
} {0}
sl@0
   234
do_test main-1.34 {
sl@0
   235
  db complete {
sl@0
   236
     /*
sl@0
   237
       CREATE TABLE
sl@0
   238
       multiple lines "*/
sl@0
   239
       of text;
sl@0
   240
  }
sl@0
   241
} {1}
sl@0
   242
do_test main-1.35 {
sl@0
   243
  db complete {hi /**/ there;}
sl@0
   244
} {1}
sl@0
   245
do_test main-1.36 {
sl@0
   246
  db complete {hi there/***/;}
sl@0
   247
} {1}
sl@0
   248
do_test main-1.37 {
sl@0
   249
  db complete {hi there/**}
sl@0
   250
} {0}
sl@0
   251
do_test main-1.38 {
sl@0
   252
  db complete {hi [there}
sl@0
   253
} {0}
sl@0
   254
sl@0
   255
ifcapable {trigger} {
sl@0
   256
  # Characters less than \040 can never be part of an identifier.
sl@0
   257
  # Characters greater than \u177 are always identifier characters.
sl@0
   258
  do_test main-1.100 {
sl@0
   259
    db complete "create \037\036\035\034trigger\001\002;"
sl@0
   260
  } {1}
sl@0
   261
  do_test main-1.101 {
sl@0
   262
    db complete "create trigger\200;"
sl@0
   263
  } {1}
sl@0
   264
  do_test main-1.102 {
sl@0
   265
    db complete "create \200trigger;"
sl@0
   266
  } {1}
sl@0
   267
}
sl@0
   268
sl@0
   269
sl@0
   270
# Try to open a database with a corrupt database file.
sl@0
   271
#
sl@0
   272
do_test main-2.0 {
sl@0
   273
  catch {db close}
sl@0
   274
  file delete -force test.db
sl@0
   275
  set fd [open test.db w]
sl@0
   276
  puts $fd hi!
sl@0
   277
  close $fd
sl@0
   278
  set v [catch {sqlite3 db test.db} msg]
sl@0
   279
  if {$v} {lappend v $msg} {lappend v {}}
sl@0
   280
} {0 {}}
sl@0
   281
sl@0
   282
# Here are some tests for tokenize.c.  
sl@0
   283
#
sl@0
   284
do_test main-3.1 {
sl@0
   285
  catch {db close}
sl@0
   286
  foreach f [glob -nocomplain testdb/*] {file delete -force $f}
sl@0
   287
  file delete -force testdb
sl@0
   288
  sqlite3 db testdb
sl@0
   289
  set v [catch {execsql {SELECT * from T1 where x!!5}} msg]
sl@0
   290
  lappend v $msg
sl@0
   291
} {1 {unrecognized token: "!!"}}
sl@0
   292
do_test main-3.2 {
sl@0
   293
  catch {db close}
sl@0
   294
  foreach f [glob -nocomplain testdb/*] {file delete -force $f}
sl@0
   295
  file delete -force testdb
sl@0
   296
  sqlite3 db testdb
sl@0
   297
  set v [catch {execsql {SELECT * from T1 where ^x}} msg]
sl@0
   298
  lappend v $msg
sl@0
   299
} {1 {unrecognized token: "^"}}
sl@0
   300
do_test main-3.2.2 {
sl@0
   301
  catchsql {select 'abc}
sl@0
   302
} {1 {unrecognized token: "'abc"}}
sl@0
   303
do_test main-3.2.3 {
sl@0
   304
  catchsql {select "abc}
sl@0
   305
} {1 {unrecognized token: ""abc"}}
sl@0
   306
do_test main-3.2.4 {
sl@0
   307
  catchsql {select [abc}
sl@0
   308
} {1 {unrecognized token: "[abc"}}
sl@0
   309
do_test main-3.2.5 {
sl@0
   310
  catchsql {select x'4869}
sl@0
   311
} {1 {unrecognized token: "x'4869"}}
sl@0
   312
do_test main-3.2.6 {
sl@0
   313
  catchsql {select x'4869'}
sl@0
   314
} {0 Hi}
sl@0
   315
do_test main-3.2.7 {
sl@0
   316
  catchsql {select x'48695'}
sl@0
   317
} {1 {unrecognized token: "x'48695'"}}
sl@0
   318
do_test main-3.2.8 {
sl@0
   319
  catchsql {select x'486x'}
sl@0
   320
} {1 {unrecognized token: "x'486x'"}}
sl@0
   321
do_test main-3.2.9 {
sl@0
   322
  catchsql {select $abc(}
sl@0
   323
} {1 {unrecognized token: "$abc("}}
sl@0
   324
do_test main-3.2.10 {
sl@0
   325
  catchsql {select $abc(x}
sl@0
   326
} {1 {unrecognized token: "$abc(x"}}
sl@0
   327
set xyz 123
sl@0
   328
do_test main-3.2.11 {
sl@0
   329
  catchsql {select $::xyz}
sl@0
   330
} {0 123}
sl@0
   331
namespace eval ::testnamespace {
sl@0
   332
  variable xyz 321
sl@0
   333
}
sl@0
   334
do_test main-3.2.12 {
sl@0
   335
  catchsql {select $testnamespace::xyz}
sl@0
   336
} {0 321}
sl@0
   337
do_test main-3.2.13 {
sl@0
   338
  catchsql {select $(abc)}
sl@0
   339
} {1 {unrecognized token: "$"}}
sl@0
   340
do_test main-3.2.14 {
sl@0
   341
  set hi\u1234x 987
sl@0
   342
  db eval "select \$hi\u1234x"
sl@0
   343
} {987}
sl@0
   344
do_test main-3.2.15 {
sl@0
   345
  catchsql "select 456\u1234"
sl@0
   346
} [list 1 "unrecognized token: \"456\u1234\""]
sl@0
   347
do_test main-3.2.16 {
sl@0
   348
  catchsql {select cast(3.14e+4 AS integer)}
sl@0
   349
} {0 31400}
sl@0
   350
do_test main-3.2.17 {
sl@0
   351
  catchsql {select cast(3.14e+04 AS integer)}
sl@0
   352
} {0 31400}
sl@0
   353
do_test main-3.2.18 {
sl@0
   354
  catchsql {select cast(3.14e+004 AS integer)}
sl@0
   355
} {0 31400}
sl@0
   356
do_test main-3.2.19 {
sl@0
   357
  catchsql {select cast(3.14e4 AS integer)}
sl@0
   358
} {0 31400}
sl@0
   359
do_test main-3.2.20 {
sl@0
   360
  catchsql {select cast(3.14e04 AS integer)}
sl@0
   361
} {0 31400}
sl@0
   362
do_test main-3.2.21 {
sl@0
   363
  catchsql {select cast(3.14e004 AS integer)}
sl@0
   364
} {0 31400}
sl@0
   365
do_test main-3.2.16 {
sl@0
   366
  catchsql {select cast(3.14E+4 AS integer)}
sl@0
   367
} {0 31400}
sl@0
   368
do_test main-3.2.17 {
sl@0
   369
  catchsql {select cast(3.14E+04 AS integer)}
sl@0
   370
} {0 31400}
sl@0
   371
do_test main-3.2.18 {
sl@0
   372
  catchsql {select cast(3.14E+004 AS integer)}
sl@0
   373
} {0 31400}
sl@0
   374
do_test main-3.2.19 {
sl@0
   375
  catchsql {select cast(3.14E4 AS integer)}
sl@0
   376
} {0 31400}
sl@0
   377
do_test main-3.2.20 {
sl@0
   378
  catchsql {select cast(3.14E04 AS integer)}
sl@0
   379
} {0 31400}
sl@0
   380
do_test main-3.2.21 {
sl@0
   381
  catchsql {select cast(3.14E004 AS integer)}
sl@0
   382
} {0 31400}
sl@0
   383
do_test main-3.2.22 {
sl@0
   384
  catchsql {select cast(3.14e-4 * 1e8 AS integer)}
sl@0
   385
} {0 31400}
sl@0
   386
do_test main-3.2.23 {
sl@0
   387
  catchsql {select cast(3.14E-04 * 1E08 AS integer)}
sl@0
   388
} {0 31400}
sl@0
   389
do_test main-3.2.24 {
sl@0
   390
  catchsql {select cast(3.14e-004 * 01.0E+8 AS integer)}
sl@0
   391
} {0 31400}
sl@0
   392
do_test main-3.2.25 {
sl@0
   393
  catchsql {select 123/*abc}
sl@0
   394
} {0 123}
sl@0
   395
do_test main-3.2.26 {
sl@0
   396
  catchsql {select 123/***abc}
sl@0
   397
} {0 123}
sl@0
   398
do_test main-3.2.27 {
sl@0
   399
  catchsql {select 123/*/*2}
sl@0
   400
} {0 123}
sl@0
   401
do_test main-3.2.28 {
sl@0
   402
  catchsql {select 123/**/*2}
sl@0
   403
} {0 246}
sl@0
   404
do_test main-3.2.29 {
sl@0
   405
  catchsql {select 123/}
sl@0
   406
} {1 {near "/": syntax error}}
sl@0
   407
do_test main-3.2.30 {
sl@0
   408
  catchsql {select 123--5}
sl@0
   409
} {0 123}
sl@0
   410
sl@0
   411
sl@0
   412
do_test main-3.3 {
sl@0
   413
  catch {db close}
sl@0
   414
  foreach f [glob -nocomplain testdb/*] {file delete -force $f}
sl@0
   415
  file delete -force testdb
sl@0
   416
  sqlite3 db testdb
sl@0
   417
  execsql {
sl@0
   418
    create table T1(X REAL);  /* C-style comments allowed */
sl@0
   419
    insert into T1 values(0.5);
sl@0
   420
    insert into T1 values(0.5e2);
sl@0
   421
    insert into T1 values(0.5e-002);
sl@0
   422
    insert into T1 values(5e-002);
sl@0
   423
    insert into T1 values(-5.0e-2);
sl@0
   424
    insert into T1 values(-5.1e-2);
sl@0
   425
    insert into T1 values(0.5e2);
sl@0
   426
    insert into T1 values(0.5E+02);
sl@0
   427
    insert into T1 values(5E+02);
sl@0
   428
    insert into T1 values(5.0E+03);
sl@0
   429
    select x*10 from T1 order by x*5;
sl@0
   430
  }
sl@0
   431
} {-0.51 -0.5 0.05 0.5 5.0 500.0 500.0 500.0 5000.0 50000.0}
sl@0
   432
do_test main-3.4 {
sl@0
   433
  set v [catch {execsql {create bogus}} msg]
sl@0
   434
  lappend v $msg
sl@0
   435
} {1 {near "bogus": syntax error}}
sl@0
   436
do_test main-3.5 {
sl@0
   437
  set v [catch {execsql {create}} msg]
sl@0
   438
  lappend v $msg
sl@0
   439
} {1 {near "create": syntax error}}
sl@0
   440
do_test main-3.6 {
sl@0
   441
  catchsql {SELECT 'abc' + #9}
sl@0
   442
} {1 {near "#9": syntax error}}
sl@0
   443
sl@0
   444
# The following test-case tests the linked list code used to manage
sl@0
   445
# sqlite3_vfs structures.
sl@0
   446
if {$::tcl_platform(platform)=="unix"} {
sl@0
   447
  ifcapable threadsafe {
sl@0
   448
    do_test main-4.1 {
sl@0
   449
sl@0
   450
      sqlite3_crash_enable 1
sl@0
   451
      sqlite3_crash_enable 0
sl@0
   452
    
sl@0
   453
      sqlite3async_enable 1
sl@0
   454
      sqlite3async_enable 0
sl@0
   455
    
sl@0
   456
      sqlite3_crash_enable 1
sl@0
   457
      sqlite3async_enable 1
sl@0
   458
      sqlite3_crash_enable 0
sl@0
   459
      sqlite3async_enable 0
sl@0
   460
    
sl@0
   461
      sqlite3_crash_enable 1
sl@0
   462
      sqlite3async_enable 1
sl@0
   463
      sqlite3async_enable 0
sl@0
   464
      sqlite3_crash_enable 0
sl@0
   465
    
sl@0
   466
      sqlite3async_enable 1
sl@0
   467
      sqlite3_crash_enable 1
sl@0
   468
      sqlite3_crash_enable 0
sl@0
   469
      sqlite3async_enable 0
sl@0
   470
    
sl@0
   471
      sqlite3async_enable 1
sl@0
   472
      sqlite3_crash_enable 1
sl@0
   473
      sqlite3async_enable 0
sl@0
   474
      sqlite3_crash_enable 0
sl@0
   475
    } {}
sl@0
   476
    do_test main-4.2 {
sl@0
   477
      set rc [catch {sqlite3 db test.db -vfs crash} msg]
sl@0
   478
      list $rc $msg
sl@0
   479
    } {1 {no such vfs: crash}}
sl@0
   480
    do_test main-4.3 {
sl@0
   481
      set rc [catch {sqlite3 db test.db -vfs async} msg]
sl@0
   482
      list $rc $msg
sl@0
   483
    } {1 {no such vfs: async}}
sl@0
   484
  }
sl@0
   485
}
sl@0
   486
sl@0
   487
finish_test