summaryrefslogtreecommitdiff
path: root/test/main.test
blob: 5bbc52b84509b901f460459c6e63ff807b81e5f6 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# 2001 September 15
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is exercising the code in main.c.
#
# $Id: main.test,v 1.32 2009/04/28 04:51:29 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Only do the next group of tests if the sqlite3_complete API is available
#
ifcapable {complete} {

# Tests of the sqlite_complete() function.
#
do_test main-1.1 {
  db complete {This is a test}
} {0}
do_test main-1.2.0 {
  db complete {
  }
} {0}
do_test main-1.2.1 {
  db complete {}
} {0}
do_test main-1.3.0 {
  db complete {
     -- a comment ;
  }
} {0}
do_test main-1.3.1 {
  db complete {
     /* a comment ; */
  }
} {0}
do_test main-1.4.0 {
  db complete {
     -- a comment ;
     ;
  }
} {1}
do_test main-1.4.1 {
  db complete {
     /* a comment ; */
     ;
  }
} {1}
do_test main-1.4.2 {
  db complete {
     /* a comment ; */ ;
  }
} {1}
do_test main-1.5 {
  db complete {DROP TABLE 'xyz;}
} {0}
do_test main-1.6 {
  db complete {DROP TABLE 'xyz';}
} {1}
do_test main-1.7 {
  db complete {DROP TABLE "xyz;}
} {0}
do_test main-1.8 {
  db complete {DROP TABLE "xyz';}
} {0}
do_test main-1.9 {
  db complete {DROP TABLE "xyz";}
} {1}
do_test main-1.10 {
  db complete {DROP TABLE xyz; hi}
} {0}
do_test main-1.11 {
  db complete {DROP TABLE xyz; }
} {1}
do_test main-1.12 {
  db complete {DROP TABLE xyz; -- hi }
} {1}
do_test main-1.13 {
  db complete {DROP TABLE xyz; -- hi
  }
} {1}
do_test main-1.14 {
  db complete {SELECT a-b FROM t1; }
} {1}
do_test main-1.15 {
  db complete {SELECT a/e FROM t1 }
} {0}
do_test main-1.16 {
  db complete {
    CREATE TABLE abc(x,y);
  }
} {1}
ifcapable {trigger} {
  do_test main-1.17 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr;
    }
  } {0}
  do_test main-1.17.2 {
    db complete {
      EXPLAIN CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr;
    }
  } {0}
  do_test main-1.17.3 {
    db complete {
      EXPLAIN QUERY PLAN CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr;
    }
  } {0}
  do_test main-1.18 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN UPDATE pqr; END;
    }
  } {1}
  do_test main-1.19 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE abc BEGIN
         UPDATE pqr;
         unknown command;
    }
  } {0}
  do_test main-1.20 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
         UPDATE pqr;
    }
  } {0}
  do_test main-1.21 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
         SELECT a, b FROM end;
    }
  } {0}
  do_test main-1.22 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
         SELECT a, b FROM end;
      END;
    }
  } {1}
  do_test main-1.23 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE end BEGIN
         SELECT a, b FROM end;
      END;
      SELECT a, b FROM end;
    }
  } {1}
  do_test main-1.24 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE [;end;] BEGIN
         UPDATE pqr;
    }
  } {0}
  do_test main-1.25 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
         UPDATE cantor SET a=[;end;];;;
    }
  } {0}
  do_test main-1.26 {
    db complete {
      CREATE -- a comment
      TRIGGER exy AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
    }
  } {0}
  do_test main-1.27.1 {
    db complete {
      CREATE -- a comment
      TRIGGERX tangentxx AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
    }
  } {1}
  do_test main-1.27.2 {
    db complete {
      CREATE/**/TRIGGER tiger00 AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
    }
  } {0}
  ifcapable {explain} {
    do_test main-1.27.3 {
      db complete {
        /* */ EXPLAIN -- A comment
        CREATE/**/TRIGGER ezxyz12 AFTER DELETE backend BEGIN
           UPDATE pqr SET a=5;
      }
    } {0}
  }
  do_test main-1.27.4 {
    db complete {
      BOGUS token
      CREATE  TRIGGER xyz AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
    }
  } {1}
  ifcapable {explain} {
    do_test main-1.27.5 {
      db complete {
        EXPLAIN 
        CREATE TEMP TRIGGER xyz AFTER DELETE backend BEGIN
           UPDATE pqr SET a=5;
      }
    } {0}
  }
  do_test main-1.28 {
    db complete {
      CREATE TEMPORARY TRIGGER xyz AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
    }
  } {0}
  do_test main-1.29 {
    db complete {
      CREATE TRIGGER xyz AFTER DELETE backend BEGIN
         UPDATE pqr SET a=5;
         EXPLAIN select * from xyz;
    }
  } {0}

} ;# end ifcapable {complete}

}
do_test main-1.30 {
  db complete {
     CREATE TABLE /* In comment ; */
  }
} {0}
do_test main-1.31 {
  db complete {
     CREATE TABLE /* In comment ; */ hi;
  }
} {1}
do_test main-1.31 {
  db complete {
     CREATE TABLE /* In comment ; */;
  }
} {1}
do_test main-1.32 {
  db complete {
     stuff;
     /*
       CREATE TABLE
       multiple lines
       of text
     */
  }
} {1}
do_test main-1.33 {
  db complete {
     /*
       CREATE TABLE
       multiple lines
       of text;
  }
} {0}
do_test main-1.34 {
  db complete {
     /*
       CREATE TABLE
       multiple lines "*/
       of text;
  }
} {1}
do_test main-1.35 {
  db complete {hi /**/ there;}
} {1}
do_test main-1.36 {
  db complete {hi there/***/;}
} {1}
do_test main-1.37 {
  db complete {hi there/**}
} {0}
do_test main-1.38 {
  db complete {hi [there}
} {0}

ifcapable {trigger} {
  # Characters less than \040 can never be part of an identifier.
  # Characters greater than \u177 are always identifier characters.
  do_test main-1.100 {
    db complete "create \037\036\035\034trigger\001\002;"
  } {1}
  do_test main-1.101 {
    db complete "create trigger\200;"
  } {1}
  do_test main-1.102 {
    db complete "create \200trigger;"
  } {1}
}


# Try to open a database with a corrupt database file.
#
if {[permutation] == ""} {
  do_test main-2.0 {
    catch {db close}
    forcedelete test.db
    set fd [open test.db w]
    puts $fd hi!
    close $fd
    set v [catch {sqlite3 db test.db} msg]
    if {$v} {lappend v $msg} {lappend v {}}
  } {0 {}}
}

# Here are some tests for tokenize.c.  
#
do_test main-3.1 {
  catch {db close}
  foreach f [glob -nocomplain testdb/*] {forcedelete $f}
  forcedelete testdb
  sqlite3 db testdb
  set v [catch {execsql {SELECT * from T1 where x!!5}} msg]
  lappend v $msg
} {1 {unrecognized token: "!!"}}
do_test main-3.2 {
  catch {db close}
  foreach f [glob -nocomplain testdb/*] {forcedelete $f}
  forcedelete testdb
  sqlite3 db testdb
  set v [catch {execsql {SELECT * from T1 where ^x}} msg]
  lappend v $msg
} {1 {unrecognized token: "^"}}
do_test main-3.2.2 {
  catchsql {select 'abc}
} {1 {unrecognized token: "'abc"}}
do_test main-3.2.3 {
  catchsql {select "abc}
} {1 {unrecognized token: ""abc"}}
do_test main-3.2.4 {
  catchsql {select [abc}
} {1 {unrecognized token: "[abc"}}
do_test main-3.2.5 {
  catchsql {select x'4869}
} {1 {unrecognized token: "x'4869"}}
do_test main-3.2.6 {
  catchsql {select x'4869'}
} {0 Hi}
do_test main-3.2.7 {
  catchsql {select x'48695'}
} {1 {unrecognized token: "x'48695'"}}
do_test main-3.2.8 {
  catchsql {select x'486x'}
} {1 {unrecognized token: "x'486x'"}}
do_test main-3.2.9 {
  catchsql {select $abc(}
} {1 {unrecognized token: "$abc("}}
do_test main-3.2.10 {
  catchsql {select $abc(x}
} {1 {unrecognized token: "$abc(x"}}
set xyz 123
do_test main-3.2.11 {
  catchsql {select $::xyz}
} {0 123}
namespace eval ::testnamespace {
  variable xyz 321
}
do_test main-3.2.12 {
  catchsql {select $testnamespace::xyz}
} {0 321}
do_test main-3.2.13 {
  catchsql {select $(abc)}
} {1 {unrecognized token: "$"}}
do_test main-3.2.14 {
  set hi\u1234x 987
  db eval "select \$hi\u1234x"
} {987}
do_test main-3.2.15 {
  catchsql "select 456\u1234"
} [list 1 "unrecognized token: \"456\u1234\""]
do_test main-3.2.16 {
  catchsql {select cast(3.14e+4 AS integer)}
} {0 31400}
do_test main-3.2.17 {
  catchsql {select cast(3.14e+04 AS integer)}
} {0 31400}
do_test main-3.2.18 {
  catchsql {select cast(3.14e+004 AS integer)}
} {0 31400}
do_test main-3.2.19 {
  catchsql {select cast(3.14e4 AS integer)}
} {0 31400}
do_test main-3.2.20 {
  catchsql {select cast(3.14e04 AS integer)}
} {0 31400}
do_test main-3.2.21 {
  catchsql {select cast(3.14e004 AS integer)}
} {0 31400}
do_test main-3.2.16 {
  catchsql {select cast(3.14E+4 AS integer)}
} {0 31400}
do_test main-3.2.17 {
  catchsql {select cast(3.14E+04 AS integer)}
} {0 31400}
do_test main-3.2.18 {
  catchsql {select cast(3.14E+004 AS integer)}
} {0 31400}
do_test main-3.2.19 {
  catchsql {select cast(3.14E4 AS integer)}
} {0 31400}
do_test main-3.2.20 {
  catchsql {select cast(3.14E04 AS integer)}
} {0 31400}
do_test main-3.2.21 {
  catchsql {select cast(3.14E004 AS integer)}
} {0 31400}
do_test main-3.2.22 {
  catchsql {select cast(3.14e-4 * 1e8 AS integer)}
} {0 31400}
do_test main-3.2.23 {
  catchsql {select cast(3.14E-04 * 1E08 AS integer)}
} {0 31400}
do_test main-3.2.24 {
  catchsql {select cast(3.14e-004 * 01.0E+8 AS integer)}
} {0 31400}
do_test main-3.2.25 {
  catchsql {select 123/*abc}
} {0 123}
do_test main-3.2.26 {
  catchsql {select 123/***abc}
} {0 123}
do_test main-3.2.27 {
  catchsql {select 123/*/*2}
} {0 123}
do_test main-3.2.28 {
  catchsql {select 123/**/*2}
} {0 246}
do_test main-3.2.29 {
  catchsql {select 123/}
} {1 {near "/": syntax error}}
do_test main-3.2.30 {
  catchsql {select 123--5}
} {0 123}


do_test main-3.3 {
  catch {db close}
  foreach f [glob -nocomplain testdb/*] {forcedelete $f}
  forcedelete testdb
  sqlite3 db testdb
  execsql {
    create table T1(X REAL);  /* C-style comments allowed */
    insert into T1 values(0.5);
    insert into T1 values(0.5e2);
    insert into T1 values(0.5e-002);
    insert into T1 values(5e-002);
    insert into T1 values(-5.0e-2);
    insert into T1 values(-5.1e-2);
    insert into T1 values(0.5e2);
    insert into T1 values(0.5E+02);
    insert into T1 values(5E+02);
    insert into T1 values(5.0E+03);
    select x*10 from T1 order by x*5;
  }
} {-0.51 -0.5 0.05 0.5 5.0 500.0 500.0 500.0 5000.0 50000.0}
do_test main-3.4 {
  set v [catch {execsql {create bogus}} msg]
  lappend v $msg
} {1 {near "bogus": syntax error}}
do_test main-3.5 {
  set v [catch {execsql {create}} msg]
  lappend v $msg
} {1 {near "create": syntax error}}
do_test main-3.6 {
  catchsql {SELECT 'abc' + #9}
} {1 {near "#9": syntax error}}

# The following test-case tests the linked list code used to manage
# sqlite3_vfs structures.
if {$::tcl_platform(platform)=="unix" 
     && [info command sqlite3async_initialize]!=""} {
  ifcapable threadsafe {
    do_test main-4.1 {
      sqlite3_crash_enable 1
      sqlite3_crash_enable 0
    
      sqlite3async_initialize "" 1
      sqlite3async_shutdown
    
      sqlite3_crash_enable 1
      sqlite3async_initialize "" 1
      sqlite3_crash_enable 0
      sqlite3async_shutdown
    
      sqlite3_crash_enable 1
      sqlite3async_initialize "" 1
      sqlite3async_shutdown
      sqlite3_crash_enable 0
    
      sqlite3async_initialize "" 1
      sqlite3_crash_enable 1
      sqlite3_crash_enable 0
      sqlite3async_shutdown
    
      sqlite3async_initialize "" 1
      sqlite3_crash_enable 1
      sqlite3async_shutdown
      sqlite3_crash_enable 0
    } {}
    do_test main-4.2 {
      set rc [catch {sqlite3 db test.db -vfs crash} msg]
      list $rc $msg
    } {1 {no such vfs: crash}}
    do_test main-4.3 {
      set rc [catch {sqlite3 db test.db -vfs async} msg]
      list $rc $msg
    } {1 {no such vfs: async}}
  }
}
    
finish_test