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 | |
Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 43 | TEST(Format, EmptyBuffer_ReturnsResourceExhausted) { |
| 44 | auto result = Format(span<char>(), "?"); |
| 45 | |
| 46 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 47 | EXPECT_EQ(0u, result.size()); |
| 48 | } |
| 49 | |
| 50 | TEST(Format, FormatLargerThanBuffer_ReturnsResourceExhausted) { |
| 51 | char buffer[5]; |
| 52 | auto result = Format(buffer, "2big!"); |
| 53 | |
| 54 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 55 | EXPECT_EQ(4u, result.size()); |
| 56 | EXPECT_STREQ("2big", buffer); |
| 57 | } |
| 58 | |
| 59 | TEST(Format, ArgumentLargerThanBuffer_ReturnsResourceExhausted) { |
| 60 | char buffer[5]; |
Wyatt Hepler | 2596fe5 | 2020-01-23 17:40:10 -0800 | [diff] [blame] | 61 | auto result = Format(buffer, "%s", "2big!"); |
Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 62 | |
| 63 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 64 | EXPECT_EQ(4u, result.size()); |
| 65 | EXPECT_STREQ("2big", buffer); |
| 66 | } |
| 67 | |
| 68 | StatusWithSize CallFormatWithVaList(span<char> buffer, const char* fmt, ...) { |
| 69 | va_list args; |
| 70 | va_start(args, fmt); |
| 71 | |
Wyatt Hepler | 2596fe5 | 2020-01-23 17:40:10 -0800 | [diff] [blame] | 72 | StatusWithSize result = FormatVaList(buffer, fmt, args); |
Wyatt Hepler | e2dc6d1 | 2019-11-15 09:05:07 -0800 | [diff] [blame] | 73 | |
| 74 | va_end(args); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | TEST(Format, CallFormatWithVaList_CallsCorrectFormatOverload) { |
| 79 | char buffer[8]; |
| 80 | auto result = CallFormatWithVaList(buffer, "Yo%s", "?!"); |
| 81 | |
| 82 | EXPECT_EQ(Status::OK, result.status()); |
| 83 | EXPECT_EQ(4u, result.size()); |
| 84 | EXPECT_STREQ("Yo?!", buffer); |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | } // namespace pw::string |