blob: 62f6443e714ebc59a86aef4a524429a4d41b21b4 [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#
drh9796ef52004-07-27 13:38:47 +000014# $Id: printf.test,v 1.13 2004/07/27 13:38: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]
drh779c6a02004-06-29 13:04:32 +000039 do_test printf-1.$n.7 [subst {
40 sqlite3_mprintf_int {Three integers: (%#6d) (%#6x) (%#6o)} $v $v $v
41 }] [format {Three integers: (%#6d) (%#6x) (%#6o)} $v $v $v]
drhd1bf3512001-04-07 15:24:33 +000042 incr n
43}
44
drh254cba22001-09-20 01:44:42 +000045
46if {$::tcl_platform(platform)!="windows"} {
47
drhd1bf3512001-04-07 15:24:33 +000048set m 1
49foreach {a b} {1 1 5 5 10 10 10 5} {
50 set n 1
51 foreach x {0.001 1.0e-20 1.0 0.0 100.0 9.99999 -0.00543 -1.0 -99.99999} {
52 do_test printf-2.$m.$n.1 [subst {
danielk19772dd59362004-06-14 23:46:47 +000053 sqlite3_mprintf_double {A double: %*.*f} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000054 }] [format {A double: %*.*f} $a $b $x]
55 do_test printf-2.$m.$n.2 [subst {
danielk19772dd59362004-06-14 23:46:47 +000056 sqlite3_mprintf_double {A double: %*.*e} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000057 }] [format {A double: %*.*e} $a $b $x]
58 do_test printf-2.$m.$n.3 [subst {
danielk19772dd59362004-06-14 23:46:47 +000059 sqlite3_mprintf_double {A double: %*.*g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000060 }] [format {A double: %*.*g} $a $b $x]
61 do_test printf-2.$m.$n.4 [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 do_test printf-2.$m.$n.5 [subst {
danielk19772dd59362004-06-14 23:46:47 +000065 sqlite3_mprintf_double {A double: %d %d %#g} $a $b $x
drhd1bf3512001-04-07 15:24:33 +000066 }] [format {A double: %d %d %#g} $a $b $x]
drh779c6a02004-06-29 13:04:32 +000067 do_test printf-2.$m.$n.6 [subst {
68 sqlite3_mprintf_double {A double: %d %d %010g} $a $b $x
69 }] [format {A double: %d %d %010g} $a $b $x]
drhd1bf3512001-04-07 15:24:33 +000070 incr n
71 }
72 incr m
73}
74
drh254cba22001-09-20 01:44:42 +000075}
76
drhd1bf3512001-04-07 15:24:33 +000077do_test printf-3.1 {
danielk19772dd59362004-06-14 23:46:47 +000078 sqlite3_mprintf_str {A String: (%*.*s)} 10 10 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000079} [format {A String: (%*.*s)} 10 10 {This is the string}]
80do_test printf-3.2 {
danielk19772dd59362004-06-14 23:46:47 +000081 sqlite3_mprintf_str {A String: (%*.*s)} 10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000082} [format {A String: (%*.*s)} 10 5 {This is the string}]
83do_test printf-3.3 {
danielk19772dd59362004-06-14 23:46:47 +000084 sqlite3_mprintf_str {A String: (%*.*s)} -10 5 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000085} [format {A String: (%*.*s)} -10 5 {This is the string}]
86do_test printf-3.4 {
danielk19772dd59362004-06-14 23:46:47 +000087 sqlite3_mprintf_str {%d %d A String: (%s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000088} [format {%d %d A String: (%s)} 1 2 {This is the string}]
89do_test printf-3.5 {
danielk19772dd59362004-06-14 23:46:47 +000090 sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000091} [format {%d %d A String: (%30s)} 1 2 {This is the string}]
92do_test printf-3.6 {
danielk19772dd59362004-06-14 23:46:47 +000093 sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
drhd1bf3512001-04-07 15:24:33 +000094} [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
95
96do_test printf-4.1 {
danielk19772dd59362004-06-14 23:46:47 +000097 sqlite3_mprintf_str {%d %d A quoted string: '%q'} 1 2 {Hi Y'all}
drhd1bf3512001-04-07 15:24:33 +000098} {1 2 A quoted string: 'Hi Y''all'}
chwb22f1b52002-06-16 04:56:36 +000099do_test printf-4.2 {
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)'}
102do_test printf-4.3 {
danielk19772dd59362004-06-14 23:46:47 +0000103 sqlite3_mprintf_str {%d %d A quoted string: %Q} 1 2 {Hi Y'all}
chwb22f1b52002-06-16 04:56:36 +0000104} {1 2 A quoted string: 'Hi Y''all'}
105do_test printf-4.4 {
danielk19772dd59362004-06-14 23:46:47 +0000106 sqlite3_mprintf_str {%d %d A NULL pointer in %%Q: %Q} 1 2
chwb22f1b52002-06-16 04:56:36 +0000107} {1 2 A NULL pointer in %Q: NULL}
drhd1bf3512001-04-07 15:24:33 +0000108
drhdaffd0e2001-04-11 14:28:42 +0000109do_test printf-5.1 {
danielk19772dd59362004-06-14 23:46:47 +0000110 set x [sqlite3_mprintf_str {%d %d %100000s} 0 0 {Hello}]
drhdaffd0e2001-04-11 14:28:42 +0000111 string length $x
112} {994}
113do_test printf-5.2 {
danielk19772dd59362004-06-14 23:46:47 +0000114 sqlite3_mprintf_str {%d %d (%-10.10s) %} -9 -10 {HelloHelloHello}
drhdaffd0e2001-04-11 14:28:42 +0000115} {-9 -10 (HelloHello) %}
drhdaffd0e2001-04-11 14:28:42 +0000116
drhd93d8a82003-06-16 03:08:18 +0000117do_test printf-6.1 {
danielk19772dd59362004-06-14 23:46:47 +0000118 sqlite3_mprintf_z_test , one two three four five six
drhd93d8a82003-06-16 03:08:18 +0000119} {,one,two,three,four,five,six}
120
drhb621c232004-02-21 19:41:04 +0000121
122do_test printf-7.1 {
danielk19772dd59362004-06-14 23:46:47 +0000123 sqlite3_mprintf_scaled {A double: %g} 1.0e307 1.0
drhb621c232004-02-21 19:41:04 +0000124} {A double: 1e+307}
125do_test printf-7.2 {
danielk19772dd59362004-06-14 23:46:47 +0000126 sqlite3_mprintf_scaled {A double: %g} 1.0e307 10.0
drhb621c232004-02-21 19:41:04 +0000127} {A double: 1e+308}
128do_test printf-7.3 {
danielk19772dd59362004-06-14 23:46:47 +0000129 sqlite3_mprintf_scaled {A double: %g} 1.0e307 100.0
drhb621c232004-02-21 19:41:04 +0000130} {A double: NaN}
131
drhe9707672004-06-25 01:10:48 +0000132do_test printf-8.1 {
133 sqlite3_mprintf_int {%u %u %u} 0x7fffffff 0x80000000 0xffffffff
134} {2147483647 2147483648 4294967295}
135do_test printf-8.2 {
136 sqlite3_mprintf_int {%lu %lu %lu} 0x7fffffff 0x80000000 0xffffffff
137} {2147483647 2147483648 4294967295}
138do_test printf-8.3 {
139 sqlite3_mprintf_int64 {%llu %llu %llu} 2147483647 2147483648 4294967296
140} {2147483647 2147483648 4294967296}
141do_test printf-8.4 {
142 sqlite3_mprintf_int64 {%lld %lld %lld} 2147483647 2147483648 4294967296
143} {2147483647 2147483648 4294967296}
144do_test printf-8.5 {
145 sqlite3_mprintf_int64 {%llx %llx %llx} 2147483647 2147483648 4294967296
146} {7fffffff 80000000 100000000}
147do_test printf-8.6 {
148 sqlite3_mprintf_int64 {%llx %llo %lld} -1 -1 -1
149} {ffffffffffffffff 1777777777777777777777 -1}
150
drh779c6a02004-06-29 13:04:32 +0000151do_test printf-9.1 {
152 sqlite3_mprintf_int {%*.*c} 4 4 65
153} {AAAA}
154do_test printf-9.2 {
155 sqlite3_mprintf_int {%*.*c} -4 1 66
156} {B }
157do_test printf-9.3 {
158 sqlite3_mprintf_int {%*.*c} 4 1 67
159} { C}
160do_test printf-9.4 {
161 sqlite3_mprintf_int {%yhello} 0 0 0
162} {%}
163
drhe29b1a02004-07-17 21:56:09 +0000164# Ticket #812
165#
166do_test printf-10.1 {
167 sqlite3_mprintf_stronly %s {}
168} {}
169
drh9796ef52004-07-27 13:38:47 +0000170# Ticket #831
171#
172do_test printf-10.2 {
173 sqlite3_mprintf_stronly %q {}
174} {}
175
drhe29b1a02004-07-17 21:56:09 +0000176
drhd1bf3512001-04-07 15:24:33 +0000177finish_test