1b19a2bc6Sdrh# 2001 September 15 2d1bf3512Sdrh# 3b19a2bc6Sdrh# The author disclaims copyright to this source code. In place of 4b19a2bc6Sdrh# a legal notice, here is a blessing: 5d1bf3512Sdrh# 6b19a2bc6Sdrh# May you do good and not evil. 7b19a2bc6Sdrh# May you find forgiveness for yourself and forgive others. 8b19a2bc6Sdrh# May you share freely, never taking more than you give. 9d1bf3512Sdrh# 10d1bf3512Sdrh#*********************************************************************** 11d1bf3512Sdrh# This file implements regression tests for SQLite library. The 12d1bf3512Sdrh# focus of this file is testing the sqlite_*_printf() interface. 13d1bf3512Sdrh# 14*b8cdbec2Sdanielk1977# $Id: printf.test,v 1.26 2007/09/01 10:01:13 danielk1977 Exp $ 15d1bf3512Sdrh 16d1bf3512Sdrhset testdir [file dirname $argv0] 17d1bf3512Sdrhsource $testdir/tester.tcl 18d1bf3512Sdrh 19d1bf3512Sdrhset n 1 20d1bf3512Sdrhforeach v {1 2 5 10 99 100 1000000 999999999 0 -1 -2 -5 -10 -99 -100 -9999999} { 216aff507eSdrh set v32 [expr {$v&0xffffffff}] 22d1bf3512Sdrh do_test printf-1.$n.1 [subst { 232dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: %d %x %o} $v $v $v 246aff507eSdrh }] [format {Three integers: %d %x %o} $v $v32 $v32] 25d1bf3512Sdrh do_test printf-1.$n.2 [subst { 262dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v 276aff507eSdrh }] [format {Three integers: (%6d) (%6x) (%6o)} $v $v32 $v32] 28d1bf3512Sdrh do_test printf-1.$n.3 [subst { 292dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v 306aff507eSdrh }] [format {Three integers: (%-6d) (%-6x) (%-6o)} $v $v32 $v32] 31daffd0e5Sdrh do_test printf-1.$n.4 [subst { 322dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v 336aff507eSdrh }] [format {Three integers: (%+6d) (%+6x) (%+6o)} $v $v32 $v32] 34daffd0e5Sdrh do_test printf-1.$n.5 [subst { 352dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v 366aff507eSdrh }] [format {Three integers: (%06d) (%06x) (%06o)} $v $v32 $v32] 37daffd0e5Sdrh do_test printf-1.$n.6 [subst { 382dd59365Sdanielk1977 sqlite3_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v 396aff507eSdrh }] [format {Three integers: (% 6d) (% 6x) (% 6o)} $v $v32 $v32] 40779c6a06Sdrh do_test printf-1.$n.7 [subst { 41779c6a06Sdrh sqlite3_mprintf_int {Three integers: (%#6d) (%#6x) (%#6o)} $v $v $v 426aff507eSdrh }] [format {Three integers: (%#6d) (%#6x) (%#6o)} $v $v32 $v32] 43d1bf3512Sdrh incr n 44d1bf3512Sdrh} 45d1bf3512Sdrh 46254cba24Sdrh 47254cba24Sdrhif {$::tcl_platform(platform)!="windows"} { 48254cba24Sdrh 49d1bf3512Sdrhset m 1 50d1bf3512Sdrhforeach {a b} {1 1 5 5 10 10 10 5} { 51d1bf3512Sdrh set n 1 52d1bf3512Sdrh foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} { 53d1bf3512Sdrh do_test printf-2.$m.$n.1 [subst { 542dd59365Sdanielk1977 sqlite3_mprintf_double {A double: %*.*f} $a $b $x 55d1bf3512Sdrh }] [format {A double: %*.*f} $a $b $x] 56d1bf3512Sdrh do_test printf-2.$m.$n.2 [subst { 572dd59365Sdanielk1977 sqlite3_mprintf_double {A double: %*.*e} $a $b $x 58d1bf3512Sdrh }] [format {A double: %*.*e} $a $b $x] 59d1bf3512Sdrh do_test printf-2.$m.$n.3 [subst { 602dd59365Sdanielk1977 sqlite3_mprintf_double {A double: %*.*g} $a $b $x 61d1bf3512Sdrh }] [format {A double: %*.*g} $a $b $x] 62d1bf3512Sdrh do_test printf-2.$m.$n.4 [subst { 632dd59365Sdanielk1977 sqlite3_mprintf_double {A double: %d %d %g} $a $b $x 64d1bf3512Sdrh }] [format {A double: %d %d %g} $a $b $x] 65d1bf3512Sdrh do_test printf-2.$m.$n.5 [subst { 662dd59365Sdanielk1977 sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x 67d1bf3512Sdrh }] [format {A double: %d %d %#g} $a $b $x] 68779c6a06Sdrh do_test printf-2.$m.$n.6 [subst { 69779c6a06Sdrh sqlite3_mprintf_double {A double: %d %d %010g} $a $b $x 70779c6a06Sdrh }] [format {A double: %d %d %010g} $a $b $x] 71d1bf3512Sdrh incr n 72d1bf3512Sdrh } 73d1bf3512Sdrh incr m 74d1bf3512Sdrh} 75d1bf3512Sdrh 76592ac8cbSdrh} ;# endif not windows 77254cba24Sdrh 78d1bf3512Sdrhdo_test printf-3.1 { 792dd59365Sdanielk1977 sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string} 80d1bf3512Sdrh} [format {A String: (%*.*s)} 10 10 {This is the string}] 81d1bf3512Sdrhdo_test printf-3.2 { 822dd59365Sdanielk1977 sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string} 83d1bf3512Sdrh} [format {A String: (%*.*s)} 10 5 {This is the string}] 84d1bf3512Sdrhdo_test printf-3.3 { 852dd59365Sdanielk1977 sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string} 86d1bf3512Sdrh} [format {A String: (%*.*s)} -10 5 {This is the string}] 87d1bf3512Sdrhdo_test printf-3.4 { 882dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string} 89d1bf3512Sdrh} [format {%d %d A String: (%s)} 1 2 {This is the string}] 90d1bf3512Sdrhdo_test printf-3.5 { 912dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string} 92d1bf3512Sdrh} [format {%d %d A String: (%30s)} 1 2 {This is the string}] 93d1bf3512Sdrhdo_test printf-3.6 { 942dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string} 95d1bf3512Sdrh} [format {%d %d A String: (%-30s)} 1 2 {This is the string}] 96b3738b6cSdrhdo_test snprintf-3.11 { 97b3738b6cSdrh sqlite3_snprintf_str 2 {x%d %d %s} 10 10 {This is the string} 98b3738b6cSdrh} {x} 99b3738b6cSdrhdo_test snprintf-3.12 { 100b3738b6cSdrh sqlite3_snprintf_str 3 {x%d %d %s} 10 10 {This is the string} 101b3738b6cSdrh} {x1} 102b3738b6cSdrhdo_test snprintf-3.13 { 103b3738b6cSdrh sqlite3_snprintf_str 4 {x%d %d %s} 10 10 {This is the string} 104b3738b6cSdrh} {x10} 105b3738b6cSdrhdo_test snprintf-3.14 { 106b3738b6cSdrh sqlite3_snprintf_str 5 {x%d %d %s} 10 10 {This is the string} 107b3738b6cSdrh} {x10 } 108b3738b6cSdrhdo_test snprintf-3.15 { 109b3738b6cSdrh sqlite3_snprintf_str 6 {x%d %d %s} 10 10 {This is the string} 110b3738b6cSdrh} {x10 1} 111b3738b6cSdrhdo_test snprintf-3.16 { 112b3738b6cSdrh sqlite3_snprintf_str 7 {x%d %d %s} 10 10 {This is the string} 113b3738b6cSdrh} {x10 10} 114b3738b6cSdrhdo_test snprintf-3.17 { 115b3738b6cSdrh sqlite3_snprintf_str 8 {x%d %d %s} 10 10 {This is the string} 116b3738b6cSdrh} {x10 10 } 117b3738b6cSdrhdo_test snprintf-3.18 { 118b3738b6cSdrh sqlite3_snprintf_str 9 {x%d %d %s} 10 10 {This is the string} 119b3738b6cSdrh} {x10 10 T} 120b3738b6cSdrhdo_test snprintf-3.19 { 121b3738b6cSdrh sqlite3_snprintf_str 100 {x%d %d %s} 10 10 {This is the string} 122b3738b6cSdrh} {x10 10 This is the string} 123d1bf3512Sdrh 124d1bf3512Sdrhdo_test printf-4.1 { 1252dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all} 126d1bf3512Sdrh} {1 2 A quoted string: 'Hi Y''all'} 127b22f1b58Schwdo_test printf-4.2 { 1282dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2 129b22f1b58Schw} {1 2 A NULL pointer in %q: '(NULL)'} 130b22f1b58Schwdo_test printf-4.3 { 1312dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all} 132b22f1b58Schw} {1 2 A quoted string: 'Hi Y''all'} 133b22f1b58Schwdo_test printf-4.4 { 1342dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2 135b22f1b58Schw} {1 2 A NULL pointer in %Q: NULL} 136d1bf3512Sdrh 137daffd0e5Sdrhdo_test printf-5.1 { 1382dd59365Sdanielk1977 set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}] 139daffd0e5Sdrh string length $x 14079158e18Sdrh} {344} 141daffd0e5Sdrhdo_test printf-5.2 { 1422dd59365Sdanielk1977 sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello} 143daffd0e5Sdrh} {-9 -10 (HelloHello) %} 144daffd0e5Sdrh 145d93d8a81Sdrhdo_test printf-6.1 { 1462dd59365Sdanielk1977 sqlite3_mprintf_z_test , one two three four five six 147d93d8a81Sdrh} {,one,two,three,four,five,six} 148d93d8a81Sdrh 149b621c237Sdrh 150b621c237Sdrhdo_test printf-7.1 { 1512dd59365Sdanielk1977 sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0 152b621c237Sdrh} {A double: 1e+307} 153b621c237Sdrhdo_test printf-7.2 { 1542dd59365Sdanielk1977 sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0 155b621c237Sdrh} {A double: 1e+308} 156b621c237Sdrhdo_test printf-7.3 { 1572dd59365Sdanielk1977 sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0 15853c14021Sdrh} {A double: Inf} 15953c14021Sdrhdo_test printf-7.4 { 16053c14021Sdrh sqlite3_mprintf_scaled {A double: %g} -1.0e307 100.0 16153c14021Sdrh} {A double: -Inf} 16253c14021Sdrhdo_test printf-7.5 { 16353c14021Sdrh sqlite3_mprintf_scaled {A double: %+g} 1.0e307 100.0 16453c14021Sdrh} {A double: +Inf} 165b621c237Sdrh 166e9707671Sdrhdo_test printf-8.1 { 167e9707671Sdrh sqlite3_mprintf_int {%u %u %u} 0x7fffffff 0x80000000 0xffffffff 168e9707671Sdrh} {2147483647 2147483648 4294967295} 169e9707671Sdrhdo_test printf-8.2 { 170e9707671Sdrh sqlite3_mprintf_int {%lu %lu %lu} 0x7fffffff 0x80000000 0xffffffff 171e9707671Sdrh} {2147483647 2147483648 4294967295} 172e9707671Sdrhdo_test printf-8.3 { 173e9707671Sdrh sqlite3_mprintf_int64 {%llu %llu %llu} 2147483647 2147483648 4294967296 174e9707671Sdrh} {2147483647 2147483648 4294967296} 175e9707671Sdrhdo_test printf-8.4 { 176e9707671Sdrh sqlite3_mprintf_int64 {%lld %lld %lld} 2147483647 2147483648 4294967296 177e9707671Sdrh} {2147483647 2147483648 4294967296} 178e9707671Sdrhdo_test printf-8.5 { 179e9707671Sdrh sqlite3_mprintf_int64 {%llx %llx %llx} 2147483647 2147483648 4294967296 180e9707671Sdrh} {7fffffff 80000000 100000000} 181e9707671Sdrhdo_test printf-8.6 { 182e9707671Sdrh sqlite3_mprintf_int64 {%llx %llo %lld} -1 -1 -1 183e9707671Sdrh} {ffffffffffffffff 1777777777777777777777 -1} 184*b8cdbec2Sdanielk1977do_test printf-8.7 { 185*b8cdbec2Sdanielk1977 sqlite3_mprintf_int64 {%llx %llx %llx} +2147483647 +2147483648 +4294967296 186*b8cdbec2Sdanielk1977} {7fffffff 80000000 100000000} 187e9707671Sdrh 188779c6a06Sdrhdo_test printf-9.1 { 189779c6a06Sdrh sqlite3_mprintf_int {%*.*c} 4 4 65 190779c6a06Sdrh} {AAAA} 191779c6a06Sdrhdo_test printf-9.2 { 192779c6a06Sdrh sqlite3_mprintf_int {%*.*c} -4 1 66 193779c6a06Sdrh} {B } 194779c6a06Sdrhdo_test printf-9.3 { 195779c6a06Sdrh sqlite3_mprintf_int {%*.*c} 4 1 67 196779c6a06Sdrh} { C} 197779c6a06Sdrhdo_test printf-9.4 { 1985eba8c09Sdrh sqlite3_mprintf_int {%d %d %c} 4 1 67 1995eba8c09Sdrh} {4 1 C} 2005eba8c09Sdrhset ten { } 2015eba8c09Sdrhset fifty $ten$ten$ten$ten$ten 2025eba8c09Sdrhdo_test printf-9.5 { 2035eba8c09Sdrh sqlite3_mprintf_int {%d %*c} 1 -201 67 2045eba8c09Sdrh} "1 C$fifty$fifty$fifty$fifty" 2055eba8c09Sdrhdo_test printf-9.6 { 20643617e9aSdrh sqlite3_mprintf_int {hi%12345.12346yhello} 0 0 0 20743617e9aSdrh} {hi} 208779c6a06Sdrh 209e29b1a05Sdrh# Ticket #812 210e29b1a05Sdrh# 211e29b1a05Sdrhdo_test printf-10.1 { 212e29b1a05Sdrh sqlite3_mprintf_stronly %s {} 213e29b1a05Sdrh} {} 214e29b1a05Sdrh 2159796ef58Sdrh# Ticket #831 2169796ef58Sdrh# 2179796ef58Sdrhdo_test printf-10.2 { 2189796ef58Sdrh sqlite3_mprintf_stronly %q {} 2199796ef58Sdrh} {} 2209796ef58Sdrh 221592ac8cbSdrh# Ticket #1340: Test for loss of precision on large positive exponents 222592ac8cbSdrh# 223592ac8cbSdrhdo_test printf-10.3 { 224592ac8cbSdrh sqlite3_mprintf_double {%d %d %f} 1 1 1e300 225592ac8cbSdrh} {1 1 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000} 226592ac8cbSdrh 227557cc60fSdrh# The non-standard '!' flag on a 'g' conversion forces a decimal point 228557cc60fSdrh# and at least one digit on either side of the decimal point. 229557cc60fSdrh# 230557cc60fSdrhdo_test printf-11.1 { 231557cc60fSdrh sqlite3_mprintf_double {%d %d %!g} 1 1 1 232557cc60fSdrh} {1 1 1.0} 233557cc60fSdrhdo_test printf-11.2 { 234557cc60fSdrh sqlite3_mprintf_double {%d %d %!g} 1 1 123 235557cc60fSdrh} {1 1 123.0} 236557cc60fSdrhdo_test printf-11.3 { 237557cc60fSdrh sqlite3_mprintf_double {%d %d %!g} 1 1 12.3 238557cc60fSdrh} {1 1 12.3} 239557cc60fSdrhdo_test printf-11.4 { 240557cc60fSdrh sqlite3_mprintf_double {%d %d %!g} 1 1 0.123 241557cc60fSdrh} {1 1 0.123} 242557cc60fSdrhdo_test printf-11.5 { 243557cc60fSdrh sqlite3_mprintf_double {%d %d %!.15g} 1 1 1 244557cc60fSdrh} {1 1 1.0} 245557cc60fSdrhdo_test printf-11.6 { 246557cc60fSdrh sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e10 247557cc60fSdrh} {1 1 10000000000.0} 248557cc60fSdrhdo_test printf-11.7 { 249557cc60fSdrh sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e300 250557cc60fSdrh} {1 1 1.0e+300} 251557cc60fSdrh 2525eba8c09Sdrh# Additional tests for coverage 2535eba8c09Sdrh# 2545eba8c09Sdrhdo_test printf-12.1 { 2555eba8c09Sdrh sqlite3_mprintf_double {%d %d %.2000g} 1 1 1.0 2565eba8c09Sdrh} {1 1 1} 257e29b1a05Sdrh 25863782855Sdrh# Floating point boundary cases 25963782855Sdrh# 26063782855Sdrhdo_test printf-13.1 { 26163782855Sdrh sqlite3_mprintf_hexdouble %.20f 4024000000000000 26263782855Sdrh} {10.00000000000000000000} 26363782855Sdrhdo_test printf-13.2 { 26463782855Sdrh sqlite3_mprintf_hexdouble %.20f 4197d78400000000 26563782855Sdrh} {100000000.00000000000000000000} 26663782855Sdrhdo_test printf-13.3 { 26763782855Sdrh sqlite3_mprintf_hexdouble %.20f 4693b8b5b5056e17 26863782855Sdrh} {100000000000000000000000000000000.00000000000000000000} 26953c14021Sdrhdo_test printf-13.4 { 27053c14021Sdrh sqlite3_mprintf_hexdouble %.20f 7ff0000000000000 27153c14021Sdrh} {Inf} 27253c14021Sdrhdo_test printf-13.5 { 27353c14021Sdrh sqlite3_mprintf_hexdouble %.20f fff0000000000000 27453c14021Sdrh} {-Inf} 27553c14021Sdrhdo_test printf-13.6 { 27653c14021Sdrh sqlite3_mprintf_hexdouble %.20f fff8000000000000 27753c14021Sdrh} {NaN} 27863782855Sdrh 27905a82983Sdrhdo_test printf-14.1 { 28005a82983Sdrh sqlite3_mprintf_str {abc-%y-123} 0 0 {not used} 28105a82983Sdrh} {abc-} 28205a82983Sdrhdo_test printf-14.2 { 28305a82983Sdrh sqlite3_mprintf_n_test {xyzzy} 28405a82983Sdrh} 5 285b3738b6cSdrhdo_test printf-14.3 { 286b3738b6cSdrh sqlite3_mprintf_str {abc-%T-123} 0 0 {not used} 287b3738b6cSdrh} {abc-} 28805a82983Sdrh 28968853907Sdrhdo_test printf-15.1 { 29068853907Sdrh sqlite3_snprintf_int 5 {12345} 0 29168853907Sdrh} {1234} 29268853907Sdrhdo_test printf-15.2 { 29368853907Sdrh sqlite3_snprintf_int 5 {} 0 29468853907Sdrh} {} 29568853907Sdrhdo_test printf-15.3 { 29668853907Sdrh sqlite3_snprintf_int 0 {} 0 29768853907Sdrh} {abcdefghijklmnopqrstuvwxyz} 29863782855Sdrh 299ca0c8971Sdanielk1977# Now test malloc() failure within a sqlite3_mprintf(): 300ca0c8971Sdanielk1977# 301ca0c8971Sdanielk1977foreach var {a b c d} { 302ca0c8971Sdanielk1977 set $var [string repeat $var 400] 303ca0c8971Sdanielk1977} 304ca0c8971Sdanielk1977set str1 "[string repeat A 360]%d%d%s" 305ca0c8971Sdanielk1977set str2 [string repeat B 5000] 306ca0c8971Sdanielk1977set zSuccess "[string repeat A 360]11[string repeat B 5000]" 307ca0c8971Sdanielk1977foreach ::iRepeat {0 1} { 308ca0c8971Sdanielk1977 set nTestNum 1 309ca0c8971Sdanielk1977 while {1} { 310ca0c8971Sdanielk1977 sqlite3_memdebug_fail $nTestNum -repeat $::iRepeat 311ca0c8971Sdanielk1977 set z [sqlite3_mprintf_str $str1 1 1 $str2] 312ca0c8971Sdanielk1977 set nFail [sqlite3_memdebug_fail -1 -benign nBenign] 313ca0c8971Sdanielk1977 do_test printf-malloc-$::iRepeat.$nTestNum { 314ca0c8971Sdanielk1977 expr {($nFail>0 && $z eq "") || ($nFail==$nBenign && $z eq $zSuccess)} 315ca0c8971Sdanielk1977 } {1} 316ca0c8971Sdanielk1977 if {$nFail == 0} break 317ca0c8971Sdanielk1977 incr nTestNum 318ca0c8971Sdanielk1977 } 319ca0c8971Sdanielk1977} 320ca0c8971Sdanielk1977 321d1bf3512Sdrhfinish_test 322ca0c8971Sdanielk1977 323