xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/strings/substitute_test.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/substitute.h"
16 
17 #include <cstdint>
18 #include <vector>
19 
20 #include "gtest/gtest.h"
21 #include "absl/strings/str_cat.h"
22 
23 namespace {
24 
25 struct MyStruct {
26   template <typename Sink>
AbslStringify(Sink & sink,const MyStruct & s)27   friend void AbslStringify(Sink& sink, const MyStruct& s) {
28     sink.Append("MyStruct{.value = ");
29     sink.Append(absl::StrCat(s.value));
30     sink.Append("}");
31   }
32   int value;
33 };
34 
TEST(SubstituteTest,Substitute)35 TEST(SubstituteTest, Substitute) {
36   // Basic.
37   EXPECT_EQ("Hello, world!", absl::Substitute("$0, $1!", "Hello", "world"));
38 
39   // Non-char* types.
40   EXPECT_EQ("123 0.2 0.1 foo true false x",
41             absl::Substitute("$0 $1 $2 $3 $4 $5 $6", 123, 0.2, 0.1f,
42                              std::string("foo"), true, false, 'x'));
43 
44   // All int types.
45   EXPECT_EQ(
46       "-32767 65535 "
47       "-1234567890 3234567890 "
48       "-1234567890 3234567890 "
49       "-1234567890123456789 9234567890123456789",
50       absl::Substitute(
51           "$0 $1 $2 $3 $4 $5 $6 $7",
52           static_cast<short>(-32767),          // NOLINT(runtime/int)
53           static_cast<unsigned short>(65535),  // NOLINT(runtime/int)
54           -1234567890, 3234567890U, -1234567890L, 3234567890UL,
55           -int64_t{1234567890123456789}, uint64_t{9234567890123456789u}));
56 
57   // Hex format
58   EXPECT_EQ("0 1 f ffff0ffff 0123456789abcdef",
59             absl::Substitute("$0$1$2$3$4 $5",  //
60                              absl::Hex(0), absl::Hex(1, absl::kSpacePad2),
61                              absl::Hex(0xf, absl::kSpacePad2),
62                              absl::Hex(int16_t{-1}, absl::kSpacePad5),
63                              absl::Hex(int16_t{-1}, absl::kZeroPad5),
64                              absl::Hex(0x123456789abcdef, absl::kZeroPad16)));
65 
66   // Dec format
67   EXPECT_EQ("0 115   -1-0001 81985529216486895",
68             absl::Substitute("$0$1$2$3$4 $5",  //
69                              absl::Dec(0), absl::Dec(1, absl::kSpacePad2),
70                              absl::Dec(0xf, absl::kSpacePad2),
71                              absl::Dec(int16_t{-1}, absl::kSpacePad5),
72                              absl::Dec(int16_t{-1}, absl::kZeroPad5),
73                              absl::Dec(0x123456789abcdef, absl::kZeroPad16)));
74 
75   // Pointer.
76   const int* int_p = reinterpret_cast<const int*>(0x12345);
77   std::string str = absl::Substitute("$0", int_p);
78   EXPECT_EQ(absl::StrCat("0x", absl::Hex(int_p)), str);
79 
80   // Volatile Pointer.
81   // Like C++ streamed I/O, such pointers implicitly become bool
82   volatile int vol = 237;
83   volatile int* volatile volptr = &vol;
84   str = absl::Substitute("$0", volptr);
85   EXPECT_EQ("true", str);
86 
87   // null is special. StrCat prints 0x0. Substitute prints NULL.
88   const uint64_t* null_p = nullptr;
89   str = absl::Substitute("$0", null_p);
90   EXPECT_EQ("NULL", str);
91 
92   // char* is also special.
93   const char* char_p = "print me";
94   str = absl::Substitute("$0", char_p);
95   EXPECT_EQ("print me", str);
96 
97   char char_buf[16];
98   strncpy(char_buf, "print me too", sizeof(char_buf));
99   str = absl::Substitute("$0", char_buf);
100   EXPECT_EQ("print me too", str);
101 
102   // null char* is "doubly" special. Represented as the empty string.
103   char_p = nullptr;
104   str = absl::Substitute("$0", char_p);
105   EXPECT_EQ("", str);
106 
107   // Out-of-order.
108   EXPECT_EQ("b, a, c, b", absl::Substitute("$1, $0, $2, $1", "a", "b", "c"));
109 
110   // Literal $
111   EXPECT_EQ("$", absl::Substitute("$$"));
112 
113   EXPECT_EQ("$1", absl::Substitute("$$1"));
114 
115   // Test all overloads.
116   EXPECT_EQ("a", absl::Substitute("$0", "a"));
117   EXPECT_EQ("a b", absl::Substitute("$0 $1", "a", "b"));
118   EXPECT_EQ("a b c", absl::Substitute("$0 $1 $2", "a", "b", "c"));
119   EXPECT_EQ("a b c d", absl::Substitute("$0 $1 $2 $3", "a", "b", "c", "d"));
120   EXPECT_EQ("a b c d e",
121             absl::Substitute("$0 $1 $2 $3 $4", "a", "b", "c", "d", "e"));
122   EXPECT_EQ("a b c d e f", absl::Substitute("$0 $1 $2 $3 $4 $5", "a", "b", "c",
123                                             "d", "e", "f"));
124   EXPECT_EQ("a b c d e f g", absl::Substitute("$0 $1 $2 $3 $4 $5 $6", "a", "b",
125                                               "c", "d", "e", "f", "g"));
126   EXPECT_EQ("a b c d e f g h",
127             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d", "e",
128                              "f", "g", "h"));
129   EXPECT_EQ("a b c d e f g h i",
130             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c", "d",
131                              "e", "f", "g", "h", "i"));
132   EXPECT_EQ("a b c d e f g h i j",
133             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b", "c",
134                              "d", "e", "f", "g", "h", "i", "j"));
135   EXPECT_EQ("a b c d e f g h i j b0",
136             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10", "a", "b", "c",
137                              "d", "e", "f", "g", "h", "i", "j"));
138 
139   const char* null_cstring = nullptr;
140   EXPECT_EQ("Text: ''", absl::Substitute("Text: '$0'", null_cstring));
141 
142   MyStruct s1 = MyStruct{17};
143   MyStruct s2 = MyStruct{1043};
144   EXPECT_EQ("MyStruct{.value = 17}, MyStruct{.value = 1043}",
145             absl::Substitute("$0, $1", s1, s2));
146 }
147 
TEST(SubstituteTest,SubstituteAndAppend)148 TEST(SubstituteTest, SubstituteAndAppend) {
149   std::string str = "Hello";
150   absl::SubstituteAndAppend(&str, ", $0!", "world");
151   EXPECT_EQ("Hello, world!", str);
152 
153   // Test all overloads.
154   str.clear();
155   absl::SubstituteAndAppend(&str, "$0", "a");
156   EXPECT_EQ("a", str);
157   str.clear();
158   absl::SubstituteAndAppend(&str, "$0 $1", "a", "b");
159   EXPECT_EQ("a b", str);
160   str.clear();
161   absl::SubstituteAndAppend(&str, "$0 $1 $2", "a", "b", "c");
162   EXPECT_EQ("a b c", str);
163   str.clear();
164   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", "a", "b", "c", "d");
165   EXPECT_EQ("a b c d", str);
166   str.clear();
167   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4", "a", "b", "c", "d", "e");
168   EXPECT_EQ("a b c d e", str);
169   str.clear();
170   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5", "a", "b", "c", "d", "e",
171                             "f");
172   EXPECT_EQ("a b c d e f", str);
173   str.clear();
174   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6", "a", "b", "c", "d",
175                             "e", "f", "g");
176   EXPECT_EQ("a b c d e f g", str);
177   str.clear();
178   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d",
179                             "e", "f", "g", "h");
180   EXPECT_EQ("a b c d e f g h", str);
181   str.clear();
182   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c",
183                             "d", "e", "f", "g", "h", "i");
184   EXPECT_EQ("a b c d e f g h i", str);
185   str.clear();
186   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b",
187                             "c", "d", "e", "f", "g", "h", "i", "j");
188   EXPECT_EQ("a b c d e f g h i j", str);
189 
190   str.clear();
191   MyStruct s1 = MyStruct{17};
192   MyStruct s2 = MyStruct{1043};
193   absl::SubstituteAndAppend(&str, "$0, $1", s1, s2);
194   EXPECT_EQ("MyStruct{.value = 17}, MyStruct{.value = 1043}", str);
195 }
196 
TEST(SubstituteTest,VectorBoolRef)197 TEST(SubstituteTest, VectorBoolRef) {
198   std::vector<bool> v = {true, false};
199   const auto& cv = v;
200   EXPECT_EQ("true false true false",
201             absl::Substitute("$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]));
202 
203   std::string str = "Logic be like: ";
204   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]);
205   EXPECT_EQ("Logic be like: true false true false", str);
206 }
207 
TEST(SubstituteTest,Enums)208 TEST(SubstituteTest, Enums) {
209   enum UnscopedEnum { kEnum0 = 0, kEnum1 = 1 };
210   EXPECT_EQ("0 1", absl::Substitute("$0 $1", UnscopedEnum::kEnum0,
211                                     UnscopedEnum::kEnum1));
212 
213   enum class ScopedEnum { kEnum0 = 0, kEnum1 = 1 };
214   EXPECT_EQ("0 1",
215             absl::Substitute("$0 $1", ScopedEnum::kEnum0, ScopedEnum::kEnum1));
216 
217   enum class ScopedEnumInt32 : int32_t { kEnum0 = 989, kEnum1 = INT32_MIN };
218   EXPECT_EQ("989 -2147483648",
219             absl::Substitute("$0 $1", ScopedEnumInt32::kEnum0,
220                              ScopedEnumInt32::kEnum1));
221 
222   enum class ScopedEnumUInt32 : uint32_t { kEnum0 = 1, kEnum1 = UINT32_MAX };
223   EXPECT_EQ("1 4294967295", absl::Substitute("$0 $1", ScopedEnumUInt32::kEnum0,
224                                              ScopedEnumUInt32::kEnum1));
225 
226   enum class ScopedEnumInt64 : int64_t { kEnum0 = -1, kEnum1 = 42949672950 };
227   EXPECT_EQ("-1 42949672950", absl::Substitute("$0 $1", ScopedEnumInt64::kEnum0,
228                                                ScopedEnumInt64::kEnum1));
229 
230   enum class ScopedEnumUInt64 : uint64_t { kEnum0 = 1, kEnum1 = 42949672950 };
231   EXPECT_EQ("1 42949672950", absl::Substitute("$0 $1", ScopedEnumUInt64::kEnum0,
232                                               ScopedEnumUInt64::kEnum1));
233 
234   enum class ScopedEnumChar : signed char { kEnum0 = -1, kEnum1 = 1 };
235   EXPECT_EQ("-1 1", absl::Substitute("$0 $1", ScopedEnumChar::kEnum0,
236                                      ScopedEnumChar::kEnum1));
237 
238   enum class ScopedEnumUChar : unsigned char {
239     kEnum0 = 0,
240     kEnum1 = 1,
241     kEnumMax = 255
242   };
243   EXPECT_EQ("0 1 255", absl::Substitute("$0 $1 $2", ScopedEnumUChar::kEnum0,
244                                         ScopedEnumUChar::kEnum1,
245                                         ScopedEnumUChar::kEnumMax));
246 
247   enum class ScopedEnumInt16 : int16_t { kEnum0 = -100, kEnum1 = 10000 };
248   EXPECT_EQ("-100 10000", absl::Substitute("$0 $1", ScopedEnumInt16::kEnum0,
249                                            ScopedEnumInt16::kEnum1));
250 
251   enum class ScopedEnumUInt16 : uint16_t { kEnum0 = 0, kEnum1 = 10000 };
252   EXPECT_EQ("0 10000", absl::Substitute("$0 $1", ScopedEnumUInt16::kEnum0,
253                                         ScopedEnumUInt16::kEnum1));
254 }
255 
256 #ifdef GTEST_HAS_DEATH_TEST
257 
TEST(SubstituteDeathTest,SubstituteDeath)258 TEST(SubstituteDeathTest, SubstituteDeath) {
259   EXPECT_DEBUG_DEATH(
260       static_cast<void>(absl::Substitute(absl::string_view("-$2"), "a", "b")),
261       "Invalid absl::Substitute\\(\\) format string: asked for \"\\$2\", "
262       "but only 2 args were given.");
263   EXPECT_DEBUG_DEATH(
264       static_cast<void>(absl::Substitute(absl::string_view("-$z-"))),
265       "Invalid absl::Substitute\\(\\) format string: \"-\\$z-\"");
266   EXPECT_DEBUG_DEATH(
267       static_cast<void>(absl::Substitute(absl::string_view("-$"))),
268       "Invalid absl::Substitute\\(\\) format string: \"-\\$\"");
269 }
270 
271 #endif  // GTEST_HAS_DEATH_TEST
272 
273 }  // namespace
274