Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: base/debug/format_unittest.cc

Issue 18656004: Added a new SafeSPrintf() function that implements snprintf() in an async-safe-fashion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed reference value in unittest Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <limits>
9
10 #include "base/debug/format.h"
11 #include "base/logging.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace base {
15 namespace debug {
16
17 TEST(FormatTest, Empty) {
18 char buf[2] = { 'X', 'X' };
19
20 // Negative buffer size should always result in an error.
21 EXPECT_EQ(-1, FormatN(buf, -1, ""));
22 EXPECT_EQ('X', buf[0]);
23 EXPECT_EQ('X', buf[1]);
24
25 // Zero buffer size should always result in an error.
26 EXPECT_EQ(-1, FormatN(buf, 0, ""));
27 EXPECT_EQ('X', buf[0]);
28 EXPECT_EQ('X', buf[1]);
29
30 // A one-byte buffer should always print a single NUL byte.
31 EXPECT_EQ(0, FormatN(buf, 1, ""));
32 EXPECT_EQ(0, buf[0]);
33 EXPECT_EQ('X', buf[1]);
34 buf[0] = 'X';
35
36 // A larger buffer should leave the trailing bytes unchanged.
37 EXPECT_EQ(0, FormatN(buf, 2, ""));
38 EXPECT_EQ(0, buf[0]);
39 EXPECT_EQ('X', buf[1]);
40 buf[0] = 'X';
41
42 // The same test using Format() instead of FormatN().
43 EXPECT_EQ(0, Format(buf, ""));
44 EXPECT_EQ(0, buf[0]);
45 EXPECT_EQ('X', buf[1]);
46 buf[0] = 'X';
47 }
48
49 TEST(FormatTest, NoArguments) {
50 // Output a text message that doesn't require any substitutions. This
51 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does
52 // always add a trailing NUL; it always deduplicates '%' characters).
53 const char text[] = "hello world";
54 char ref[20], buf[20];
55 memset(ref, 'X', sizeof(buf));
56 memcpy(buf, ref, sizeof(buf));
57
58 // A negative buffer size should always result in an error.
59 EXPECT_EQ(-1, FormatN(buf, -1, text));
60 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
61
62 // Zero buffer size should always result in an error.
63 EXPECT_EQ(-1, FormatN(buf, 0, text));
64 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
65
66 // A one-byte buffer should always print a single NUL byte.
67 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 1, text));
68 EXPECT_EQ(0, buf[0]);
69 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1));
70 memcpy(buf, ref, sizeof(buf));
71
72 // A larger (but limited) buffer should always leave the trailing bytes
73 // unchanged.
74 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 2, text));
75 EXPECT_EQ(text[0], buf[0]);
76 EXPECT_EQ(0, buf[1]);
77 EXPECT_TRUE(!memcmp(buf+2, ref+2, sizeof(buf)-2));
78 memcpy(buf, ref, sizeof(buf));
79
80 // A unrestricted buffer length should always leave the trailing bytes
81 // unchanged.
82 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1,
83 FormatN(buf, sizeof(buf), text));
84 EXPECT_EQ(std::string(text), std::string(buf));
85 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
86 sizeof(buf) - sizeof(text)));
87 memcpy(buf, ref, sizeof(buf));
88
89 // The same test using Format() instead of FormatN().
90 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, Format(buf, text));
91 EXPECT_EQ(std::string(text), std::string(buf));
92 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
93 sizeof(buf) - sizeof(text)));
94 memcpy(buf, ref, sizeof(buf));
95
96 // Check for deduplication of '%' percent characters.
97 EXPECT_EQ(1, Format(buf, "%"));
98 EXPECT_EQ(1, Format(buf, "%%"));
99 EXPECT_EQ(2, Format(buf, "%%%"));
100 EXPECT_EQ(2, Format(buf, "%%%%"));
101 EXPECT_EQ(2, Format(buf, "%X"));
102 EXPECT_EQ(2, Format(buf, "%%X"));
103 EXPECT_EQ(3, Format(buf, "%%%X"));
104 EXPECT_EQ(3, Format(buf, "%%%%X"));
105 }
106
107 TEST(FormatTest, OneArgument) {
108 // Test basic single-argument single-character substitution.
109 const char text[] = "hello world";
110 const char fmt[] = "hello%cworld";
111 char ref[20], buf[20];
112 memset(ref, 'X', sizeof(buf));
113 memcpy(buf, ref, sizeof(buf));
114
115 // A negative buffer size should always result in an error.
116 EXPECT_EQ(-1, FormatN(buf, -1, fmt, ' '));
117 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
118
119 // Zero buffer size should always result in an error.
120 EXPECT_EQ(-1, FormatN(buf, 0, fmt, ' '));
121 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
122
123 // A one-byte buffer should always print a single NUL byte.
124 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 1, fmt, ' '));
125 EXPECT_EQ(0, buf[0]);
126 EXPECT_TRUE(!memcmp(buf+1, ref+1, sizeof(buf)-1));
127 memcpy(buf, ref, sizeof(buf));
128
129 // A larger (but limited) buffer should always leave the trailing bytes
130 // unchanged.
131 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, FormatN(buf, 2, fmt, ' '));
132 EXPECT_EQ(text[0], buf[0]);
133 EXPECT_EQ(0, buf[1]);
134 EXPECT_TRUE(!memcmp(buf+2, ref+2, sizeof(buf)-2));
135 memcpy(buf, ref, sizeof(buf));
136
137 // A unrestricted buffer length should always leave the trailing bytes
138 // unchanged.
139 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1,
140 FormatN(buf, sizeof(buf), fmt, ' '));
141 EXPECT_EQ(std::string(text), std::string(buf));
142 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
143 sizeof(buf) - sizeof(text)));
144 memcpy(buf, ref, sizeof(buf));
145
146 // The same test using Format() instead of FormatN().
147 EXPECT_EQ(static_cast<ssize_t>(sizeof(text))-1, Format(buf, fmt, ' '));
148 EXPECT_EQ(std::string(text), std::string(buf));
149 EXPECT_TRUE(!memcmp(buf + sizeof(text), ref + sizeof(text),
150 sizeof(buf) - sizeof(text)));
151 memcpy(buf, ref, sizeof(buf));
152
153 // Check for deduplication of '%' percent characters.
154 EXPECT_EQ(1, Format(buf, "%", 0));
155 EXPECT_EQ(1, Format(buf, "%%", 0));
156 EXPECT_EQ(2, Format(buf, "%%%", 0));
157 EXPECT_EQ(2, Format(buf, "%%%%", 0));
158 EXPECT_EQ(2, Format(buf, "%Y", 0));
159 EXPECT_EQ(2, Format(buf, "%%Y", 0));
160 EXPECT_EQ(3, Format(buf, "%%%Y", 0));
161 EXPECT_EQ(3, Format(buf, "%%%%Y", 0));
162 }
163
164 TEST(FormatTest, MissingArg) {
165 char buf[20];
166 EXPECT_EQ(3, Format(buf, "%c%c", 'A'));
167 EXPECT_EQ("A%c", std::string(buf));
168 }
169
170 TEST(FormatTest, NArgs) {
171 // Pre-C++11 compilers have a different code path, that can only print
172 // up to ten distinct arguments.
173 // We test both Format() and FormatN(). This makes sure we don't have
174 // typos in the copy-n-pasted code that is needed to deal with various
175 // numbers of arguments.
176 char buf[12];
177 EXPECT_EQ(1, Format(buf, "%c", 1));
178 EXPECT_EQ("\1", std::string(buf));
179 EXPECT_EQ(2, Format(buf, "%c%c", 1, 2));
180 EXPECT_EQ("\1\2", std::string(buf));
181 EXPECT_EQ(3, Format(buf, "%c%c%c", 1, 2, 3));
182 EXPECT_EQ("\1\2\3", std::string(buf));
183 EXPECT_EQ(4, Format(buf, "%c%c%c%c", 1, 2, 3, 4));
184 EXPECT_EQ("\1\2\3\4", std::string(buf));
185 EXPECT_EQ(5, Format(buf, "%c%c%c%c%c", 1, 2, 3, 4, 5));
186 EXPECT_EQ("\1\2\3\4\5", std::string(buf));
187 EXPECT_EQ(6, Format(buf, "%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6));
188 EXPECT_EQ("\1\2\3\4\5\6", std::string(buf));
189 EXPECT_EQ(7, Format(buf, "%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7));
190 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf));
191 EXPECT_EQ(8, Format(buf, "%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8));
192 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf));
193 EXPECT_EQ(9, Format(buf, "%c%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8, 9));
194 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf));
195 EXPECT_EQ(10, Format(buf, "%c%c%c%c%c%c%c%c%c%c",
196 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
197
Jeffrey Yasskin 2013/07/30 23:13:53 Weirdly-located blank line.
198 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf));
199 EXPECT_EQ(1, FormatN(buf, 11, "%c", 1));
200 EXPECT_EQ("\1", std::string(buf));
201 EXPECT_EQ(2, FormatN(buf, 11, "%c%c", 1, 2));
202 EXPECT_EQ("\1\2", std::string(buf));
203 EXPECT_EQ(3, FormatN(buf, 11, "%c%c%c", 1, 2, 3));
204 EXPECT_EQ("\1\2\3", std::string(buf));
205 EXPECT_EQ(4, FormatN(buf, 11, "%c%c%c%c", 1, 2, 3, 4));
206 EXPECT_EQ("\1\2\3\4", std::string(buf));
207 EXPECT_EQ(5, FormatN(buf, 11, "%c%c%c%c%c", 1, 2, 3, 4, 5));
208 EXPECT_EQ("\1\2\3\4\5", std::string(buf));
209 EXPECT_EQ(6, FormatN(buf, 11, "%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6));
210 EXPECT_EQ("\1\2\3\4\5\6", std::string(buf));
211 EXPECT_EQ(7, FormatN(buf, 11, "%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7));
212 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf));
213 EXPECT_EQ(8, FormatN(buf, 11, "%c%c%c%c%c%c%c%c", 1, 2, 3, 4, 5, 6, 7, 8));
214 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf));
215 EXPECT_EQ(9, FormatN(buf, 11, "%c%c%c%c%c%c%c%c%c",
216 1, 2, 3, 4, 5, 6, 7, 8, 9));
217 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf));
218 EXPECT_EQ(10, FormatN(buf, 11, "%c%c%c%c%c%c%c%c%c%c",
219 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
220 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf));
221
222
223 // C++11 is smart enough to handle variadic template arguments. It can
224 // deal with arbitrary numbers of arguments.
225 #if __cplusplus >= 201103 // C++11
226 EXPECT_EQ(11, Format(buf, "%c%c%c%c%c%c%c%c%c%c%c",
227 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
228 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
229 EXPECT_EQ(11, FormatN(buf, 12, "%c%c%c%c%c%c%c%c%c%c%c",
230 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
231 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
232 #endif
233 }
234
235 TEST(FormatTest, DataTypes) {
236 char buf[40];
237
238 // Bytes
239 EXPECT_EQ(1, Format(buf, "%d", (uint8_t)1));
240 EXPECT_EQ("1", std::string(buf));
241 EXPECT_EQ(3, Format(buf, "%d", (uint8_t)-1));
242 EXPECT_EQ("255", std::string(buf));
243 EXPECT_EQ(1, Format(buf, "%d", (int8_t)1));
244 EXPECT_EQ("1", std::string(buf));
245 EXPECT_EQ(2, Format(buf, "%d", (int8_t)-1));
246 EXPECT_EQ("-1", std::string(buf));
247 EXPECT_EQ(4, Format(buf, "%d", (int8_t)-128));
248 EXPECT_EQ("-128", std::string(buf));
249
250 // Half-words
251 EXPECT_EQ(1, Format(buf, "%d", (uint16_t)1));
252 EXPECT_EQ("1", std::string(buf));
253 EXPECT_EQ(5, Format(buf, "%d", (uint16_t)-1));
254 EXPECT_EQ("65535", std::string(buf));
255 EXPECT_EQ(1, Format(buf, "%d", (int16_t)1));
256 EXPECT_EQ("1", std::string(buf));
257 EXPECT_EQ(2, Format(buf, "%d", (int16_t)-1));
258 EXPECT_EQ("-1", std::string(buf));
259 EXPECT_EQ(6, Format(buf, "%d", (int16_t)-32768));
260 EXPECT_EQ("-32768", std::string(buf));
261
262 // Words
263 EXPECT_EQ(1, Format(buf, "%d", (uint32_t)1));
264 EXPECT_EQ("1", std::string(buf));
265 EXPECT_EQ(10, Format(buf, "%d", (uint32_t)-1));
266 EXPECT_EQ("4294967295", std::string(buf));
267 EXPECT_EQ(1, Format(buf, "%d", (int32_t)1));
268 EXPECT_EQ("1", std::string(buf));
269 EXPECT_EQ(2, Format(buf, "%d", (int32_t)-1));
270 EXPECT_EQ("-1", std::string(buf));
271 // Work-around for an limitation of C90
272 EXPECT_EQ(11, Format(buf, "%d", (int32_t)-2147483647-1));
273 EXPECT_EQ("-2147483648", std::string(buf));
274
275 // Quads
276 EXPECT_EQ(1, Format(buf, "%d", (uint64_t)1));
277 EXPECT_EQ("1", std::string(buf));
278 EXPECT_EQ(20, Format(buf, "%d", (uint64_t)-1));
279 EXPECT_EQ("18446744073709551615", std::string(buf));
280 EXPECT_EQ(1, Format(buf, "%d", (int64_t)1));
281 EXPECT_EQ("1", std::string(buf));
282 EXPECT_EQ(2, Format(buf, "%d", (int64_t)-1));
283 EXPECT_EQ("-1", std::string(buf));
284 // Work-around for an limitation of C90
285 EXPECT_EQ(20, Format(buf, "%d", (int64_t)-9223372036854775807LL-1));
286 EXPECT_EQ("-9223372036854775808", std::string(buf));
287
288 // Strings (both const and mutable).
289 EXPECT_EQ(4, Format(buf, "test"));
290 EXPECT_EQ("test", std::string(buf));
291 EXPECT_EQ(4, Format(buf, buf));
292 EXPECT_EQ("test", std::string(buf));
293
294 // Pointer
295 char addr[20];
296 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)buf);
297 Format(buf, "%p", buf);
298 EXPECT_EQ(std::string(addr), std::string(buf));
299 Format(buf, "%p", (const char *)buf);
300 EXPECT_EQ(std::string(addr), std::string(buf));
301 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)sprintf);
302 Format(buf, "%p", sprintf);
303 EXPECT_EQ(std::string(addr), std::string(buf));
304
305 // Padding for pointers is a little more complicated because of the "0x"
306 // prefix. Padding with '0' zeros is relatively straight-forward, but
307 // padding with ' ' spaces requires more effort.
308 sprintf(addr, "0x%017llX", (unsigned long long)(uintptr_t)buf);
309 Format(buf, "%019p", buf);
310 EXPECT_EQ(std::string(addr), std::string(buf));
311 sprintf(addr, "0x%llX", (unsigned long long)(uintptr_t)buf);
312 memset(addr, ' ',
313 (char *)memmove(addr + sizeof(addr) - strlen(addr) - 1,
314 addr, strlen(addr)+1) - addr);
315 Format(buf, "%19p", buf);
316 EXPECT_EQ(std::string(addr), std::string(buf));
317 }
318
319 namespace {
320 void PrintLongString(char* buf, size_t sz) {
321 // Output a reasonably complex expression into a limited-size buffer.
322 // At least one byte is available for writing the NUL character.
323 CHECK_GT(sz, static_cast<size_t>(0));
324
325 // Allocate slightly more space, so that we can verify that Format()
326 // never writes past the end of the buffer.
327 char *tmp = new char[sz+2];
328 memset(tmp, 'X', sz+2);
329
330 // Use Format() to output a complex list of arguments:
331 // - test padding and truncating %c single characters.
332 // - test truncating %s simple strings.
333 // - test mismatching arguments and truncating (for %d != %s).
334 // - test zero-padding and truncating %x hexadecimal numbers.
335 // - test outputting and truncating %d MININT.
336 // - test outputting and truncating %p arbitrary pointer values.
337 // - test outputting, padding and truncating NULL-pointer %s strings.
338 size_t needed = FormatN(tmp, sz,
339 "A%2cong %s: %d %010X %d %p%7s",
340 'l', "string", "", 0xDEADBEEF,
341 std::numeric_limits<intptr_t>::min(),
342 PrintLongString, static_cast<char*>(NULL)) + 1;
343
344 // Various sanity checks:
345 // The numbered of characters needed to print the full string should always
346 // be bigger or equal to the bytes that have actually been output.
347 size_t len = strlen(tmp);
348 CHECK_GE(needed, len+1);
349
350 // The number of characters output should always fit into the buffer that
351 // was passed into Format().
352 CHECK_LT(len, sz);
353
354 // The output is always terminated with a NUL byte (actually, this test is
355 // always going to pass, as strlen() already verified this)
356 EXPECT_FALSE(tmp[len]);
357
358 // All trailing bytes are unchanged.
359 for (size_t i = len+1; i < sz+2; ++i)
360 EXPECT_EQ('X', tmp[i]);
361
362 // The text that was generated by Format() should always match the
363 // equivalent text generated by sprintf(). Please note that the format
364 // string for sprintf() is nor complicated, as it does not have the
365 // benefit of getting type information from the C++ compiler.
366 //
367 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately,
368 // Visual Studio doesn't support this function, and the work-arounds
369 // are all really awkward.
370 char ref[256];
371 CHECK_LE(sz, sizeof(ref));
372 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>",
373 static_cast<long long>(std::numeric_limits<intptr_t>::min()),
374 (long long)PrintLongString);
375 ref[sz-1] = '\000';
376
377 // Compare the output from Format() to the one from sprintf().
378 EXPECT_EQ(std::string(ref), std::string(tmp));
379
380 // We allocated a slightly larger buffer, so that we could perform some
381 // extra sanity checks. Now that the tests have all passed, we copy the
382 // data to the output buffer that the caller provided.
383 memcpy(buf, tmp, len+1);
384 delete[] tmp;
385 }
386 } // anonymous namespace
387
388 TEST(FormatTest, Truncation) {
389 // We use PrintLongString() to print a complex long string and then
390 // truncate to all possible lengths. This ends up exercising a lot of
391 // different code paths in Format() and itoa_r(), as truncation can
392 // happen in a lot of different states.
393 char ref[256];
394 PrintLongString(ref, sizeof(ref));
395 for (size_t i = strlen(ref)+1; i; --i) {
396 char buf[sizeof(ref)];
397 PrintLongString(buf, i);
398 EXPECT_EQ(std::string(ref, i - 1), std::string(buf));
399 }
400 }
401
402 TEST(FormatTest, Padding) {
403 char buf[40], fmt[40];
404
405 // Chars %c
406 EXPECT_EQ(1, Format(buf, "%c", 'A'));
407 EXPECT_EQ("A", std::string(buf));
408 EXPECT_EQ(2, Format(buf, "%2c", 'A'));
409 EXPECT_EQ(" A", std::string(buf));
410 EXPECT_EQ(2, Format(buf, "%02c", 'A'));
411 EXPECT_EQ(" A", std::string(buf));
412 EXPECT_EQ(4, Format(buf, "%-2c", 'A'));
413 EXPECT_EQ("%-2c", std::string(buf));
414 Format(fmt, "%%%dc", std::numeric_limits<ssize_t>::max());
415 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, Format(buf, fmt, 'A'));
416 Format(fmt, "%%%dc",
417 static_cast<size_t>(std::numeric_limits<ssize_t>::max())+1);
418 EXPECT_EQ(2, Format(buf, fmt, 'A'));
419 EXPECT_EQ("%c", std::string(buf));
420
421 // Decimals %d
Jeffrey Yasskin 2013/07/30 23:13:53 I think you're missing tests for truncated numbers
422 EXPECT_EQ(1, Format(buf, "%d", 1));
423 EXPECT_EQ("1", std::string(buf));
424 EXPECT_EQ(2, Format(buf, "%2d", 1));
425 EXPECT_EQ(" 1", std::string(buf));
426 EXPECT_EQ(2, Format(buf, "%02d", 1));
427 EXPECT_EQ("01", std::string(buf));
428 EXPECT_EQ(3, Format(buf, "%3d", -1));
429 EXPECT_EQ(" -1", std::string(buf));
430 EXPECT_EQ(3, Format(buf, "%03d", -1));
431 EXPECT_EQ("-01", std::string(buf));
432 EXPECT_EQ(3, Format(buf, "%2d", 111));
433 EXPECT_EQ("111", std::string(buf));
434 EXPECT_EQ(4, Format(buf, "%2d", -111));
435 EXPECT_EQ("-111", std::string(buf));
436 EXPECT_EQ(4, Format(buf, "%-2d", 1));
437 EXPECT_EQ("%-2d", std::string(buf));
438 Format(fmt, "%%%dd", std::numeric_limits<ssize_t>::max());
439 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
440 EXPECT_EQ(" ", std::string(buf));
441 Format(fmt, "%%0%dd", std::numeric_limits<ssize_t>::max());
442 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
443 EXPECT_EQ("000", std::string(buf));
444 Format(fmt, "%%%dd",
445 static_cast<size_t>(std::numeric_limits<ssize_t>::max())+1);
446 EXPECT_EQ(2, Format(buf, fmt, 1));
447 EXPECT_EQ("%d", std::string(buf));
448
449 // Hex %X
450 EXPECT_EQ(1, Format(buf, "%X", 1));
451 EXPECT_EQ("1", std::string(buf));
452 EXPECT_EQ(2, Format(buf, "%2X", 1));
453 EXPECT_EQ(" 1", std::string(buf));
454 EXPECT_EQ(2, Format(buf, "%02X", 1));
455 EXPECT_EQ("01", std::string(buf));
456 EXPECT_EQ(8, Format(buf, "%3X", -1));
457 EXPECT_EQ("FFFFFFFF", std::string(buf));
458 EXPECT_EQ(8, Format(buf, "%03X", -1));
459 EXPECT_EQ("FFFFFFFF", std::string(buf));
460 EXPECT_EQ(16, Format(buf, "%3X", -1LL));
461 EXPECT_EQ("FFFFFFFFFFFFFFFF", std::string(buf));
462 EXPECT_EQ(16, Format(buf, "%03X", -1LL));
463 EXPECT_EQ("FFFFFFFFFFFFFFFF", std::string(buf));
464 EXPECT_EQ(3, Format(buf, "%2X", 0x111));
465 EXPECT_EQ("111", std::string(buf));
466 EXPECT_EQ(4, Format(buf, "%-2X", 1));
467 EXPECT_EQ("%-2X", std::string(buf));
468 Format(fmt, "%%%dX", std::numeric_limits<ssize_t>::max());
469 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
470 EXPECT_EQ(" ", std::string(buf));
471 Format(fmt, "%%0%dX", std::numeric_limits<ssize_t>::max());
472 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, 1));
473 EXPECT_EQ("000", std::string(buf));
474 Format(fmt, "%%%dX",
475 static_cast<size_t>(std::numeric_limits<ssize_t>::max())+1);
476 EXPECT_EQ(2, Format(buf, fmt, 1));
477 EXPECT_EQ("%X", std::string(buf));
478
479 // Pointer %p
480 EXPECT_EQ(3, Format(buf, "%p", (void*)1));
481 EXPECT_EQ("0x1", std::string(buf));
482 EXPECT_EQ(4, Format(buf, "%4p", (void*)1));
483 EXPECT_EQ(" 0x1", std::string(buf));
484 EXPECT_EQ(4, Format(buf, "%04p", (void*)1));
485 EXPECT_EQ("0x01", std::string(buf));
486 EXPECT_EQ(5, Format(buf, "%4p", (void*)0x111));
487 EXPECT_EQ("0x111", std::string(buf));
488 EXPECT_EQ(4, Format(buf, "%-2p", (void*)1));
489 EXPECT_EQ("%-2p", std::string(buf));
490 Format(fmt, "%%%dp", std::numeric_limits<ssize_t>::max());
491 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1,
492 FormatN(buf, 4, fmt, (void*)1));
493 EXPECT_EQ(" ", std::string(buf));
494 Format(fmt, "%%0%dp", std::numeric_limits<ssize_t>::max());
495 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1,
496 FormatN(buf, 4, fmt, (void*)1));
497 EXPECT_EQ("0x0", std::string(buf));
498 Format(fmt, "%%%dp",
499 static_cast<size_t>(std::numeric_limits<ssize_t>::max())+1);
500 EXPECT_EQ(2, Format(buf, fmt, 1));
501 EXPECT_EQ("%p", std::string(buf));
502
503 // String
504 EXPECT_EQ(1, Format(buf, "%s", "A"));
505 EXPECT_EQ("A", std::string(buf));
506 EXPECT_EQ(2, Format(buf, "%2s", "A"));
507 EXPECT_EQ(" A", std::string(buf));
508 EXPECT_EQ(2, Format(buf, "%02s", "A"));
509 EXPECT_EQ(" A", std::string(buf));
510 EXPECT_EQ(3, Format(buf, "%2s", "AAA"));
511 EXPECT_EQ("AAA", std::string(buf));
512 EXPECT_EQ(4, Format(buf, "%-2s", "A"));
513 EXPECT_EQ("%-2s", std::string(buf));
514 Format(fmt, "%%%ds", std::numeric_limits<ssize_t>::max());
515 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, "A"));
516 EXPECT_EQ(" ", std::string(buf));
517 Format(fmt, "%%0%ds", std::numeric_limits<ssize_t>::max());
518 EXPECT_EQ(std::numeric_limits<ssize_t>::max()-1, FormatN(buf, 4, fmt, "A"));
519 EXPECT_EQ(" ", std::string(buf));
520 Format(fmt, "%%%ds",
521 static_cast<size_t>(std::numeric_limits<ssize_t>::max())+1);
522 EXPECT_EQ(2, Format(buf, fmt, "A"));
523 EXPECT_EQ("%s", std::string(buf));
524 }
525
526 TEST(FormatTest, EmbeddedNul) {
527 char buf[] = { 'X', 'X', 'X', 'X' };
528 EXPECT_EQ(2, Format(buf, "%3c", 0));
529 EXPECT_EQ(' ', buf[0]);
530 EXPECT_EQ(' ', buf[1]);
531 EXPECT_EQ(0, buf[2]);
532 EXPECT_EQ('X', buf[3]);
533
534 // Check handling of a NUL format character. N.B. this takes two different
535 // code paths depending on whether we are actually passing arguments. If
536 // we don't have any arguments, we are running in the fast-path code, that
537 // looks (almost) like a strncpy().
538 EXPECT_EQ(2, Format(buf, "%%%"));
539 EXPECT_EQ("%%", std::string(buf));
540 EXPECT_EQ(2, Format(buf, "%%%", 0));
541 EXPECT_EQ("%%", std::string(buf));
542 }
543
544 TEST(FormatTest, PointerSize) {
545 // The internal data representation is a 64bit value, independent of the
546 // native word size. We want to perform sign-extension for signed integers,
547 // but we want to avoid doing so for pointer types. This could be a
548 // problem on systems, where pointers are only 32bit. This tests verifies
549 // that there is no such problem.
550 char *str = reinterpret_cast<char *>(0x80000000u);
551 void *ptr = str;
552 char buf[40];
553 EXPECT_EQ(10, Format(buf, "%p", str));
554 EXPECT_EQ("0x80000000", std::string(buf));
555 EXPECT_EQ(10, Format(buf, "%p", ptr));
556 EXPECT_EQ("0x80000000", std::string(buf));
557 }
558
559 } // namespace debug
560 } // namespace base
OLDNEW
« base/debug/format.cc ('K') | « base/debug/format.cc ('k') | base/debug/stack_trace.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698