drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the sqlite_*_printf() interface. |
| 13 | # |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 14 | # $Id: printf.test,v 1.9 2004/06/14 23:46:48 danielk1977 Exp $ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | set n 1 |
| 20 | foreach v {1 2 5 10 99 100 1000000 999999999 0 -1 -2 -5 -10 -99 -100 -9999999} { |
| 21 | do_test printf-1.$n.1 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 22 | sqlite3_mprintf_int {Three integers: %d %x %o} $v $v $v |
| 23 | }] [format {Three integers: %d %lx %lo} $v $v $v] |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 24 | do_test printf-1.$n.2 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 25 | sqlite3_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v |
| 26 | }] [format {Three integers: (%6d) (%6lx) (%6lo)} $v $v $v] |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 27 | do_test printf-1.$n.3 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 28 | sqlite3_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v |
| 29 | }] [format {Three integers: (%-6d) (%-6lx) (%-6lo)} $v $v $v] |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 30 | do_test printf-1.$n.4 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 31 | sqlite3_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v |
| 32 | }] [format {Three integers: (%+6d) (%+6lx) (%+6lo)} $v $v $v] |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 33 | do_test printf-1.$n.5 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 34 | sqlite3_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v |
| 35 | }] [format {Three integers: (%06d) (%06lx) (%06lo)} $v $v $v] |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 36 | do_test printf-1.$n.6 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 37 | sqlite3_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v |
| 38 | }] [format {Three integers: (% 6d) (% 6lx) (% 6lo)} $v $v $v] |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 39 | incr n |
| 40 | } |
| 41 | |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 42 | |
| 43 | if {$::tcl_platform(platform)!="windows"} { |
| 44 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 45 | set m 1 |
| 46 | foreach {a b} {1 1 5 5 10 10 10 5} { |
| 47 | set n 1 |
| 48 | foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} { |
| 49 | do_test printf-2.$m.$n.1 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 50 | sqlite3_mprintf_double {A double: %*.*f} $a $b $x |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 51 | }] [format {A double: %*.*f} $a $b $x] |
| 52 | do_test printf-2.$m.$n.2 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 53 | sqlite3_mprintf_double {A double: %*.*e} $a $b $x |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 54 | }] [format {A double: %*.*e} $a $b $x] |
| 55 | do_test printf-2.$m.$n.3 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 56 | sqlite3_mprintf_double {A double: %*.*g} $a $b $x |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 57 | }] [format {A double: %*.*g} $a $b $x] |
| 58 | do_test printf-2.$m.$n.4 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 59 | sqlite3_mprintf_double {A double: %d %d %g} $a $b $x |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 60 | }] [format {A double: %d %d %g} $a $b $x] |
| 61 | do_test printf-2.$m.$n.5 [subst { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 62 | sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 63 | }] [format {A double: %d %d %#g} $a $b $x] |
| 64 | incr n |
| 65 | } |
| 66 | incr m |
| 67 | } |
| 68 | |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 71 | do_test printf-3.1 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 72 | sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 73 | } [format {A String: (%*.*s)} 10 10 {This is the string}] |
| 74 | do_test printf-3.2 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 75 | sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 76 | } [format {A String: (%*.*s)} 10 5 {This is the string}] |
| 77 | do_test printf-3.3 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 78 | sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 79 | } [format {A String: (%*.*s)} -10 5 {This is the string}] |
| 80 | do_test printf-3.4 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 81 | sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 82 | } [format {%d %d A String: (%s)} 1 2 {This is the string}] |
| 83 | do_test printf-3.5 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 84 | sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 85 | } [format {%d %d A String: (%30s)} 1 2 {This is the string}] |
| 86 | do_test printf-3.6 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 87 | sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 88 | } [format {%d %d A String: (%-30s)} 1 2 {This is the string}] |
| 89 | |
| 90 | do_test printf-4.1 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 91 | sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 92 | } {1 2 A quoted string: 'Hi Y''all'} |
chw | b22f1b5 | 2002-06-16 04:56:36 +0000 | [diff] [blame] | 93 | do_test printf-4.2 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 94 | sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2 |
chw | b22f1b5 | 2002-06-16 04:56:36 +0000 | [diff] [blame] | 95 | } {1 2 A NULL pointer in %q: '(NULL)'} |
| 96 | do_test printf-4.3 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 97 | sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all} |
chw | b22f1b5 | 2002-06-16 04:56:36 +0000 | [diff] [blame] | 98 | } {1 2 A quoted string: 'Hi Y''all'} |
| 99 | do_test printf-4.4 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 100 | sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2 |
chw | b22f1b5 | 2002-06-16 04:56:36 +0000 | [diff] [blame] | 101 | } {1 2 A NULL pointer in %Q: NULL} |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 102 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 103 | do_test printf-5.1 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 104 | set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}] |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 105 | string length $x |
| 106 | } {994} |
| 107 | do_test printf-5.2 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 108 | sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello} |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 109 | } {-9 -10 (HelloHello) %} |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 110 | |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 111 | do_test printf-6.1 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 112 | sqlite3_mprintf_z_test , one two three four five six |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 113 | } {,one,two,three,four,five,six} |
| 114 | |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 115 | |
| 116 | do_test printf-7.1 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 117 | sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0 |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 118 | } {A double: 1e+307} |
| 119 | do_test printf-7.2 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 120 | sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0 |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 121 | } {A double: 1e+308} |
| 122 | do_test printf-7.3 { |
danielk1977 | 2dd5936 | 2004-06-14 23:46:47 +0000 | [diff] [blame^] | 123 | sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0 |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 124 | } {A double: NaN} |
| 125 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 126 | finish_test |