1 /* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License as
5    published by the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful, but
8    WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10    General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
15 
16 #include <my_global.h>
17 #include <m_string.h>
18 #include <tap.h>
19 
20 char buf[1024]; /* let's hope that's enough */
21 
test1(const char * res,const char * fmt,...)22 void test1(const char *res, const char *fmt, ...)
23 {
24   va_list args;
25   size_t len;
26   va_start(args,fmt);
27   len= my_vsnprintf(buf, sizeof(buf)-1, fmt, args);
28   va_end(args);
29   ok(strlen(res) == len && strcmp(buf, res) == 0, "\"%s\"", buf);
30 }
31 
main(void)32 int main(void)
33 {
34   plan(58);
35 
36   test1("Constant string",
37         "Constant string");
38 
39   test1("Format specifier s works",
40         "Format specifier s %s", "works");
41   test1("Format specifier b works (mysql extension)",
42         "Format specifier b %.5b (mysql extension)", "works!!!");
43   test1("Format specifier c !",
44         "Format specifier c %c", '!');
45   test1("Format specifier d 1",
46         "Format specifier d %d", 1);
47   test1("Format specifier i 1",
48         "Format specifier i %i", 1);
49   test1("Format specifier u 2",
50         "Format specifier u %u", 2);
51   test1("Format specifier o 375",
52         "Format specifier o %o", 0375);
53   test1("Format specifier x a",
54         "Format specifier x %x", 10);
55   test1("Format specifier X B",
56         "Format specifier X %X", 11);
57   test1("Format specifier p 0x5",
58         "Format specifier p %p", 5);
59   test1("Format specifier f 3.141593",
60         "Format specifier f %f", 3.1415926);
61   test1("Format specifier g 3.1416",
62         "Format specifier g %g", 3.1415926);
63 
64   test1("Flag '-' is ignored <   1>",
65         "Flag '-' is ignored <%-4d>", 1);
66   test1("Flag '0' works <0006>",
67         "Flag '0' works <%04d>", 6);
68 
69   test1("Width is ignored for strings <x> <y>",
70         "Width is ignored for strings <%04s> <%5s>", "x", "y");
71 
72   test1("Precision works for strings <abcde>",
73         "Precision works for strings <%.5s>", "abcdef!");
74 
75   test1("Flag '`' (backtick) works: `abcd` `op``q` (mysql extension)",
76         "Flag '`' (backtick) works: %`s %`.4s (mysql extension)",
77         "abcd", "op`qrst");
78 
79   test1("Length modifiers work: 1 * -1 * 2 * 3",
80         "Length modifiers work: %d * %ld * %lld * %zd", 1, -1L, 2LL, (size_t)3);
81 
82   test1("Length modifiers work: 1 * -1 * 2 * 3",
83         "Length modifiers work: %i * %li * %lli * %zd", 1, -1L, 2LL, (size_t)3);
84 
85   test1("long long X: 123456789abcdef0",
86         "long long X: %llx", 0x123456789abcdef0LL);
87 
88   test1("(null) pointer is fine",
89         "%s pointer is fine", NULL);
90 
91   test1("Positional arguments work: on the dark side they are",
92         "Positional arguments work: %3$s %1$s %2$s",
93         "they", "are", "on the dark side");
94 
95   test1("Asterisk '*' as a width works: <    4>",
96         "Asterisk '*' as a width works: <%*d>", 5, 4);
97 
98   test1("Asterisk '*' as a precision works: <qwerty>",
99         "Asterisk '*' as a precision works: <%.*s>", 6, "qwertyuiop");
100 
101   test1("Positional arguments for a width: <    4>",
102         "Positional arguments for a width: <%1$*2$d>", 4, 5);
103 
104   test1("Positional arguments for a precision: <qwerty>",
105         "Positional arguments for a precision: <%1$.*2$s>", "qwertyuiop", 6);
106 
107   test1("Positional arguments and a width: <0000ab>",
108         "Positional arguments and a width: <%1$06x>", 0xab);
109 
110   test1("Positional arguments octal: <7777>",
111         "Positional arguments octal: <%1$o>", 07777);
112 
113   /* Can't use int arguments, as they may be different size from pointers */
114 
115   test1("Padding and %p <0x12> <0x034> <0x0000ab> <    0xcd>",
116         "Padding and %%p <%04p> <%05p> <%08p> <%8p>",
117         (void*) 0x12, (void*) 0x34, (void*) 0xab, (void*) 0xcd);
118 
119   test1("F with a width (ignored) and precision: <12.34568>",
120         "F with a width (ignored) and precision: <%10.5f>", 12.3456789);
121   test1("G with a width (ignored) and precision: <12.35>",
122         "G with a width (ignored) and precision: <%10.5g>", 12.3456789);
123 
124   diag("================================================================");
125 
126   test1("Hello",
127         "Hello");
128   test1("Hello int, 1",
129         "Hello int, %d", 1);
130   test1("Hello int, -1",
131         "Hello int, %d", -1);
132   test1("Hello int, 1",
133         "Hello int, %i", 1);
134   test1("Hello int, -1",
135         "Hello int, %i", -1);
136   test1("Hello string 'I am a string'",
137         "Hello string '%s'", "I am a string");
138   test1("Hello hack hack hack hack hack hack hack 1",
139         "Hello hack hack hack hack hack hack hack %d", 1);
140   test1("Hello 1 hack 4",
141         "Hello %d hack %d", 1, 4);
142   test1("Hello 1 hack hack hack hack hack 4",
143         "Hello %d hack hack hack hack hack %d", 1, 4);
144   test1("Hello 'hack' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh",
145         "Hello '%s' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "hack");
146   test1("Hello hhhhhhhhhhhhhh 1 sssssssssssssss",
147         "Hello hhhhhhhhhhhhhh %d sssssssssssssss", 1);
148   test1("Hello 1",
149         "Hello %u", 1);
150   test1("Hello 4294967295",
151         "Hello %u", -1);
152   test1("Hex:   20  '    41'",
153         "Hex:   %lx  '%6lx'", 32, 65);
154   test1("conn 1 to: '(null)' user: '(null)' host: '(null)' ((null))",
155         "conn %ld to: '%-.64s' user: '%-.32s' host: '%-.64s' (%-.64s)",
156                    1L,     NULL,          NULL,          NULL,    NULL);
157   test1("Hello string `I am a string`",
158         "Hello string %`s", "I am a string");
159   test1("Hello TEST",
160         "Hello %05s", "TEST");
161   test1("My `Q` test",
162         "My %1$`-.1s test", "QQQQ");
163   test1("My AAAA test done DDDD",
164         "My %2$s test done %1$s", "DDDD", "AAAA");
165   test1("My DDDD test CCCC, DDD",
166         "My %1$s test %2$s, %1$-.3s", "DDDD", "CCCC");
167   test1("My QQQQ test",
168         "My %1$`-.4b test", "QQQQ");
169   test1("My X test",
170         "My %1$c test", 'X');
171   test1("My <0000000010> test1 <   a> test2 <   A>",
172         "My <%010d> test1 <%4x> test2 <%4X>", 10, 10, 10);
173   test1("My <0000000010> test1 <   a> test2 <   a>",
174         "My <%1$010d> test1 <%2$4x> test2 <%2$4x>", 10, 10);
175   test1("My 00010 test",
176         "My %1$*02$d test", 10, 5);
177   test1("My `DDDD` test CCCC, `DDD`",
178         "My %1$`s test %2$s, %1$`-.3s", "DDDD", "CCCC");
179 
180   return exit_status();
181 }
182 
183