sl@0: # 2006 January 02
sl@0: #
sl@0: # The author disclaims copyright to this source code.  In place of
sl@0: # a legal notice, here is a blessing:
sl@0: #
sl@0: #    May you do good and not evil.
sl@0: #    May you find forgiveness for yourself and forgive others.
sl@0: #    May you share freely, never taking more than you give.
sl@0: #
sl@0: #*************************************************************************
sl@0: # This file implements regression tests for SQLite library.  The
sl@0: # focus of this script is descending indices.
sl@0: #
sl@0: # $Id: descidx3.test,v 1.6 2008/03/19 00:21:31 drh Exp $
sl@0: #
sl@0: 
sl@0: set testdir [file dirname $argv0]
sl@0: source $testdir/tester.tcl
sl@0: 
sl@0: ifcapable !bloblit {
sl@0:   finish_test
sl@0:   return
sl@0: }
sl@0: db eval {PRAGMA legacy_file_format=OFF}
sl@0: 
sl@0: # This procedure sets the value of the file-format in file 'test.db'
sl@0: # to $newval. Also, the schema cookie is incremented.
sl@0: # 
sl@0: proc set_file_format {newval} {
sl@0:   hexio_write test.db 44 [hexio_render_int32 $newval]
sl@0:   set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
sl@0:   incr schemacookie
sl@0:   hexio_write test.db 40 [hexio_render_int32 $schemacookie]
sl@0:   return {}
sl@0: }
sl@0: 
sl@0: # This procedure returns the value of the file-format in file 'test.db'.
sl@0: # 
sl@0: proc get_file_format {{fname test.db}} {
sl@0:   return [hexio_get_int [hexio_read $fname 44 4]]
sl@0: }
sl@0: 
sl@0: # Verify that the file format starts as 4.
sl@0: #
sl@0: do_test descidx3-1.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE t1(i INTEGER PRIMARY KEY,a,b,c,d);
sl@0:     CREATE INDEX t1i1 ON t1(a DESC, b ASC, c DESC);
sl@0:     CREATE INDEX t1i2 ON t1(b DESC, c ASC, d DESC);
sl@0:   }
sl@0:   get_file_format
sl@0: } {4}
sl@0: 
sl@0: # Put some information in the table and verify that the descending
sl@0: # index actually works.
sl@0: #
sl@0: do_test descidx3-2.1 {
sl@0:   execsql {
sl@0:     INSERT INTO t1 VALUES(1, NULL, NULL, NULL, NULL);
sl@0:     INSERT INTO t1 VALUES(2, 2, 2, 2, 2);
sl@0:     INSERT INTO t1 VALUES(3, 3, 3, 3, 3);
sl@0:     INSERT INTO t1 VALUES(4, 2.5, 2.5, 2.5, 2.5);
sl@0:     INSERT INTO t1 VALUES(5, -5, -5, -5, -5);
sl@0:     INSERT INTO t1 VALUES(6, 'six', 'six', 'six', 'six');
sl@0:     INSERT INTO t1 VALUES(7, x'77', x'77', x'77', x'77');
sl@0:     INSERT INTO t1 VALUES(8, 'eight', 'eight', 'eight', 'eight');
sl@0:     INSERT INTO t1 VALUES(9, x'7979', x'7979', x'7979', x'7979');
sl@0:     SELECT count(*) FROM t1;
sl@0:   }
sl@0: } 9
sl@0: do_test descidx3-2.2 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 ORDER BY a;
sl@0:   }
sl@0: } {1 5 2 4 3 8 6 7 9}
sl@0: do_test descidx3-2.3 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 ORDER BY a DESC;
sl@0:   }
sl@0: } {9 7 6 8 3 4 2 5 1}
sl@0: 
sl@0: # The "natural" order for the index is decreasing
sl@0: do_test descidx3-2.4 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE a<=x'7979';
sl@0:   }
sl@0: } {9 7 6 8 3 4 2 5}
sl@0: do_test descidx3-2.5 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE a>-99;
sl@0:   }
sl@0: } {9 7 6 8 3 4 2 5}
sl@0: 
sl@0: # Even when all values of t1.a are the same, sorting by A returns
sl@0: # the rows in reverse order because this the natural order of the
sl@0: # index.
sl@0: #
sl@0: do_test descidx3-3.1 {
sl@0:   execsql {
sl@0:     UPDATE t1 SET a=1;
sl@0:     SELECT i FROM t1 ORDER BY a;
sl@0:   }
sl@0: } {9 7 6 8 3 4 2 5 1}
sl@0: do_test descidx3-3.2 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE a=1 AND b>0 AND b<'zzz'
sl@0:   }
sl@0: } {2 4 3 8 6}
sl@0: do_test descidx3-3.3 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE b>0 AND b<'zzz'
sl@0:   }
sl@0: } {6 8 3 4 2}
sl@0: do_test descidx3-3.4 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE a=1 AND b>-9999 AND b<x'ffffffff'
sl@0:   }
sl@0: } {5 2 4 3 8 6 7 9}
sl@0: do_test descidx3-3.5 {
sl@0:   execsql {
sl@0:     SELECT i FROM t1 WHERE b>-9999 AND b<x'ffffffff'
sl@0:   }
sl@0: } {9 7 6 8 3 4 2 5}
sl@0: 
sl@0: ifcapable subquery {
sl@0:   # If the subquery capability is not compiled in to the binary, then
sl@0:   # the IN(...) operator is not available. Hence these tests cannot be 
sl@0:   # run.
sl@0:   do_test descidx3-4.1 {
sl@0:     execsql {
sl@0:       UPDATE t1 SET a=2 WHERE i<6;
sl@0:       SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
sl@0:     }
sl@0:   } {8 6 2 4 3}
sl@0:   do_test descidx3-4.2 {
sl@0:     execsql {
sl@0:       UPDATE t1 SET a=1;
sl@0:       SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
sl@0:     }
sl@0:   } {2 4 3 8 6}
sl@0:   do_test descidx3-4.3 {
sl@0:     execsql {
sl@0:       UPDATE t1 SET b=2;
sl@0:       SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
sl@0:     }
sl@0:   } {9 7 6 8 3 4 2 5 1}
sl@0: }
sl@0: 
sl@0: finish_test