MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mysys_my_vsnprintf-t.cc
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 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include <my_global.h>
21 #include <m_string.h>
22 
23 namespace mysys_my_vsnprintf_unittest {
24 
25 char buf[1024]; /* let's hope that's enough */
26 
27 void test1(const char *res, const char *fmt, ...)
28 {
29  va_list args;
30  size_t len;
31  va_start(args,fmt);
32  len= my_vsnprintf(buf, sizeof(buf)-1, fmt, args);
33  va_end(args);
34  EXPECT_EQ(len, strlen(res));
35  EXPECT_STREQ(buf, res);
36 }
37 
38 
39 TEST(Mysys, Vsnprintf)
40 {
41  test1("Constant string",
42  "Constant string");
43 
44  test1("Format specifier s works",
45  "Format specifier s %s", "works");
46  test1("Format specifier b works (mysql extension)",
47  "Format specifier b %.5b (mysql extension)", "works!!!");
48  test1("Format specifier c !",
49  "Format specifier c %c", '!');
50  test1("Format specifier d 1",
51  "Format specifier d %d", 1);
52  test1("Format specifier i 1",
53  "Format specifier i %i", 1);
54  test1("Format specifier u 2",
55  "Format specifier u %u", 2);
56  test1("Format specifier o 375",
57  "Format specifier o %o", 0375);
58  test1("Format specifier x a",
59  "Format specifier x %x", 10);
60  test1("Format specifier X B",
61  "Format specifier X %X", 11);
62  test1("Format specifier p 0x5",
63  "Format specifier p %p", 5);
64  test1("Format specifier f 3.141593",
65  "Format specifier f %f", 3.1415926);
66  test1("Format specifier g 3.1416",
67  "Format specifier g %g", 3.1415926);
68 
69  test1("Flag '-' is ignored < 1>",
70  "Flag '-' is ignored <%-4d>", 1);
71  test1("Flag '0' works <0006>",
72  "Flag '0' works <%04d>", 6);
73 
74  test1("Width is ignored for strings <x> <y>",
75  "Width is ignored for strings <%04s> <%5s>", "x", "y");
76 
77  test1("Precision works for strings <abcde>",
78  "Precision works for strings <%.5s>", "abcdef!");
79 
80  test1("Flag '`' (backtick) works: `abcd` `op``q` (mysql extension)",
81  "Flag '`' (backtick) works: %`s %`.4s (mysql extension)",
82  "abcd", "op`qrst");
83 
84  test1("Length modifiers work: 1 * -1 * 2 * 3",
85  "Length modifiers work: %d * %ld * %lld * %zd", 1, -1L, 2LL, (size_t)3);
86 
87  test1("Length modifiers work: 1 * -1 * 2 * 3",
88  "Length modifiers work: %i * %li * %lli * %zd", 1, -1L, 2LL, (size_t)3);
89 
90  test1("long long X: 123456789abcdef0",
91  "long long X: %llx", 0x123456789abcdef0LL);
92 
93  test1("(null) pointer is fine",
94  "%s pointer is fine", NULL);
95 
96  test1("Positional arguments work: on the dark side they are",
97  "Positional arguments work: %3$s %1$s %2$s",
98  "they", "are", "on the dark side");
99 
100  test1("Asterisk '*' as a width works: < 4>",
101  "Asterisk '*' as a width works: <%*d>", 5, 4);
102 
103  test1("Asterisk '*' as a precision works: <qwerty>",
104  "Asterisk '*' as a precision works: <%.*s>", 6, "qwertyuiop");
105 
106  test1("Positional arguments for a width: < 4>",
107  "Positional arguments for a width: <%1$*2$d>", 4, 5);
108 
109  test1("Positional arguments for a precision: <qwerty>",
110  "Positional arguments for a precision: <%1$.*2$s>", "qwertyuiop", 6);
111 
112  test1("Positional arguments and a width: <0000ab>",
113  "Positional arguments and a width: <%1$06x>", 0xab);
114 
115  test1("Positional arguments octal: <7777>",
116  "Positional arguments octal: <%1$o>", 07777);
117 
118  /* Can't use int arguments, as they may be different size from pointers */
119 
120  test1("Padding and %p <0x12> <0x034> <0x0000ab> < 0xcd>",
121  "Padding and %%p <%04p> <%05p> <%08p> <%8p>",
122  (void*) 0x12, (void*) 0x34, (void*) 0xab, (void*) 0xcd);
123 
124  test1("F with a width (ignored) and precision: <12.34568>",
125  "F with a width (ignored) and precision: <%10.5f>", 12.3456789);
126  test1("G with a width (ignored) and precision: <12.35>",
127  "G with a width (ignored) and precision: <%10.5g>", 12.3456789);
128 
129  test1("Hello",
130  "Hello");
131  test1("Hello int, 1",
132  "Hello int, %d", 1);
133  test1("Hello int, -1",
134  "Hello int, %d", -1);
135  test1("Hello int, 1",
136  "Hello int, %i", 1);
137  test1("Hello int, -1",
138  "Hello int, %i", -1);
139  test1("Hello string 'I am a string'",
140  "Hello string '%s'", "I am a string");
141  test1("Hello hack hack hack hack hack hack hack 1",
142  "Hello hack hack hack hack hack hack hack %d", 1);
143  test1("Hello 1 hack 4",
144  "Hello %d hack %d", 1, 4);
145  test1("Hello 1 hack hack hack hack hack 4",
146  "Hello %d hack hack hack hack hack %d", 1, 4);
147  test1("Hello 'hack' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh",
148  "Hello '%s' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "hack");
149  test1("Hello hhhhhhhhhhhhhh 1 sssssssssssssss",
150  "Hello hhhhhhhhhhhhhh %d sssssssssssssss", 1);
151  test1("Hello 1",
152  "Hello %u", 1);
153  test1("Hello 4294967295",
154  "Hello %u", -1);
155  test1("Hex: 20 ' 41'",
156  "Hex: %lx '%6lx'", 32, 65);
157  test1("conn 1 to: '(null)' user: '(null)' host: '(null)' ((null))",
158  "conn %ld to: '%-.64s' user: '%-.32s' host: '%-.64s' (%-.64s)",
159  1L, NULL, NULL, NULL, NULL);
160  test1("Hello string `I am a string`",
161  "Hello string %`s", "I am a string");
162  test1("Hello TEST",
163  "Hello %05s", "TEST");
164  test1("My `Q` test",
165  "My %1$`-.1s test", "QQQQ");
166  test1("My AAAA test done DDDD",
167  "My %2$s test done %1$s", "DDDD", "AAAA");
168  test1("My DDDD test CCCC, DDD",
169  "My %1$s test %2$s, %1$-.3s", "DDDD", "CCCC");
170  test1("My QQQQ test",
171  "My %1$`-.4b test", "QQQQ");
172  test1("My X test",
173  "My %1$c test", 'X');
174  test1("My <0000000010> test1 < a> test2 < A>",
175  "My <%010d> test1 <%4x> test2 <%4X>", 10, 10, 10);
176  test1("My <0000000010> test1 < a> test2 < a>",
177  "My <%1$010d> test1 <%2$4x> test2 <%2$4x>", 10, 10);
178  test1("My 00010 test",
179  "My %1$*02$d test", 10, 5);
180  test1("My `DDDD` test CCCC, `DDD`",
181  "My %1$`s test %2$s, %1$`-.3s", "DDDD", "CCCC");
182 
183 }
184 
185 }