18 #include <initializer_list> 23 #include <type_traits> 24 #include <unordered_map> 25 #include <unordered_set> 28 #include "gmock/gmock.h" 29 #include "gtest/gtest.h" 36 using ::testing::ElementsAre;
37 using ::testing::Pair;
38 using ::testing::UnorderedElementsAre;
40 TEST(Split, TraitsTest) {
46 std::vector<std::string>>::
value,
52 std::vector<absl::string_view>>::
value,
55 std::map<std::string, std::string>>::
value,
58 std::map<absl::string_view, absl::string_view>>::
value,
61 std::map<int, std::string>>::
value,
64 std::map<std::string, int>>::
value,
72 TEST(Split, APIExamples) {
76 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
81 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
85 ElementsAre(
"a",
"b",
"c"));
91 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
96 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
101 const std::vector<std::string> v =
absl::StrSplit(
"a=>b=>c",
"=>");
102 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
108 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
114 EXPECT_THAT(v, ElementsAre(
"",
"a",
"b",
"c",
""));
120 EXPECT_THAT(v, ElementsAre(
"abc"));
127 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
136 std::string embedded_nulls(
"a\0b\0c", 5);
137 std::string null_delim(
"\0", 1);
138 std::vector<std::string> v =
absl::StrSplit(embedded_nulls, null_delim);
139 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
144 std::pair<std::string, std::string> p =
absl::StrSplit(
"a,b,c",
',');
145 EXPECT_EQ(
"a", p.first);
146 EXPECT_EQ(
"b", p.second);
152 std::set<std::string> v =
absl::StrSplit(
"a,b,c,a,b,c,a,b,c",
',');
153 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
161 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
167 std::vector<std::string> v =
absl::StrSplit(
"a,b;c", ByAnyChar(
",;"));
168 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
174 std::vector<std::string> v =
176 EXPECT_THAT(v, ElementsAre(
" a ",
"b"));
182 std::vector<std::string> v =
absl::StrSplit(
"abcdefg", ByLength(3));
183 EXPECT_THAT(v, ElementsAre(
"abc",
"def",
"g"));
189 EXPECT_THAT(v1, ElementsAre(
"a",
"b",
"c"));
191 EXPECT_THAT(v2, ElementsAre(
"a",
"b",
"c"));
193 EXPECT_THAT(v3, ElementsAre(
"a",
"b",
"c"));
195 EXPECT_THAT(v3, ElementsAre(
"a",
"b",
"c"));
200 std::map<std::string, std::string> m =
absl::StrSplit(
"a,1,b,2,a,3",
',');
201 EXPECT_EQ(2, m.size());
202 EXPECT_EQ(
"3", m[
"a"]);
203 EXPECT_EQ(
"2", m[
"b"]);
208 std::multimap<std::string, std::string> m =
210 EXPECT_EQ(3, m.size());
211 auto it = m.find(
"a");
212 EXPECT_EQ(
"1", it->second);
214 EXPECT_EQ(
"3", it->second);
216 EXPECT_EQ(
"2", it->second);
221 std::string s =
"x,x,x,x,x,x,x";
230 std::string s =
" ,x,,x,,x,x,x,,";
241 std::map<std::string, std::string> m;
245 EXPECT_EQ(
"b=c", m.find(
"a")->second);
246 EXPECT_EQ(
"e", m.find(
"d")->second);
247 EXPECT_EQ(
"", m.find(
"f")->second);
248 EXPECT_EQ(
"", m.find(
"g")->second);
256 TEST(SplitIterator, Basics) {
258 auto it = splitter.begin();
259 auto end = splitter.end();
266 std::string(it->data(), it->size()));
274 explicit Skip(
const std::string& s) : s_(s) {}
281 TEST(SplitIterator, Predicate) {
283 auto it = splitter.begin();
284 auto end = splitter.end();
291 std::string(it->data(), it->size()));
296 TEST(SplitIterator, EdgeCases) {
300 std::vector<std::string> expect;
305 {
",foo", {
"",
"foo"}},
306 {
"foo,", {
"foo",
""}},
307 {
",foo,", {
"",
"foo",
""}},
308 {
"foo,bar", {
"foo",
"bar"}},
311 for (
const auto& spec : specs) {
312 SCOPED_TRACE(spec.in);
314 auto it = splitter.begin();
315 auto end = splitter.end();
316 for (
const auto& expected : spec.expect) {
318 EXPECT_EQ(expected, *it++);
324 TEST(Splitter, Const) {
326 EXPECT_THAT(splitter, ElementsAre(
"a",
"b",
"c"));
329 TEST(Split, EmptyAndNull) {
340 TEST(SplitIterator, EqualityAsEndCondition) {
342 auto it = splitter.begin();
348 EXPECT_EQ(
"c", *it2);
354 std::vector<absl::string_view>
v;
355 for (; it != it2; ++it) {
358 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
365 TEST(Splitter, RangeIterators) {
367 std::vector<absl::string_view> output;
371 EXPECT_THAT(output, ElementsAre(
"a",
"b",
"c"));
375 template <
typename ContainerType,
typename Splitter>
376 void TestConversionOperator(
const Splitter& splitter) {
377 ContainerType output = splitter;
378 EXPECT_THAT(output, UnorderedElementsAre(
"a",
"b",
"c",
"d"));
381 template <
typename MapType,
typename Splitter>
382 void TestMapConversionOperator(
const Splitter& splitter) {
383 MapType m = splitter;
384 EXPECT_THAT(m, UnorderedElementsAre(Pair(
"a",
"b"), Pair(
"c",
"d")));
387 template <
typename FirstType,
typename SecondType,
typename Splitter>
388 void TestPairConversionOperator(
const Splitter& splitter) {
389 std::pair<FirstType, SecondType> p = splitter;
390 EXPECT_EQ(p, (std::pair<FirstType, SecondType>(
"a",
"b")));
393 TEST(Splitter, ConversionOperator) {
396 TestConversionOperator<std::vector<absl::string_view>>(splitter);
397 TestConversionOperator<std::vector<std::string>>(splitter);
398 TestConversionOperator<std::list<absl::string_view>>(splitter);
399 TestConversionOperator<std::list<std::string>>(splitter);
400 TestConversionOperator<std::deque<absl::string_view>>(splitter);
401 TestConversionOperator<std::deque<std::string>>(splitter);
402 TestConversionOperator<std::set<absl::string_view>>(splitter);
403 TestConversionOperator<std::set<std::string>>(splitter);
404 TestConversionOperator<std::multiset<absl::string_view>>(splitter);
405 TestConversionOperator<std::multiset<std::string>>(splitter);
406 TestConversionOperator<std::unordered_set<std::string>>(splitter);
410 TestMapConversionOperator<std::map<absl::string_view, absl::string_view>>(
412 TestMapConversionOperator<std::map<absl::string_view, std::string>>(splitter);
413 TestMapConversionOperator<std::map<std::string, absl::string_view>>(splitter);
414 TestMapConversionOperator<std::map<std::string, std::string>>(splitter);
415 TestMapConversionOperator<
416 std::multimap<absl::string_view, absl::string_view>>(splitter);
417 TestMapConversionOperator<std::multimap<absl::string_view, std::string>>(
419 TestMapConversionOperator<std::multimap<std::string, absl::string_view>>(
421 TestMapConversionOperator<std::multimap<std::string, std::string>>(splitter);
422 TestMapConversionOperator<std::unordered_map<std::string, std::string>>(
427 TestPairConversionOperator<absl::string_view, absl::string_view>(splitter);
428 TestPairConversionOperator<absl::string_view, std::string>(splitter);
429 TestPairConversionOperator<std::string, absl::string_view>(splitter);
430 TestPairConversionOperator<std::string, std::string>(splitter);
437 TEST(Splitter, ToPair) {
441 EXPECT_EQ(
"", p.first);
442 EXPECT_EQ(
"", p.second);
448 EXPECT_EQ(
"a", p.first);
449 EXPECT_EQ(
"", p.second);
454 std::pair<std::string, std::string> p =
absl::StrSplit(
",b",
',');
455 EXPECT_EQ(
"", p.first);
456 EXPECT_EQ(
"b", p.second);
461 std::pair<std::string, std::string> p =
absl::StrSplit(
"a,b",
',');
462 EXPECT_EQ(
"a", p.first);
463 EXPECT_EQ(
"b", p.second);
468 std::pair<std::string, std::string> p =
absl::StrSplit(
"a,b,c",
',');
469 EXPECT_EQ(
"a", p.first);
470 EXPECT_EQ(
"b", p.second);
475 TEST(Splitter, Predicates) {
476 static const char kTestChars[] =
",a, ,b,";
484 std::vector<std::string> v = splitter;
485 EXPECT_THAT(v, ElementsAre(
"",
"a",
" ",
"b",
""));
491 std::vector<std::string> v_allowempty = splitter;
492 EXPECT_THAT(v_allowempty, ElementsAre(
"",
"a",
" ",
"b",
""));
496 std::vector<std::string> v_nopredicate = splitter_nopredicate;
497 EXPECT_EQ(v_allowempty, v_nopredicate);
503 std::vector<std::string> v = splitter;
504 EXPECT_THAT(v, ElementsAre(
"a",
" ",
"b"));
509 auto splitter =
absl::StrSplit(kTestChars,
',', SkipWhitespace());
510 std::vector<std::string> v = splitter;
511 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
519 TEST(Split, Basics) {
528 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
533 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
539 std::vector<std::string>
v;
542 EXPECT_THAT(v, ElementsAre(
"a",
"b",
"c"));
543 std::map<std::string, std::string> m;
545 EXPECT_EQ(2, m.size());
546 std::unordered_map<std::string, std::string> hm;
548 EXPECT_EQ(2, hm.size());
553 const char* ReturnConstCharP() {
return "Hello World"; }
554 char* ReturnCharP() {
return const_cast<char*
>(
"Hello World"); }
556 TEST(Split, AcceptsCertainTemporaries) {
557 std::vector<std::string>
v;
559 EXPECT_THAT(v, ElementsAre(
"Hello",
"World"));
561 EXPECT_THAT(v, ElementsAre(
"Hello",
"World"));
563 EXPECT_THAT(v, ElementsAre(
"Hello",
"World"));
566 TEST(Split, Temporary) {
570 const char input[] =
"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u";
572 <<
"Input should be larger than fits on the stack.";
576 std::string expected =
"a";
578 EXPECT_EQ(expected, letter);
581 EXPECT_EQ(
"v", expected);
587 EXPECT_EQ(expected, letter);
590 EXPECT_EQ(
"v", expected);
593 template <
typename T>
594 static std::unique_ptr<T> CopyToHeap(
const T&
value) {
595 return std::unique_ptr<T>(
new T(value));
598 TEST(Split, LvalueCaptureIsCopyable) {
599 std::string input =
"a,b";
601 auto stack_splitter = *heap_splitter;
602 heap_splitter.reset();
603 std::vector<std::string> result = stack_splitter;
604 EXPECT_THAT(result, testing::ElementsAre(
"a",
"b"));
607 TEST(Split, TemporaryCaptureIsCopyable) {
608 auto heap_splitter = CopyToHeap(
absl::StrSplit(std::string(
"a,b"),
','));
609 auto stack_splitter = *heap_splitter;
610 heap_splitter.reset();
611 std::vector<std::string> result = stack_splitter;
612 EXPECT_THAT(result, testing::ElementsAre(
"a",
"b"));
615 TEST(Split, SplitterIsCopyableAndMoveable) {
624 EXPECT_THAT(c, ElementsAre(
"foo"));
627 TEST(Split, StringDelimiter) {
630 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
634 std::vector<absl::string_view> v =
absl::StrSplit(
"a,b", std::string(
","));
635 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
639 std::vector<absl::string_view> v =
641 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
645 #if !defined(__cpp_char8_t) 646 #if defined(__clang__) 647 #pragma clang diagnostic push 648 #pragma clang diagnostic ignored "-Wc++2a-compat" 652 std::string utf8_string = u8
"\u03BA\u1F79\u03C3\u03BC\u03B5";
655 std::string to_split =
"a," + utf8_string;
657 EXPECT_THAT(v, ElementsAre(
"a", utf8_string));
662 std::string to_split =
"a," + utf8_string +
",b";
663 std::string unicode_delimiter =
"," + utf8_string +
",";
664 std::vector<absl::string_view> v =
666 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
671 std::vector<absl::string_view> v =
673 EXPECT_THAT(v, ElementsAre(
"Foo", u8
"h\u00E4llo", u8
"th\u4E1Ere"));
676 #if defined(__clang__) 677 #pragma clang diagnostic pop 679 #endif // !defined(__cpp_char8_t) 681 TEST(Split, EmptyStringDelimiter) {
684 EXPECT_THAT(v, ElementsAre(
""));
689 EXPECT_THAT(v, ElementsAre(
"a"));
694 EXPECT_THAT(v, ElementsAre(
"a",
"b"));
699 EXPECT_THAT(v, ElementsAre(
"a",
" ",
"b"));
703 TEST(Split, SubstrDelimiter) {
704 std::vector<absl::string_view> results;
708 EXPECT_THAT(results, ElementsAre(
""));
711 EXPECT_THAT(results, ElementsAre(
"",
""));
714 EXPECT_THAT(results, ElementsAre(
"ab"));
717 EXPECT_THAT(results, ElementsAre(
"ab",
""));
720 EXPECT_THAT(results, ElementsAre(
"ab/"));
723 EXPECT_THAT(results, ElementsAre(
"a/b"));
726 EXPECT_THAT(results, ElementsAre(
"a",
"b"));
729 EXPECT_THAT(results, ElementsAre(
"a",
"/b"));
732 EXPECT_THAT(results, ElementsAre(
"a",
"",
"b"));
735 TEST(Split, EmptyResults) {
736 std::vector<absl::string_view> results;
739 EXPECT_THAT(results, ElementsAre(
""));
742 EXPECT_THAT(results, ElementsAre(
"",
""));
745 EXPECT_THAT(results, ElementsAre(
"",
"cd"));
748 EXPECT_THAT(results, ElementsAre(
"ab",
"cd",
""));
751 EXPECT_THAT(results, ElementsAre(
"ab",
"",
"cd"));
754 EXPECT_THAT(results, ElementsAre(
"ab",
"",
""));
757 EXPECT_THAT(results, ElementsAre(
"ab",
"ab",
""));
760 EXPECT_THAT(results, ElementsAre(
"",
"",
"",
"",
""));
763 EXPECT_THAT(results, ElementsAre());
766 template <
typename Delimiter>
768 size_t starting_pos,
int expected_pos) {
771 expected_pos == found.
data() - text.
data();
779 template <
typename Delimiter>
781 const std::string leading_text =
",x,y,z,";
782 return IsFoundAtStartingPos(text, d, 0, expected_pos) &&
783 IsFoundAtStartingPos(leading_text + std::string(text), d,
784 leading_text.length(),
785 expected_pos + leading_text.length());
793 template <
typename Delimiter>
794 void TestComma(Delimiter d) {
795 EXPECT_TRUE(IsFoundAt(
",", d, 0));
796 EXPECT_TRUE(IsFoundAt(
"a,", d, 1));
797 EXPECT_TRUE(IsFoundAt(
",b", d, 0));
798 EXPECT_TRUE(IsFoundAt(
"a,b", d, 1));
799 EXPECT_TRUE(IsFoundAt(
"a,b,", d, 1));
800 EXPECT_TRUE(IsFoundAt(
"a,b,c", d, 1));
801 EXPECT_FALSE(IsFoundAt(
"", d, -1));
802 EXPECT_FALSE(IsFoundAt(
" ", d, -1));
803 EXPECT_FALSE(IsFoundAt(
"a", d, -1));
804 EXPECT_FALSE(IsFoundAt(
"a b c", d, -1));
805 EXPECT_FALSE(IsFoundAt(
"a;b;c", d, -1));
806 EXPECT_FALSE(IsFoundAt(
";", d, -1));
809 TEST(Delimiter, ByString) {
811 TestComma(ByString(
","));
814 ByString comma_string(
",");
815 TestComma(comma_string);
823 EXPECT_EQ(0, abc.find(
""));
825 EXPECT_FALSE(IsFoundAt(
"", empty, 0));
826 EXPECT_FALSE(IsFoundAt(
"a", empty, 0));
827 EXPECT_TRUE(IsFoundAt(
"ab", empty, 1));
828 EXPECT_TRUE(IsFoundAt(
"abc", empty, 1));
831 TEST(Split, ByChar) {
833 TestComma(ByChar(
','));
836 ByChar comma_char(
',');
837 TestComma(comma_char);
844 TEST(Delimiter, ByAnyChar) {
846 ByAnyChar one_delim(
",");
848 EXPECT_TRUE(IsFoundAt(
",", one_delim, 0));
849 EXPECT_TRUE(IsFoundAt(
"a,", one_delim, 1));
850 EXPECT_TRUE(IsFoundAt(
"a,b", one_delim, 1));
851 EXPECT_TRUE(IsFoundAt(
",b", one_delim, 0));
853 EXPECT_FALSE(IsFoundAt(
"", one_delim, -1));
854 EXPECT_FALSE(IsFoundAt(
" ", one_delim, -1));
855 EXPECT_FALSE(IsFoundAt(
"a", one_delim, -1));
856 EXPECT_FALSE(IsFoundAt(
"a;b;c", one_delim, -1));
857 EXPECT_FALSE(IsFoundAt(
";", one_delim, -1));
859 ByAnyChar two_delims(
",;");
861 EXPECT_TRUE(IsFoundAt(
",", two_delims, 0));
862 EXPECT_TRUE(IsFoundAt(
";", two_delims, 0));
863 EXPECT_TRUE(IsFoundAt(
",;", two_delims, 0));
864 EXPECT_TRUE(IsFoundAt(
";,", two_delims, 0));
865 EXPECT_TRUE(IsFoundAt(
",;b", two_delims, 0));
866 EXPECT_TRUE(IsFoundAt(
";,b", two_delims, 0));
867 EXPECT_TRUE(IsFoundAt(
"a;,", two_delims, 1));
868 EXPECT_TRUE(IsFoundAt(
"a,;", two_delims, 1));
869 EXPECT_TRUE(IsFoundAt(
"a;,b", two_delims, 1));
870 EXPECT_TRUE(IsFoundAt(
"a,;b", two_delims, 1));
872 EXPECT_FALSE(IsFoundAt(
"", two_delims, -1));
873 EXPECT_FALSE(IsFoundAt(
" ", two_delims, -1));
874 EXPECT_FALSE(IsFoundAt(
"a", two_delims, -1));
875 EXPECT_FALSE(IsFoundAt(
"a=b=c", two_delims, -1));
876 EXPECT_FALSE(IsFoundAt(
"=", two_delims, -1));
882 EXPECT_FALSE(IsFoundAt(
"", empty, 0));
883 EXPECT_FALSE(IsFoundAt(
"a", empty, 0));
884 EXPECT_TRUE(IsFoundAt(
"ab", empty, 1));
885 EXPECT_TRUE(IsFoundAt(
"abc", empty, 1));
892 TEST(Delimiter, ByLength) {
895 ByLength four_char_delim(4);
898 EXPECT_TRUE(IsFoundAt(
"abcde", four_char_delim, 4));
899 EXPECT_TRUE(IsFoundAt(
"abcdefghijklmnopqrstuvwxyz", four_char_delim, 4));
900 EXPECT_TRUE(IsFoundAt(
"a b,c\nd", four_char_delim, 4));
902 EXPECT_FALSE(IsFoundAt(
"", four_char_delim, 0));
903 EXPECT_FALSE(IsFoundAt(
"a", four_char_delim, 0));
904 EXPECT_FALSE(IsFoundAt(
"ab", four_char_delim, 0));
905 EXPECT_FALSE(IsFoundAt(
"abc", four_char_delim, 0));
906 EXPECT_FALSE(IsFoundAt(
"abcd", four_char_delim, 0));
909 TEST(Split, WorksWithLargeStrings) {
910 if (
sizeof(
size_t) > 4) {
911 std::string s((uint32_t{1} << 31) + 1,
'x');
914 EXPECT_EQ(2, v.size());
917 EXPECT_EQ(
'x', v[0][0]);
918 EXPECT_EQ(
'x', v[0][1]);
919 EXPECT_EQ(
'x', v[0][3]);
924 TEST(SplitInternalTest, TypeTraits) {
936 std::initializer_list<int>>::
value));
strings_internal::MaxSplitsImpl< typename strings_internal::SelectDelimiter< Delimiter >::type > MaxSplits(Delimiter delimiter, int limit)
constexpr size_type size() const noexcept
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
static PerThreadSynch * Skip(PerThreadSynch *x)
#define ABSL_ARRAYSIZE(array)
constexpr const_pointer data() const noexcept
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept