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

Side by Side Diff: mojo/edk/util/string_printf_unittest.cc

Issue 1486923002: EDK: Add mojo::util::StringPrintf() (etc.) and convert away from base's. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "mojo/edk/util/string_printf.h"
6
7 #include <errno.h>
8 #include <stdarg.h>
9
10 #include <string>
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace mojo {
15 namespace util {
16 namespace {
17
18 // Note: |runnable| can't be a reference since that'd make the behavior of
19 // |va_start()| undefined.
20 template <typename Runnable>
21 std::string VAListHelper(Runnable runnable, ...) {
22 va_list ap;
23 va_start(ap, runnable);
24 std::string rv = runnable(ap);
25 va_end(ap);
26 return rv;
27 }
28
29 TEST(StringPrintfTest, StringPrintf_Basic) {
30 EXPECT_EQ("", StringPrintf(""));
31 EXPECT_EQ("hello", StringPrintf("hello"));
32 EXPECT_EQ("hello-123", StringPrintf("hello%d", -123));
33 EXPECT_EQ("hello0123FACE", StringPrintf("%s%04d%X", "hello", 123, 0xfaceU));
34 }
35
36 TEST(StringPrintfTest, StringVPrintf_Basic) {
37 EXPECT_EQ("", VAListHelper([](va_list ap) -> std::string {
38 return StringVPrintf("", ap);
39 }));
40 EXPECT_EQ("hello", VAListHelper([](va_list ap) -> std::string {
41 return StringVPrintf("hello", ap);
42 }));
43 EXPECT_EQ("hello-123", VAListHelper([](va_list ap) -> std::string {
44 return StringVPrintf("hello%d", ap);
45 }, -123));
46 EXPECT_EQ("hello0123FACE", VAListHelper([](va_list ap) -> std::string {
47 return StringVPrintf("%s%04d%X", ap);
48 }, "hello", 123, 0xfaceU));
49 }
50
51 TEST(StringPrintfTest, StringAppendf_Basic) {
52 {
53 std::string s = "existing";
54 StringAppendf(&s, "");
55 EXPECT_EQ("existing", s);
56 }
57 {
58 std::string s = "existing";
59 StringAppendf(&s, "hello");
60 EXPECT_EQ("existinghello", s);
61 }
62 {
63 std::string s = "existing";
64 StringAppendf(&s, "hello%d", -123);
65 EXPECT_EQ("existinghello-123", s);
66 }
67 {
68 std::string s = "existing";
69 StringAppendf(&s, "%s%04d%X", "hello", 123, 0xfaceU);
70 EXPECT_EQ("existinghello0123FACE", s);
71 }
72 }
73
74 TEST(StringPrintfTest, StringVAppendf_Basic) {
75 EXPECT_EQ("existing", VAListHelper([](va_list ap) -> std::string {
76 std::string s = "existing";
77 StringVAppendf(&s, "", ap);
78 return s;
79 }));
80 EXPECT_EQ("existinghello", VAListHelper([](va_list ap) -> std::string {
81 std::string s = "existing";
82 StringVAppendf(&s, "hello", ap);
83 return s;
84 }));
85 EXPECT_EQ("existinghello-123", VAListHelper([](va_list ap) -> std::string {
86 std::string s = "existing";
87 StringVAppendf(&s, "hello%d", ap);
88 return s;
89 }, -123));
90 EXPECT_EQ("existinghello0123FACE",
91 VAListHelper([](va_list ap) -> std::string {
92 std::string s = "existing";
93 StringVAppendf(&s, "%s%04d%X", ap);
94 return s;
95 }, "hello", 123, 0xfaceU));
96 }
97
98 // Generally, we assume that everything forwards to |StringVAppendf()|, so
99 // testing |StringPrintf()| more carefully suffices.
100
101 TEST(StringPrintfTest, StringPrintf_Boundary) {
102 // Note: The size of strings generated should cover the boundary cases in the
103 // constant |kStackBufferSize| in |StringVAppendf()|.
104 for (size_t i = 800; i < 1200; i++) {
105 std::string stuff(i, 'x');
106 std::string format = stuff + "%d" + "%s" + " world";
107 EXPECT_EQ(stuff + "123" + "hello world",
108 StringPrintf(format.c_str(), 123, "hello"))
109 << i;
110 }
111 }
112
113 TEST(StringPrintfTest, StringPrintf_VeryBig) {
114 // 4 megabytes of exes (we'll generate 5 times this).
115 std::string stuff(4u << 20u, 'x');
116 std::string format = "%s" + stuff + "%s" + stuff + "%s";
117 EXPECT_EQ(stuff + stuff + stuff + stuff + stuff,
118 StringPrintf(format.c_str(), stuff.c_str(), stuff.c_str(),
119 stuff.c_str()));
120 }
121
122 } // namespace
123 } // namespace util
124 } // namespace mojo
OLDNEW
« mojo/edk/util/string_printf.cc ('K') | « mojo/edk/util/string_printf.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698