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