/ Check-in [b685d323]
Login

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

Overview
Comment:Make the tests in func6.test more robust against implementation changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b685d3231097fb90e7d61d9ac01cc560e8bf2671d49390ae7af5bfdbd6d04f11
User & Date: dan 2018-02-09 15:42:40
Context
2018-02-09
20:49
Add the zorder.c extension implementing zorder() and unzorder() SQL functions. check-in: a57a77dc user: drh tags: trunk
15:42
Make the tests in func6.test more robust against implementation changes. check-in: b685d323 user: dan tags: trunk
15:04
Fix a harmless compiler warning. check-in: a6c31154 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to test/func6.test.

    29     29     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
    30     30      INSERT INTO t1(a,b,c,d) SELECT printf('abc%03x',x), x, 1000-x, NULL FROM c;
    31     31     CREATE INDEX t1a ON t1(a);
    32     32     CREATE INDEX t1bc ON t1(b,c);
    33     33     CREATE TABLE t2(x TEXT PRIMARY KEY, y) WITHOUT ROWID;
    34     34     INSERT INTO t2(x,y) SELECT a, b FROM t1;
    35     35   }
           36  +
           37  +# Load the contents of $file from disk and return it encoded as a hex
           38  +# string.
           39  +proc loadhex {file} {
           40  +  set fd [open $file]
           41  +  fconfigure $fd -translation binary -encoding binary
           42  +  set data [read $fd]
           43  +  close $fd
           44  +  binary encode hex $data 
           45  +}
           46  +
           47  +# Each argument is either an integer between 0 and 65535, a text value, or
           48  +# an empty string representing an SQL NULL. This command builds an SQLite
           49  +# record containing the values passed as arguments and returns it encoded
           50  +# as a hex string.
           51  +proc hexrecord {args} {
           52  +  set hdr ""
           53  +  set body ""
           54  +  foreach x $args {
           55  +    if {$x==""} {
           56  +      append hdr 00
           57  +    } elseif {[string is integer $x]==0} {
           58  +      set n [string length $x]
           59  +      append hdr [format %02x [expr $n*2 + 13]]
           60  +      append body [binary encode hex $x]
           61  +    } elseif {$x == 0} {
           62  +      append hdr 08
           63  +    } elseif {$x == 1} {
           64  +      append hdr 09
           65  +    } elseif {$x <= 127} {
           66  +      append hdr 01
           67  +      append body [format %02x $x]
           68  +    } else {
           69  +      append hdr 02
           70  +      append body [format %04x $x]
           71  +    }
           72  +  }
           73  +  set res [format %02x [expr 1 + [string length $hdr]/2]]
           74  +  append res $hdr
           75  +  append res $body
           76  +}
           77  +
           78  +# Argument $off is an offset into the database image encoded as a hex string
           79  +# in argument $hexdb. This command returns 0 if the offset contains the hex
           80  +# $hexrec, or throws an exception otherwise.
           81  +#
           82  +proc offset_contains_record {off hexdb hexrec} {
           83  +  set n [string length $hexrec]
           84  +  set off [expr $off*2]
           85  +  if { [string compare $hexrec [string range $hexdb $off [expr $off+$n-1]]] } {
           86  +    error "record not found!"
           87  +  }
           88  +  return 0
           89  +}
           90  +
           91  +# This command is the implementation of SQL function "offrec()". The first
           92  +# argument to this is an offset value. The remaining values are used to
           93  +# formulate an SQLite record. If database file test.db does not contain
           94  +# an equivalent record at the specified offset, an exception is thrown.
           95  +# Otherwise, 0 is returned.
           96  +#
           97  +proc offrec {args} {
           98  +  set offset [lindex $args 0]
           99  +  set rec [hexrecord {*}[lrange $args 1 end]]
          100  +  offset_contains_record $offset $::F $rec
          101  +}
          102  +set F [loadhex test.db]
          103  +db func offrec offrec
          104  +
          105  +# Test the sanity of the tests.
          106  +do_execsql_test func6-105 {
          107  +  SELECT sqlite_offset(d) FROM t1 ORDER BY rowid LIMIT 1;
          108  +} {8179}
          109  +do_test func6-106 {
          110  +  set r [hexrecord abc001 1 999 {}]
          111  +  offset_contains_record 8179 $F $r
          112  +} 0
          113  +
          114  +set z100 [string trim [string repeat "0 " 100]]
          115  +
          116  +# Test offsets within table b-tree t1.
    36    117   do_execsql_test func6-110 {
    37         -  SELECT a, sqlite_offset(d)/4096 + 1,
    38         -            sqlite_offset(d)%4096 FROM t1
    39         -   ORDER BY rowid LIMIT 2;
    40         -} {abc001 2 4084 abc002 2 4069}
          118  +  SELECT offrec(sqlite_offset(d), a, b, c, d) FROM t1 ORDER BY rowid
          119  +} $z100
          120  +
    41    121   do_execsql_test func6-120 {
    42    122     SELECT a, typeof(sqlite_offset(+a)) FROM t1
    43    123      ORDER BY rowid LIMIT 2;
    44    124   } {abc001 null abc002 null}
          125  +
          126  +# Test offsets within index b-tree t1a.
    45    127   do_execsql_test func6-130 {
    46         -  SELECT a, sqlite_offset(a)/4096+1, 
    47         -         sqlite_offset(a)%4096
    48         -   FROM t1
    49         -   ORDER BY a LIMIT 2;
    50         -} {abc001 3 4087 abc002 3 4076}
          128  +  SELECT offrec(sqlite_offset(a), a, rowid) FROM t1 ORDER BY a
          129  +} $z100
          130  +
          131  +# Test offsets within table b-tree t1 with a temp b-tree ORDER BY.
    51    132   do_execsql_test func6-140 {
    52         -  SELECT a, sqlite_offset(d)/4096+1, 
    53         -         sqlite_offset(d)%4096
    54         -   FROM t1
    55         -   ORDER BY a LIMIT 2;
    56         -} {abc001 2 4084 abc002 2 4069}
          133  +  SELECT offrec(sqlite_offset(d), a, b, c, d) FROM t1 ORDER BY a
          134  +} $z100
          135  +
          136  +# Test offsets from both index t1a and table t1 in the same query.
    57    137   do_execsql_test func6-150 {
    58         -  SELECT a,
    59         -         sqlite_offset(a)/4096+1, 
    60         -         sqlite_offset(a)%4096,
    61         -         sqlite_offset(d)/4096+1, 
    62         -         sqlite_offset(d)%4096
    63         -   FROM t1
    64         -   ORDER BY a LIMIT 2;
    65         -} {abc001 3 4087 2 4084 abc002 3 4076 2 4069}
          138  +  SELECT offrec(sqlite_offset(a), a, rowid),
          139  +         offrec(sqlite_offset(d), a, b, c, d)
          140  +  FROM t1 ORDER BY a
          141  +} [concat $z100 $z100]
          142  +
          143  +# Test offsets from both index t1bc and table t1 in the same query.
    66    144   do_execsql_test func6-160 {
    67         -  SELECT b,
    68         -         sqlite_offset(b)/4096+1, 
    69         -         sqlite_offset(b)%4096,
    70         -         sqlite_offset(c)/4096+1, 
    71         -         sqlite_offset(c)%4096,
    72         -         sqlite_offset(d)/4096+1, 
    73         -         sqlite_offset(d)%4096
    74         -   FROM t1
    75         -   ORDER BY b LIMIT 2;
    76         -} {1 4 4090 4 4090 2 4084 2 4 4081 4 4081 2 4069}
          145  +  SELECT offrec(sqlite_offset(b), b, c, rowid),
          146  +         offrec(sqlite_offset(c), b, c, rowid),
          147  +         offrec(sqlite_offset(d), a, b, c, d)
          148  +  FROM t1
          149  +  ORDER BY b
          150  +} [concat $z100 $z100 $z100]
    77    151   
    78         -
          152  +# Test offsets in WITHOUT ROWID table t2.
    79    153   do_execsql_test func6-200 {
    80         -  SELECT y, sqlite_offset(y)/4096+1,
    81         -         sqlite_offset(y)%4096
    82         -   FROM t2
    83         -   ORDER BY x LIMIT 2;
    84         -} {1 5 4087 2 5 4076}
          154  +  SELECT offrec( sqlite_offset(y), x, y ) FROM t2 ORDER BY x
          155  +} $z100
    85    156   
    86    157   finish_test