str_split_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/str_split.h"
00016 
00017 #include <deque>
00018 #include <initializer_list>
00019 #include <list>
00020 #include <map>
00021 #include <memory>
00022 #include <string>
00023 #include <type_traits>
00024 #include <unordered_map>
00025 #include <unordered_set>
00026 #include <vector>
00027 
00028 #include "gmock/gmock.h"
00029 #include "gtest/gtest.h"
00030 #include "absl/base/dynamic_annotations.h"  // for RunningOnValgrind
00031 #include "absl/base/macros.h"
00032 #include "absl/strings/numbers.h"
00033 
00034 namespace {
00035 
00036 using ::testing::ElementsAre;
00037 using ::testing::Pair;
00038 using ::testing::UnorderedElementsAre;
00039 
00040 TEST(Split, TraitsTest) {
00041   static_assert(!absl::strings_internal::SplitterIsConvertibleTo<int>::value,
00042                 "");
00043   static_assert(
00044       !absl::strings_internal::SplitterIsConvertibleTo<std::string>::value, "");
00045   static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00046                     std::vector<std::string>>::value,
00047                 "");
00048   static_assert(
00049       !absl::strings_internal::SplitterIsConvertibleTo<std::vector<int>>::value,
00050       "");
00051   static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00052                     std::vector<absl::string_view>>::value,
00053                 "");
00054   static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00055                     std::map<std::string, std::string>>::value,
00056                 "");
00057   static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00058                     std::map<absl::string_view, absl::string_view>>::value,
00059                 "");
00060   static_assert(!absl::strings_internal::SplitterIsConvertibleTo<
00061                     std::map<int, std::string>>::value,
00062                 "");
00063   static_assert(!absl::strings_internal::SplitterIsConvertibleTo<
00064                     std::map<std::string, int>>::value,
00065                 "");
00066 }
00067 
00068 // This tests the overall split API, which is made up of the absl::StrSplit()
00069 // function and the Delimiter objects in the absl:: namespace.
00070 // This TEST macro is outside of any namespace to require full specification of
00071 // namespaces just like callers will need to use.
00072 TEST(Split, APIExamples) {
00073   {
00074     // Passes std::string delimiter. Assumes the default of ByString.
00075     std::vector<std::string> v = absl::StrSplit("a,b,c", ",");  // NOLINT
00076     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00077 
00078     // Equivalent to...
00079     using absl::ByString;
00080     v = absl::StrSplit("a,b,c", ByString(","));
00081     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00082 
00083     // Equivalent to...
00084     EXPECT_THAT(absl::StrSplit("a,b,c", ByString(",")),
00085                 ElementsAre("a", "b", "c"));
00086   }
00087 
00088   {
00089     // Same as above, but using a single character as the delimiter.
00090     std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
00091     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00092 
00093     // Equivalent to...
00094     using absl::ByChar;
00095     v = absl::StrSplit("a,b,c", ByChar(','));
00096     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00097   }
00098 
00099   {
00100     // Uses the Literal std::string "=>" as the delimiter.
00101     const std::vector<std::string> v = absl::StrSplit("a=>b=>c", "=>");
00102     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00103   }
00104 
00105   {
00106     // The substrings are returned as string_views, eliminating copying.
00107     std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
00108     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00109   }
00110 
00111   {
00112     // Leading and trailing empty substrings.
00113     std::vector<std::string> v = absl::StrSplit(",a,b,c,", ',');
00114     EXPECT_THAT(v, ElementsAre("", "a", "b", "c", ""));
00115   }
00116 
00117   {
00118     // Splits on a delimiter that is not found.
00119     std::vector<std::string> v = absl::StrSplit("abc", ',');
00120     EXPECT_THAT(v, ElementsAre("abc"));
00121   }
00122 
00123   {
00124     // Splits the input std::string into individual characters by using an empty
00125     // std::string as the delimiter.
00126     std::vector<std::string> v = absl::StrSplit("abc", "");
00127     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00128   }
00129 
00130   {
00131     // Splits std::string data with embedded NUL characters, using NUL as the
00132     // delimiter. A simple delimiter of "\0" doesn't work because strlen() will
00133     // say that's the empty std::string when constructing the absl::string_view
00134     // delimiter. Instead, a non-empty std::string containing NUL can be used as the
00135     // delimiter.
00136     std::string embedded_nulls("a\0b\0c", 5);
00137     std::string null_delim("\0", 1);
00138     std::vector<std::string> v = absl::StrSplit(embedded_nulls, null_delim);
00139     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00140   }
00141 
00142   {
00143     // Stores first two split strings as the members in a std::pair.
00144     std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
00145     EXPECT_EQ("a", p.first);
00146     EXPECT_EQ("b", p.second);
00147     // "c" is omitted because std::pair can hold only two elements.
00148   }
00149 
00150   {
00151     // Results stored in std::set<std::string>
00152     std::set<std::string> v = absl::StrSplit("a,b,c,a,b,c,a,b,c", ',');
00153     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00154   }
00155 
00156   {
00157     // Uses a non-const char* delimiter.
00158     char a[] = ",";
00159     char* d = a + 0;
00160     std::vector<std::string> v = absl::StrSplit("a,b,c", d);
00161     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00162   }
00163 
00164   {
00165     // Results split using either of , or ;
00166     using absl::ByAnyChar;
00167     std::vector<std::string> v = absl::StrSplit("a,b;c", ByAnyChar(",;"));
00168     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00169   }
00170 
00171   {
00172     // Uses the SkipWhitespace predicate.
00173     using absl::SkipWhitespace;
00174     std::vector<std::string> v =
00175         absl::StrSplit(" a , ,,b,", ',', SkipWhitespace());
00176     EXPECT_THAT(v, ElementsAre(" a ", "b"));
00177   }
00178 
00179   {
00180     // Uses the ByLength delimiter.
00181     using absl::ByLength;
00182     std::vector<std::string> v = absl::StrSplit("abcdefg", ByLength(3));
00183     EXPECT_THAT(v, ElementsAre("abc", "def", "g"));
00184   }
00185 
00186   {
00187     // Different forms of initialization / conversion.
00188     std::vector<std::string> v1 = absl::StrSplit("a,b,c", ',');
00189     EXPECT_THAT(v1, ElementsAre("a", "b", "c"));
00190     std::vector<std::string> v2(absl::StrSplit("a,b,c", ','));
00191     EXPECT_THAT(v2, ElementsAre("a", "b", "c"));
00192     auto v3 = std::vector<std::string>(absl::StrSplit("a,b,c", ','));
00193     EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
00194     v3 = absl::StrSplit("a,b,c", ',');
00195     EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
00196   }
00197 
00198   {
00199     // Results stored in a std::map.
00200     std::map<std::string, std::string> m = absl::StrSplit("a,1,b,2,a,3", ',');
00201     EXPECT_EQ(2, m.size());
00202     EXPECT_EQ("3", m["a"]);
00203     EXPECT_EQ("2", m["b"]);
00204   }
00205 
00206   {
00207     // Results stored in a std::multimap.
00208     std::multimap<std::string, std::string> m =
00209         absl::StrSplit("a,1,b,2,a,3", ',');
00210     EXPECT_EQ(3, m.size());
00211     auto it = m.find("a");
00212     EXPECT_EQ("1", it->second);
00213     ++it;
00214     EXPECT_EQ("3", it->second);
00215     it = m.find("b");
00216     EXPECT_EQ("2", it->second);
00217   }
00218 
00219   {
00220     // Demonstrates use in a range-based for loop in C++11.
00221     std::string s = "x,x,x,x,x,x,x";
00222     for (absl::string_view sp : absl::StrSplit(s, ',')) {
00223       EXPECT_EQ("x", sp);
00224     }
00225   }
00226 
00227   {
00228     // Demonstrates use with a Predicate in a range-based for loop.
00229     using absl::SkipWhitespace;
00230     std::string s = " ,x,,x,,x,x,x,,";
00231     for (absl::string_view sp : absl::StrSplit(s, ',', SkipWhitespace())) {
00232       EXPECT_EQ("x", sp);
00233     }
00234   }
00235 
00236   {
00237     // Demonstrates a "smart" split to std::map using two separate calls to
00238     // absl::StrSplit. One call to split the records, and another call to split
00239     // the keys and values. This also uses the Limit delimiter so that the
00240     // std::string "a=b=c" will split to "a" -> "b=c".
00241     std::map<std::string, std::string> m;
00242     for (absl::string_view sp : absl::StrSplit("a=b=c,d=e,f=,g", ',')) {
00243       m.insert(absl::StrSplit(sp, absl::MaxSplits('=', 1)));
00244     }
00245     EXPECT_EQ("b=c", m.find("a")->second);
00246     EXPECT_EQ("e", m.find("d")->second);
00247     EXPECT_EQ("", m.find("f")->second);
00248     EXPECT_EQ("", m.find("g")->second);
00249   }
00250 }
00251 
00252 //
00253 // Tests for SplitIterator
00254 //
00255 
00256 TEST(SplitIterator, Basics) {
00257   auto splitter = absl::StrSplit("a,b", ',');
00258   auto it = splitter.begin();
00259   auto end = splitter.end();
00260 
00261   EXPECT_NE(it, end);
00262   EXPECT_EQ("a", *it);  // tests dereference
00263   ++it;                 // tests preincrement
00264   EXPECT_NE(it, end);
00265   EXPECT_EQ("b",
00266             std::string(it->data(), it->size()));  // tests dereference as ptr
00267   it++;                                            // tests postincrement
00268   EXPECT_EQ(it, end);
00269 }
00270 
00271 // Simple Predicate to skip a particular string.
00272 class Skip {
00273  public:
00274   explicit Skip(const std::string& s) : s_(s) {}
00275   bool operator()(absl::string_view sp) { return sp != s_; }
00276 
00277  private:
00278   std::string s_;
00279 };
00280 
00281 TEST(SplitIterator, Predicate) {
00282   auto splitter = absl::StrSplit("a,b,c", ',', Skip("b"));
00283   auto it = splitter.begin();
00284   auto end = splitter.end();
00285 
00286   EXPECT_NE(it, end);
00287   EXPECT_EQ("a", *it);  // tests dereference
00288   ++it;                 // tests preincrement -- "b" should be skipped here.
00289   EXPECT_NE(it, end);
00290   EXPECT_EQ("c",
00291             std::string(it->data(), it->size()));  // tests dereference as ptr
00292   it++;                                            // tests postincrement
00293   EXPECT_EQ(it, end);
00294 }
00295 
00296 TEST(SplitIterator, EdgeCases) {
00297   // Expected input and output, assuming a delimiter of ','
00298   struct {
00299     std::string in;
00300     std::vector<std::string> expect;
00301   } specs[] = {
00302       {"", {""}},
00303       {"foo", {"foo"}},
00304       {",", {"", ""}},
00305       {",foo", {"", "foo"}},
00306       {"foo,", {"foo", ""}},
00307       {",foo,", {"", "foo", ""}},
00308       {"foo,bar", {"foo", "bar"}},
00309   };
00310 
00311   for (const auto& spec : specs) {
00312     SCOPED_TRACE(spec.in);
00313     auto splitter = absl::StrSplit(spec.in, ',');
00314     auto it = splitter.begin();
00315     auto end = splitter.end();
00316     for (const auto& expected : spec.expect) {
00317       EXPECT_NE(it, end);
00318       EXPECT_EQ(expected, *it++);
00319     }
00320     EXPECT_EQ(it, end);
00321   }
00322 }
00323 
00324 TEST(Splitter, Const) {
00325   const auto splitter = absl::StrSplit("a,b,c", ',');
00326   EXPECT_THAT(splitter, ElementsAre("a", "b", "c"));
00327 }
00328 
00329 TEST(Split, EmptyAndNull) {
00330   // Attention: Splitting a null absl::string_view is different than splitting
00331   // an empty absl::string_view even though both string_views are considered
00332   // equal. This behavior is likely surprising and undesirable. However, to
00333   // maintain backward compatibility, there is a small "hack" in
00334   // str_split_internal.h that preserves this behavior. If that behavior is ever
00335   // changed/fixed, this test will need to be updated.
00336   EXPECT_THAT(absl::StrSplit(absl::string_view(""), '-'), ElementsAre(""));
00337   EXPECT_THAT(absl::StrSplit(absl::string_view(), '-'), ElementsAre());
00338 }
00339 
00340 TEST(SplitIterator, EqualityAsEndCondition) {
00341   auto splitter = absl::StrSplit("a,b,c", ',');
00342   auto it = splitter.begin();
00343   auto it2 = it;
00344 
00345   // Increments it2 twice to point to "c" in the input text.
00346   ++it2;
00347   ++it2;
00348   EXPECT_EQ("c", *it2);
00349 
00350   // This test uses a non-end SplitIterator as the terminating condition in a
00351   // for loop. This relies on SplitIterator equality for non-end SplitIterators
00352   // working correctly. At this point it2 points to "c", and we use that as the
00353   // "end" condition in this test.
00354   std::vector<absl::string_view> v;
00355   for (; it != it2; ++it) {
00356     v.push_back(*it);
00357   }
00358   EXPECT_THAT(v, ElementsAre("a", "b"));
00359 }
00360 
00361 //
00362 // Tests for Splitter
00363 //
00364 
00365 TEST(Splitter, RangeIterators) {
00366   auto splitter = absl::StrSplit("a,b,c", ',');
00367   std::vector<absl::string_view> output;
00368   for (const absl::string_view p : splitter) {
00369     output.push_back(p);
00370   }
00371   EXPECT_THAT(output, ElementsAre("a", "b", "c"));
00372 }
00373 
00374 // Some template functions for use in testing conversion operators
00375 template <typename ContainerType, typename Splitter>
00376 void TestConversionOperator(const Splitter& splitter) {
00377   ContainerType output = splitter;
00378   EXPECT_THAT(output, UnorderedElementsAre("a", "b", "c", "d"));
00379 }
00380 
00381 template <typename MapType, typename Splitter>
00382 void TestMapConversionOperator(const Splitter& splitter) {
00383   MapType m = splitter;
00384   EXPECT_THAT(m, UnorderedElementsAre(Pair("a", "b"), Pair("c", "d")));
00385 }
00386 
00387 template <typename FirstType, typename SecondType, typename Splitter>
00388 void TestPairConversionOperator(const Splitter& splitter) {
00389   std::pair<FirstType, SecondType> p = splitter;
00390   EXPECT_EQ(p, (std::pair<FirstType, SecondType>("a", "b")));
00391 }
00392 
00393 TEST(Splitter, ConversionOperator) {
00394   auto splitter = absl::StrSplit("a,b,c,d", ',');
00395 
00396   TestConversionOperator<std::vector<absl::string_view>>(splitter);
00397   TestConversionOperator<std::vector<std::string>>(splitter);
00398   TestConversionOperator<std::list<absl::string_view>>(splitter);
00399   TestConversionOperator<std::list<std::string>>(splitter);
00400   TestConversionOperator<std::deque<absl::string_view>>(splitter);
00401   TestConversionOperator<std::deque<std::string>>(splitter);
00402   TestConversionOperator<std::set<absl::string_view>>(splitter);
00403   TestConversionOperator<std::set<std::string>>(splitter);
00404   TestConversionOperator<std::multiset<absl::string_view>>(splitter);
00405   TestConversionOperator<std::multiset<std::string>>(splitter);
00406   TestConversionOperator<std::unordered_set<std::string>>(splitter);
00407 
00408   // Tests conversion to map-like objects.
00409 
00410   TestMapConversionOperator<std::map<absl::string_view, absl::string_view>>(
00411       splitter);
00412   TestMapConversionOperator<std::map<absl::string_view, std::string>>(splitter);
00413   TestMapConversionOperator<std::map<std::string, absl::string_view>>(splitter);
00414   TestMapConversionOperator<std::map<std::string, std::string>>(splitter);
00415   TestMapConversionOperator<
00416       std::multimap<absl::string_view, absl::string_view>>(splitter);
00417   TestMapConversionOperator<std::multimap<absl::string_view, std::string>>(
00418       splitter);
00419   TestMapConversionOperator<std::multimap<std::string, absl::string_view>>(
00420       splitter);
00421   TestMapConversionOperator<std::multimap<std::string, std::string>>(splitter);
00422   TestMapConversionOperator<std::unordered_map<std::string, std::string>>(
00423       splitter);
00424 
00425   // Tests conversion to std::pair
00426 
00427   TestPairConversionOperator<absl::string_view, absl::string_view>(splitter);
00428   TestPairConversionOperator<absl::string_view, std::string>(splitter);
00429   TestPairConversionOperator<std::string, absl::string_view>(splitter);
00430   TestPairConversionOperator<std::string, std::string>(splitter);
00431 }
00432 
00433 // A few additional tests for conversion to std::pair. This conversion is
00434 // different from others because a std::pair always has exactly two elements:
00435 // .first and .second. The split has to work even when the split has
00436 // less-than, equal-to, and more-than 2 strings.
00437 TEST(Splitter, ToPair) {
00438   {
00439     // Empty std::string
00440     std::pair<std::string, std::string> p = absl::StrSplit("", ',');
00441     EXPECT_EQ("", p.first);
00442     EXPECT_EQ("", p.second);
00443   }
00444 
00445   {
00446     // Only first
00447     std::pair<std::string, std::string> p = absl::StrSplit("a", ',');
00448     EXPECT_EQ("a", p.first);
00449     EXPECT_EQ("", p.second);
00450   }
00451 
00452   {
00453     // Only second
00454     std::pair<std::string, std::string> p = absl::StrSplit(",b", ',');
00455     EXPECT_EQ("", p.first);
00456     EXPECT_EQ("b", p.second);
00457   }
00458 
00459   {
00460     // First and second.
00461     std::pair<std::string, std::string> p = absl::StrSplit("a,b", ',');
00462     EXPECT_EQ("a", p.first);
00463     EXPECT_EQ("b", p.second);
00464   }
00465 
00466   {
00467     // First and second and then more stuff that will be ignored.
00468     std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
00469     EXPECT_EQ("a", p.first);
00470     EXPECT_EQ("b", p.second);
00471     // "c" is omitted.
00472   }
00473 }
00474 
00475 TEST(Splitter, Predicates) {
00476   static const char kTestChars[] = ",a, ,b,";
00477   using absl::AllowEmpty;
00478   using absl::SkipEmpty;
00479   using absl::SkipWhitespace;
00480 
00481   {
00482     // No predicate. Does not skip empties.
00483     auto splitter = absl::StrSplit(kTestChars, ',');
00484     std::vector<std::string> v = splitter;
00485     EXPECT_THAT(v, ElementsAre("", "a", " ", "b", ""));
00486   }
00487 
00488   {
00489     // Allows empty strings. Same behavior as no predicate at all.
00490     auto splitter = absl::StrSplit(kTestChars, ',', AllowEmpty());
00491     std::vector<std::string> v_allowempty = splitter;
00492     EXPECT_THAT(v_allowempty, ElementsAre("", "a", " ", "b", ""));
00493 
00494     // Ensures AllowEmpty equals the behavior with no predicate.
00495     auto splitter_nopredicate = absl::StrSplit(kTestChars, ',');
00496     std::vector<std::string> v_nopredicate = splitter_nopredicate;
00497     EXPECT_EQ(v_allowempty, v_nopredicate);
00498   }
00499 
00500   {
00501     // Skips empty strings.
00502     auto splitter = absl::StrSplit(kTestChars, ',', SkipEmpty());
00503     std::vector<std::string> v = splitter;
00504     EXPECT_THAT(v, ElementsAre("a", " ", "b"));
00505   }
00506 
00507   {
00508     // Skips empty and all-whitespace strings.
00509     auto splitter = absl::StrSplit(kTestChars, ',', SkipWhitespace());
00510     std::vector<std::string> v = splitter;
00511     EXPECT_THAT(v, ElementsAre("a", "b"));
00512   }
00513 }
00514 
00515 //
00516 // Tests for StrSplit()
00517 //
00518 
00519 TEST(Split, Basics) {
00520   {
00521     // Doesn't really do anything useful because the return value is ignored,
00522     // but it should work.
00523     absl::StrSplit("a,b,c", ',');
00524   }
00525 
00526   {
00527     std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
00528     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00529   }
00530 
00531   {
00532     std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
00533     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00534   }
00535 
00536   {
00537     // Ensures that assignment works. This requires a little extra work with
00538     // C++11 because of overloads with initializer_list.
00539     std::vector<std::string> v;
00540     v = absl::StrSplit("a,b,c", ',');
00541 
00542     EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00543     std::map<std::string, std::string> m;
00544     m = absl::StrSplit("a,b,c", ',');
00545     EXPECT_EQ(2, m.size());
00546     std::unordered_map<std::string, std::string> hm;
00547     hm = absl::StrSplit("a,b,c", ',');
00548     EXPECT_EQ(2, hm.size());
00549   }
00550 }
00551 
00552 absl::string_view ReturnStringView() { return "Hello World"; }
00553 const char* ReturnConstCharP() { return "Hello World"; }
00554 char* ReturnCharP() { return const_cast<char*>("Hello World"); }
00555 
00556 TEST(Split, AcceptsCertainTemporaries) {
00557   std::vector<std::string> v;
00558   v = absl::StrSplit(ReturnStringView(), ' ');
00559   EXPECT_THAT(v, ElementsAre("Hello", "World"));
00560   v = absl::StrSplit(ReturnConstCharP(), ' ');
00561   EXPECT_THAT(v, ElementsAre("Hello", "World"));
00562   v = absl::StrSplit(ReturnCharP(), ' ');
00563   EXPECT_THAT(v, ElementsAre("Hello", "World"));
00564 }
00565 
00566 TEST(Split, Temporary) {
00567   // Use a std::string longer than the SSO length, so that when the temporary is
00568   // destroyed, if the splitter keeps a reference to the std::string's contents,
00569   // it'll reference freed memory instead of just dead on-stack memory.
00570   const char input[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u";
00571   EXPECT_LT(sizeof(std::string), ABSL_ARRAYSIZE(input))
00572       << "Input should be larger than fits on the stack.";
00573 
00574   // This happens more often in C++11 as part of a range-based for loop.
00575   auto splitter = absl::StrSplit(std::string(input), ',');
00576   std::string expected = "a";
00577   for (absl::string_view letter : splitter) {
00578     EXPECT_EQ(expected, letter);
00579     ++expected[0];
00580   }
00581   EXPECT_EQ("v", expected);
00582 
00583   // This happens more often in C++11 as part of a range-based for loop.
00584   auto std_splitter = absl::StrSplit(std::string(input), ',');
00585   expected = "a";
00586   for (absl::string_view letter : std_splitter) {
00587     EXPECT_EQ(expected, letter);
00588     ++expected[0];
00589   }
00590   EXPECT_EQ("v", expected);
00591 }
00592 
00593 template <typename T>
00594 static std::unique_ptr<T> CopyToHeap(const T& value) {
00595   return std::unique_ptr<T>(new T(value));
00596 }
00597 
00598 TEST(Split, LvalueCaptureIsCopyable) {
00599   std::string input = "a,b";
00600   auto heap_splitter = CopyToHeap(absl::StrSplit(input, ','));
00601   auto stack_splitter = *heap_splitter;
00602   heap_splitter.reset();
00603   std::vector<std::string> result = stack_splitter;
00604   EXPECT_THAT(result, testing::ElementsAre("a", "b"));
00605 }
00606 
00607 TEST(Split, TemporaryCaptureIsCopyable) {
00608   auto heap_splitter = CopyToHeap(absl::StrSplit(std::string("a,b"), ','));
00609   auto stack_splitter = *heap_splitter;
00610   heap_splitter.reset();
00611   std::vector<std::string> result = stack_splitter;
00612   EXPECT_THAT(result, testing::ElementsAre("a", "b"));
00613 }
00614 
00615 TEST(Split, SplitterIsCopyableAndMoveable) {
00616   auto a = absl::StrSplit("foo", '-');
00617 
00618   // Ensures that the following expressions compile.
00619   auto b = a;             // Copy construct
00620   auto c = std::move(a);  // Move construct
00621   b = c;                  // Copy assign
00622   c = std::move(b);       // Move assign
00623 
00624   EXPECT_THAT(c, ElementsAre("foo"));
00625 }
00626 
00627 TEST(Split, StringDelimiter) {
00628   {
00629     std::vector<absl::string_view> v = absl::StrSplit("a,b", ',');
00630     EXPECT_THAT(v, ElementsAre("a", "b"));
00631   }
00632 
00633   {
00634     std::vector<absl::string_view> v = absl::StrSplit("a,b", std::string(","));
00635     EXPECT_THAT(v, ElementsAre("a", "b"));
00636   }
00637 
00638   {
00639     std::vector<absl::string_view> v =
00640         absl::StrSplit("a,b", absl::string_view(","));
00641     EXPECT_THAT(v, ElementsAre("a", "b"));
00642   }
00643 }
00644 
00645 #if !defined(__cpp_char8_t)
00646 #if defined(__clang__)
00647 #pragma clang diagnostic push
00648 #pragma clang diagnostic ignored "-Wc++2a-compat"
00649 #endif
00650 TEST(Split, UTF8) {
00651   // Tests splitting utf8 strings and utf8 delimiters.
00652   std::string utf8_string = u8"\u03BA\u1F79\u03C3\u03BC\u03B5";
00653   {
00654     // A utf8 input std::string with an ascii delimiter.
00655     std::string to_split = "a," + utf8_string;
00656     std::vector<absl::string_view> v = absl::StrSplit(to_split, ',');
00657     EXPECT_THAT(v, ElementsAre("a", utf8_string));
00658   }
00659 
00660   {
00661     // A utf8 input std::string and a utf8 delimiter.
00662     std::string to_split = "a," + utf8_string + ",b";
00663     std::string unicode_delimiter = "," + utf8_string + ",";
00664     std::vector<absl::string_view> v =
00665         absl::StrSplit(to_split, unicode_delimiter);
00666     EXPECT_THAT(v, ElementsAre("a", "b"));
00667   }
00668 
00669   {
00670     // A utf8 input std::string and ByAnyChar with ascii chars.
00671     std::vector<absl::string_view> v =
00672         absl::StrSplit(u8"Foo h\u00E4llo th\u4E1Ere", absl::ByAnyChar(" \t"));
00673     EXPECT_THAT(v, ElementsAre("Foo", u8"h\u00E4llo", u8"th\u4E1Ere"));
00674   }
00675 }
00676 #if defined(__clang__)
00677 #pragma clang diagnostic pop
00678 #endif
00679 #endif  // !defined(__cpp_char8_t)
00680 
00681 TEST(Split, EmptyStringDelimiter) {
00682   {
00683     std::vector<std::string> v = absl::StrSplit("", "");
00684     EXPECT_THAT(v, ElementsAre(""));
00685   }
00686 
00687   {
00688     std::vector<std::string> v = absl::StrSplit("a", "");
00689     EXPECT_THAT(v, ElementsAre("a"));
00690   }
00691 
00692   {
00693     std::vector<std::string> v = absl::StrSplit("ab", "");
00694     EXPECT_THAT(v, ElementsAre("a", "b"));
00695   }
00696 
00697   {
00698     std::vector<std::string> v = absl::StrSplit("a b", "");
00699     EXPECT_THAT(v, ElementsAre("a", " ", "b"));
00700   }
00701 }
00702 
00703 TEST(Split, SubstrDelimiter) {
00704   std::vector<absl::string_view> results;
00705   absl::string_view delim("//");
00706 
00707   results = absl::StrSplit("", delim);
00708   EXPECT_THAT(results, ElementsAre(""));
00709 
00710   results = absl::StrSplit("//", delim);
00711   EXPECT_THAT(results, ElementsAre("", ""));
00712 
00713   results = absl::StrSplit("ab", delim);
00714   EXPECT_THAT(results, ElementsAre("ab"));
00715 
00716   results = absl::StrSplit("ab//", delim);
00717   EXPECT_THAT(results, ElementsAre("ab", ""));
00718 
00719   results = absl::StrSplit("ab/", delim);
00720   EXPECT_THAT(results, ElementsAre("ab/"));
00721 
00722   results = absl::StrSplit("a/b", delim);
00723   EXPECT_THAT(results, ElementsAre("a/b"));
00724 
00725   results = absl::StrSplit("a//b", delim);
00726   EXPECT_THAT(results, ElementsAre("a", "b"));
00727 
00728   results = absl::StrSplit("a///b", delim);
00729   EXPECT_THAT(results, ElementsAre("a", "/b"));
00730 
00731   results = absl::StrSplit("a////b", delim);
00732   EXPECT_THAT(results, ElementsAre("a", "", "b"));
00733 }
00734 
00735 TEST(Split, EmptyResults) {
00736   std::vector<absl::string_view> results;
00737 
00738   results = absl::StrSplit("", '#');
00739   EXPECT_THAT(results, ElementsAre(""));
00740 
00741   results = absl::StrSplit("#", '#');
00742   EXPECT_THAT(results, ElementsAre("", ""));
00743 
00744   results = absl::StrSplit("#cd", '#');
00745   EXPECT_THAT(results, ElementsAre("", "cd"));
00746 
00747   results = absl::StrSplit("ab#cd#", '#');
00748   EXPECT_THAT(results, ElementsAre("ab", "cd", ""));
00749 
00750   results = absl::StrSplit("ab##cd", '#');
00751   EXPECT_THAT(results, ElementsAre("ab", "", "cd"));
00752 
00753   results = absl::StrSplit("ab##", '#');
00754   EXPECT_THAT(results, ElementsAre("ab", "", ""));
00755 
00756   results = absl::StrSplit("ab#ab#", '#');
00757   EXPECT_THAT(results, ElementsAre("ab", "ab", ""));
00758 
00759   results = absl::StrSplit("aaaa", 'a');
00760   EXPECT_THAT(results, ElementsAre("", "", "", "", ""));
00761 
00762   results = absl::StrSplit("", '#', absl::SkipEmpty());
00763   EXPECT_THAT(results, ElementsAre());
00764 }
00765 
00766 template <typename Delimiter>
00767 static bool IsFoundAtStartingPos(absl::string_view text, Delimiter d,
00768                                  size_t starting_pos, int expected_pos) {
00769   absl::string_view found = d.Find(text, starting_pos);
00770   return found.data() != text.data() + text.size() &&
00771          expected_pos == found.data() - text.data();
00772 }
00773 
00774 // Helper function for testing Delimiter objects. Returns true if the given
00775 // Delimiter is found in the given string at the given position. This function
00776 // tests two cases:
00777 //   1. The actual text given, staring at position 0
00778 //   2. The text given with leading padding that should be ignored
00779 template <typename Delimiter>
00780 static bool IsFoundAt(absl::string_view text, Delimiter d, int expected_pos) {
00781   const std::string leading_text = ",x,y,z,";
00782   return IsFoundAtStartingPos(text, d, 0, expected_pos) &&
00783          IsFoundAtStartingPos(leading_text + std::string(text), d,
00784                               leading_text.length(),
00785                               expected_pos + leading_text.length());
00786 }
00787 
00788 //
00789 // Tests for ByString
00790 //
00791 
00792 // Tests using any delimiter that represents a single comma.
00793 template <typename Delimiter>
00794 void TestComma(Delimiter d) {
00795   EXPECT_TRUE(IsFoundAt(",", d, 0));
00796   EXPECT_TRUE(IsFoundAt("a,", d, 1));
00797   EXPECT_TRUE(IsFoundAt(",b", d, 0));
00798   EXPECT_TRUE(IsFoundAt("a,b", d, 1));
00799   EXPECT_TRUE(IsFoundAt("a,b,", d, 1));
00800   EXPECT_TRUE(IsFoundAt("a,b,c", d, 1));
00801   EXPECT_FALSE(IsFoundAt("", d, -1));
00802   EXPECT_FALSE(IsFoundAt(" ", d, -1));
00803   EXPECT_FALSE(IsFoundAt("a", d, -1));
00804   EXPECT_FALSE(IsFoundAt("a b c", d, -1));
00805   EXPECT_FALSE(IsFoundAt("a;b;c", d, -1));
00806   EXPECT_FALSE(IsFoundAt(";", d, -1));
00807 }
00808 
00809 TEST(Delimiter, ByString) {
00810   using absl::ByString;
00811   TestComma(ByString(","));
00812 
00813   // Works as named variable.
00814   ByString comma_string(",");
00815   TestComma(comma_string);
00816 
00817   // The first occurrence of empty std::string ("") in a std::string is at position 0.
00818   // There is a test below that demonstrates this for absl::string_view::find().
00819   // If the ByString delimiter returned position 0 for this, there would
00820   // be an infinite loop in the SplitIterator code. To avoid this, empty std::string
00821   // is a special case in that it always returns the item at position 1.
00822   absl::string_view abc("abc");
00823   EXPECT_EQ(0, abc.find(""));  // "" is found at position 0
00824   ByString empty("");
00825   EXPECT_FALSE(IsFoundAt("", empty, 0));
00826   EXPECT_FALSE(IsFoundAt("a", empty, 0));
00827   EXPECT_TRUE(IsFoundAt("ab", empty, 1));
00828   EXPECT_TRUE(IsFoundAt("abc", empty, 1));
00829 }
00830 
00831 TEST(Split, ByChar) {
00832   using absl::ByChar;
00833   TestComma(ByChar(','));
00834 
00835   // Works as named variable.
00836   ByChar comma_char(',');
00837   TestComma(comma_char);
00838 }
00839 
00840 //
00841 // Tests for ByAnyChar
00842 //
00843 
00844 TEST(Delimiter, ByAnyChar) {
00845   using absl::ByAnyChar;
00846   ByAnyChar one_delim(",");
00847   // Found
00848   EXPECT_TRUE(IsFoundAt(",", one_delim, 0));
00849   EXPECT_TRUE(IsFoundAt("a,", one_delim, 1));
00850   EXPECT_TRUE(IsFoundAt("a,b", one_delim, 1));
00851   EXPECT_TRUE(IsFoundAt(",b", one_delim, 0));
00852   // Not found
00853   EXPECT_FALSE(IsFoundAt("", one_delim, -1));
00854   EXPECT_FALSE(IsFoundAt(" ", one_delim, -1));
00855   EXPECT_FALSE(IsFoundAt("a", one_delim, -1));
00856   EXPECT_FALSE(IsFoundAt("a;b;c", one_delim, -1));
00857   EXPECT_FALSE(IsFoundAt(";", one_delim, -1));
00858 
00859   ByAnyChar two_delims(",;");
00860   // Found
00861   EXPECT_TRUE(IsFoundAt(",", two_delims, 0));
00862   EXPECT_TRUE(IsFoundAt(";", two_delims, 0));
00863   EXPECT_TRUE(IsFoundAt(",;", two_delims, 0));
00864   EXPECT_TRUE(IsFoundAt(";,", two_delims, 0));
00865   EXPECT_TRUE(IsFoundAt(",;b", two_delims, 0));
00866   EXPECT_TRUE(IsFoundAt(";,b", two_delims, 0));
00867   EXPECT_TRUE(IsFoundAt("a;,", two_delims, 1));
00868   EXPECT_TRUE(IsFoundAt("a,;", two_delims, 1));
00869   EXPECT_TRUE(IsFoundAt("a;,b", two_delims, 1));
00870   EXPECT_TRUE(IsFoundAt("a,;b", two_delims, 1));
00871   // Not found
00872   EXPECT_FALSE(IsFoundAt("", two_delims, -1));
00873   EXPECT_FALSE(IsFoundAt(" ", two_delims, -1));
00874   EXPECT_FALSE(IsFoundAt("a", two_delims, -1));
00875   EXPECT_FALSE(IsFoundAt("a=b=c", two_delims, -1));
00876   EXPECT_FALSE(IsFoundAt("=", two_delims, -1));
00877 
00878   // ByAnyChar behaves just like ByString when given a delimiter of empty
00879   // std::string. That is, it always returns a zero-length absl::string_view
00880   // referring to the item at position 1, not position 0.
00881   ByAnyChar empty("");
00882   EXPECT_FALSE(IsFoundAt("", empty, 0));
00883   EXPECT_FALSE(IsFoundAt("a", empty, 0));
00884   EXPECT_TRUE(IsFoundAt("ab", empty, 1));
00885   EXPECT_TRUE(IsFoundAt("abc", empty, 1));
00886 }
00887 
00888 //
00889 // Tests for ByLength
00890 //
00891 
00892 TEST(Delimiter, ByLength) {
00893   using absl::ByLength;
00894 
00895   ByLength four_char_delim(4);
00896 
00897   // Found
00898   EXPECT_TRUE(IsFoundAt("abcde", four_char_delim, 4));
00899   EXPECT_TRUE(IsFoundAt("abcdefghijklmnopqrstuvwxyz", four_char_delim, 4));
00900   EXPECT_TRUE(IsFoundAt("a b,c\nd", four_char_delim, 4));
00901   // Not found
00902   EXPECT_FALSE(IsFoundAt("", four_char_delim, 0));
00903   EXPECT_FALSE(IsFoundAt("a", four_char_delim, 0));
00904   EXPECT_FALSE(IsFoundAt("ab", four_char_delim, 0));
00905   EXPECT_FALSE(IsFoundAt("abc", four_char_delim, 0));
00906   EXPECT_FALSE(IsFoundAt("abcd", four_char_delim, 0));
00907 }
00908 
00909 TEST(Split, WorksWithLargeStrings) {
00910   if (sizeof(size_t) > 4) {
00911     std::string s((uint32_t{1} << 31) + 1, 'x');  // 2G + 1 byte
00912     s.back() = '-';
00913     std::vector<absl::string_view> v = absl::StrSplit(s, '-');
00914     EXPECT_EQ(2, v.size());
00915     // The first element will contain 2G of 'x's.
00916     // testing::StartsWith is too slow with a 2G std::string.
00917     EXPECT_EQ('x', v[0][0]);
00918     EXPECT_EQ('x', v[0][1]);
00919     EXPECT_EQ('x', v[0][3]);
00920     EXPECT_EQ("", v[1]);
00921   }
00922 }
00923 
00924 TEST(SplitInternalTest, TypeTraits) {
00925   EXPECT_FALSE(absl::strings_internal::HasMappedType<int>::value);
00926   EXPECT_TRUE(
00927       (absl::strings_internal::HasMappedType<std::map<int, int>>::value));
00928   EXPECT_FALSE(absl::strings_internal::HasValueType<int>::value);
00929   EXPECT_TRUE(
00930       (absl::strings_internal::HasValueType<std::map<int, int>>::value));
00931   EXPECT_FALSE(absl::strings_internal::HasConstIterator<int>::value);
00932   EXPECT_TRUE(
00933       (absl::strings_internal::HasConstIterator<std::map<int, int>>::value));
00934   EXPECT_FALSE(absl::strings_internal::IsInitializerList<int>::value);
00935   EXPECT_TRUE((absl::strings_internal::IsInitializerList<
00936                std::initializer_list<int>>::value));
00937 }
00938 
00939 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15