1*1da57d55SToomas Soome# 2c5c4113dSnw141292# 2001 September 15 3c5c4113dSnw141292# 4c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 5c5c4113dSnw141292# a legal notice, here is a blessing: 6c5c4113dSnw141292# 7c5c4113dSnw141292# May you do good and not evil. 8c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 9c5c4113dSnw141292# May you share freely, never taking more than you give. 10c5c4113dSnw141292# 11c5c4113dSnw141292#*********************************************************************** 12c5c4113dSnw141292# This file implements regression tests for SQLite library. The 13c5c4113dSnw141292# focus of this file is testing the sqlite_*_printf() interface. 14c5c4113dSnw141292# 15c5c4113dSnw141292# $Id: printf.test,v 1.8 2004/02/21 19:41:05 drh Exp $ 16c5c4113dSnw141292 17c5c4113dSnw141292set testdir [file dirname $argv0] 18c5c4113dSnw141292source $testdir/tester.tcl 19c5c4113dSnw141292 20c5c4113dSnw141292set n 1 21c5c4113dSnw141292foreach v {1 2 5 10 99 100 1000000 999999999 0 -1 -2 -5 -10 -99 -100 -9999999} { 22c5c4113dSnw141292 do_test printf-1.$n.1 [subst { 23c5c4113dSnw141292 sqlite_mprintf_int {Three integers: %d %x %o} $v $v $v 24c5c4113dSnw141292 }] [format {Three integers: %d %x %o} $v $v $v] 25c5c4113dSnw141292 do_test printf-1.$n.2 [subst { 26c5c4113dSnw141292 sqlite_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v 27c5c4113dSnw141292 }] [format {Three integers: (%6d) (%6x) (%6o)} $v $v $v] 28c5c4113dSnw141292 do_test printf-1.$n.3 [subst { 29c5c4113dSnw141292 sqlite_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v 30c5c4113dSnw141292 }] [format {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v] 31c5c4113dSnw141292 do_test printf-1.$n.4 [subst { 32c5c4113dSnw141292 sqlite_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v 33c5c4113dSnw141292 }] [format {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v] 34c5c4113dSnw141292 do_test printf-1.$n.5 [subst { 35c5c4113dSnw141292 sqlite_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v 36c5c4113dSnw141292 }] [format {Three integers: (%06d) (%06x) (%06o)} $v $v $v] 37c5c4113dSnw141292 do_test printf-1.$n.6 [subst { 38c5c4113dSnw141292 sqlite_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v 39c5c4113dSnw141292 }] [format {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v] 40c5c4113dSnw141292 incr n 41c5c4113dSnw141292} 42c5c4113dSnw141292 43c5c4113dSnw141292 44c5c4113dSnw141292if {$::tcl_platform(platform)!="windows"} { 45c5c4113dSnw141292 46c5c4113dSnw141292set m 1 47c5c4113dSnw141292foreach {a b} {1 1 5 5 10 10 10 5} { 48c5c4113dSnw141292 set n 1 49c5c4113dSnw141292 foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} { 50c5c4113dSnw141292 do_test printf-2.$m.$n.1 [subst { 51c5c4113dSnw141292 sqlite_mprintf_double {A double: %*.*f} $a $b $x 52c5c4113dSnw141292 }] [format {A double: %*.*f} $a $b $x] 53c5c4113dSnw141292 do_test printf-2.$m.$n.2 [subst { 54c5c4113dSnw141292 sqlite_mprintf_double {A double: %*.*e} $a $b $x 55c5c4113dSnw141292 }] [format {A double: %*.*e} $a $b $x] 56c5c4113dSnw141292 do_test printf-2.$m.$n.3 [subst { 57c5c4113dSnw141292 sqlite_mprintf_double {A double: %*.*g} $a $b $x 58c5c4113dSnw141292 }] [format {A double: %*.*g} $a $b $x] 59c5c4113dSnw141292 do_test printf-2.$m.$n.4 [subst { 60c5c4113dSnw141292 sqlite_mprintf_double {A double: %d %d %g} $a $b $x 61c5c4113dSnw141292 }] [format {A double: %d %d %g} $a $b $x] 62c5c4113dSnw141292 do_test printf-2.$m.$n.5 [subst { 63c5c4113dSnw141292 sqlite_mprintf_double {A double: %d %d %#g} $a $b $x 64c5c4113dSnw141292 }] [format {A double: %d %d %#g} $a $b $x] 65c5c4113dSnw141292 incr n 66c5c4113dSnw141292 } 67c5c4113dSnw141292 incr m 68c5c4113dSnw141292} 69c5c4113dSnw141292 70c5c4113dSnw141292} 71c5c4113dSnw141292 72c5c4113dSnw141292do_test printf-3.1 { 73c5c4113dSnw141292 sqlite_mprintf_str {A String: (%*.*s)} 10 10 {This is the string} 74c5c4113dSnw141292} [format {A String: (%*.*s)} 10 10 {This is the string}] 75c5c4113dSnw141292do_test printf-3.2 { 76c5c4113dSnw141292 sqlite_mprintf_str {A String: (%*.*s)} 10 5 {This is the string} 77c5c4113dSnw141292} [format {A String: (%*.*s)} 10 5 {This is the string}] 78c5c4113dSnw141292do_test printf-3.3 { 79c5c4113dSnw141292 sqlite_mprintf_str {A String: (%*.*s)} -10 5 {This is the string} 80c5c4113dSnw141292} [format {A String: (%*.*s)} -10 5 {This is the string}] 81c5c4113dSnw141292do_test printf-3.4 { 82c5c4113dSnw141292 sqlite_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string} 83c5c4113dSnw141292} [format {%d %d A String: (%s)} 1 2 {This is the string}] 84c5c4113dSnw141292do_test printf-3.5 { 85c5c4113dSnw141292 sqlite_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string} 86c5c4113dSnw141292} [format {%d %d A String: (%30s)} 1 2 {This is the string}] 87c5c4113dSnw141292do_test printf-3.6 { 88c5c4113dSnw141292 sqlite_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string} 89c5c4113dSnw141292} [format {%d %d A String: (%-30s)} 1 2 {This is the string}] 90c5c4113dSnw141292 91c5c4113dSnw141292do_test printf-4.1 { 92c5c4113dSnw141292 sqlite_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all} 93c5c4113dSnw141292} {1 2 A quoted string: 'Hi Y''all'} 94c5c4113dSnw141292do_test printf-4.2 { 95c5c4113dSnw141292 sqlite_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2 96c5c4113dSnw141292} {1 2 A NULL pointer in %q: '(NULL)'} 97c5c4113dSnw141292do_test printf-4.3 { 98c5c4113dSnw141292 sqlite_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all} 99c5c4113dSnw141292} {1 2 A quoted string: 'Hi Y''all'} 100c5c4113dSnw141292do_test printf-4.4 { 101c5c4113dSnw141292 sqlite_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2 102c5c4113dSnw141292} {1 2 A NULL pointer in %Q: NULL} 103c5c4113dSnw141292 104c5c4113dSnw141292do_test printf-5.1 { 105c5c4113dSnw141292 set x [sqlite_mprintf_str {%d %d %100000s} 0 0 {Hello}] 106c5c4113dSnw141292 string length $x 107c5c4113dSnw141292} {994} 108c5c4113dSnw141292do_test printf-5.2 { 109c5c4113dSnw141292 sqlite_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello} 110c5c4113dSnw141292} {-9 -10 (HelloHello) %} 111c5c4113dSnw141292 112c5c4113dSnw141292do_test printf-6.1 { 113c5c4113dSnw141292 sqlite_mprintf_z_test , one two three four five six 114c5c4113dSnw141292} {,one,two,three,four,five,six} 115c5c4113dSnw141292 116c5c4113dSnw141292 117c5c4113dSnw141292do_test printf-7.1 { 118c5c4113dSnw141292 sqlite_mprintf_scaled {A double: %g} 1.0e307 1.0 119c5c4113dSnw141292} {A double: 1e+307} 120c5c4113dSnw141292do_test printf-7.2 { 121c5c4113dSnw141292 sqlite_mprintf_scaled {A double: %g} 1.0e307 10.0 122c5c4113dSnw141292} {A double: 1e+308} 123c5c4113dSnw141292do_test printf-7.3 { 124c5c4113dSnw141292 sqlite_mprintf_scaled {A double: %g} 1.0e307 100.0 125c5c4113dSnw141292} {A double: NaN} 126c5c4113dSnw141292 127c5c4113dSnw141292finish_test 128