/ Check-in [9b207d1a]
Login

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

Overview
Comment:Fix assertion fault in sqllimits1.test encountered when auto_vacuum is enabled. Correct the sqllimit1 tests to account for extra pages inserted by auto_vacuum. (CVS 4759)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:9b207d1a6690206a6d7a10fcb517579b8bdadca2
User & Date: drh 2008-01-31 14:54:44
Context
2008-01-31
15:31
Add "return rc;" to the end of sqlite3_test_control(). (CVS 4760) check-in: b547e7ea user: danielk1977 tags: trunk
14:54
Fix assertion fault in sqllimits1.test encountered when auto_vacuum is enabled. Correct the sqllimit1 tests to account for extra pages inserted by auto_vacuum. (CVS 4759) check-in: 9b207d1a user: drh tags: trunk
14:43
Add the sqlite3_test_control() API. Use it to control the fault injector. (CVS 4758) check-in: 413ddade user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/btree.c.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
....
4094
4095
4096
4097
4098
4099
4100

4101
4102
4103
4104
4105
4106
4107
....
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
** 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.
**
*************************************************************************
** $Id: btree.c,v 1.437 2008/01/19 23:50:26 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"

................................................................................
      /* If *pPgno refers to a pointer-map page, allocate two new pages
      ** at the end of the file instead of one. The first allocated page
      ** becomes a new pointer-map page, the second is used by the caller.
      */
      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
      assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
      (*pPgno)++;

    }
    if( pBt->nTrunc ){
      pBt->nTrunc = *pPgno;
    }
#endif

    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
................................................................................
      return rc;
    }
    pgnoRoot++;

    /* The new root-page may not be allocated on a pointer-map page, or the
    ** PENDING_BYTE page.
    */
    if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
      pgnoRoot++;
    }
    assert( pgnoRoot>=3 );

    /* Allocate a page. The page that currently resides at pgnoRoot will
    ** be moved to the allocated page (unless the allocated page happens







|







 







>







 







|







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
....
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
....
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
** 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.
**
*************************************************************************
** $Id: btree.c,v 1.438 2008/01/31 14:54:44 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"

................................................................................
      /* If *pPgno refers to a pointer-map page, allocate two new pages
      ** at the end of the file instead of one. The first allocated page
      ** becomes a new pointer-map page, the second is used by the caller.
      */
      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
      assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
      (*pPgno)++;
      if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
    }
    if( pBt->nTrunc ){
      pBt->nTrunc = *pPgno;
    }
#endif

    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
................................................................................
      return rc;
    }
    pgnoRoot++;

    /* The new root-page may not be allocated on a pointer-map page, or the
    ** PENDING_BYTE page.
    */
    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
      pgnoRoot++;
    }
    assert( pgnoRoot>=3 );

    /* Allocate a page. The page that currently resides at pgnoRoot will
    ** be moved to the allocated page (unless the allocated page happens

Changes to test/sqllimits1.test.

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
..
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
...
287
288
289
290
291
292
293





294
295
296
297
298
299
300
...
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
...
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests to verify that the limits defined in
# sqlite source file limits.h are enforced.
#
# $Id: sqllimits1.test,v 1.23 2008/01/22 01:48:09 drh Exp $

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

# Test organization:
#
#     sqllimits-1.*:  SQLITE_MAX_LENGTH
#     sqllimits-2.*:  SQLITE_MAX_SQL_LENGTH
#     sqllimits-3.*:  SQLITE_MAX_PAGE_COUNT (and the max_page_count pragma)
#     sqllimits-4.*:  SQLITE_MAX_COLUMN
#
#
#     sqllimits-7.*:   SQLITE_MAX_FUNCTION_ARG  
#     sqllimits-8.*:   SQLITE_MAX_ATTACHED
#     sqllimits-9.*:   SQLITE_MAX_VARIABLE_NUMBER
#     sqllimits-10.*:  SQLITE_MAX_PAGE_SIZE
#     sqllimits-11.*:  SQLITE_MAX_LIKE_PATTERN_LENGTH
#
# Todo:
#
#     sqllimits-5.*:   SQLITE_MAX_EXPR_DEPTH            (sqlite todo)
#     sqllimits-6.*:   SQLITE_MAX_VDBE_OP               (sqlite todo)
#     sqllimits-12.*:  SQLITE_MAX_PAGE_COUNT            (sqlite todo)
#

unset -nocomplain saved
foreach var [info vars SQLITE_MAX_*] {
  set saved($var) [set $var]
}

................................................................................
set SQLITE_MAX_FUNCTION_ARG
set SQLITE_MAX_VARIABLE_NUMBER
set SQLITE_MAX_PAGE_SIZE
set SQLITE_MAX_PAGE_COUNT
set SQLITE_MAX_LIKE_PATTERN_LENGTH      1000

#--------------------------------------------------------------------
# Test cases sqllimits-1.* test that the SQLITE_MAX_LENGTH limit
# is enforced.
#
do_test sqllimits-1.1.1 {
  catchsql { SELECT randomblob(2147483647) }
} {1 {string or blob too big}}
do_test sqllimits-1.1.2 {
  catchsql { SELECT zeroblob(2147483647) }
} {1 {string or blob too big}}

# Large, but allowable, blob-size.
#
set ::LARGESIZE [expr $SQLITE_MAX_LENGTH - 1] 

do_test sqllimits-1.2 {
  catchsql { SELECT LENGTH(randomblob($::LARGESIZE)) }
} "0 $::LARGESIZE"

do_test sqllimits-1.3 {
  catchsql { SELECT quote(randomblob($::LARGESIZE)) }
} {1 {string or blob too big}}

do_test sqllimits-1.4 {
  catchsql { SELECT LENGTH(zeroblob($::LARGESIZE)) }
} "0 $::LARGESIZE"

do_test sqllimits-1.5 {
  catchsql { SELECT quote(zeroblob($::LARGESIZE)) }
} {1 {string or blob too big}}

do_test sqllimits-1.6 {
  catchsql { SELECT zeroblob(-1) }
} {0 {{}}}

do_test sqllimits-1.9 {
  set ::str [string repeat A 65537]
  set ::rep [string repeat B 65537]
  catchsql { SELECT replace($::str, 'A', $::rep) }
} {1 {string or blob too big}}

do_test sqllimits-1.10 {
  set ::str [string repeat %J 2100]
  catchsql { SELECT strftime($::str, '2003-10-31') }
} {1 {string or blob too big}}

do_test sqllimits-1.11 {
  set ::str1 [string repeat A [expr {$SQLITE_MAX_LENGTH - 10}]]
  set ::str2 [string repeat B [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT $::str1 || $::str2 }
} {1 {string or blob too big}}

do_test sqllimits-1.12 {
  set ::str1 [string repeat ' [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT quote($::str1) }
} {1 {string or blob too big}}

do_test sqllimits-1.13 {
  set ::str1 [string repeat ' [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT hex($::str1) }
} {1 {string or blob too big}}

do_test sqllimits-1.14.1 {
  set ::STMT [sqlite3_prepare $::DB "SELECT ?" -1 TAIL]
  sqlite3_bind_zeroblob $::STMT 1 [expr {$SQLITE_MAX_LENGTH + 1}]
} {}
do_test sqllimits-1.14.2 {
  sqlite3_step $::STMT 
} {SQLITE_ERROR}
do_test sqllimits-1.14.3 {
  sqlite3_finalize $::STMT 
} {SQLITE_TOOBIG}

do_test sqllimits-1.15 {
  execsql {
    CREATE TABLE t4(x);
    INSERT INTO t4 VALUES(1);
    INSERT INTO t4 VALUES(2);
    INSERT INTO t4 SELECT 2+x FROM t4;
  }
  catchsql {
................................................................................
    SELECT group_concat(hex(randomblob(20000))) FROM t4;
  }
} {1 {string or blob too big}}
db eval {DROP TABLE t4}

set ::SQLITE_MAX_SQL_LENGTH 0
set strvalue [string repeat A $::SQLITE_MAX_LENGTH]
do_test sqllimits-1.16 {
  catchsql "SELECT '$strvalue'"
} [list 0 $strvalue]
do_test sqllimits-1.17 {
  catchsql "SELECT 'A$strvalue'"
} [list 1 {string or blob too big}]
set blobvalue [string repeat 41 $::SQLITE_MAX_LENGTH]
do_test sqllimits-1.18 {
  catchsql "SELECT x'$blobvalue'"
} [list 0 $strvalue]
do_test sqllimits-1.19 {
  catchsql "SELECT '41$blobvalue'"
} [list 1 {string or blob too big}]
unset blobvalue

ifcapable datetime {
  set strvalue [string repeat D [expr {$SQLITE_MAX_LENGTH-12}]]
  do_test sqllimits-1.20 {
    catchsql {SELECT strftime('%Y ' || $::strvalue, '2008-01-02')}
  } [list 0 [list "2008 $strvalue"]]
  do_test sqllimits-1.21 {
    catchsql {SELECT strftime('%Y-%m-%d ' || $::strvalue, '2008-01-02')}
  } {1 {string or blob too big}}
}
unset strvalue

#--------------------------------------------------------------------
# Test cases sqllimits-2.* test that the SQLITE_MAX_SQL_LENGTH limit
# is enforced.
#
do_test sqllimits-2.1 {
  set ::SQLITE_MAX_SQL_LENGTH 50000
  set sql "SELECT 1 WHERE 1==1"
  set tail " /* A comment to take up space in order to make the string\
                longer without increasing the expression depth */\
                AND   1  ==  1"
  set N [expr {($::SQLITE_MAX_SQL_LENGTH / [string length $tail])+1}]
  append sql [string repeat $tail $N]
  catchsql $sql
} {1 {String or BLOB exceeded size limit}}
do_test sqllimits-2.2 {
  set ::SQLITE_MAX_SQL_LENGTH 0
  catchsql $sql
} {0 1}
do_test sqllimits-2.3 {
  set ::SQLITE_MAX_SQL_LENGTH 50000
  set sql "SELECT 1 WHERE 1==1"
  set tail " /* A comment to take up space in order to make the string\
                longer without increasing the expression depth */\
                AND   1  ==  1"
  set N [expr {($::SQLITE_MAX_SQL_LENGTH / [string length $tail])+1}]
  append sql [string repeat $tail $N]
  set nbytes [string length $sql]
  append sql { AND 0}
  set rc [catch {sqlite3_prepare $DB $sql $nbytes TAIL} STMT]
  lappend rc $STMT
} {1 {(18) statement too long}}
do_test sqllimits-2.4 {
  sqlite3_errmsg $DB
} {statement too long}


#--------------------------------------------------------------------
# Test cases sqllimits-3.* test that the limit set using the
# max_page_count pragma.
#
do_test sqllimits-3.1 {
  execsql {
    PRAGMA max_page_count = 1000;
  }
} {1000}
do_test sqllimits-3.2 {
  execsql { CREATE TABLE trig (a INTEGER, b INTEGER); }

  # Set up a tree of triggers to fire when a row is inserted
  # into table "trig".
  #
  # INSERT -> insert_b -> update_b -> insert_a -> update_a      (chain 1)
  #                    -> update_a -> insert_a -> update_b      (chain 2)
................................................................................
    SELECT COUNT(*) FROM trig;
  }
} {0 7}

# Now check the response of the library to opening a file larger than
# the current max_page_count value. The response is to change the
# internal max_page_count value to match the actual size of the file.





do_test sqllimits1-3.7.1 {
  execsql {
    PRAGMA max_page_count = 1000000;
    CREATE TABLE abc(a, b, c);
    INSERT INTO abc VALUES(1, 2, 3);
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
................................................................................
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a, b, c FROM abc;
    INSERT INTO abc SELECT b, a, c FROM abc;
    INSERT INTO abc SELECT c, b, a FROM abc;
  }
  expr [file size test.db] / 1024
} {1691}
do_test sqllimits1-3.7.2 {
  db close
  sqlite3 db test.db
  execsql {
    PRAGMA max_page_count = 1000;
  }
  execsql {
................................................................................
    SELECT count(*) FROM sqlite_master;
  }
} {6}
do_test sqllimits1-3.7.3 {
  execsql {
    PRAGMA max_page_count;
  }
} {1691}
do_test sqllimits1-3.7.4 {
  execsql {
    DROP TABLE abc;
  }
} {}

#--------------------------------------------------------------------
# Test cases sqllimits1-4.* test the SQLITE_MAX_COLUMN limit.
#
do_test sqllimits-1.4.1 {
  # Columns in a table.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE TABLE t([join $cols ,])" 
} {1 {too many columns on t}}

do_test sqllimits-1.4.2 {
  # Columns in the result-set of a SELECT.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "sql AS sql$i"
  }
  catchsql "SELECT [join $cols ,] FROM sqlite_master"
} {1 {too many columns in result set}}

do_test sqllimits-1.4.3 {
  # Columns in the result-set of a sub-SELECT.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "sql AS sql$i"
  }
  catchsql "SELECT sql4 FROM (SELECT [join $cols ,] FROM sqlite_master)"
} {1 {too many columns in result set}}

do_test sqllimits-1.4.4 {
  # Columns in an index.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols c
  }
  set sql1 "CREATE TABLE t1(c);"
  set sql2 "CREATE INDEX i1 ON t1([join $cols ,]);"
  catchsql "$sql1 ; $sql2"
} {1 {too many columns in index}}

do_test sqllimits-1.4.5 {
  # Columns in a GROUP BY clause.
  catchsql "SELECT * FROM t1 GROUP BY [join $cols ,]"
} {1 {too many terms in GROUP BY clause}}

do_test sqllimits-1.4.6 {
  # Columns in an ORDER BY clause.
  catchsql "SELECT * FROM t1 ORDER BY [join $cols ,]"
} {1 {too many terms in ORDER BY clause}}

do_test sqllimits-1.4.7 {
  # Assignments in an UPDATE statement.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c = 1"
  }
  catchsql "UPDATE t1 SET [join $cols ,];"
} {1 {too many columns in set list}}

do_test sqllimits-1.4.8 {
  # Columns in a view definition:
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE VIEW v1 AS SELECT [join $cols ,] FROM t1;"
} {1 {too many columns in result set}}

do_test sqllimits-1.4.9 {
  # Columns in a view definition (testing * expansion):
  set cols [list]
  for {set i 0} {$i < $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE TABLE t2([join $cols ,])"
  catchsql "CREATE VIEW v1 AS SELECT *, c1 AS o FROM t2;"
} {1 {too many columns in result set}}

#--------------------------------------------------------------------
# These tests - sqllimits-5.* - test that the SQLITE_MAX_EXPR_DEPTH
# limit is enforced. The limit refers to the number of terms in 
# the expression.
#
if {$::SQLITE_MAX_EXPR_DEPTH != 1000} {
  puts -nonewline stderr "WARNING: Compile with -DSQLITE_MAX_EXPR_DEPTH to run "
  puts stderr "tests sqllimits-1.5.X"
} else {
  do_test sqllimits-1.5.1 {
    set max $::SQLITE_MAX_EXPR_DEPTH
    set expr "(1 [string repeat {AND 1 } $max])"
    catchsql [subst {
      SELECT $expr
    }]
  } "1 {Expression tree is too large (maximum depth $::SQLITE_MAX_EXPR_DEPTH)}"
  
  # Attempting to beat the expression depth limit using nested SELECT
  # queries causes a parser stack overflow. 
  do_test sqllimits-1.5.2 {
    set max $::SQLITE_MAX_EXPR_DEPTH
    set expr "SELECT 1"
    for {set i 0} {$i <= $max} {incr i} {
      set expr "SELECT ($expr)"
    }
    catchsql [subst { $expr }]
  } "1 {parser stack overflow}"
  
  
  do_test sqllimits-1.5.3 {
    execsql {
      PRAGMA max_page_count = 1000000;  -- 1 GB
      CREATE TABLE v0(a);
      INSERT INTO v0 VALUES(1);
    }
    db transaction {
      for {set i 1} {$i < 200} {incr i} {
................................................................................
        execsql [subst {
          CREATE VIEW v${i} AS SELECT $expr FROM v[expr {$i-1}]
        }]
      }
    }
  } {}
  
  do_test sqllimits-1.5.4 {
    catchsql {
      SELECT a FROM v199
    }
  } "1 {Expression tree is too large (maximum depth $::SQLITE_MAX_EXPR_DEPTH)}"
}

#--------------------------------------------------------------------
# Test cases sqllimits-6.* test that the SQLITE_MAX_VDBE_OP
# limit works as expected. The limit refers to the number of opcodes
# in a single VDBE program.
#
# TODO

#--------------------------------------------------------------------
# Test the SQLITE_MAX_FUNCTION_ARG limit works. Test case names
# match the pattern "sqllimits-7.*".
#
do_test sqllimits-1.7.1 {
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i < $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT max([join $vals ,])"
} "0 [expr {$::SQLITE_MAX_FUNCTION_ARG - 1}]"
do_test sqllimits-1.7.2 {
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i <= $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT max([join $vals ,])"
} {1 {too many arguments on function max}}

# Test that it is SQLite, and not the implementation of the
# user function that is throwing the error.
proc myfunc {args} {error "I don't like to be called!"}
do_test sqllimits-1.7.2 {
  db function myfunc myfunc
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i <= $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT myfunc([join $vals ,])"
} {1 {too many arguments on function myfunc}}

#--------------------------------------------------------------------
# Test cases sqllimits-8.*: Test the SQLITE_MAX_ATTACHED limit.
#
ifcapable attach {
  do_test sqllimits-1.8.1 {
    set max $::SQLITE_MAX_ATTACHED
    for {set i 0} {$i < ($max)} {incr i} {
      file delete -force test${i}.db test${i}.db-journal
    }
    for {set i 0} {$i < ($max)} {incr i} {
      execsql "ATTACH 'test${i}.db' AS aux${i}"
    }
    catchsql "ATTACH 'test${i}.db' AS aux${i}"
  } "1 {too many attached databases - max $::SQLITE_MAX_ATTACHED}"
  do_test sqllimits-1.8.2 {
    set max $::SQLITE_MAX_ATTACHED
    for {set i 0} {$i < ($max)} {incr i} {
      execsql "DETACH aux${i}"
    }
  } {}
}

#--------------------------------------------------------------------
# Test cases sqllimits-9.*: Check that the SQLITE_MAX_VARIABLE_NUMBER 
# limit works.
#
do_test sqllimits-1.9.1 {
  set max $::SQLITE_MAX_VARIABLE_NUMBER
  catchsql "SELECT ?[expr {$max+1}] FROM t1"
} "1 {variable number must be between ?1 and ?$::SQLITE_MAX_VARIABLE_NUMBER}"
do_test sqllimits-1.9.2 {
  set max $::SQLITE_MAX_VARIABLE_NUMBER
  set vals [list]
  for {set i 0} {$i < ($max+3)} {incr i} {
    lappend vals ?
  }
  catchsql "SELECT [join $vals ,] FROM t1"
} "1 {too many SQL variables}"


#--------------------------------------------------------------------
# sqllimits-10.*:  Test the SQLITE_MAX_PAGE_SIZE define is enforced. 
# This is probably tested elsewhere too (pagerX.test). Attempts
# to raise the page size above this limit are silently ignored.
#
do_test sqllimits-1.10.1 {
  db close
  file delete -force test.db test.db-journal
  sqlite3 db test.db
  set max $::SQLITE_MAX_PAGE_SIZE
  catchsql "PRAGMA page_size = [expr {$max*2}]"
} {0 {}}
do_test sqllimits-1.10.2 {
  catchsql "PRAGMA page_size"
} {0 1024}
do_test sqllimits-1.10.3 {
  set max $::SQLITE_MAX_PAGE_SIZE
  catchsql "PRAGMA page_size = $max"
} {0 {}}
do_test sqllimits-1.10.4 {
  execsql "pragma page_size"
} $::SQLITE_MAX_PAGE_SIZE
do_test sqllimits-1.10.5 {
  set max $::SQLITE_MAX_PAGE_SIZE
  execsql "pragma page_size = [expr {$max - 5}]"
  execsql "pragma page_size"
} $::SQLITE_MAX_PAGE_SIZE

# Opening a database where the page size is too large should generate
# an error.
#
do_test sqllimits-1.10.5 {
  db close
  file delete -force test.db
  set ::SQLITE_MAX_PAGE_SIZE 32768
  sqlite3 db test.db
  db eval {PRAGMA page_size=32768}
  db eval {CREATE TABLE t1(x);}
  db eval {PRAGMA page_size}
} {32768}
do_test sqllimits-1.10.6 {
  db close
  set ::SQLITE_MAX_PAGE_SIZE 8192
  sqlite3 db test.db
  catchsql {SELECT name FROM sqlite_master}
} {1 {file is encrypted or is not a database}}
db close
file delete -force test.db
sqlite3 db test.db

#--------------------------------------------------------------------
# Test cases sqllimits-11.* verify that the 
# SQLITE_MAX_LIKE_PATTERN_LENGTH limit is enforced. This limit only
# applies to the built-in LIKE operator, supplying an external 
# implementation by overriding the like() scalar function bypasses
# this limitation.
#
# These tests check that the limit is not incorrectly applied to
# the left-hand-side of the LIKE operator (the string being tested
# against the pattern).
#
do_test sqllimits-1.11.1 {
  set max $::SQLITE_MAX_LIKE_PATTERN_LENGTH
  set ::pattern [string repeat "A%" [expr $max/2]]
  set ::string  [string repeat "A" [expr {$max*2}]]
  execsql {
    SELECT $::string LIKE $::pattern;
  }
} {1}
do_test sqllimits-1.11.2 {
  set max $::SQLITE_MAX_LIKE_PATTERN_LENGTH
  set ::pattern [string repeat "A%" [expr {($max/2) + 1}]]
  set ::string  [string repeat "A" [expr {$max*2}]]
  catchsql {
    SELECT $::string LIKE $::pattern;
  }
} {1 {LIKE or GLOB pattern too complex}}

#--------------------------------------------------------------------
# This test case doesn't really belong with the other limits tests.
# It is in this file because it is taxing to run, like the limits tests.
#
do_test sqllimits-1.12.1 {
  set ::N [expr int(([expr pow(2,32)]/50) + 1)]
  expr (($::N*50) & 0xffffffff)<55
} {1}
do_test sqllimits-1.12.2 {
  set ::format "[string repeat A 60][string repeat "%J" $::N]"
  catchsql {
    SELECT strftime($::format, 1);
  }
} {1 {string or blob too big}}


foreach {key value} [array get saved] {
  catch {set $key $value}
}
finish_test







|






|
|
|
|


|
|
|
|
|



|
|
|







 







|


|


|







|



|



|



|



|



|





|




|





|




|




|



|


|



|







 







|


|



|


|






|


|






|


|









|



|












|





|


|




|







 







>
>
>
>
>







 







|







 







|









|








|








|








|










|




|




|








|








|










|





|

|









|









|







 







|







|







|

|







|











|










|


|









|








|


|



|










|



|






|


|



|


|








|








|










|









|







|












|



|











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
..
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
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
...
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
...
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests to verify that the limits defined in
# sqlite source file limits.h are enforced.
#
# $Id: sqllimits1.test,v 1.24 2008/01/31 14:54:44 drh Exp $

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

# Test organization:
#
#     sqllimits1-1.*:  SQLITE_MAX_LENGTH
#     sqllimits1-2.*:  SQLITE_MAX_SQL_LENGTH
#     sqllimits1-3.*:  SQLITE_MAX_PAGE_COUNT (and the max_page_count pragma)
#     sqllimits1-4.*:  SQLITE_MAX_COLUMN
#
#
#     sqllimits1-7.*:   SQLITE_MAX_FUNCTION_ARG  
#     sqllimits1-8.*:   SQLITE_MAX_ATTACHED
#     sqllimits1-9.*:   SQLITE_MAX_VARIABLE_NUMBER
#     sqllimits1-10.*:  SQLITE_MAX_PAGE_SIZE
#     sqllimits1-11.*:  SQLITE_MAX_LIKE_PATTERN_LENGTH
#
# Todo:
#
#     sqllimits1-5.*:   SQLITE_MAX_EXPR_DEPTH            (sqlite todo)
#     sqllimits1-6.*:   SQLITE_MAX_VDBE_OP               (sqlite todo)
#     sqllimits1-12.*:  SQLITE_MAX_PAGE_COUNT            (sqlite todo)
#

unset -nocomplain saved
foreach var [info vars SQLITE_MAX_*] {
  set saved($var) [set $var]
}

................................................................................
set SQLITE_MAX_FUNCTION_ARG
set SQLITE_MAX_VARIABLE_NUMBER
set SQLITE_MAX_PAGE_SIZE
set SQLITE_MAX_PAGE_COUNT
set SQLITE_MAX_LIKE_PATTERN_LENGTH      1000

#--------------------------------------------------------------------
# Test cases sqllimits1-1.* test that the SQLITE_MAX_LENGTH limit
# is enforced.
#
do_test sqllimits1-1.1.1 {
  catchsql { SELECT randomblob(2147483647) }
} {1 {string or blob too big}}
do_test sqllimits1-1.1.2 {
  catchsql { SELECT zeroblob(2147483647) }
} {1 {string or blob too big}}

# Large, but allowable, blob-size.
#
set ::LARGESIZE [expr $SQLITE_MAX_LENGTH - 1] 

do_test sqllimits1-1.2 {
  catchsql { SELECT LENGTH(randomblob($::LARGESIZE)) }
} "0 $::LARGESIZE"

do_test sqllimits1-1.3 {
  catchsql { SELECT quote(randomblob($::LARGESIZE)) }
} {1 {string or blob too big}}

do_test sqllimits1-1.4 {
  catchsql { SELECT LENGTH(zeroblob($::LARGESIZE)) }
} "0 $::LARGESIZE"

do_test sqllimits1-1.5 {
  catchsql { SELECT quote(zeroblob($::LARGESIZE)) }
} {1 {string or blob too big}}

do_test sqllimits1-1.6 {
  catchsql { SELECT zeroblob(-1) }
} {0 {{}}}

do_test sqllimits1-1.9 {
  set ::str [string repeat A 65537]
  set ::rep [string repeat B 65537]
  catchsql { SELECT replace($::str, 'A', $::rep) }
} {1 {string or blob too big}}

do_test sqllimits1-1.10 {
  set ::str [string repeat %J 2100]
  catchsql { SELECT strftime($::str, '2003-10-31') }
} {1 {string or blob too big}}

do_test sqllimits1-1.11 {
  set ::str1 [string repeat A [expr {$SQLITE_MAX_LENGTH - 10}]]
  set ::str2 [string repeat B [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT $::str1 || $::str2 }
} {1 {string or blob too big}}

do_test sqllimits1-1.12 {
  set ::str1 [string repeat ' [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT quote($::str1) }
} {1 {string or blob too big}}

do_test sqllimits1-1.13 {
  set ::str1 [string repeat ' [expr {$SQLITE_MAX_LENGTH - 10}]]
  catchsql { SELECT hex($::str1) }
} {1 {string or blob too big}}

do_test sqllimits1-1.14.1 {
  set ::STMT [sqlite3_prepare $::DB "SELECT ?" -1 TAIL]
  sqlite3_bind_zeroblob $::STMT 1 [expr {$SQLITE_MAX_LENGTH + 1}]
} {}
do_test sqllimits1-1.14.2 {
  sqlite3_step $::STMT 
} {SQLITE_ERROR}
do_test sqllimits1-1.14.3 {
  sqlite3_finalize $::STMT 
} {SQLITE_TOOBIG}

do_test sqllimits1-1.15 {
  execsql {
    CREATE TABLE t4(x);
    INSERT INTO t4 VALUES(1);
    INSERT INTO t4 VALUES(2);
    INSERT INTO t4 SELECT 2+x FROM t4;
  }
  catchsql {
................................................................................
    SELECT group_concat(hex(randomblob(20000))) FROM t4;
  }
} {1 {string or blob too big}}
db eval {DROP TABLE t4}

set ::SQLITE_MAX_SQL_LENGTH 0
set strvalue [string repeat A $::SQLITE_MAX_LENGTH]
do_test sqllimits1-1.16 {
  catchsql "SELECT '$strvalue'"
} [list 0 $strvalue]
do_test sqllimits1-1.17 {
  catchsql "SELECT 'A$strvalue'"
} [list 1 {string or blob too big}]
set blobvalue [string repeat 41 $::SQLITE_MAX_LENGTH]
do_test sqllimits1-1.18 {
  catchsql "SELECT x'$blobvalue'"
} [list 0 $strvalue]
do_test sqllimits1-1.19 {
  catchsql "SELECT '41$blobvalue'"
} [list 1 {string or blob too big}]
unset blobvalue

ifcapable datetime {
  set strvalue [string repeat D [expr {$SQLITE_MAX_LENGTH-12}]]
  do_test sqllimits1-1.20 {
    catchsql {SELECT strftime('%Y ' || $::strvalue, '2008-01-02')}
  } [list 0 [list "2008 $strvalue"]]
  do_test sqllimits1-1.21 {
    catchsql {SELECT strftime('%Y-%m-%d ' || $::strvalue, '2008-01-02')}
  } {1 {string or blob too big}}
}
unset strvalue

#--------------------------------------------------------------------
# Test cases sqllimits1-2.* test that the SQLITE_MAX_SQL_LENGTH limit
# is enforced.
#
do_test sqllimits1-2.1 {
  set ::SQLITE_MAX_SQL_LENGTH 50000
  set sql "SELECT 1 WHERE 1==1"
  set tail " /* A comment to take up space in order to make the string\
                longer without increasing the expression depth */\
                AND   1  ==  1"
  set N [expr {($::SQLITE_MAX_SQL_LENGTH / [string length $tail])+1}]
  append sql [string repeat $tail $N]
  catchsql $sql
} {1 {String or BLOB exceeded size limit}}
do_test sqllimits1-2.2 {
  set ::SQLITE_MAX_SQL_LENGTH 0
  catchsql $sql
} {0 1}
do_test sqllimits1-2.3 {
  set ::SQLITE_MAX_SQL_LENGTH 50000
  set sql "SELECT 1 WHERE 1==1"
  set tail " /* A comment to take up space in order to make the string\
                longer without increasing the expression depth */\
                AND   1  ==  1"
  set N [expr {($::SQLITE_MAX_SQL_LENGTH / [string length $tail])+1}]
  append sql [string repeat $tail $N]
  set nbytes [string length $sql]
  append sql { AND 0}
  set rc [catch {sqlite3_prepare $DB $sql $nbytes TAIL} STMT]
  lappend rc $STMT
} {1 {(18) statement too long}}
do_test sqllimits1-2.4 {
  sqlite3_errmsg $DB
} {statement too long}


#--------------------------------------------------------------------
# Test cases sqllimits1-3.* test that the limit set using the
# max_page_count pragma.
#
do_test sqllimits1-3.1 {
  execsql {
    PRAGMA max_page_count = 1000;
  }
} {1000}
do_test sqllimits1-3.2 {
  execsql { CREATE TABLE trig (a INTEGER, b INTEGER); }

  # Set up a tree of triggers to fire when a row is inserted
  # into table "trig".
  #
  # INSERT -> insert_b -> update_b -> insert_a -> update_a      (chain 1)
  #                    -> update_a -> insert_a -> update_b      (chain 2)
................................................................................
    SELECT COUNT(*) FROM trig;
  }
} {0 7}

# Now check the response of the library to opening a file larger than
# the current max_page_count value. The response is to change the
# internal max_page_count value to match the actual size of the file.
if {[db eval {PRAGMA auto_vacuum}]} {
   set fsize 1700
} else {
   set fsize 1691
}
do_test sqllimits1-3.7.1 {
  execsql {
    PRAGMA max_page_count = 1000000;
    CREATE TABLE abc(a, b, c);
    INSERT INTO abc VALUES(1, 2, 3);
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
................................................................................
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a||b||c, b||c||a, c||a||b FROM abc;
    INSERT INTO abc SELECT a, b, c FROM abc;
    INSERT INTO abc SELECT b, a, c FROM abc;
    INSERT INTO abc SELECT c, b, a FROM abc;
  }
  expr [file size test.db] / 1024
} $fsize
do_test sqllimits1-3.7.2 {
  db close
  sqlite3 db test.db
  execsql {
    PRAGMA max_page_count = 1000;
  }
  execsql {
................................................................................
    SELECT count(*) FROM sqlite_master;
  }
} {6}
do_test sqllimits1-3.7.3 {
  execsql {
    PRAGMA max_page_count;
  }
} $fsize
do_test sqllimits1-3.7.4 {
  execsql {
    DROP TABLE abc;
  }
} {}

#--------------------------------------------------------------------
# Test cases sqllimits1-4.* test the SQLITE_MAX_COLUMN limit.
#
do_test sqllimits1-4.1 {
  # Columns in a table.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE TABLE t([join $cols ,])" 
} {1 {too many columns on t}}

do_test sqllimits1-4.2 {
  # Columns in the result-set of a SELECT.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "sql AS sql$i"
  }
  catchsql "SELECT [join $cols ,] FROM sqlite_master"
} {1 {too many columns in result set}}

do_test sqllimits1-4.3 {
  # Columns in the result-set of a sub-SELECT.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "sql AS sql$i"
  }
  catchsql "SELECT sql4 FROM (SELECT [join $cols ,] FROM sqlite_master)"
} {1 {too many columns in result set}}

do_test sqllimits1-4.4 {
  # Columns in an index.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols c
  }
  set sql1 "CREATE TABLE t1(c);"
  set sql2 "CREATE INDEX i1 ON t1([join $cols ,]);"
  catchsql "$sql1 ; $sql2"
} {1 {too many columns in index}}

do_test sqllimits1-4.5 {
  # Columns in a GROUP BY clause.
  catchsql "SELECT * FROM t1 GROUP BY [join $cols ,]"
} {1 {too many terms in GROUP BY clause}}

do_test sqllimits1-4.6 {
  # Columns in an ORDER BY clause.
  catchsql "SELECT * FROM t1 ORDER BY [join $cols ,]"
} {1 {too many terms in ORDER BY clause}}

do_test sqllimits1-4.7 {
  # Assignments in an UPDATE statement.
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c = 1"
  }
  catchsql "UPDATE t1 SET [join $cols ,];"
} {1 {too many columns in set list}}

do_test sqllimits1-4.8 {
  # Columns in a view definition:
  set cols [list]
  for {set i 0} {$i <= $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE VIEW v1 AS SELECT [join $cols ,] FROM t1;"
} {1 {too many columns in result set}}

do_test sqllimits1-4.9 {
  # Columns in a view definition (testing * expansion):
  set cols [list]
  for {set i 0} {$i < $SQLITE_MAX_COLUMN} {incr i} {
    lappend cols "c$i"
  }
  catchsql "CREATE TABLE t2([join $cols ,])"
  catchsql "CREATE VIEW v1 AS SELECT *, c1 AS o FROM t2;"
} {1 {too many columns in result set}}

#--------------------------------------------------------------------
# These tests - sqllimits1-5.* - test that the SQLITE_MAX_EXPR_DEPTH
# limit is enforced. The limit refers to the number of terms in 
# the expression.
#
if {$::SQLITE_MAX_EXPR_DEPTH != 1000} {
  puts -nonewline stderr "WARNING: Compile with -DSQLITE_MAX_EXPR_DEPTH to run "
  puts stderr "tests sqllimits1-5.X"
} else {
  do_test sqllimits1-5.1 {
    set max $::SQLITE_MAX_EXPR_DEPTH
    set expr "(1 [string repeat {AND 1 } $max])"
    catchsql [subst {
      SELECT $expr
    }]
  } "1 {Expression tree is too large (maximum depth $::SQLITE_MAX_EXPR_DEPTH)}"
  
  # Attempting to beat the expression depth limit using nested SELECT
  # queries causes a parser stack overflow. 
  do_test sqllimits1-5.2 {
    set max $::SQLITE_MAX_EXPR_DEPTH
    set expr "SELECT 1"
    for {set i 0} {$i <= $max} {incr i} {
      set expr "SELECT ($expr)"
    }
    catchsql [subst { $expr }]
  } "1 {parser stack overflow}"
  
  
  do_test sqllimits1-5.3 {
    execsql {
      PRAGMA max_page_count = 1000000;  -- 1 GB
      CREATE TABLE v0(a);
      INSERT INTO v0 VALUES(1);
    }
    db transaction {
      for {set i 1} {$i < 200} {incr i} {
................................................................................
        execsql [subst {
          CREATE VIEW v${i} AS SELECT $expr FROM v[expr {$i-1}]
        }]
      }
    }
  } {}
  
  do_test sqllimits1-5.4 {
    catchsql {
      SELECT a FROM v199
    }
  } "1 {Expression tree is too large (maximum depth $::SQLITE_MAX_EXPR_DEPTH)}"
}

#--------------------------------------------------------------------
# Test cases sqllimits1-6.* test that the SQLITE_MAX_VDBE_OP
# limit works as expected. The limit refers to the number of opcodes
# in a single VDBE program.
#
# TODO

#--------------------------------------------------------------------
# Test the SQLITE_MAX_FUNCTION_ARG limit works. Test case names
# match the pattern "sqllimits1-7.*".
#
do_test sqllimits1-7.1 {
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i < $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT max([join $vals ,])"
} "0 [expr {$::SQLITE_MAX_FUNCTION_ARG - 1}]"
do_test sqllimits1-7.2 {
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i <= $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT max([join $vals ,])"
} {1 {too many arguments on function max}}

# Test that it is SQLite, and not the implementation of the
# user function that is throwing the error.
proc myfunc {args} {error "I don't like to be called!"}
do_test sqllimits1-7.2 {
  db function myfunc myfunc
  set max $::SQLITE_MAX_FUNCTION_ARG
  set vals [list]
  for {set i 0} {$i <= $SQLITE_MAX_FUNCTION_ARG} {incr i} {
    lappend vals $i
  }
  catchsql "SELECT myfunc([join $vals ,])"
} {1 {too many arguments on function myfunc}}

#--------------------------------------------------------------------
# Test cases sqllimits1-8.*: Test the SQLITE_MAX_ATTACHED limit.
#
ifcapable attach {
  do_test sqllimits1-8.1 {
    set max $::SQLITE_MAX_ATTACHED
    for {set i 0} {$i < ($max)} {incr i} {
      file delete -force test${i}.db test${i}.db-journal
    }
    for {set i 0} {$i < ($max)} {incr i} {
      execsql "ATTACH 'test${i}.db' AS aux${i}"
    }
    catchsql "ATTACH 'test${i}.db' AS aux${i}"
  } "1 {too many attached databases - max $::SQLITE_MAX_ATTACHED}"
  do_test sqllimits1-8.2 {
    set max $::SQLITE_MAX_ATTACHED
    for {set i 0} {$i < ($max)} {incr i} {
      execsql "DETACH aux${i}"
    }
  } {}
}

#--------------------------------------------------------------------
# Test cases sqllimits1-9.*: Check that the SQLITE_MAX_VARIABLE_NUMBER 
# limit works.
#
do_test sqllimits1-9.1 {
  set max $::SQLITE_MAX_VARIABLE_NUMBER
  catchsql "SELECT ?[expr {$max+1}] FROM t1"
} "1 {variable number must be between ?1 and ?$::SQLITE_MAX_VARIABLE_NUMBER}"
do_test sqllimits1-9.2 {
  set max $::SQLITE_MAX_VARIABLE_NUMBER
  set vals [list]
  for {set i 0} {$i < ($max+3)} {incr i} {
    lappend vals ?
  }
  catchsql "SELECT [join $vals ,] FROM t1"
} "1 {too many SQL variables}"


#--------------------------------------------------------------------
# sqllimits1-10.*:  Test the SQLITE_MAX_PAGE_SIZE define is enforced. 
# This is probably tested elsewhere too (pagerX.test). Attempts
# to raise the page size above this limit are silently ignored.
#
do_test sqllimits1-10.1 {
  db close
  file delete -force test.db test.db-journal
  sqlite3 db test.db
  set max $::SQLITE_MAX_PAGE_SIZE
  catchsql "PRAGMA page_size = [expr {$max*2}]"
} {0 {}}
do_test sqllimits1-10.2 {
  catchsql "PRAGMA page_size"
} {0 1024}
do_test sqllimits1-10.3 {
  set max $::SQLITE_MAX_PAGE_SIZE
  catchsql "PRAGMA page_size = $max"
} {0 {}}
do_test sqllimits1-10.4 {
  execsql "pragma page_size"
} $::SQLITE_MAX_PAGE_SIZE
do_test sqllimits1-10.5 {
  set max $::SQLITE_MAX_PAGE_SIZE
  execsql "pragma page_size = [expr {$max - 5}]"
  execsql "pragma page_size"
} $::SQLITE_MAX_PAGE_SIZE

# Opening a database where the page size is too large should generate
# an error.
#
do_test sqllimits1-10.5 {
  db close
  file delete -force test.db
  set ::SQLITE_MAX_PAGE_SIZE 32768
  sqlite3 db test.db
  db eval {PRAGMA page_size=32768}
  db eval {CREATE TABLE t1(x);}
  db eval {PRAGMA page_size}
} {32768}
do_test sqllimits1-10.6 {
  db close
  set ::SQLITE_MAX_PAGE_SIZE 8192
  sqlite3 db test.db
  catchsql {SELECT name FROM sqlite_master}
} {1 {file is encrypted or is not a database}}
db close
file delete -force test.db
sqlite3 db test.db

#--------------------------------------------------------------------
# Test cases sqllimits1-11.* verify that the 
# SQLITE_MAX_LIKE_PATTERN_LENGTH limit is enforced. This limit only
# applies to the built-in LIKE operator, supplying an external 
# implementation by overriding the like() scalar function bypasses
# this limitation.
#
# These tests check that the limit is not incorrectly applied to
# the left-hand-side of the LIKE operator (the string being tested
# against the pattern).
#
do_test sqllimits1-11.1 {
  set max $::SQLITE_MAX_LIKE_PATTERN_LENGTH
  set ::pattern [string repeat "A%" [expr $max/2]]
  set ::string  [string repeat "A" [expr {$max*2}]]
  execsql {
    SELECT $::string LIKE $::pattern;
  }
} {1}
do_test sqllimits1-11.2 {
  set max $::SQLITE_MAX_LIKE_PATTERN_LENGTH
  set ::pattern [string repeat "A%" [expr {($max/2) + 1}]]
  set ::string  [string repeat "A" [expr {$max*2}]]
  catchsql {
    SELECT $::string LIKE $::pattern;
  }
} {1 {LIKE or GLOB pattern too complex}}

#--------------------------------------------------------------------
# This test case doesn't really belong with the other limits tests.
# It is in this file because it is taxing to run, like the limits tests.
#
do_test sqllimits1-12.1 {
  set ::N [expr int(([expr pow(2,32)]/50) + 1)]
  expr (($::N*50) & 0xffffffff)<55
} {1}
do_test sqllimits1-12.2 {
  set ::format "[string repeat A 60][string repeat "%J" $::N]"
  catchsql {
    SELECT strftime($::format, 1);
  }
} {1 {string or blob too big}}


foreach {key value} [array get saved] {
  catch {set $key $value}
}
finish_test