forked from sqlite/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.tcl
2617 lines (2385 loc) · 75 KB
/
tester.tcl
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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
#-------------------------------------------------------------------------
# The commands provided by the code in this file to help with creating
# test cases are as follows:
#
# Commands to manipulate the db and the file-system at a high level:
#
# is_relative_file
# test_pwd
# get_pwd
# copy_file FROM TO
# delete_file FILENAME
# drop_all_tables ?DB?
# drop_all_indexes ?DB?
# forcecopy FROM TO
# forcedelete FILENAME
#
# Test the capability of the SQLite version built into the interpreter to
# determine if a specific test can be run:
#
# capable EXPR
# ifcapable EXPR
#
# Calulate checksums based on database contents:
#
# dbcksum DB DBNAME
# allcksum ?DB?
# cksum ?DB?
#
# Commands to execute/explain SQL statements:
#
# memdbsql SQL
# stepsql DB SQL
# execsql2 SQL
# explain_no_trace SQL
# explain SQL ?DB?
# catchsql SQL ?DB?
# execsql SQL ?DB?
#
# Commands to run test cases:
#
# do_ioerr_test TESTNAME ARGS...
# crashsql ARGS...
# integrity_check TESTNAME ?DB?
# verify_ex_errcode TESTNAME EXPECTED ?DB?
# do_test TESTNAME SCRIPT EXPECTED
# do_execsql_test TESTNAME SQL EXPECTED
# do_catchsql_test TESTNAME SQL EXPECTED
# do_timed_execsql_test TESTNAME SQL EXPECTED
#
# Commands providing a lower level interface to the global test counters:
#
# set_test_counter COUNTER ?VALUE?
# omit_test TESTNAME REASON ?APPEND?
# fail_test TESTNAME
# incr_ntest
#
# Command run at the end of each test file:
#
# finish_test
#
# Commands to help create test files that run with the "WAL" and other
# permutations (see file permutations.test):
#
# wal_is_wal_mode
# wal_set_journal_mode ?DB?
# wal_check_journal_mode TESTNAME?DB?
# permutation
# presql
#
# Command to test whether or not --verbose=1 was specified on the command
# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
# output file only").
#
# verbose
#
# Only run this script once. If sourced a second time, make it a no-op
if {[info exists ::tester_tcl_has_run]} return
# Set the precision of FP arithmatic used by the interpreter. And
# configure SQLite to take database file locks on the page that begins
# 64KB into the database file instead of the one 1GB in. This means
# the code that handles that special case can be tested without creating
# very large database files.
#
set tcl_precision 15
sqlite3_test_control_pending_byte 0x0010000
# If the pager codec is available, create a wrapper for the [sqlite3]
# command that appends "-key {xyzzy}" to the command line. i.e. this:
#
# sqlite3 db test.db
#
# becomes
#
# sqlite3 db test.db -key {xyzzy}
#
if {[info command sqlite_orig]==""} {
rename sqlite3 sqlite_orig
proc sqlite3 {args} {
if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
# This command is opening a new database connection.
#
if {[info exists ::G(perm:sqlite3_args)]} {
set args [concat $args $::G(perm:sqlite3_args)]
}
if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
lappend args -key {xyzzy}
}
set res [uplevel 1 sqlite_orig $args]
if {[info exists ::G(perm:presql)]} {
[lindex $args 0] eval $::G(perm:presql)
}
if {[info exists ::G(perm:dbconfig)]} {
set ::dbhandle [lindex $args 0]
uplevel #0 $::G(perm:dbconfig)
}
[lindex $args 0] cache size 3
set res
} else {
# This command is not opening a new database connection. Pass the
# arguments through to the C implementation as the are.
#
uplevel 1 sqlite_orig $args
}
}
}
proc getFileRetries {} {
if {![info exists ::G(file-retries)]} {
#
# NOTE: Return the default number of retries for [file] operations. A
# value of zero or less here means "disabled".
#
return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
}
return $::G(file-retries)
}
proc getFileRetryDelay {} {
if {![info exists ::G(file-retry-delay)]} {
#
# NOTE: Return the default number of milliseconds to wait when retrying
# failed [file] operations. A value of zero or less means "do not
# wait".
#
return 100; # TODO: Good default?
}
return $::G(file-retry-delay)
}
# Return the string representing the name of the current directory. On
# Windows, the result is "normalized" to whatever our parent command shell
# is using to prevent case-mismatch issues.
#
proc get_pwd {} {
if {$::tcl_platform(platform) eq "windows"} {
#
# NOTE: Cannot use [file normalize] here because it would alter the
# case of the result to what Tcl considers canonical, which would
# defeat the purpose of this procedure.
#
if {[info exists ::env(ComSpec)]} {
set comSpec $::env(ComSpec)
} else {
# NOTE: Hard-code the typical default value.
set comSpec {C:\Windows\system32\cmd.exe}
}
return [string map [list \\ /] \
[string trim [exec -- $comSpec /c CD]]]
} else {
return [pwd]
}
}
# Copy file $from into $to. This is used because some versions of
# TCL for windows (notably the 8.4.1 binary package shipped with the
# current mingw release) have a broken "file copy" command.
#
proc copy_file {from to} {
do_copy_file false $from $to
}
proc forcecopy {from to} {
do_copy_file true $from $to
}
proc do_copy_file {force from to} {
set nRetry [getFileRetries] ;# Maximum number of retries.
set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
# On windows, sometimes even a [file copy -force] can fail. The cause is
# usually "tag-alongs" - programs like anti-virus software, automatic backup
# tools and various explorer extensions that keep a file open a little longer
# than we expect, causing the delete to fail.
#
# The solution is to wait a short amount of time before retrying the copy.
#
if {$nRetry > 0} {
for {set i 0} {$i<$nRetry} {incr i} {
set rc [catch {
if {$force} {
file copy -force $from $to
} else {
file copy $from $to
}
} msg]
if {$rc==0} break
if {$nDelay > 0} { after $nDelay }
}
if {$rc} { error $msg }
} else {
if {$force} {
file copy -force $from $to
} else {
file copy $from $to
}
}
}
# Check if a file name is relative
#
proc is_relative_file { file } {
return [expr {[file pathtype $file] != "absolute"}]
}
# If the VFS supports using the current directory, returns [pwd];
# otherwise, it returns only the provided suffix string (which is
# empty by default).
#
proc test_pwd { args } {
if {[llength $args] > 0} {
set suffix1 [lindex $args 0]
if {[llength $args] > 1} {
set suffix2 [lindex $args 1]
} else {
set suffix2 $suffix1
}
} else {
set suffix1 ""; set suffix2 ""
}
ifcapable curdir {
return "[get_pwd]$suffix1"
} else {
return $suffix2
}
}
# Delete a file or directory
#
proc delete_file {args} {
do_delete_file false {*}$args
}
proc forcedelete {args} {
do_delete_file true {*}$args
}
proc do_delete_file {force args} {
set nRetry [getFileRetries] ;# Maximum number of retries.
set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
foreach filename $args {
# On windows, sometimes even a [file delete -force] can fail just after
# a file is closed. The cause is usually "tag-alongs" - programs like
# anti-virus software, automatic backup tools and various explorer
# extensions that keep a file open a little longer than we expect, causing
# the delete to fail.
#
# The solution is to wait a short amount of time before retrying the
# delete.
#
if {$nRetry > 0} {
for {set i 0} {$i<$nRetry} {incr i} {
set rc [catch {
if {$force} {
file delete -force $filename
} else {
file delete $filename
}
} msg]
if {$rc==0} break
if {$nDelay > 0} { after $nDelay }
}
if {$rc} { error $msg }
} else {
if {$force} {
file delete -force $filename
} else {
file delete $filename
}
}
}
}
proc execpresql {handle args} {
trace remove execution $handle enter [list execpresql $handle]
if {[info exists ::G(perm:presql)]} {
$handle eval $::G(perm:presql)
}
}
# This command should be called after loading tester.tcl from within
# all test scripts that are incompatible with encryption codecs.
#
proc do_not_use_codec {} {
set ::do_not_use_codec 1
reset_db
}
unset -nocomplain do_not_use_codec
# Return true if the "reserved_bytes" integer on database files is non-zero.
#
proc nonzero_reserved_bytes {} {
return [sqlite3 -has-codec]
}
# Print a HELP message and exit
#
proc print_help_and_quit {} {
puts {Options:
--pause Wait for user input before continuing
--soft-heap-limit=N Set the soft-heap-limit to N
--hard-heap-limit=N Set the hard-heap-limit to N
--maxerror=N Quit after N errors
--verbose=(0|1) Control the amount of output. Default '1'
--output=FILE set --verbose=2 and output to FILE. Implies -q
-q Shorthand for --verbose=0
--help This message
}
exit 1
}
# The following block only runs the first time this file is sourced. It
# does not run in slave interpreters (since the ::cmdlinearg array is
# populated before the test script is run in slave interpreters).
#
if {[info exists cmdlinearg]==0} {
# Parse any options specified in the $argv array. This script accepts the
# following options:
#
# --pause
# --soft-heap-limit=NN
# --hard-heap-limit=NN
# --maxerror=NN
# --malloctrace=N
# --backtrace=N
# --binarylog=N
# --soak=N
# --file-retries=N
# --file-retry-delay=N
# --start=[$permutation:]$testfile
# --match=$pattern
# --verbose=$val
# --output=$filename
# -q Reduce output
# --testdir=$dir Run tests in subdirectory $dir
# --help
#
set cmdlinearg(soft-heap-limit) 0
set cmdlinearg(hard-heap-limit) 0
set cmdlinearg(maxerror) 1000
set cmdlinearg(malloctrace) 0
set cmdlinearg(backtrace) 10
set cmdlinearg(binarylog) 0
set cmdlinearg(soak) 0
set cmdlinearg(file-retries) 0
set cmdlinearg(file-retry-delay) 0
set cmdlinearg(start) ""
set cmdlinearg(match) ""
set cmdlinearg(verbose) ""
set cmdlinearg(output) ""
set cmdlinearg(testdir) "testdir"
set leftover [list]
foreach a $argv {
switch -regexp -- $a {
{^-+pause$} {
# Wait for user input before continuing. This is to give the user an
# opportunity to connect profiling tools to the process.
puts -nonewline "Press RETURN to begin..."
flush stdout
gets stdin
}
{^-+soft-heap-limit=.+$} {
foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
}
{^-+hard-heap-limit=.+$} {
foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break
}
{^-+maxerror=.+$} {
foreach {dummy cmdlinearg(maxerror)} [split $a =] break
}
{^-+malloctrace=.+$} {
foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
if {$cmdlinearg(malloctrace)} {
if {0==$::sqlite_options(memdebug)} {
set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build"
puts stderr $err
exit 1
}
sqlite3_memdebug_log start
}
}
{^-+backtrace=.+$} {
foreach {dummy cmdlinearg(backtrace)} [split $a =] break
sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
}
{^-+binarylog=.+$} {
foreach {dummy cmdlinearg(binarylog)} [split $a =] break
set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
}
{^-+soak=.+$} {
foreach {dummy cmdlinearg(soak)} [split $a =] break
set ::G(issoak) $cmdlinearg(soak)
}
{^-+file-retries=.+$} {
foreach {dummy cmdlinearg(file-retries)} [split $a =] break
set ::G(file-retries) $cmdlinearg(file-retries)
}
{^-+file-retry-delay=.+$} {
foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
}
{^-+start=.+$} {
foreach {dummy cmdlinearg(start)} [split $a =] break
set ::G(start:file) $cmdlinearg(start)
if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
set ::G(start:permutation) ${s.perm}
set ::G(start:file) ${s.file}
}
if {$::G(start:file) == ""} {unset ::G(start:file)}
}
{^-+match=.+$} {
foreach {dummy cmdlinearg(match)} [split $a =] break
set ::G(match) $cmdlinearg(match)
if {$::G(match) == ""} {unset ::G(match)}
}
{^-+output=.+$} {
foreach {dummy cmdlinearg(output)} [split $a =] break
set cmdlinearg(output) [file normalize $cmdlinearg(output)]
if {$cmdlinearg(verbose)==""} {
set cmdlinearg(verbose) 2
}
}
{^-+verbose=.+$} {
foreach {dummy cmdlinearg(verbose)} [split $a =] break
if {$cmdlinearg(verbose)=="file"} {
set cmdlinearg(verbose) 2
} elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
error "option --verbose= must be set to a boolean or to \"file\""
}
}
{^-+testdir=.*$} {
foreach {dummy cmdlinearg(testdir)} [split $a =] break
}
{.*help.*} {
print_help_and_quit
}
{^-q$} {
set cmdlinearg(output) test-out.txt
set cmdlinearg(verbose) 2
}
default {
if {[file tail $a]==$a} {
lappend leftover $a
} else {
lappend leftover [file normalize $a]
}
}
}
}
unset -nocomplain a
set testdir [file normalize $testdir]
set cmdlinearg(TESTFIXTURE_HOME) [file dirname [info nameofexec]]
set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
set argv0 [file normalize $argv0]
if {$cmdlinearg(testdir)!=""} {
file mkdir $cmdlinearg(testdir)
cd $cmdlinearg(testdir)
}
set argv $leftover
# Install the malloc layer used to inject OOM errors. And the 'automatic'
# extensions. This only needs to be done once for the process.
#
sqlite3_shutdown
install_malloc_faultsim 1
sqlite3_initialize
autoinstall_test_functions
# If the --binarylog option was specified, create the logging VFS. This
# call installs the new VFS as the default for all SQLite connections.
#
if {$cmdlinearg(binarylog)} {
vfslog new binarylog {} vfslog.bin
}
# Set the backtrace depth, if malloc tracing is enabled.
#
if {$cmdlinearg(malloctrace)} {
sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
}
if {$cmdlinearg(output)!=""} {
puts "Copying output to file $cmdlinearg(output)"
set ::G(output_fd) [open $cmdlinearg(output) w]
fconfigure $::G(output_fd) -buffering line
}
if {$cmdlinearg(verbose)==""} {
set cmdlinearg(verbose) 1
}
if {[info commands vdbe_coverage]!=""} {
vdbe_coverage start
}
}
# Update the soft-heap-limit each time this script is run. In that
# way if an individual test file changes the soft-heap-limit, it
# will be reset at the start of the next test file.
#
sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit)
sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit)
# Create a test database
#
proc reset_db {} {
catch {db close}
forcedelete test.db
forcedelete test.db-journal
forcedelete test.db-wal
sqlite3 db ./test.db
set ::DB [sqlite3_connection_pointer db]
if {[info exists ::SETUP_SQL]} {
db eval $::SETUP_SQL
}
}
reset_db
# Abort early if this script has been run before.
#
if {[info exists TC(count)]} return
# Make sure memory statistics are enabled.
#
sqlite3_config_memstatus 1
# Initialize the test counters and set up commands to access them.
# Or, if this is a slave interpreter, set up aliases to write the
# counters in the parent interpreter.
#
if {0==[info exists ::SLAVE]} {
set TC(errors) 0
set TC(count) 0
set TC(fail_list) [list]
set TC(omit_list) [list]
set TC(warn_list) [list]
proc set_test_counter {counter args} {
if {[llength $args]} {
set ::TC($counter) [lindex $args 0]
}
set ::TC($counter)
}
}
# Record the fact that a sequence of tests were omitted.
#
proc omit_test {name reason {append 1}} {
set omitList [set_test_counter omit_list]
if {$append} {
lappend omitList [list $name $reason]
}
set_test_counter omit_list $omitList
}
# Record the fact that a test failed.
#
proc fail_test {name} {
set f [set_test_counter fail_list]
lappend f $name
set_test_counter fail_list $f
set_test_counter errors [expr [set_test_counter errors] + 1]
set nFail [set_test_counter errors]
if {$nFail>=$::cmdlinearg(maxerror)} {
output2 "*** Giving up..."
finalize_testing
}
}
# Remember a warning message to be displayed at the conclusion of all testing
#
proc warning {msg {append 1}} {
output2 "Warning: $msg"
set warnList [set_test_counter warn_list]
if {$append} {
lappend warnList $msg
}
set_test_counter warn_list $warnList
}
# Increment the number of tests run
#
proc incr_ntest {} {
set_test_counter count [expr [set_test_counter count] + 1]
}
# Return true if --verbose=1 was specified on the command line. Otherwise,
# return false.
#
proc verbose {} {
return $::cmdlinearg(verbose)
}
# Use the following commands instead of [puts] for test output within
# this file. Test scripts can still use regular [puts], which is directed
# to stdout and, if one is open, the --output file.
#
# output1: output that should be printed if --verbose=1 was specified.
# output2: output that should be printed unconditionally.
# output2_if_no_verbose: output that should be printed only if --verbose=0.
#
proc output1 {args} {
set v [verbose]
if {$v==1} {
uplevel output2 $args
} elseif {$v==2} {
uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
}
}
proc output2 {args} {
set nArg [llength $args]
uplevel puts $args
}
proc output2_if_no_verbose {args} {
set v [verbose]
if {$v==0} {
uplevel output2 $args
} elseif {$v==2} {
uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
}
}
# Override the [puts] command so that if no channel is explicitly
# specified the string is written to both stdout and to the file
# specified by "--output=", if any.
#
proc puts_override {args} {
set nArg [llength $args]
if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
uplevel puts_original $args
if {[info exists ::G(output_fd)]} {
uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
}
} else {
# A channel was explicitly specified.
uplevel puts_original $args
}
}
rename puts puts_original
proc puts {args} { uplevel puts_override $args }
# Invoke the do_test procedure to run a single test
#
# The $expected parameter is the expected result. The result is the return
# value from the last TCL command in $cmd.
#
# Normally, $expected must match exactly. But if $expected is of the form
# "/regexp/" then regular expression matching is used. If $expected is
# "~/regexp/" then the regular expression must NOT match. If $expected is
# of the form "#/value-list/" then each term in value-list must be numeric
# and must approximately match the corresponding numeric term in $result.
# Values must match within 10%. Or if the $expected term is A..B then the
# $result term must be in between A and B.
#
proc do_test {name cmd expected} {
global argv cmdlinearg
fix_testname name
sqlite3_memdebug_settitle $name
# if {[llength $argv]==0} {
# set go 1
# } else {
# set go 0
# foreach pattern $argv {
# if {[string match $pattern $name]} {
# set go 1
# break
# }
# }
# }
if {[info exists ::G(perm:prefix)]} {
set name "$::G(perm:prefix)$name"
}
incr_ntest
output1 -nonewline $name...
flush stdout
if {![info exists ::G(match)] || [string match $::G(match) $name]} {
if {[catch {uplevel #0 "$cmd;\n"} result]} {
output2_if_no_verbose -nonewline $name...
output2 "\nError: $result"
fail_test $name
} else {
if {[permutation]=="maindbname"} {
set result [string map [list [string tolower ICECUBE] main] $result]
}
if {[regexp {^[~#]?/.*/$} $expected]} {
# "expected" is of the form "/PATTERN/" then the result if correct if
# regular expression PATTERN matches the result. "~/PATTERN/" means
# the regular expression must not match.
if {[string index $expected 0]=="~"} {
set re [string range $expected 2 end-1]
if {[string index $re 0]=="*"} {
# If the regular expression begins with * then treat it as a glob instead
set ok [string match $re $result]
} else {
set re [string map {# {[-0-9.]+}} $re]
set ok [regexp $re $result]
}
set ok [expr {!$ok}]
} elseif {[string index $expected 0]=="#"} {
# Numeric range value comparison. Each term of the $result is matched
# against one term of $expect. Both $result and $expected terms must be
# numeric. The values must match within 10%. Or if $expected is of the
# form A..B then the $result term must be between A and B.
set e2 [string range $expected 2 end-1]
foreach i $result j $e2 {
if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
set ok [expr {$i+0>=$A && $i+0<=$B}]
} else {
set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
}
if {!$ok} break
}
if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
} else {
set re [string range $expected 1 end-1]
if {[string index $re 0]=="*"} {
# If the regular expression begins with * then treat it as a glob instead
set ok [string match $re $result]
} else {
set re [string map {# {[-0-9.]+}} $re]
set ok [regexp $re $result]
}
}
} elseif {[regexp {^~?\*.*\*$} $expected]} {
# "expected" is of the form "*GLOB*" then the result if correct if
# glob pattern GLOB matches the result. "~/GLOB/" means
# the glob must not match.
if {[string index $expected 0]=="~"} {
set e [string range $expected 1 end]
set ok [expr {![string match $e $result]}]
} else {
set ok [string match $expected $result]
}
} else {
set ok [expr {[string compare $result $expected]==0}]
if {!$ok} {
set ok [fpnum_compare $result $expected]
}
}
if {!$ok} {
# if {![info exists ::testprefix] || $::testprefix eq ""} {
# error "no test prefix"
# }
output1 ""
output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
fail_test $name
} else {
output1 " Ok"
}
}
} else {
output1 " Omitted"
omit_test $name "pattern mismatch" 0
}
flush stdout
}
proc dumpbytes {s} {
set r ""
for {set i 0} {$i < [string length $s]} {incr i} {
if {$i > 0} {append r " "}
append r [format %02X [scan [string index $s $i] %c]]
}
return $r
}
proc catchcmd {db {cmd ""}} {
global CLI
set out [open cmds.txt w]
puts $out $cmd
close $out
set line "exec $CLI $db < cmds.txt"
set rc [catch { eval $line } msg]
list $rc $msg
}
proc catchsafecmd {db {cmd ""}} {
global CLI
set out [open cmds.txt w]
puts $out $cmd
close $out
set line "exec $CLI -safe $db < cmds.txt"
set rc [catch { eval $line } msg]
list $rc $msg
}
proc catchcmdex {db {cmd ""}} {
global CLI
set out [open cmds.txt w]
fconfigure $out -translation binary
puts -nonewline $out $cmd
close $out
set line "exec -keepnewline -- $CLI $db < cmds.txt"
set chans [list stdin stdout stderr]
foreach chan $chans {
catch {
set modes($chan) [fconfigure $chan]
fconfigure $chan -translation binary -buffering none
}
}
set rc [catch { eval $line } msg]
foreach chan $chans {
catch {
eval fconfigure [list $chan] $modes($chan)
}
}
# puts [dumpbytes $msg]
list $rc $msg
}
proc filepath_normalize {p} {
# test cases should be written to assume "unix"-like file paths
if {$::tcl_platform(platform)!="unix"} {
string map [list \\ / \{/ / .db\} .db] \
[regsub -nocase -all {[a-z]:[/\\]+} $p {/}]
} {
set p
}
}
proc do_filepath_test {name cmd expected} {
uplevel [list do_test $name [
subst -nocommands { filepath_normalize [ $cmd ] }
] [filepath_normalize $expected]]
}
proc realnum_normalize {r} {
# different TCL versions display floating point values differently.
string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
}
proc do_realnum_test {name cmd expected} {
uplevel [list do_test $name [
subst -nocommands { realnum_normalize [ $cmd ] }
] [realnum_normalize $expected]]
}
proc fix_testname {varname} {
upvar $varname testname
if {[info exists ::testprefix]
&& [string is digit [string range $testname 0 0]]
} {
set testname "${::testprefix}-$testname"
}
}
proc normalize_list {L} {
set L2 [list]
foreach l $L {lappend L2 $l}
set L2
}
# Run SQL and verify that the number of "vmsteps" required is greater
# than or less than some constant.
#
proc do_vmstep_test {tn sql nstep {res {}}} {
uplevel [list do_execsql_test $tn.0 $sql $res]
set vmstep [db status vmstep]
if {[string range $nstep 0 0]=="+"} {
set body "if {$vmstep<$nstep} {
error \"got $vmstep, expected more than [string range $nstep 1 end]\"
}"
} else {
set body "if {$vmstep>$nstep} {
error \"got $vmstep, expected less than $nstep\"
}"
}
# set name "$tn.vmstep=$vmstep,expect=$nstep"
set name "$tn.1"
uplevel [list do_test $name $body {}]
}
# Either:
#
# do_execsql_test TESTNAME SQL ?RES?
# do_execsql_test -db DB TESTNAME SQL ?RES?
#
proc do_execsql_test {args} {
set db db
if {[lindex $args 0]=="-db"} {
set db [lindex $args 1]
set args [lrange $args 2 end]
}
if {[llength $args]==2} {
foreach {testname sql} $args {}
set result ""
} elseif {[llength $args]==3} {
foreach {testname sql result} $args {}
# With some versions of Tcl on windows, if $result is all whitespace but
# contains some CR/LF characters, the [list {*}$result] below returns a
# copy of $result instead of a zero length string. Not clear exactly why
# this is. The following is a workaround.
if {[llength $result]==0} { set result "" }
} else {
error [string trim {
wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?"
}]
}
fix_testname testname
uplevel do_test \
[list $testname] \
[list "execsql {$sql} $db"] \
[list [list {*}$result]]
}
proc do_catchsql_test {testname sql result} {
fix_testname testname
uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
}
proc do_timed_execsql_test {testname sql {result {}}} {
fix_testname testname
uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
[list [list {*}$result]]
}
# Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output
# as an ASCII-art graph and return a string that is that graph.
#
# Hexadecimal literals in the output text are converted into "xxxxxx" since those
# literals are pointer values that might very from one run of the test to the
# next, yet we want the output to be consistent.
#
proc query_plan_graph {sql} {
db eval "EXPLAIN QUERY PLAN $sql" {
set dx($id) $detail
lappend cx($parent) $id
}
set a "\n QUERY PLAN\n"
append a [append_graph " " dx cx 0]
regsub -all {SUBQUERY 0x[A-F0-9]+\y} $a {SUBQUERY xxxxxx} a
regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a
regsub -all {\((join|subquery)-\d+\)} $a {(\1-xxxxxx)} a
return $a
}
# Helper routine for [query_plan_graph SQL]:
#
# Output rows of the graph that are children of $level.
#
# prefix: Prepend to every output line
#
# dxname: Name of an array variable that stores text describe