1# 2001 September 15 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 sqlite_*_printf() interface. 13# 14# $Id: printf.test,v 1.24 2007/05/10 17:23:12 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19set n 1 20foreach v {1 2 5 10 99 100 1000000 999999999 0 -1 -2 -5 -10 -99 -100 -9999999} { 21 set v32 [expr {$v&0xffffffff}] 22 do_test printf-1.$n.1 [subst { 23 sqlite3_mprintf_int {Three integers: %d %x %o} $v $v $v 24 }] [format {Three integers: %d %x %o} $v $v32 $v32] 25 do_test printf-1.$n.2 [subst { 26 sqlite3_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v 27 }] [format {Three integers: (%6d) (%6x) (%6o)} $v $v32 $v32] 28 do_test printf-1.$n.3 [subst { 29 sqlite3_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v 30 }] [format {Three integers: (%-6d) (%-6x) (%-6o)} $v $v32 $v32] 31 do_test printf-1.$n.4 [subst { 32 sqlite3_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v 33 }] [format {Three integers: (%+6d) (%+6x) (%+6o)} $v $v32 $v32] 34 do_test printf-1.$n.5 [subst { 35 sqlite3_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v 36 }] [format {Three integers: (%06d) (%06x) (%06o)} $v $v32 $v32] 37 do_test printf-1.$n.6 [subst { 38 sqlite3_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v 39 }] [format {Three integers: (% 6d) (% 6x) (% 6o)} $v $v32 $v32] 40 do_test printf-1.$n.7 [subst { 41 sqlite3_mprintf_int {Three integers: (%#6d) (%#6x) (%#6o)} $v $v $v 42 }] [format {Three integers: (%#6d) (%#6x) (%#6o)} $v $v32 $v32] 43 incr n 44} 45 46 47if {$::tcl_platform(platform)!="windows"} { 48 49set m 1 50foreach {a b} {1 1 5 5 10 10 10 5} { 51 set n 1 52 foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} { 53 do_test printf-2.$m.$n.1 [subst { 54 sqlite3_mprintf_double {A double: %*.*f} $a $b $x 55 }] [format {A double: %*.*f} $a $b $x] 56 do_test printf-2.$m.$n.2 [subst { 57 sqlite3_mprintf_double {A double: %*.*e} $a $b $x 58 }] [format {A double: %*.*e} $a $b $x] 59 do_test printf-2.$m.$n.3 [subst { 60 sqlite3_mprintf_double {A double: %*.*g} $a $b $x 61 }] [format {A double: %*.*g} $a $b $x] 62 do_test printf-2.$m.$n.4 [subst { 63 sqlite3_mprintf_double {A double: %d %d %g} $a $b $x 64 }] [format {A double: %d %d %g} $a $b $x] 65 do_test printf-2.$m.$n.5 [subst { 66 sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x 67 }] [format {A double: %d %d %#g} $a $b $x] 68 do_test printf-2.$m.$n.6 [subst { 69 sqlite3_mprintf_double {A double: %d %d %010g} $a $b $x 70 }] [format {A double: %d %d %010g} $a $b $x] 71 incr n 72 } 73 incr m 74} 75 76} ;# endif not windows 77 78do_test printf-3.1 { 79 sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string} 80} [format {A String: (%*.*s)} 10 10 {This is the string}] 81do_test printf-3.2 { 82 sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string} 83} [format {A String: (%*.*s)} 10 5 {This is the string}] 84do_test printf-3.3 { 85 sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string} 86} [format {A String: (%*.*s)} -10 5 {This is the string}] 87do_test printf-3.4 { 88 sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string} 89} [format {%d %d A String: (%s)} 1 2 {This is the string}] 90do_test printf-3.5 { 91 sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string} 92} [format {%d %d A String: (%30s)} 1 2 {This is the string}] 93do_test printf-3.6 { 94 sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string} 95} [format {%d %d A String: (%-30s)} 1 2 {This is the string}] 96do_test snprintf-3.11 { 97 sqlite3_snprintf_str 2 {x%d %d %s} 10 10 {This is the string} 98} {x} 99do_test snprintf-3.12 { 100 sqlite3_snprintf_str 3 {x%d %d %s} 10 10 {This is the string} 101} {x1} 102do_test snprintf-3.13 { 103 sqlite3_snprintf_str 4 {x%d %d %s} 10 10 {This is the string} 104} {x10} 105do_test snprintf-3.14 { 106 sqlite3_snprintf_str 5 {x%d %d %s} 10 10 {This is the string} 107} {x10 } 108do_test snprintf-3.15 { 109 sqlite3_snprintf_str 6 {x%d %d %s} 10 10 {This is the string} 110} {x10 1} 111do_test snprintf-3.16 { 112 sqlite3_snprintf_str 7 {x%d %d %s} 10 10 {This is the string} 113} {x10 10} 114do_test snprintf-3.17 { 115 sqlite3_snprintf_str 8 {x%d %d %s} 10 10 {This is the string} 116} {x10 10 } 117do_test snprintf-3.18 { 118 sqlite3_snprintf_str 9 {x%d %d %s} 10 10 {This is the string} 119} {x10 10 T} 120do_test snprintf-3.19 { 121 sqlite3_snprintf_str 100 {x%d %d %s} 10 10 {This is the string} 122} {x10 10 This is the string} 123 124do_test printf-4.1 { 125 sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all} 126} {1 2 A quoted string: 'Hi Y''all'} 127do_test printf-4.2 { 128 sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2 129} {1 2 A NULL pointer in %q: '(NULL)'} 130do_test printf-4.3 { 131 sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all} 132} {1 2 A quoted string: 'Hi Y''all'} 133do_test printf-4.4 { 134 sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2 135} {1 2 A NULL pointer in %Q: NULL} 136 137do_test printf-5.1 { 138 set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}] 139 string length $x 140} {344} 141do_test printf-5.2 { 142 sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello} 143} {-9 -10 (HelloHello) %} 144 145do_test printf-6.1 { 146 sqlite3_mprintf_z_test , one two three four five six 147} {,one,two,three,four,five,six} 148 149 150do_test printf-7.1 { 151 sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0 152} {A double: 1e+307} 153do_test printf-7.2 { 154 sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0 155} {A double: 1e+308} 156do_test printf-7.3 { 157 sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0 158} {A double: Inf} 159do_test printf-7.4 { 160 sqlite3_mprintf_scaled {A double: %g} -1.0e307 100.0 161} {A double: -Inf} 162do_test printf-7.5 { 163 sqlite3_mprintf_scaled {A double: %+g} 1.0e307 100.0 164} {A double: +Inf} 165 166do_test printf-8.1 { 167 sqlite3_mprintf_int {%u %u %u} 0x7fffffff 0x80000000 0xffffffff 168} {2147483647 2147483648 4294967295} 169do_test printf-8.2 { 170 sqlite3_mprintf_int {%lu %lu %lu} 0x7fffffff 0x80000000 0xffffffff 171} {2147483647 2147483648 4294967295} 172do_test printf-8.3 { 173 sqlite3_mprintf_int64 {%llu %llu %llu} 2147483647 2147483648 4294967296 174} {2147483647 2147483648 4294967296} 175do_test printf-8.4 { 176 sqlite3_mprintf_int64 {%lld %lld %lld} 2147483647 2147483648 4294967296 177} {2147483647 2147483648 4294967296} 178do_test printf-8.5 { 179 sqlite3_mprintf_int64 {%llx %llx %llx} 2147483647 2147483648 4294967296 180} {7fffffff 80000000 100000000} 181do_test printf-8.6 { 182 sqlite3_mprintf_int64 {%llx %llo %lld} -1 -1 -1 183} {ffffffffffffffff 1777777777777777777777 -1} 184 185do_test printf-9.1 { 186 sqlite3_mprintf_int {%*.*c} 4 4 65 187} {AAAA} 188do_test printf-9.2 { 189 sqlite3_mprintf_int {%*.*c} -4 1 66 190} {B } 191do_test printf-9.3 { 192 sqlite3_mprintf_int {%*.*c} 4 1 67 193} { C} 194do_test printf-9.4 { 195 sqlite3_mprintf_int {%d %d %c} 4 1 67 196} {4 1 C} 197set ten { } 198set fifty $ten$ten$ten$ten$ten 199do_test printf-9.5 { 200 sqlite3_mprintf_int {%d %*c} 1 -201 67 201} "1 C$fifty$fifty$fifty$fifty" 202do_test printf-9.6 { 203 sqlite3_mprintf_int {hi%12345.12346yhello} 0 0 0 204} {hi} 205 206# Ticket #812 207# 208do_test printf-10.1 { 209 sqlite3_mprintf_stronly %s {} 210} {} 211 212# Ticket #831 213# 214do_test printf-10.2 { 215 sqlite3_mprintf_stronly %q {} 216} {} 217 218# Ticket #1340: Test for loss of precision on large positive exponents 219# 220do_test printf-10.3 { 221 sqlite3_mprintf_double {%d %d %f} 1 1 1e300 222} {1 1 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000} 223 224# The non-standard '!' flag on a 'g' conversion forces a decimal point 225# and at least one digit on either side of the decimal point. 226# 227do_test printf-11.1 { 228 sqlite3_mprintf_double {%d %d %!g} 1 1 1 229} {1 1 1.0} 230do_test printf-11.2 { 231 sqlite3_mprintf_double {%d %d %!g} 1 1 123 232} {1 1 123.0} 233do_test printf-11.3 { 234 sqlite3_mprintf_double {%d %d %!g} 1 1 12.3 235} {1 1 12.3} 236do_test printf-11.4 { 237 sqlite3_mprintf_double {%d %d %!g} 1 1 0.123 238} {1 1 0.123} 239do_test printf-11.5 { 240 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1 241} {1 1 1.0} 242do_test printf-11.6 { 243 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e10 244} {1 1 10000000000.0} 245do_test printf-11.7 { 246 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e300 247} {1 1 1.0e+300} 248 249# Additional tests for coverage 250# 251do_test printf-12.1 { 252 sqlite3_mprintf_double {%d %d %.2000g} 1 1 1.0 253} {1 1 1} 254 255# Floating point boundary cases 256# 257do_test printf-13.1 { 258 sqlite3_mprintf_hexdouble %.20f 4024000000000000 259} {10.00000000000000000000} 260do_test printf-13.2 { 261 sqlite3_mprintf_hexdouble %.20f 4197d78400000000 262} {100000000.00000000000000000000} 263do_test printf-13.3 { 264 sqlite3_mprintf_hexdouble %.20f 4693b8b5b5056e17 265} {100000000000000000000000000000000.00000000000000000000} 266do_test printf-13.4 { 267 sqlite3_mprintf_hexdouble %.20f 7ff0000000000000 268} {Inf} 269do_test printf-13.5 { 270 sqlite3_mprintf_hexdouble %.20f fff0000000000000 271} {-Inf} 272do_test printf-13.6 { 273 sqlite3_mprintf_hexdouble %.20f fff8000000000000 274} {NaN} 275 276do_test printf-14.1 { 277 sqlite3_mprintf_str {abc-%y-123} 0 0 {not used} 278} {abc-} 279do_test printf-14.2 { 280 sqlite3_mprintf_n_test {xyzzy} 281} 5 282do_test printf-14.3 { 283 sqlite3_mprintf_str {abc-%T-123} 0 0 {not used} 284} {abc-} 285 286do_test printf-15.1 { 287 sqlite3_snprintf_int 5 {12345} 0 288} {1234} 289do_test printf-15.2 { 290 sqlite3_snprintf_int 5 {} 0 291} {} 292do_test printf-15.3 { 293 sqlite3_snprintf_int 0 {} 0 294} {abcdefghijklmnopqrstuvwxyz} 295 296finish_test 297