1 //===-- StringListTest.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StringList.h"
10 #include "lldb/Utility/StreamString.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
14 using namespace lldb_private
;
16 TEST(StringListTest
, DefaultConstructor
) {
18 EXPECT_EQ(0U, s
.GetSize());
21 TEST(StringListTest
, Assignment
) {
23 orig
.AppendString("foo");
24 orig
.AppendString("bar");
28 ASSERT_EQ(2U, s
.GetSize());
29 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
30 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
32 ASSERT_EQ(2U, orig
.GetSize());
33 EXPECT_STREQ("foo", orig
.GetStringAtIndex(0));
34 EXPECT_STREQ("bar", orig
.GetStringAtIndex(1));
37 TEST(StringListTest
, AppendStringStdString
) {
39 s
.AppendString("foo");
40 ASSERT_EQ(1U, s
.GetSize());
41 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
43 s
.AppendString("bar");
44 ASSERT_EQ(2U, s
.GetSize());
45 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
46 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
49 TEST(StringListTest
, AppendStringCString
) {
51 s
.AppendString("foo", strlen("foo"));
52 ASSERT_EQ(1U, s
.GetSize());
53 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
55 s
.AppendString("bar", strlen("bar"));
56 ASSERT_EQ(2U, s
.GetSize());
57 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
58 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
61 TEST(StringListTest
, AppendStringMove
) {
63 std::string foo
= "foo";
64 std::string bar
= "bar";
66 s
.AppendString(std::move(foo
));
67 ASSERT_EQ(1U, s
.GetSize());
68 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
70 s
.AppendString(std::move(bar
));
71 ASSERT_EQ(2U, s
.GetSize());
72 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
73 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
76 TEST(StringListTest
, ShiftStdString
) {
78 std::string foo
= "foo";
79 std::string bar
= "bar";
82 ASSERT_EQ(1U, s
.GetSize());
83 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
86 ASSERT_EQ(2U, s
.GetSize());
87 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
88 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
91 TEST(StringListTest
, ShiftCString
) {
94 ASSERT_EQ(1U, s
.GetSize());
95 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
98 ASSERT_EQ(2U, s
.GetSize());
99 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
100 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
103 TEST(StringListTest
, ShiftMove
) {
105 std::string foo
= "foo";
106 std::string bar
= "bar";
109 ASSERT_EQ(1U, s
.GetSize());
110 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
113 ASSERT_EQ(2U, s
.GetSize());
114 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
115 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
118 TEST(StringListTest
, AppendListCStringArrayEmpty
) {
120 s
.AppendList(nullptr, 0);
121 EXPECT_EQ(0U, s
.GetSize());
124 TEST(StringListTest
, AppendListCStringArray
) {
126 const char *items
[3] = {"foo", "", "bar"};
127 s
.AppendList(items
, 3);
129 EXPECT_EQ(3U, s
.GetSize());
130 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
131 EXPECT_STREQ("", s
.GetStringAtIndex(1));
132 EXPECT_STREQ("bar", s
.GetStringAtIndex(2));
135 TEST(StringListTest
, AppendList
) {
137 other
.AppendString("foo");
138 other
.AppendString("");
139 other
.AppendString("bar");
146 EXPECT_EQ(3U, s
.GetSize());
147 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
148 EXPECT_STREQ("", s
.GetStringAtIndex(1));
149 EXPECT_STREQ("bar", s
.GetStringAtIndex(2));
151 EXPECT_EQ(3U, other
.GetSize());
152 EXPECT_STREQ("foo", other
.GetStringAtIndex(0));
153 EXPECT_STREQ("", other
.GetStringAtIndex(1));
154 EXPECT_STREQ("bar", other
.GetStringAtIndex(2));
158 EXPECT_EQ(6U, s
.GetSize());
159 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
160 EXPECT_STREQ("", s
.GetStringAtIndex(1));
161 EXPECT_STREQ("bar", s
.GetStringAtIndex(2));
162 EXPECT_STREQ("foo", s
.GetStringAtIndex(3));
163 EXPECT_STREQ("", s
.GetStringAtIndex(4));
164 EXPECT_STREQ("bar", s
.GetStringAtIndex(5));
166 EXPECT_EQ(3U, other
.GetSize());
167 EXPECT_STREQ("foo", other
.GetStringAtIndex(0));
168 EXPECT_STREQ("", other
.GetStringAtIndex(1));
169 EXPECT_STREQ("bar", other
.GetStringAtIndex(2));
172 TEST(StringListTest
, GetSize
) {
174 s
.AppendString("foo");
175 EXPECT_EQ(1U, s
.GetSize());
177 s
.AppendString("foo");
178 EXPECT_EQ(2U, s
.GetSize());
180 s
.AppendString("foobar");
181 EXPECT_EQ(3U, s
.GetSize());
184 TEST(StringListTest
, SetSize
) {
187 EXPECT_EQ(3U, s
.GetSize());
188 EXPECT_STREQ("", s
.GetStringAtIndex(0));
189 EXPECT_STREQ("", s
.GetStringAtIndex(1));
190 EXPECT_STREQ("", s
.GetStringAtIndex(2));
193 TEST(StringListTest
, SplitIntoLines
) {
195 s
.SplitIntoLines("\nfoo\nbar\n\n");
196 EXPECT_EQ(4U, s
.GetSize());
197 EXPECT_STREQ("", s
.GetStringAtIndex(0));
198 EXPECT_STREQ("foo", s
.GetStringAtIndex(1));
199 EXPECT_STREQ("bar", s
.GetStringAtIndex(2));
200 EXPECT_STREQ("", s
.GetStringAtIndex(3));
203 TEST(StringListTest
, SplitIntoLinesSingleTrailingCR
) {
205 s
.SplitIntoLines("\r");
206 EXPECT_EQ(1U, s
.GetSize());
207 EXPECT_STREQ("", s
.GetStringAtIndex(0));
210 TEST(StringListTest
, SplitIntoLinesEmpty
) {
212 s
.SplitIntoLines("");
213 EXPECT_EQ(0U, s
.GetSize());
216 TEST(StringListTest
, LongestCommonPrefixEmpty
) {
218 std::string prefix
= s
.LongestCommonPrefix();
219 EXPECT_EQ("", prefix
);
222 TEST(StringListTest
, LongestCommonPrefix
) {
224 s
.AppendString("foo");
225 s
.AppendString("foobar");
226 s
.AppendString("foo");
227 s
.AppendString("foozar");
229 std::string prefix
= s
.LongestCommonPrefix();
230 EXPECT_EQ("foo", prefix
);
233 TEST(StringListTest
, LongestCommonPrefixSingleElement
) {
235 s
.AppendString("foo");
237 std::string prefix
= s
.LongestCommonPrefix();
238 EXPECT_EQ("foo", prefix
);
241 TEST(StringListTest
, LongestCommonPrefixDuplicateElement
) {
243 s
.AppendString("foo");
244 s
.AppendString("foo");
246 std::string prefix
= s
.LongestCommonPrefix();
247 EXPECT_EQ("foo", prefix
);
250 TEST(StringListTest
, LongestCommonPrefixNoPrefix
) {
252 s
.AppendString("foo");
253 s
.AppendString("1foobar");
254 s
.AppendString("2foo");
255 s
.AppendString("3foozar");
257 std::string prefix
= s
.LongestCommonPrefix();
258 EXPECT_EQ("", prefix
);
261 TEST(StringListTest
, Clear
) {
264 EXPECT_EQ(0U, s
.GetSize());
266 s
.AppendString("foo");
268 EXPECT_EQ(0U, s
.GetSize());
270 s
.AppendString("foo");
271 s
.AppendString("foo");
273 EXPECT_EQ(0U, s
.GetSize());
276 TEST(StringListTest
, PopBack
) {
278 s
.AppendString("foo");
279 s
.AppendString("bar");
280 s
.AppendString("boo");
283 EXPECT_EQ(2U, s
.GetSize());
284 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
285 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
288 EXPECT_EQ(1U, s
.GetSize());
289 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
292 EXPECT_EQ(0U, s
.GetSize());
295 TEST(StringListTest
, RemoveBlankLines
) {
298 // Nothing to remove yet.
299 s
.RemoveBlankLines();
300 EXPECT_EQ(0U, s
.GetSize());
305 s
.AppendString("\t");
314 // And remove all the empty ones again.
315 s
.RemoveBlankLines();
317 EXPECT_EQ(3U, s
.GetSize());
318 EXPECT_STREQ("\t", s
.GetStringAtIndex(0));
319 EXPECT_STREQ(" ", s
.GetStringAtIndex(1));
320 EXPECT_STREQ("f", s
.GetStringAtIndex(2));
323 TEST(StringListTest
, InsertStringAtIndexStart
) {
326 s
.InsertStringAtIndex(0, "bar");
327 EXPECT_EQ(1U, s
.GetSize());
328 EXPECT_STREQ("bar", s
.GetStringAtIndex(0));
330 s
.InsertStringAtIndex(0, "foo");
331 EXPECT_EQ(2U, s
.GetSize());
332 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
333 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
336 TEST(StringListTest
, InsertStringAtIndexEnd
) {
339 s
.InsertStringAtIndex(0, "foo");
340 EXPECT_EQ(1U, s
.GetSize());
341 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
343 s
.InsertStringAtIndex(1, "bar");
344 EXPECT_EQ(2U, s
.GetSize());
345 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
346 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
349 TEST(StringListTest
, InsertStringAtIndexOutOfBounds
) {
352 s
.InsertStringAtIndex(1, "foo");
353 EXPECT_EQ(1U, s
.GetSize());
354 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
356 // FIXME: Inserting at an OOB index will always just append to the list. This
357 // seems not very intuitive.
358 s
.InsertStringAtIndex(3, "bar");
359 EXPECT_EQ(2U, s
.GetSize());
360 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
361 EXPECT_STREQ("bar", s
.GetStringAtIndex(1));
364 TEST(StringListTest
, InsertStringAtIndexStdString
) {
367 std::string foo
= "foo";
368 s
.InsertStringAtIndex(0, foo
);
369 EXPECT_EQ(1U, s
.GetSize());
370 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
373 TEST(StringListTest
, InsertStringAtIndexMove
) {
376 std::string foo
= "foo";
377 s
.InsertStringAtIndex(0, std::move(foo
));
378 EXPECT_EQ(1U, s
.GetSize());
379 EXPECT_STREQ("foo", s
.GetStringAtIndex(0));
382 TEST(StringListTest
, CopyListEmpty
) {
385 EXPECT_EQ("", s
.CopyList());
386 EXPECT_EQ("", s
.CopyList("+"));
389 TEST(StringListTest
, CopyListSingle
) {
391 s
.AppendString("ab");
393 EXPECT_EQ("ab", s
.CopyList());
394 EXPECT_EQ("-ab", s
.CopyList("-"));
397 TEST(StringListTest
, CopyList
) {
399 s
.AppendString("ab");
400 s
.AppendString("cd");
402 EXPECT_EQ("ab\ncd", s
.CopyList());
403 EXPECT_EQ("-ab\n-cd", s
.CopyList("-"));
406 TEST(StringListTest
, Join
) {
408 s
.AppendString("ab");
409 s
.AppendString("cd");
414 EXPECT_EQ("ab cd", ss
.GetString());
417 TEST(StringListTest
, JoinEmpty
) {
423 EXPECT_EQ("", ss
.GetString());
426 TEST(StringListTest
, JoinSingle
) {
428 s
.AppendString("foo");
433 EXPECT_EQ("foo", ss
.GetString());
436 TEST(StringListTest
, JoinThree
) {
445 EXPECT_EQ("1 2 3", ss
.GetString());
448 TEST(StringListTest
, JoinNonSpace
) {
457 EXPECT_EQ("1.2.3", ss
.GetString());
460 TEST(StringListTest
, JoinMultiCharSeparator
) {
469 EXPECT_EQ("1--2--3", ss
.GetString());
472 TEST(StringListTest
, GetMaxStringLengthEqualSize
) {
474 s
.AppendString("123");
475 s
.AppendString("123");
476 EXPECT_EQ(3U, s
.GetMaxStringLength());
479 TEST(StringListTest
, GetMaxStringLengthIncreasingSize
) {
481 s
.AppendString("123");
482 s
.AppendString("1234");
483 EXPECT_EQ(4U, s
.GetMaxStringLength());
486 TEST(StringListTest
, GetMaxStringLengthDecreasingSize
) {
488 s
.AppendString("1234");
489 s
.AppendString("123");
490 EXPECT_EQ(4U, s
.GetMaxStringLength());
493 TEST(StringListTest
, GetMaxStringLengthMixed
) {
495 s
.AppendString("123");
497 s
.AppendString("123");
498 s
.AppendString("1234");
499 s
.AppendString("123");
501 EXPECT_EQ(4U, s
.GetMaxStringLength());
504 TEST(StringListTest
, GetMaxStringLengthEmpty
) {
506 EXPECT_EQ(0U, s
.GetMaxStringLength());
509 TEST(StringListTest
, ForRangeEmpty
) {
511 for (const std::string
&e
: s
)
512 FAIL() << "Shouldn't have hit an element in for range" << e
;
515 TEST(StringListTest
, ForRange
) {
520 std::vector
<std::string
> recorded
;
521 for (const std::string
&e
: s
)
522 recorded
.push_back(e
);
523 EXPECT_THAT(recorded
, testing::ElementsAre("a", "b", "c"));