• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_string/format.h"
16 
17 #include <cstdarg>
18 #include <span>
19 
20 #include "gtest/gtest.h"
21 
22 namespace pw::string {
23 namespace {
24 
TEST(Format,ValidFormatString_Succeeds)25 TEST(Format, ValidFormatString_Succeeds) {
26   char buffer[32];
27   auto result = Format(buffer, "-_-");
28 
29   EXPECT_EQ(OkStatus(), result.status());
30   EXPECT_EQ(3u, result.size());
31   EXPECT_STREQ("-_-", buffer);
32 }
33 
TEST(Format,ValidFormatStringAndArguments_Succeeds)34 TEST(Format, ValidFormatStringAndArguments_Succeeds) {
35   char buffer[32];
36   auto result = Format(buffer, "%d4%s", 123, "5");
37 
38   EXPECT_EQ(OkStatus(), result.status());
39   EXPECT_EQ(5u, result.size());
40   EXPECT_STREQ("12345", buffer);
41 }
42 
TEST(Format,EmptyBuffer_ReturnsResourceExhausted)43 TEST(Format, EmptyBuffer_ReturnsResourceExhausted) {
44   auto result = Format(std::span<char>(), "?");
45 
46   EXPECT_EQ(Status::ResourceExhausted(), result.status());
47   EXPECT_EQ(0u, result.size());
48 }
49 
TEST(Format,FormatLargerThanBuffer_ReturnsResourceExhausted)50 TEST(Format, FormatLargerThanBuffer_ReturnsResourceExhausted) {
51   char buffer[5];
52   auto result = Format(buffer, "2big!");
53 
54   EXPECT_EQ(Status::ResourceExhausted(), result.status());
55   EXPECT_EQ(4u, result.size());
56   EXPECT_STREQ("2big", buffer);
57 }
58 
TEST(Format,ArgumentLargerThanBuffer_ReturnsResourceExhausted)59 TEST(Format, ArgumentLargerThanBuffer_ReturnsResourceExhausted) {
60   char buffer[5];
61   auto result = Format(buffer, "%s", "2big!");
62 
63   EXPECT_EQ(Status::ResourceExhausted(), result.status());
64   EXPECT_EQ(4u, result.size());
65   EXPECT_STREQ("2big", buffer);
66 }
67 
CallFormatWithVaList(std::span<char> buffer,const char * fmt,...)68 StatusWithSize CallFormatWithVaList(std::span<char> buffer,
69                                     const char* fmt,
70                                     ...) {
71   va_list args;
72   va_start(args, fmt);
73 
74   StatusWithSize result = FormatVaList(buffer, fmt, args);
75 
76   va_end(args);
77   return result;
78 }
79 
TEST(Format,CallFormatWithVaList_CallsCorrectFormatOverload)80 TEST(Format, CallFormatWithVaList_CallsCorrectFormatOverload) {
81   char buffer[8];
82   auto result = CallFormatWithVaList(buffer, "Yo%s", "?!");
83 
84   EXPECT_EQ(OkStatus(), result.status());
85   EXPECT_EQ(4u, result.size());
86   EXPECT_STREQ("Yo?!", buffer);
87 }
88 
89 }  // namespace
90 }  // namespace pw::string
91