1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include "berberis/base/format_buffer.h"
20
21 #include <string>
22
23 namespace berberis {
24
25 namespace {
26
TEST(FormatBufferTest,NullBuffer)27 TEST(FormatBufferTest, NullBuffer) {
28 EXPECT_EQ(0U, FormatBuffer(nullptr, 0, "test"));
29 EXPECT_EQ(0U, FormatBuffer(nullptr, 128, "test"));
30 }
31
TEST(FormatBufferTest,ZeroBufferSize)32 TEST(FormatBufferTest, ZeroBufferSize) {
33 char buf[128] = "hello";
34 EXPECT_EQ(0U, FormatBuffer(buf, 0, "test"));
35 // Should write nothing.
36 EXPECT_STREQ("hello", buf);
37 }
38
TEST(FormatBufferTest,NullFormat)39 TEST(FormatBufferTest, NullFormat) {
40 char buf[128] = "hello";
41 EXPECT_EQ(0U, FormatBuffer(buf, sizeof(buf), nullptr));
42 // Should write '\0'.
43 // TODO(eaeltsin): maybe "(null)"?
44 EXPECT_STREQ("", buf);
45 }
46
TEST(FormatBufferTest,EmptyFormat)47 TEST(FormatBufferTest, EmptyFormat) {
48 char buf[128] = "hello";
49 EXPECT_EQ(0U, FormatBuffer(buf, sizeof(buf), ""));
50 // Should write '\0'.
51 EXPECT_STREQ("", buf);
52 }
53
TEST(FormatBufferTest,FixedFormat)54 TEST(FormatBufferTest, FixedFormat) {
55 char buf[128];
56 EXPECT_EQ(4U, FormatBuffer(buf, sizeof(buf), "test"));
57 EXPECT_STREQ("test", buf);
58 }
59
TEST(FormatBufferTest,FixedFormatSmallBuffer)60 TEST(FormatBufferTest, FixedFormatSmallBuffer) {
61 char buf[4];
62 EXPECT_EQ(3U, FormatBuffer(buf, sizeof(buf), "test"));
63 EXPECT_STREQ("tes", buf);
64 }
65
TEST(FormatBufferTest,SpecMissing)66 TEST(FormatBufferTest, SpecMissing) {
67 char buf[128];
68 // Should print nothing for missing specifier.
69 EXPECT_EQ(5U, FormatBuffer(buf, sizeof(buf), "test %"));
70 EXPECT_STREQ("test ", buf);
71 }
72
TEST(FormatBufferTest,SpecUnknown)73 TEST(FormatBufferTest, SpecUnknown) {
74 char buf[128];
75 // ATTENTION: assume '?' is not a valid specifier!
76 // Should print nothing for unknown specifier.
77 EXPECT_EQ(5U, FormatBuffer(buf, sizeof(buf), "test %?"));
78 EXPECT_STREQ("test ", buf);
79 }
80
TEST(FormatBufferTest,SpecPercent)81 TEST(FormatBufferTest, SpecPercent) {
82 char buf[128];
83 EXPECT_EQ(6U, FormatBuffer(buf, sizeof(buf), "%% test"));
84 EXPECT_STREQ("% test", buf);
85 }
86
TEST(FormatBufferTest,SpecString)87 TEST(FormatBufferTest, SpecString) {
88 char buf[128];
89 EXPECT_EQ(11U, FormatBuffer(buf, sizeof(buf), "%s test", "string"));
90 EXPECT_STREQ("string test", buf);
91 }
92
TEST(FormatBufferTest,SpecStringNull)93 TEST(FormatBufferTest, SpecStringNull) {
94 char buf[128];
95 EXPECT_EQ(11U, FormatBuffer(buf, sizeof(buf), "%s test", nullptr));
96 EXPECT_STREQ("(null) test", buf);
97 }
98
TEST(FormatBufferTest,SpecStringSmallBuffer)99 TEST(FormatBufferTest, SpecStringSmallBuffer) {
100 char buf[10];
101 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "%s test", "string"));
102 EXPECT_STREQ("string te", buf);
103 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "test %s", "string"));
104 EXPECT_STREQ("test stri", buf);
105 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "test %s", nullptr));
106 EXPECT_STREQ("test (nul", buf);
107 }
108
TEST(FormatBufferTest,SpecDec)109 TEST(FormatBufferTest, SpecDec) {
110 char buf[128];
111 EXPECT_EQ(6U, FormatBuffer(buf, sizeof(buf), "%d test", 0));
112 EXPECT_STREQ("0 test", buf);
113 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%d test", 123));
114 EXPECT_STREQ("123 test", buf);
115 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "%d test", -123));
116 EXPECT_STREQ("-123 test", buf);
117 }
118
TEST(FormatBufferTest,SpecDecSmallBuffer)119 TEST(FormatBufferTest, SpecDecSmallBuffer) {
120 char buf[4];
121 EXPECT_EQ(3U, FormatBuffer(buf, sizeof(buf), "%d test", 123456));
122 EXPECT_STREQ("123", buf);
123 EXPECT_EQ(3U, FormatBuffer(buf, sizeof(buf), "%d test", -123456));
124 EXPECT_STREQ("-12", buf);
125 }
126
TEST(FormatBufferTest,SpecHex)127 TEST(FormatBufferTest, SpecHex) {
128 char buf[128];
129 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%x test", 0xabc));
130 EXPECT_STREQ("abc test", buf);
131 // Max hex digits count for unsigned (2 hex digits per byte).
132 const size_t n = sizeof(unsigned) * 2;
133 EXPECT_EQ(n, FormatBuffer(buf, sizeof(buf), "%x", static_cast<unsigned>(-1)));
134 for (size_t i = 0; i < n; ++i) {
135 // Abort on first mismatch.
136 ASSERT_EQ('f', buf[i]);
137 }
138 }
139
TEST(FormatBufferTest,SpecPtr)140 TEST(FormatBufferTest, SpecPtr) {
141 char buf[128];
142 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%p test", nullptr));
143 EXPECT_STREQ("0x0 test", buf);
144 }
145
TEST(FormatBufferTest,SpecMany)146 TEST(FormatBufferTest, SpecMany) {
147 char buf[128];
148 EXPECT_EQ(12U, FormatBuffer(buf, sizeof(buf), "%p %d %s test", nullptr, 1, "2"));
149 EXPECT_STREQ("0x0 1 2 test", buf);
150 }
151
TEST(FormatBufferTest,SpecLongLongUnknown)152 TEST(FormatBufferTest, SpecLongLongUnknown) {
153 char buf[128];
154 // ATTENTION: assume '?' is not a valid specifier!
155 // Should print nothing for unknown specifier after length modifier.
156 EXPECT_EQ(5U, FormatBuffer(buf, sizeof(buf), "test %ll?"));
157 EXPECT_STREQ("test ", buf);
158 }
159
TEST(FormatBufferTest,SpecLongLongHex)160 TEST(FormatBufferTest, SpecLongLongHex) {
161 char buf[128];
162 // Max hex digits count for unsigned long long (2 hex digits per byte).
163 const size_t n = sizeof(unsigned long long) * 2;
164 EXPECT_EQ(n, FormatBuffer(buf, sizeof(buf), "%llx", static_cast<unsigned long long>(-1)));
165 for (size_t i = 0; i < n; ++i) {
166 // Abort on first mismatch.
167 ASSERT_EQ('f', buf[i]);
168 }
169 }
170
TEST(FormatBufferTest,SpecWidthString)171 TEST(FormatBufferTest, SpecWidthString) {
172 char buf[128];
173 EXPECT_EQ(11U, FormatBuffer(buf, sizeof(buf), "%4s test", "string"));
174 EXPECT_STREQ("string test", buf);
175 EXPECT_EQ(13U, FormatBuffer(buf, sizeof(buf), "%8s test", "string"));
176 EXPECT_STREQ(" string test", buf);
177 EXPECT_EQ(21U, FormatBuffer(buf, sizeof(buf), "%16s test", "string"));
178 EXPECT_STREQ(" string test", buf);
179 }
180
TEST(FormatBufferTest,SpecWidthDec)181 TEST(FormatBufferTest, SpecWidthDec) {
182 char buf[128];
183 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%3d test", 0));
184 EXPECT_STREQ(" 0 test", buf);
185 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%2d test", 123));
186 EXPECT_STREQ("123 test", buf);
187 EXPECT_EQ(10U, FormatBuffer(buf, sizeof(buf), "%5d test", 123));
188 EXPECT_STREQ(" 123 test", buf);
189 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "%2d test", -123));
190 EXPECT_STREQ("-123 test", buf);
191 EXPECT_EQ(10U, FormatBuffer(buf, sizeof(buf), "%5d test", -123));
192 EXPECT_STREQ(" -123 test", buf);
193 }
194
TEST(FormatBufferTest,SpecWidthPtr)195 TEST(FormatBufferTest, SpecWidthPtr) {
196 char buf[128];
197 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%2p test", nullptr));
198 EXPECT_STREQ("0x0 test", buf);
199 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "%4p test", nullptr));
200 EXPECT_STREQ(" 0x0 test", buf);
201 }
202
TEST(FormatBufferTest,SpecChar)203 TEST(FormatBufferTest, SpecChar) {
204 char buf[128];
205 EXPECT_EQ(6U, FormatBuffer(buf, sizeof(buf), "%c test", 'a'));
206 EXPECT_STREQ("a test", buf);
207 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%3c test", 'a'));
208 EXPECT_STREQ(" a test", buf);
209 EXPECT_EQ(10U, FormatBuffer(buf, sizeof(buf), "%c%d test", 'a', -123));
210 EXPECT_STREQ("a-123 test", buf);
211 }
212
TEST(FormatBufferTest,SpecVariableWidth)213 TEST(FormatBufferTest, SpecVariableWidth) {
214 char buf[128];
215 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%*d test", 3, 0));
216 EXPECT_STREQ(" 0 test", buf);
217 EXPECT_EQ(8U, FormatBuffer(buf, sizeof(buf), "%*d test", 3, 123));
218 EXPECT_STREQ("123 test", buf);
219 EXPECT_EQ(10U, FormatBuffer(buf, sizeof(buf), "%*d test", 5, 123));
220 EXPECT_STREQ(" 123 test", buf);
221 EXPECT_EQ(9U, FormatBuffer(buf, sizeof(buf), "%*d test", 2, -123));
222 EXPECT_STREQ("-123 test", buf);
223 EXPECT_EQ(10U, FormatBuffer(buf, sizeof(buf), "%*d test", 5, -123));
224 EXPECT_STREQ(" -123 test", buf);
225 EXPECT_EQ(11U, FormatBuffer(buf, sizeof(buf), "%*s test", 4, "string"));
226 EXPECT_STREQ("string test", buf);
227 EXPECT_EQ(13U, FormatBuffer(buf, sizeof(buf), "%*s test", 8, "string"));
228 EXPECT_STREQ(" string test", buf);
229 EXPECT_EQ(21U, FormatBuffer(buf, sizeof(buf), "%*s test", 16, "string"));
230 EXPECT_STREQ(" string test", buf);
231 }
232
TEST(FormatBufferTest,PadNumberWithZeroes)233 TEST(FormatBufferTest, PadNumberWithZeroes) {
234 char buf[128];
235 EXPECT_EQ(1U, FormatBuffer(buf, sizeof(buf), "%0d", 1));
236 EXPECT_STREQ("1", buf);
237 EXPECT_EQ(4U, FormatBuffer(buf, sizeof(buf), "%04d", 1));
238 EXPECT_STREQ("0001", buf);
239 EXPECT_EQ(4U, FormatBuffer(buf, sizeof(buf), "%04d", -1));
240 EXPECT_STREQ("-001", buf);
241 EXPECT_EQ(4U, FormatBuffer(buf, sizeof(buf), "%0*d", 4, -1));
242 EXPECT_STREQ("-001", buf);
243 EXPECT_EQ(4U, FormatBuffer(buf, sizeof(buf), "%04s", "hi"));
244 EXPECT_STREQ(" hi", buf);
245 }
246
TEST(FormatBufferTest,SpecSizeT)247 TEST(FormatBufferTest, SpecSizeT) {
248 char buf[128];
249 EXPECT_EQ(3U, FormatBuffer(buf, sizeof(buf), "%zu", sizeof(buf)));
250 EXPECT_STREQ("128", buf);
251 EXPECT_EQ(2U, FormatBuffer(buf, sizeof(buf), "%zx", sizeof(buf)));
252 EXPECT_STREQ("80", buf);
253 }
254
TEST(FormatBufferTest,DynamicCStrBuffer)255 TEST(FormatBufferTest, DynamicCStrBuffer) {
256 DynamicCStrBuffer buf;
257 EXPECT_TRUE(buf.Put('c'));
258 EXPECT_TRUE(std::string("c") == std::string(buf.Data(), buf.Size()));
259 EXPECT_EQ(1u, buf.Size());
260 EXPECT_FALSE(buf.IsDynamicForTesting());
261
262 for (int i = 0; i < 1023; i++) {
263 EXPECT_TRUE(buf.Put('c'));
264 }
265 EXPECT_TRUE(std::string(1024, 'c') == std::string(buf.Data(), buf.Size()));
266 EXPECT_EQ(1024u, buf.Size());
267 EXPECT_TRUE(buf.IsDynamicForTesting());
268 }
269
270 } // namespace
271
272 } // namespace berberis
273