blob: 727bf2dbff0f7458804d0c2abf9d23a570d12df3 [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#
drhe9707672004-06-25 01:10:48 +000014# $Id: printf.test,v 1.10 2004/06/25 01:10:48 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} {
21 do_test printf-1.$n.1 [subst {
danielk19772dd59362004-06-14 23:46:47 +000022 sqlite3_mprintf_int {Three integers: %d %x %o} $v $v $v
drhe9707672004-06-25 01:10:48 +000023 }] [format {Three integers: %d %x %o} $v $v $v]
drhd1bf3512001-04-07 15:24:33 +000024 do_test printf-1.$n.2 [subst {
danielk19772dd59362004-06-14 23:46:47 +000025 sqlite3_mprintf_int {Three integers: (%6d) (%6x) (%6o)} $v $v $v
drhe9707672004-06-25 01:10:48 +000026 }] [format {Three integers: (%6d) (%6x) (%6o)} $v $v $v]
drhd1bf3512001-04-07 15:24:33 +000027 do_test printf-1.$n.3 [subst {
danielk19772dd59362004-06-14 23:46:47 +000028 sqlite3_mprintf_int {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v
drhe9707672004-06-25 01:10:48 +000029 }] [format {Three integers: (%-6d) (%-6x) (%-6o)} $v $v $v]
drhdaffd0e2001-04-11 14:28:42 +000030 do_test printf-1.$n.4 [subst {
danielk19772dd59362004-06-14 23:46:47 +000031 sqlite3_mprintf_int {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v
drhe9707672004-06-25 01:10:48 +000032 }] [format {Three integers: (%+6d) (%+6x) (%+6o)} $v $v $v]
drhdaffd0e2001-04-11 14:28:42 +000033 do_test printf-1.$n.5 [subst {
danielk19772dd59362004-06-14 23:46:47 +000034 sqlite3_mprintf_int {Three integers: (%06d) (%06x) (%06o)} $v $v $v
drhe9707672004-06-25 01:10:48 +000035 }] [format {Three integers: (%06d) (%06x) (%06o)} $v $v $v]
drhdaffd0e2001-04-11 14:28:42 +000036 do_test printf-1.$n.6 [subst {
danielk19772dd59362004-06-14 23:46:47 +000037 sqlite3_mprintf_int {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v
drhe9707672004-06-25 01:10:48 +000038 }] [format {Three integers: (% 6d) (% 6x) (% 6o)} $v $v $v]
drhd1bf3512001-04-07 15:24:33 +000039 incr n
40}
41
drh254cba22001-09-20 01:44:42 +000042
43if {$::tcl_platform(platform)!="windows"} {
44
drhd1bf3512001-04-07 15:24:33 +000045set m 1
46foreach {a b} {1 1 5 5 10 10 10 5} {
47 set n 1
48 foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} {
49 do_test printf-2.$m.$n.1 [subst {
danielk19772dd59362004-06-14 23:46:47 +000050 sqlite3_mprintf_double {A double: %*.*f} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000051 }] [format {A double: %*.*f} $a $b $x]
52 do_test printf-2.$m.$n.2 [subst {
danielk19772dd59362004-06-14 23:46:47 +000053 sqlite3_mprintf_double {A double: %*.*e} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000054 }] [format {A double: %*.*e} $a $b $x]
55 do_test printf-2.$m.$n.3 [subst {
danielk19772dd59362004-06-14 23:46:47 +000056 sqlite3_mprintf_double {A double: %*.*g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000057 }] [format {A double: %*.*g} $a $b $x]
58 do_test printf-2.$m.$n.4 [subst {
danielk19772dd59362004-06-14 23:46:47 +000059 sqlite3_mprintf_double {A double: %d %d %g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000060 }] [format {A double: %d %d %g} $a $b $x]
61 do_test printf-2.$m.$n.5 [subst {
danielk19772dd59362004-06-14 23:46:47 +000062 sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000063 }] [format {A double: %d %d %#g} $a $b $x]
64 incr n
65 }
66 incr m
67}
68
drh254cba22001-09-20 01:44:42 +000069}
70
drhd1bf3512001-04-07 15:24:33 +000071do_test printf-3.1 {
danielk19772dd59362004-06-14 23:46:47 +000072 sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000073} [format {A String: (%*.*s)} 10 10 {This is the string}]
74do_test printf-3.2 {
danielk19772dd59362004-06-14 23:46:47 +000075 sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000076} [format {A String: (%*.*s)} 10 5 {This is the string}]
77do_test printf-3.3 {
danielk19772dd59362004-06-14 23:46:47 +000078 sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000079} [format {A String: (%*.*s)} -10 5 {This is the string}]
80do_test printf-3.4 {
danielk19772dd59362004-06-14 23:46:47 +000081 sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000082} [format {%d %d A String: (%s)} 1 2 {This is the string}]
83do_test printf-3.5 {
danielk19772dd59362004-06-14 23:46:47 +000084 sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000085} [format {%d %d A String: (%30s)} 1 2 {This is the string}]
86do_test printf-3.6 {
danielk19772dd59362004-06-14 23:46:47 +000087 sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000088} [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
89
90do_test printf-4.1 {
danielk19772dd59362004-06-14 23:46:47 +000091 sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all}
drhd1bf3512001-04-07 15:24:33 +000092} {1 2 A quoted string: 'Hi Y''all'}
chwb22f1b52002-06-16 04:56:36 +000093do_test printf-4.2 {
danielk19772dd59362004-06-14 23:46:47 +000094 sqlite3_mprintf_str {%d %d A NULL pointer in %%q: '%q'} 1 2
chwb22f1b52002-06-16 04:56:36 +000095} {1 2 A NULL pointer in %q: '(NULL)'}
96do_test printf-4.3 {
danielk19772dd59362004-06-14 23:46:47 +000097 sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all}
chwb22f1b52002-06-16 04:56:36 +000098} {1 2 A quoted string: 'Hi Y''all'}
99do_test printf-4.4 {
danielk19772dd59362004-06-14 23:46:47 +0000100 sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2
chwb22f1b52002-06-16 04:56:36 +0000101} {1 2 A NULL pointer in %Q: NULL}
drhd1bf3512001-04-07 15:24:33 +0000102
drhdaffd0e2001-04-11 14:28:42 +0000103do_test printf-5.1 {
danielk19772dd59362004-06-14 23:46:47 +0000104 set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}]
drhdaffd0e2001-04-11 14:28:42 +0000105 string length $x
106} {994}
107do_test printf-5.2 {
danielk19772dd59362004-06-14 23:46:47 +0000108 sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello}
drhdaffd0e2001-04-11 14:28:42 +0000109} {-9 -10 (HelloHello) %}
drhdaffd0e2001-04-11 14:28:42 +0000110
drhd93d8a82003-06-16 03:08:18 +0000111do_test printf-6.1 {
danielk19772dd59362004-06-14 23:46:47 +0000112 sqlite3_mprintf_z_test , one two three four five six
drhd93d8a82003-06-16 03:08:18 +0000113} {,one,two,three,four,five,six}
114
drhb621c232004-02-21 19:41:04 +0000115
116do_test printf-7.1 {
danielk19772dd59362004-06-14 23:46:47 +0000117 sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0
drhb621c232004-02-21 19:41:04 +0000118} {A double: 1e+307}
119do_test printf-7.2 {
danielk19772dd59362004-06-14 23:46:47 +0000120 sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0
drhb621c232004-02-21 19:41:04 +0000121} {A double: 1e+308}
122do_test printf-7.3 {
danielk19772dd59362004-06-14 23:46:47 +0000123 sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0
drhb621c232004-02-21 19:41:04 +0000124} {A double: NaN}
125
drhe9707672004-06-25 01:10:48 +0000126do_test printf-8.1 {
127 sqlite3_mprintf_int {%u %u %u} 0x7fffffff 0x80000000 0xffffffff
128} {2147483647 2147483648 4294967295}
129do_test printf-8.2 {
130 sqlite3_mprintf_int {%lu %lu %lu} 0x7fffffff 0x80000000 0xffffffff
131} {2147483647 2147483648 4294967295}
132do_test printf-8.3 {
133 sqlite3_mprintf_int64 {%llu %llu %llu} 2147483647 2147483648 4294967296
134} {2147483647 2147483648 4294967296}
135do_test printf-8.4 {
136 sqlite3_mprintf_int64 {%lld %lld %lld} 2147483647 2147483648 4294967296
137} {2147483647 2147483648 4294967296}
138do_test printf-8.5 {
139 sqlite3_mprintf_int64 {%llx %llx %llx} 2147483647 2147483648 4294967296
140} {7fffffff 80000000 100000000}
141do_test printf-8.6 {
142 sqlite3_mprintf_int64 {%llx %llo %lld} -1 -1 -1
143} {ffffffffffffffff 1777777777777777777777 -1}
144
drhd1bf3512001-04-07 15:24:33 +0000145finish_test