14 #include "absl/strings/str_format.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 #include "sw/device/lib/base/status.h"
29 using ::testing::StartsWith;
34 void SetUp()
override {
36 .data =
static_cast<void *
>(&buf_),
38 +[](
void *data,
const char *buf,
size_t len) {
39 static_cast<std::string *
>(data)->append(buf, len);
53 TEST_F(PrintfTest, TrivialText) {
55 EXPECT_EQ(buf_,
"Hello, World!\n");
58 TEST_F(PrintfTest, PartialPrints) {
60 EXPECT_EQ(buf_,
"Hello, ");
62 EXPECT_EQ(buf_,
"Hello, World!\n");
65 TEST_F(PrintfTest, LiteralPct) {
67 EXPECT_EQ(buf_,
"Hello, %!\n");
70 TEST_F(PrintfTest, Character) {
72 EXPECT_EQ(buf_,
"Hello, X!\n");
75 TEST_F(PrintfTest, Bool) {
76 EXPECT_EQ(
base_printf(
"Hello, %!b, %!b!\n",
true,
false), 20);
77 EXPECT_EQ(buf_,
"Hello, true, false!\n");
80 TEST_F(PrintfTest, StringWithNul) {
81 EXPECT_EQ(
base_printf(
"Hello, %s!\n",
"abcxyz"), 15);
82 EXPECT_EQ(buf_,
"Hello, abcxyz!\n");
85 TEST_F(PrintfTest, StringWithLen) {
86 EXPECT_EQ(
base_printf(
"Hello, %!s!\n", 6,
"abcxyz"), 15);
87 EXPECT_EQ(buf_,
"Hello, abcxyz!\n");
90 TEST_F(PrintfTest, StringWithLenPrefix) {
91 EXPECT_EQ(
base_printf(
"Hello, %!s!\n", 3,
"abcxyz"), 12);
92 EXPECT_EQ(buf_,
"Hello, abc!\n");
95 TEST_F(PrintfTest, StringWithLenZeroLen) {
96 EXPECT_EQ(
base_printf(
"Hello, %!s!\n", 0,
"abcxyz"), 9);
97 EXPECT_EQ(buf_,
"Hello, !\n");
100 TEST_F(PrintfTest, HexStringWithLen) {
101 uint32_t val = 0xdeadbeef;
102 EXPECT_EQ(
base_printf(
"Hello, %!x!\n", 4, &val), 17);
103 EXPECT_EQ(buf_,
"Hello, deadbeef!\n");
106 TEST_F(PrintfTest, HexStringWithLenPrefix) {
107 uint32_t val = 0xdeadbeef;
108 EXPECT_EQ(
base_printf(
"Hello, %!x!\n", 1, &val), 11);
109 EXPECT_EQ(buf_,
"Hello, ef!\n");
112 TEST_F(PrintfTest, HexStringWithLenZeroLen) {
113 uint32_t val = 0xdeadbeef;
114 EXPECT_EQ(
base_printf(
"Hello, %!x!\n", 0, &val), 9);
115 EXPECT_EQ(buf_,
"Hello, !\n");
118 TEST_F(PrintfTest, UpperHexStringWithLen) {
119 uint32_t val = 0xdeadbeef;
120 EXPECT_EQ(
base_printf(
"Hello, %!X!\n", 4, &val), 17);
121 EXPECT_EQ(buf_,
"Hello, DEADBEEF!\n");
124 TEST_F(PrintfTest, UpperHexStringWithLenPrefix) {
125 uint32_t val = 0xdeadbeef;
126 EXPECT_EQ(
base_printf(
"Hello, %!X!\n", 1, &val), 11);
127 EXPECT_EQ(buf_,
"Hello, EF!\n");
130 TEST_F(PrintfTest, UpperHexStringWithLenZeroLen) {
131 uint32_t val = 0xdeadbeef;
132 EXPECT_EQ(
base_printf(
"Hello, %!X!\n", 0, &val), 9);
133 EXPECT_EQ(buf_,
"Hello, !\n");
136 TEST_F(PrintfTest, LeHexStringWithLen) {
137 uint32_t val = 0xdeadbeef;
138 EXPECT_EQ(
base_printf(
"Hello, %!y!\n", 4, &val), 17);
139 EXPECT_EQ(buf_,
"Hello, efbeadde!\n");
142 TEST_F(PrintfTest, UpperLeHexStringWithLen) {
143 uint32_t val = 0xdeadbeef;
144 EXPECT_EQ(
base_printf(
"Hello, %!Y!\n", 4, &val), 17);
145 EXPECT_EQ(buf_,
"Hello, EFBEADDE!\n");
148 TEST_F(PrintfTest, SignedInt) {
150 EXPECT_EQ(buf_,
"Hello, 42!\n");
153 TEST_F(PrintfTest, SignedIntZero) {
155 EXPECT_EQ(buf_,
"Hello, 0!\n");
158 TEST_F(PrintfTest, SignedIntAlt) {
160 EXPECT_EQ(buf_,
"Hello, 42!\n");
163 TEST_F(PrintfTest, SignedIntNegative) {
165 EXPECT_EQ(buf_,
"Hello, -800!\n");
168 TEST_F(PrintfTest, SignedIntWithWidth) {
170 EXPECT_EQ(buf_,
"Hello, 42!\n");
173 TEST_F(PrintfTest, SignedIntWithWidthTooShort) {
175 EXPECT_EQ(buf_,
"Hello, 9001!\n");
178 TEST_F(PrintfTest, SignedIntWithZeros) {
180 EXPECT_EQ(buf_,
"Hello, 042!\n");
183 TEST_F(PrintfTest, SignedIntWithZerosTooShort) {
184 EXPECT_EQ(
base_printf(
"Hello, %03i!\n", 9001), 13);
185 EXPECT_EQ(buf_,
"Hello, 9001!\n");
188 TEST_F(PrintfTest, UnsignedInt) {
190 EXPECT_EQ(buf_,
"Hello, 42!\n");
193 TEST_F(PrintfTest, UnsignedIntNegative) {
195 EXPECT_EQ(buf_,
"Hello, 4294967295!\n");
198 TEST_F(PrintfTest, HexFromDec) {
200 EXPECT_EQ(buf_,
"Hello, 400!\n");
203 TEST_F(PrintfTest, HexFromDecWithWidth) {
204 EXPECT_EQ(
base_printf(
"Hello, %08x!\n", 1024), 17);
205 EXPECT_EQ(buf_,
"Hello, 00000400!\n");
208 TEST_F(PrintfTest, HexLower) {
209 EXPECT_EQ(
base_printf(
"Hello, %x!\n", 0xdead'beef), 17);
210 EXPECT_EQ(buf_,
"Hello, deadbeef!\n");
213 TEST_F(PrintfTest, HexUpper) {
214 EXPECT_EQ(
base_printf(
"Hello, %X!\n", 0xdead'beef), 17);
215 EXPECT_EQ(buf_,
"Hello, DEADBEEF!\n");
218 TEST_F(PrintfTest, HexNegative) {
220 EXPECT_EQ(buf_,
"Hello, ffffffff!\n");
223 TEST_F(PrintfTest, HexSvLower) {
224 EXPECT_EQ(
base_printf(
"Hello, %h!\n", 0xdead'beef), 17);
225 EXPECT_EQ(buf_,
"Hello, deadbeef!\n");
228 TEST_F(PrintfTest, HexSvUpper) {
229 EXPECT_EQ(
base_printf(
"Hello, %H!\n", 0xdead'beef), 17);
230 EXPECT_EQ(buf_,
"Hello, DEADBEEF!\n");
233 TEST_F(PrintfTest, Pointer) {
234 auto *ptr =
reinterpret_cast<uint32_t *
>(0x1234);
236 switch (
sizeof(uintptr_t)) {
238 EXPECT_EQ(buf_,
"Hello, 0x00001234!\n");
241 EXPECT_EQ(buf_,
"Hello, 0x0000000000001234!\n");
244 FAIL() <<
"Unknown pointer size";
249 TEST_F(PrintfTest, NullPtr) {
251 switch (
sizeof(uintptr_t)) {
253 EXPECT_EQ(buf_,
"Hello, 0x00000000!\n");
256 EXPECT_EQ(buf_,
"Hello, 0x0000000000000000!\n");
259 FAIL() <<
"Unknown pointer size";
264 TEST_F(PrintfTest, Octal) {
265 EXPECT_EQ(
base_printf(
"Hello, %o!\n", 01234567), 16);
266 EXPECT_EQ(buf_,
"Hello, 1234567!\n");
269 TEST_F(PrintfTest, Binary) {
270 EXPECT_EQ(
base_printf(
"Hello, %b!\n", 0b1010'1010), 17);
271 EXPECT_EQ(buf_,
"Hello, 10101010!\n");
274 TEST_F(PrintfTest, BinaryWithWidth) {
275 EXPECT_EQ(
base_printf(
"Hello, %032b!\n", 0b1010'1010), 41);
276 EXPECT_EQ(buf_,
"Hello, 00000000000000000000000010101010!\n");
279 TEST_F(PrintfTest, StatusOk) {
282 EXPECT_EQ(buf_,
"Hello, Ok:0\n");
285 TEST_F(PrintfTest, StatusOkWithArg) {
288 EXPECT_EQ(buf_,
"Hello, Ok:12345\n");
291 TEST_F(PrintfTest, StatusError) {
293 int line = __LINE__ - 1;
295 EXPECT_EQ(buf_, absl::StrFormat(
"Hello, Unknown:[\"PRI\",%d]\n", line));
298 TEST_F(PrintfTest, StatusErrorAsJson) {
300 int line = __LINE__ - 1;
302 EXPECT_EQ(buf_, absl::StrFormat(
"Hello, {\"Unknown\":[\"PRI\",%d]}\n", line));
305 TEST_F(PrintfTest, StatusErrorWithArg) {
306 status_t value = INVALID_ARGUMENT(2);
308 EXPECT_EQ(buf_, absl::StrFormat(
"Hello, InvalidArgument:[\"PRI\",%d]\n", 2));
311 TEST_F(PrintfTest, FourCharacterCode) {
312 EXPECT_EQ(
base_printf(
"Hello, %C\n", 0x5CA245D3), 18);
313 EXPECT_EQ(buf_,
"Hello, \\xd3E\\xa2\\\n");
316 TEST_F(PrintfTest, FourCharacterCodePrintable) {
317 EXPECT_EQ(
base_printf(
"Hello, %C\n", 0x65766144), 12);
318 EXPECT_EQ(buf_,
"Hello, Dave\n");
321 TEST_F(PrintfTest, FourCharacterCodeNonPrintable) {
322 EXPECT_EQ(
base_printf(
"Hello, %C\n", 0xAABBCCDD), 24);
323 EXPECT_EQ(buf_,
"Hello, \\xdd\\xcc\\xbb\\xaa\n");
326 TEST_F(PrintfTest, IncompleteSpec) {
328 EXPECT_THAT(buf_, StartsWith(
"Hello, "));
331 TEST_F(PrintfTest, UnknownSpec) {
333 EXPECT_THAT(buf_, StartsWith(
"Hello, "));
336 TEST_F(PrintfTest, WidthTooNarrow) {
338 EXPECT_THAT(buf_, StartsWith(
"Hello, "));
341 TEST_F(PrintfTest, WidthTooWide) {
343 EXPECT_THAT(buf_, StartsWith(
"Hello, "));
346 TEST_F(PrintfTest, ManySpecifiers) {
347 base_printf(
"%d + %d == %d, also spelled 0x%x", 2, 8, 2 + 8, 2 + 8);
348 EXPECT_THAT(buf_, StartsWith(
"2 + 8 == 10, also spelled 0xa"));
351 TEST_F(PrintfTest, HexDump) {
352 constexpr
char kStuff[] =
353 "a very long string pot\x12\x02\xAA entially containing garbage";
357 R
"hex(00000000: 6120 7665 7279 206c 6f6e 6720 7374 7269 a very long stri
358 00000010: 6e67 2070 6f74 1202 aa20 656e 7469 616c ng pot... ential
359 00000020: 6c79 2063 6f6e 7461 696e 696e 6720 6761 ly containing ga
360 00000030: 7262 6167 65 rbage
367 buf_, R
"hex(00000000: 612076 657279 206c6f 6e6720 737472 a very long str
368 0000000f: 696e67 20706f 741202 aa2065 6e7469 ing pot... enti
369 0000001e: 616c6c 792063 6f6e74 61696e 696e67 ally containing
370 0000002d: 206761 726261 6765 garbage
374 TEST(SnprintfTest, SimpleWrite) {
375 std::string buf(128, '\0');
376 auto len = base_snprintf(&buf[0], buf.size(),
"Hello, World!\n");
379 EXPECT_EQ(buf,
"Hello, World!\n");
382 TEST(SnprintfTest, ComplexFormating) {
383 std::string buf(128,
'\0');
385 base_snprintf(&buf[0], buf.size(),
"%d + %d == %d, also spelled 0x%x", 2,
388 EXPECT_EQ(buf,
"2 + 8 == 10, also spelled 0xa");
391 TEST(SnprintfTest, PartialWrite) {
392 std::string buf(16,
'\0');
394 base_snprintf(&buf[0], buf.size(),
"%d + %d == %d, also spelled 0x%x", 2,
398 EXPECT_EQ(buf,
"2 + 8 == 10, als");