drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 1 | # Copyright (c) 1999, 2000 D. Richard Hipp |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or |
| 4 | # modify it under the terms of the GNU General Public |
| 5 | # License as published by the Free Software Foundation; either |
| 6 | # version 2 of the License, or (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | # General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public |
| 14 | # License along with this library; if not, write to the |
| 15 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | # Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | # Author contact information: |
| 19 | # drh@hwaci.com |
| 20 | # http://www.hwaci.com/drh/ |
| 21 | # |
| 22 | #*********************************************************************** |
| 23 | # This file implements regression tests for SQLite library. The |
| 24 | # focus of this file is testing built-in functions. |
| 25 | # |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame^] | 26 | # $Id: func.test,v 1.2 2001/04/05 15:57:14 drh Exp $ |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 27 | |
| 28 | set testdir [file dirname $argv0] |
| 29 | source $testdir/tester.tcl |
| 30 | |
| 31 | # Create a table to work with. |
| 32 | # |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 33 | do_test func-0.0 { |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame^] | 34 | execsql {CREATE TABLE tbl1(t1 text)} |
| 35 | foreach word {this program is free software} { |
| 36 | execsql "INSERT INTO tbl1 VALUES('$word')" |
| 37 | } |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 38 | execsql {SELECT t1 FROM tbl1 ORDER BY t1} |
| 39 | } {free is program software this} |
| 40 | |
| 41 | # Check out the length() function |
| 42 | # |
| 43 | do_test func-1.0 { |
| 44 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
| 45 | } {4 2 7 8 4} |
| 46 | do_test func-1.1 { |
| 47 | set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg] |
| 48 | lappend r $msg |
| 49 | } {1 {too few arguments to function length()}} |
| 50 | do_test func-1.2 { |
| 51 | set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg] |
| 52 | lappend r $msg |
| 53 | } {1 {too many arguments to function length()}} |
| 54 | do_test func-1.3 { |
| 55 | execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1) |
| 56 | ORDER BY length(t1)} |
| 57 | } {2 1 4 2 7 1 8 1} |
| 58 | |
| 59 | # Check out the substr() function |
| 60 | # |
| 61 | do_test func-2.0 { |
| 62 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
| 63 | } {fr is pr so th} |
| 64 | do_test func-2.1 { |
| 65 | execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1} |
| 66 | } {r s r o h} |
| 67 | do_test func-2.2 { |
| 68 | execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1} |
| 69 | } {ee {} ogr ftw is} |
| 70 | do_test func-2.3 { |
| 71 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
| 72 | } {e s m e s} |
| 73 | do_test func-2.4 { |
| 74 | execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1} |
| 75 | } {e s m e s} |
| 76 | do_test func-2.5 { |
| 77 | execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1} |
| 78 | } {e i a r i} |
| 79 | do_test func-2.6 { |
| 80 | execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1} |
| 81 | } {ee is am re is} |
| 82 | do_test func-2.7 { |
| 83 | execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1} |
| 84 | } {fr {} gr wa th} |
| 85 | do_test func-2.8 { |
| 86 | execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)} |
| 87 | } {this software free program is} |
| 88 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame^] | 89 | # Only do the following tests if TCL has UTF-8 capabilities and |
| 90 | # the UTF-8 encoding is turned on in the SQLite library. |
| 91 | # |
| 92 | if {$::sqlite_encoding=="UTF-8" && "\u1234"!="u1234"} { |
| 93 | |
| 94 | # Put some UTF-8 characters in the database |
| 95 | # |
| 96 | do_test func-3.0 { |
| 97 | execsql {DELETE FROM tbl1} |
| 98 | foreach word "contains UTF-8 characters hi\u1234ho" { |
| 99 | execsql "INSERT INTO tbl1 VALUES('$word')" |
| 100 | } |
| 101 | execsql {SELECT t1 FROM tbl1 ORDER BY t1} |
| 102 | } "characters contains hi\u1234ho UTF-8" |
| 103 | do_test func-3.1 { |
| 104 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
| 105 | } {10 8 5 5} |
| 106 | do_test func-3.2 { |
| 107 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
| 108 | } {ch co hi UT} |
| 109 | do_test func-3.3 { |
| 110 | execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1} |
| 111 | } "cha con hi\u1234 UTF" |
| 112 | do_test func-3.4 { |
| 113 | execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1} |
| 114 | } "ha on i\u1234 TF" |
| 115 | do_test func-3.5 { |
| 116 | execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1} |
| 117 | } "har ont i\u1234h TF-" |
| 118 | do_test func-3.6 { |
| 119 | execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1} |
| 120 | } "ar nt \u1234h F-" |
| 121 | do_test func-3.7 { |
| 122 | execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1} |
| 123 | } "ra ta ho -8" |
| 124 | do_test func-3.8 { |
| 125 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
| 126 | } "s s o 8" |
| 127 | do_test func-3.9 { |
| 128 | execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1} |
| 129 | } "er in \u1234h F-" |
| 130 | do_test func-3.10 { |
| 131 | execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1} |
| 132 | } "ter ain i\u1234h TF-" |
| 133 | |
| 134 | } ;# End sqlite_encoding==UTF-8 and \u1234!=u1234 |
| 135 | |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 136 | finish_test |