Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame^] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame^] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pw_string/format.h" |
| 16 | |
| 17 | #include <cstdarg> |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | #include "pw_span/span.h" |
| 21 | |
| 22 | namespace pw::string { |
| 23 | namespace { |
| 24 | |
| 25 | TEST(Format, ValidFormatString_Succeeds) { |
| 26 | char buffer[32]; |
| 27 | auto result = Format(buffer, "-_-"); |
| 28 | |
| 29 | EXPECT_EQ(Status::OK, result.status()); |
| 30 | EXPECT_EQ(3u, result.size()); |
| 31 | EXPECT_STREQ("-_-", buffer); |
| 32 | } |
| 33 | |
| 34 | TEST(Format, ValidFormatStringAndArguments_Succeeds) { |
| 35 | char buffer[32]; |
| 36 | auto result = Format(buffer, "%d4%s", 123, "5"); |
| 37 | |
| 38 | EXPECT_EQ(Status::OK, result.status()); |
| 39 | EXPECT_EQ(5u, result.size()); |
| 40 | EXPECT_STREQ("12345", buffer); |
| 41 | } |
| 42 | |
| 43 | TEST(Format, InvalidConversionSpecifier_ReturnsInvalidArgumentAndTerminates) { |
| 44 | char buffer[32] = {'?', '?', '?', '?', '\0'}; |
| 45 | |
| 46 | // Make the format string volatile to prevent the compiler from potentially |
| 47 | // checking this as a format string. |
| 48 | const char* volatile fmt = "abc %9999999999999999999999999999999999d4%s"; |
| 49 | auto result = Format(buffer, fmt, 123, "5"); |
| 50 | |
| 51 | EXPECT_EQ(Status::INVALID_ARGUMENT, result.status()); |
| 52 | EXPECT_STREQ("", buffer); |
| 53 | } |
| 54 | |
| 55 | TEST(Format, EmptyBuffer_ReturnsResourceExhausted) { |
| 56 | auto result = Format(span<char>(), "?"); |
| 57 | |
| 58 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 59 | EXPECT_EQ(0u, result.size()); |
| 60 | } |
| 61 | |
| 62 | TEST(Format, FormatLargerThanBuffer_ReturnsResourceExhausted) { |
| 63 | char buffer[5]; |
| 64 | auto result = Format(buffer, "2big!"); |
| 65 | |
| 66 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 67 | EXPECT_EQ(4u, result.size()); |
| 68 | EXPECT_STREQ("2big", buffer); |
| 69 | } |
| 70 | |
| 71 | TEST(Format, ArgumentLargerThanBuffer_ReturnsResourceExhausted) { |
| 72 | char buffer[5]; |
| 73 | auto result = Format(buffer, "%s", "2big!"); |
| 74 | |
| 75 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 76 | EXPECT_EQ(4u, result.size()); |
| 77 | EXPECT_STREQ("2big", buffer); |
| 78 | } |
| 79 | |
| 80 | StatusWithSize CallFormatWithVaList(span<char> buffer, const char* fmt, ...) { |
| 81 | va_list args; |
| 82 | va_start(args, fmt); |
| 83 | |
| 84 | StatusWithSize result = Format(buffer, fmt, args); |
| 85 | |
| 86 | va_end(args); |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | TEST(Format, CallFormatWithVaList_CallsCorrectFormatOverload) { |
| 91 | char buffer[8]; |
| 92 | auto result = CallFormatWithVaList(buffer, "Yo%s", "?!"); |
| 93 | |
| 94 | EXPECT_EQ(Status::OK, result.status()); |
| 95 | EXPECT_EQ(4u, result.size()); |
| 96 | EXPECT_STREQ("Yo?!", buffer); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | } // namespace pw::string |