drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 1 | # 2013-12-17 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the printf() SQL function. |
| 13 | # |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 14 | # |
| 15 | # EVIDENCE-OF: R-63057-40065 The printf(FORMAT,...) SQL function works |
| 16 | # like the sqlite3_mprintf() C-language function and the printf() |
| 17 | # function from the standard C library. |
| 18 | # |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 19 | |
| 20 | set testdir [file dirname $argv0] |
| 21 | source $testdir/tester.tcl |
| 22 | |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 23 | # EVIDENCE-OF: R-40086-60101 If the FORMAT argument is missing or NULL |
| 24 | # then the result is NULL. |
| 25 | # |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 26 | do_execsql_test printf2-1.1 { |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 27 | SELECT quote(printf()), quote(printf(NULL,1,2,3)); |
| 28 | } {NULL NULL} |
| 29 | |
| 30 | |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 31 | do_execsql_test printf2-1.2 { |
| 32 | SELECT printf('hello'); |
| 33 | } {hello} |
| 34 | do_execsql_test printf2-1.3 { |
| 35 | SELECT printf('%d,%d,%d',55,-11,3421); |
| 36 | } {55,-11,3421} |
| 37 | do_execsql_test printf2-1.4 { |
| 38 | SELECT printf('%d,%d,%d',55,'-11',3421); |
| 39 | } {55,-11,3421} |
| 40 | do_execsql_test printf2-1.5 { |
| 41 | SELECT printf('%d,%d,%d,%d',55,'-11',3421); |
| 42 | } {55,-11,3421,0} |
| 43 | do_execsql_test printf2-1.6 { |
| 44 | SELECT printf('%.2f',3.141592653); |
| 45 | } {3.14} |
| 46 | do_execsql_test printf2-1.7 { |
| 47 | SELECT printf('%.*f',2,3.141592653); |
| 48 | } {3.14} |
| 49 | do_execsql_test printf2-1.8 { |
| 50 | SELECT printf('%*.*f',5,2,3.141592653); |
| 51 | } {{ 3.14}} |
| 52 | do_execsql_test printf2-1.9 { |
| 53 | SELECT printf('%d',314159.2653); |
| 54 | } {314159} |
| 55 | do_execsql_test printf2-1.10 { |
| 56 | SELECT printf('%lld',314159.2653); |
| 57 | } {314159} |
| 58 | do_execsql_test printf2-1.11 { |
| 59 | SELECT printf('%lld%n',314159.2653,'hi'); |
| 60 | } {314159} |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 61 | |
drh | a820c05 | 2014-01-27 15:02:07 +0000 | [diff] [blame] | 62 | # EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s. |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 63 | # |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 64 | do_execsql_test printf2-1.12 { |
| 65 | SELECT printf('%.*z',5,'abcdefghijklmnop'); |
| 66 | } {abcde} |
drh | fc6ee9d | 2013-12-17 15:58:42 +0000 | [diff] [blame] | 67 | do_execsql_test printf2-1.13 { |
| 68 | SELECT printf('%c','abcdefghijklmnop'); |
| 69 | } {a} |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 70 | |
drh | 3a8aec5 | 2013-12-17 16:32:56 +0000 | [diff] [blame] | 71 | # EVIDENCE-OF: R-02347-27622 The %n format is silently ignored and does |
| 72 | # not consume an argument. |
| 73 | # |
| 74 | do_execsql_test printf2-2.1 { |
| 75 | CREATE TABLE t1(a,b,c); |
| 76 | INSERT INTO t1 VALUES(1,2,3); |
| 77 | INSERT INTO t1 VALUES(-1,-2,-3); |
| 78 | INSERT INTO t1 VALUES('abc','def','ghi'); |
| 79 | INSERT INTO t1 VALUES(1.5,2.25,3.125); |
| 80 | SELECT printf('(%s)-%n-(%s)',a,b,c) FROM t1 ORDER BY rowid; |
| 81 | } {(1)--(2) (-1)--(-2) (abc)--(def) (1.5)--(2.25)} |
| 82 | |
| 83 | # EVIDENCE-OF: R-56064-04001 The %p format is an alias for %X. |
| 84 | # |
| 85 | do_execsql_test printf2-2.2 { |
| 86 | SELECT printf('%s=(%p)',a,a) FROM t1 ORDER BY a; |
| 87 | } {-1=(FFFFFFFFFFFFFFFF) 1=(1) 1.5=(1) abc=(0)} |
| 88 | |
| 89 | # EVIDENCE-OF: R-29410-53018 If there are too few arguments in the |
| 90 | # argument list, missing arguments are assumed to have a NULL value, |
| 91 | # which is translated into 0 or 0.0 for numeric formats or an empty |
| 92 | # string for %s. |
| 93 | # |
| 94 | do_execsql_test printf2-2.3 { |
| 95 | SELECT printf('%s=(%d/%g/%s)',a) FROM t1 ORDER BY a; |
| 96 | } {-1=(0/0/) 1=(0/0/) 1.5=(0/0/) abc=(0/0/)} |
| 97 | |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 98 | |
| 99 | finish_test |