xref: /sqlite-3.40.0/test/printf.test (revision 4dcbdbff)
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.14 2004/09/09 00:35:30 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}
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}]
96
97do_test printf-4.1 {
98  sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all}
99} {1 2 A quoted string: 'Hi Y''all'}
100do_test printf-4.2 {
101  sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2
102} {1 2 A NULL pointer in %q: '(NULL)'}
103do_test printf-4.3 {
104  sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all}
105} {1 2 A quoted string: 'Hi Y''all'}
106do_test printf-4.4 {
107  sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2
108} {1 2 A NULL pointer in %Q: NULL}
109
110do_test printf-5.1 {
111  set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}]
112  string length $x
113} {994}
114do_test printf-5.2 {
115  sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello}
116} {-9 -10 (HelloHello) %}
117
118do_test printf-6.1 {
119  sqlite3_mprintf_z_test , one two three four five six
120} {,one,two,three,four,five,six}
121
122
123do_test printf-7.1 {
124  sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0
125} {A double: 1e+307}
126do_test printf-7.2 {
127  sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0
128} {A double: 1e+308}
129do_test printf-7.3 {
130  sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0
131} {A double: NaN}
132
133do_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
152do_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 {
162  sqlite3_mprintf_int {%yhello} 0 0 0
163} {%}
164
165# Ticket #812
166#
167do_test printf-10.1 {
168  sqlite3_mprintf_stronly %s {}
169} {}
170
171# Ticket #831
172#
173do_test printf-10.2 {
174  sqlite3_mprintf_stronly %q {}
175} {}
176
177
178finish_test
179