blob: b3eb225e96a202e772628337a4ccdd13c0504869 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhd1bf3512001-04-07 15:24:33 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhd1bf3512001-04-07 15:24:33 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhd1bf3512001-04-07 15:24:33 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the sqlite_*_printf() interface.
13#
drh43617e92006-03-06 20:55:46 +000014# $Id: printf.test,v 1.20 2006/03/06 20:55:46 drh Exp $
drhd1bf3512001-04-07 15:24:33 +000015
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} {
drh6aff5072004-09-09 00:35:29 +000021 set v32 [expr {$v&0xffffffff}]
drhd1bf3512001-04-07 15:24:33 +000022 do_test printf-1.$n.1 [subst {
danielk19772dd59362004-06-14 23:46:47 +000023 sqlite3_mprintf_int {Three integers: %d %x %o} $v $v $v
drh6aff5072004-09-09 00:35:29 +000024 }] [format {Three integers: %d %x %o} $v $v32 $v32]
drhd1bf3512001-04-07 15:24:33 +000025 do_test printf-1.$n.2 [subst {
danielk19772dd59362004-06-14 23:46:47 +000026 sqlite3_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000027 }] [format {Three integers: (%6d) (%6x) (%6o)} $v $v32 $v32]
drhd1bf3512001-04-07 15:24:33 +000028 do_test printf-1.$n.3 [subst {
danielk19772dd59362004-06-14 23:46:47 +000029 sqlite3_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000030 }] [format {Three integers: (%-6d) (%-6x) (%-6o)} $v $v32 $v32]
drhdaffd0e2001-04-11 14:28:42 +000031 do_test printf-1.$n.4 [subst {
danielk19772dd59362004-06-14 23:46:47 +000032 sqlite3_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000033 }] [format {Three integers: (%+6d) (%+6x) (%+6o)} $v $v32 $v32]
drhdaffd0e2001-04-11 14:28:42 +000034 do_test printf-1.$n.5 [subst {
danielk19772dd59362004-06-14 23:46:47 +000035 sqlite3_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000036 }] [format {Three integers: (%06d) (%06x) (%06o)} $v $v32 $v32]
drhdaffd0e2001-04-11 14:28:42 +000037 do_test printf-1.$n.6 [subst {
danielk19772dd59362004-06-14 23:46:47 +000038 sqlite3_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000039 }] [format {Three integers: (% 6d) (% 6x) (% 6o)} $v $v32 $v32]
drh779c6a02004-06-29 13:04:32 +000040 do_test printf-1.$n.7 [subst {
41 sqlite3_mprintf_int {Three integers: (%#6d) (%#6x) (%#6o)} $v $v $v
drh6aff5072004-09-09 00:35:29 +000042 }] [format {Three integers: (%#6d) (%#6x) (%#6o)} $v $v32 $v32]
drhd1bf3512001-04-07 15:24:33 +000043 incr n
44}
45
drh254cba22001-09-20 01:44:42 +000046
47if {$::tcl_platform(platform)!="windows"} {
48
drhd1bf3512001-04-07 15:24:33 +000049set 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 {
danielk19772dd59362004-06-14 23:46:47 +000054 sqlite3_mprintf_double {A double: %*.*f} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000055 }] [format {A double: %*.*f} $a $b $x]
56 do_test printf-2.$m.$n.2 [subst {
danielk19772dd59362004-06-14 23:46:47 +000057 sqlite3_mprintf_double {A double: %*.*e} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000058 }] [format {A double: %*.*e} $a $b $x]
59 do_test printf-2.$m.$n.3 [subst {
danielk19772dd59362004-06-14 23:46:47 +000060 sqlite3_mprintf_double {A double: %*.*g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000061 }] [format {A double: %*.*g} $a $b $x]
62 do_test printf-2.$m.$n.4 [subst {
danielk19772dd59362004-06-14 23:46:47 +000063 sqlite3_mprintf_double {A double: %d %d %g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000064 }] [format {A double: %d %d %g} $a $b $x]
65 do_test printf-2.$m.$n.5 [subst {
danielk19772dd59362004-06-14 23:46:47 +000066 sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000067 }] [format {A double: %d %d %#g} $a $b $x]
drh779c6a02004-06-29 13:04:32 +000068 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]
drhd1bf3512001-04-07 15:24:33 +000071 incr n
72 }
73 incr m
74}
75
drh592ac8c2005-08-13 03:07:47 +000076} ;# endif not windows
drh254cba22001-09-20 01:44:42 +000077
drhd1bf3512001-04-07 15:24:33 +000078do_test printf-3.1 {
danielk19772dd59362004-06-14 23:46:47 +000079 sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000080} [format {A String: (%*.*s)} 10 10 {This is the string}]
81do_test printf-3.2 {
danielk19772dd59362004-06-14 23:46:47 +000082 sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000083} [format {A String: (%*.*s)} 10 5 {This is the string}]
84do_test printf-3.3 {
danielk19772dd59362004-06-14 23:46:47 +000085 sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000086} [format {A String: (%*.*s)} -10 5 {This is the string}]
87do_test printf-3.4 {
danielk19772dd59362004-06-14 23:46:47 +000088 sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000089} [format {%d %d A String: (%s)} 1 2 {This is the string}]
90do_test printf-3.5 {
danielk19772dd59362004-06-14 23:46:47 +000091 sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000092} [format {%d %d A String: (%30s)} 1 2 {This is the string}]
93do_test printf-3.6 {
danielk19772dd59362004-06-14 23:46:47 +000094 sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000095} [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
96
97do_test printf-4.1 {
danielk19772dd59362004-06-14 23:46:47 +000098 sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all}
drhd1bf3512001-04-07 15:24:33 +000099} {1 2 A quoted string: 'Hi Y''all'}
chwb22f1b52002-06-16 04:56:36 +0000100do_test printf-4.2 {
danielk19772dd59362004-06-14 23:46:47 +0000101 sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2
chwb22f1b52002-06-16 04:56:36 +0000102} {1 2 A NULL pointer in %q: '(NULL)'}
103do_test printf-4.3 {
danielk19772dd59362004-06-14 23:46:47 +0000104 sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all}
chwb22f1b52002-06-16 04:56:36 +0000105} {1 2 A quoted string: 'Hi Y''all'}
106do_test printf-4.4 {
danielk19772dd59362004-06-14 23:46:47 +0000107 sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2
chwb22f1b52002-06-16 04:56:36 +0000108} {1 2 A NULL pointer in %Q: NULL}
drhd1bf3512001-04-07 15:24:33 +0000109
drhdaffd0e2001-04-11 14:28:42 +0000110do_test printf-5.1 {
danielk19772dd59362004-06-14 23:46:47 +0000111 set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}]
drhdaffd0e2001-04-11 14:28:42 +0000112 string length $x
drh79158e12005-09-06 21:40:45 +0000113} {344}
drhdaffd0e2001-04-11 14:28:42 +0000114do_test printf-5.2 {
danielk19772dd59362004-06-14 23:46:47 +0000115 sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello}
drhdaffd0e2001-04-11 14:28:42 +0000116} {-9 -10 (HelloHello) %}
drhdaffd0e2001-04-11 14:28:42 +0000117
drhd93d8a82003-06-16 03:08:18 +0000118do_test printf-6.1 {
danielk19772dd59362004-06-14 23:46:47 +0000119 sqlite3_mprintf_z_test , one two three four five six
drhd93d8a82003-06-16 03:08:18 +0000120} {,one,two,three,four,five,six}
121
drhb621c232004-02-21 19:41:04 +0000122
123do_test printf-7.1 {
danielk19772dd59362004-06-14 23:46:47 +0000124 sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0
drhb621c232004-02-21 19:41:04 +0000125} {A double: 1e+307}
126do_test printf-7.2 {
danielk19772dd59362004-06-14 23:46:47 +0000127 sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0
drhb621c232004-02-21 19:41:04 +0000128} {A double: 1e+308}
129do_test printf-7.3 {
danielk19772dd59362004-06-14 23:46:47 +0000130 sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0
drhb621c232004-02-21 19:41:04 +0000131} {A double: NaN}
132
drhe9707672004-06-25 01:10:48 +0000133do_test printf-8.1 {
134 sqlite3_mprintf_int {%u %u %u} 0x7fffffff 0x80000000 0xffffffff
135} {2147483647 2147483648 4294967295}
136do_test printf-8.2 {
137 sqlite3_mprintf_int {%lu %lu %lu} 0x7fffffff 0x80000000 0xffffffff
138} {2147483647 2147483648 4294967295}
139do_test printf-8.3 {
140 sqlite3_mprintf_int64 {%llu %llu %llu} 2147483647 2147483648 4294967296
141} {2147483647 2147483648 4294967296}
142do_test printf-8.4 {
143 sqlite3_mprintf_int64 {%lld %lld %lld} 2147483647 2147483648 4294967296
144} {2147483647 2147483648 4294967296}
145do_test printf-8.5 {
146 sqlite3_mprintf_int64 {%llx %llx %llx} 2147483647 2147483648 4294967296
147} {7fffffff 80000000 100000000}
148do_test printf-8.6 {
149 sqlite3_mprintf_int64 {%llx %llo %lld} -1 -1 -1
150} {ffffffffffffffff 1777777777777777777777 -1}
151
drh779c6a02004-06-29 13:04:32 +0000152do_test printf-9.1 {
153 sqlite3_mprintf_int {%*.*c} 4 4 65
154} {AAAA}
155do_test printf-9.2 {
156 sqlite3_mprintf_int {%*.*c} -4 1 66
157} {B }
158do_test printf-9.3 {
159 sqlite3_mprintf_int {%*.*c} 4 1 67
160} { C}
161do_test printf-9.4 {
drh5eba8c02005-08-19 02:26:27 +0000162 sqlite3_mprintf_int {%d %d %c} 4 1 67
163} {4 1 C}
164set ten { }
165set fifty $ten$ten$ten$ten$ten
166do_test printf-9.5 {
167 sqlite3_mprintf_int {%d %*c} 1 -201 67
168} "1 C$fifty$fifty$fifty$fifty"
169do_test printf-9.6 {
drh43617e92006-03-06 20:55:46 +0000170 sqlite3_mprintf_int {hi%12345.12346yhello} 0 0 0
171} {hi}
drh779c6a02004-06-29 13:04:32 +0000172
drhe29b1a02004-07-17 21:56:09 +0000173# Ticket #812
174#
175do_test printf-10.1 {
176 sqlite3_mprintf_stronly %s {}
177} {}
178
drh9796ef52004-07-27 13:38:47 +0000179# Ticket #831
180#
181do_test printf-10.2 {
182 sqlite3_mprintf_stronly %q {}
183} {}
184
drh592ac8c2005-08-13 03:07:47 +0000185# Ticket #1340: Test for loss of precision on large positive exponents
186#
187do_test printf-10.3 {
188 sqlite3_mprintf_double {%d %d %f} 1 1 1e300
189} {1 1 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000}
190
drh557cc602005-08-13 12:59:14 +0000191# The non-standard '!' flag on a 'g' conversion forces a decimal point
192# and at least one digit on either side of the decimal point.
193#
194do_test printf-11.1 {
195 sqlite3_mprintf_double {%d %d %!g} 1 1 1
196} {1 1 1.0}
197do_test printf-11.2 {
198 sqlite3_mprintf_double {%d %d %!g} 1 1 123
199} {1 1 123.0}
200do_test printf-11.3 {
201 sqlite3_mprintf_double {%d %d %!g} 1 1 12.3
202} {1 1 12.3}
203do_test printf-11.4 {
204 sqlite3_mprintf_double {%d %d %!g} 1 1 0.123
205} {1 1 0.123}
206do_test printf-11.5 {
207 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1
208} {1 1 1.0}
209do_test printf-11.6 {
210 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e10
211} {1 1 10000000000.0}
212do_test printf-11.7 {
213 sqlite3_mprintf_double {%d %d %!.15g} 1 1 1e300
214} {1 1 1.0e+300}
215
drh5eba8c02005-08-19 02:26:27 +0000216# Additional tests for coverage
217#
218do_test printf-12.1 {
219 sqlite3_mprintf_double {%d %d %.2000g} 1 1 1.0
220} {1 1 1}
drhe29b1a02004-07-17 21:56:09 +0000221
drh63782852005-08-30 19:30:59 +0000222# Floating point boundary cases
223#
224do_test printf-13.1 {
225 sqlite3_mprintf_hexdouble %.20f 4024000000000000
226} {10.00000000000000000000}
227do_test printf-13.2 {
228 sqlite3_mprintf_hexdouble %.20f 4197d78400000000
229} {100000000.00000000000000000000}
230do_test printf-13.3 {
231 sqlite3_mprintf_hexdouble %.20f 4693b8b5b5056e17
232} {100000000000000000000000000000000.00000000000000000000}
233
234
drhd1bf3512001-04-07 15:24:33 +0000235finish_test