StringRefTest.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/Hashing.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/Support/Allocator.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace llvm {
  18. std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
  19. OS << S.str();
  20. return OS;
  21. }
  22. std::ostream &operator<<(std::ostream &OS,
  23. const std::pair<StringRef, StringRef> &P) {
  24. OS << "(" << P.first << ", " << P.second << ")";
  25. return OS;
  26. }
  27. }
  28. // Check that we can't accidentally assign a temporary std::string to a
  29. // StringRef. (Unfortunately we can't make use of the same thing with
  30. // constructors.)
  31. static_assert(
  32. !std::is_assignable<StringRef&, std::string>::value,
  33. "Assigning from prvalue std::string");
  34. static_assert(
  35. !std::is_assignable<StringRef&, std::string &&>::value,
  36. "Assigning from xvalue std::string");
  37. static_assert(
  38. std::is_assignable<StringRef&, std::string &>::value,
  39. "Assigning from lvalue std::string");
  40. static_assert(
  41. std::is_assignable<StringRef&, const char *>::value,
  42. "Assigning from prvalue C string");
  43. static_assert(
  44. std::is_assignable<StringRef&, const char * &&>::value,
  45. "Assigning from xvalue C string");
  46. static_assert(
  47. std::is_assignable<StringRef&, const char * &>::value,
  48. "Assigning from lvalue C string");
  49. namespace {
  50. TEST(StringRefTest, Construction) {
  51. EXPECT_EQ("", StringRef());
  52. EXPECT_EQ("hello", StringRef("hello"));
  53. EXPECT_EQ("hello", StringRef("hello world", 5));
  54. EXPECT_EQ("hello", StringRef(std::string("hello")));
  55. }
  56. TEST(StringRefTest, EmptyInitializerList) {
  57. StringRef S = {};
  58. EXPECT_TRUE(S.empty());
  59. S = {};
  60. EXPECT_TRUE(S.empty());
  61. }
  62. TEST(StringRefTest, Iteration) {
  63. StringRef S("hello");
  64. const char *p = "hello";
  65. for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
  66. EXPECT_EQ(*it, *p);
  67. }
  68. TEST(StringRefTest, StringOps) {
  69. const char *p = "hello";
  70. EXPECT_EQ(p, StringRef(p, 0).data());
  71. EXPECT_TRUE(StringRef().empty());
  72. EXPECT_EQ((size_t) 5, StringRef("hello").size());
  73. EXPECT_EQ(-1, StringRef("aab").compare("aad"));
  74. EXPECT_EQ( 0, StringRef("aab").compare("aab"));
  75. EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
  76. EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
  77. EXPECT_EQ( 1, StringRef("aab").compare("aa"));
  78. EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
  79. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
  80. EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
  81. EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
  82. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
  83. EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
  84. EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
  85. EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
  86. EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
  87. EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
  88. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
  89. EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
  90. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
  91. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
  92. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
  93. EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
  94. EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
  95. EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
  96. EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
  97. EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
  98. EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
  99. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
  100. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
  101. EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
  102. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
  103. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
  104. EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
  105. }
  106. TEST(StringRefTest, Operators) {
  107. EXPECT_EQ("", StringRef());
  108. EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
  109. EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
  110. EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
  111. EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
  112. EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
  113. EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
  114. EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
  115. EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
  116. EXPECT_EQ(StringRef("aab"), StringRef("aab"));
  117. EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
  118. EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
  119. EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
  120. EXPECT_EQ('a', StringRef("aab")[1]);
  121. }
  122. TEST(StringRefTest, Substr) {
  123. StringRef Str("hello");
  124. EXPECT_EQ("lo", Str.substr(3));
  125. EXPECT_EQ("", Str.substr(100));
  126. EXPECT_EQ("hello", Str.substr(0, 100));
  127. EXPECT_EQ("o", Str.substr(4, 10));
  128. }
  129. TEST(StringRefTest, Slice) {
  130. StringRef Str("hello");
  131. EXPECT_EQ("l", Str.slice(2, 3));
  132. EXPECT_EQ("ell", Str.slice(1, 4));
  133. EXPECT_EQ("llo", Str.slice(2, 100));
  134. EXPECT_EQ("", Str.slice(2, 1));
  135. EXPECT_EQ("", Str.slice(10, 20));
  136. }
  137. TEST(StringRefTest, Split) {
  138. StringRef Str("hello");
  139. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  140. Str.split('X'));
  141. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  142. Str.split('e'));
  143. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  144. Str.split('h'));
  145. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
  146. Str.split('l'));
  147. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  148. Str.split('o'));
  149. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  150. Str.rsplit('X'));
  151. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  152. Str.rsplit('e'));
  153. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  154. Str.rsplit('h'));
  155. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  156. Str.rsplit('l'));
  157. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  158. Str.rsplit('o'));
  159. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
  160. Str.rsplit("ll"));
  161. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  162. Str.rsplit("h"));
  163. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  164. Str.rsplit("o"));
  165. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  166. Str.rsplit("::"));
  167. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  168. Str.rsplit("l"));
  169. }
  170. TEST(StringRefTest, Split2) {
  171. SmallVector<StringRef, 5> parts;
  172. SmallVector<StringRef, 5> expected;
  173. expected.push_back("ab"); expected.push_back("c");
  174. StringRef(",ab,,c,").split(parts, ",", -1, false);
  175. EXPECT_TRUE(parts == expected);
  176. expected.clear(); parts.clear();
  177. expected.push_back(""); expected.push_back("ab"); expected.push_back("");
  178. expected.push_back("c"); expected.push_back("");
  179. StringRef(",ab,,c,").split(parts, ",", -1, true);
  180. EXPECT_TRUE(parts == expected);
  181. expected.clear(); parts.clear();
  182. expected.push_back("");
  183. StringRef("").split(parts, ",", -1, true);
  184. EXPECT_TRUE(parts == expected);
  185. expected.clear(); parts.clear();
  186. StringRef("").split(parts, ",", -1, false);
  187. EXPECT_TRUE(parts == expected);
  188. expected.clear(); parts.clear();
  189. StringRef(",").split(parts, ",", -1, false);
  190. EXPECT_TRUE(parts == expected);
  191. expected.clear(); parts.clear();
  192. expected.push_back(""); expected.push_back("");
  193. StringRef(",").split(parts, ",", -1, true);
  194. EXPECT_TRUE(parts == expected);
  195. expected.clear(); parts.clear();
  196. expected.push_back("a"); expected.push_back("b");
  197. StringRef("a,b").split(parts, ",", -1, true);
  198. EXPECT_TRUE(parts == expected);
  199. // Test MaxSplit
  200. expected.clear(); parts.clear();
  201. expected.push_back("a,,b,c");
  202. StringRef("a,,b,c").split(parts, ",", 0, true);
  203. EXPECT_TRUE(parts == expected);
  204. expected.clear(); parts.clear();
  205. expected.push_back("a,,b,c");
  206. StringRef("a,,b,c").split(parts, ",", 0, false);
  207. EXPECT_TRUE(parts == expected);
  208. expected.clear(); parts.clear();
  209. expected.push_back("a"); expected.push_back(",b,c");
  210. StringRef("a,,b,c").split(parts, ",", 1, true);
  211. EXPECT_TRUE(parts == expected);
  212. expected.clear(); parts.clear();
  213. expected.push_back("a"); expected.push_back(",b,c");
  214. StringRef("a,,b,c").split(parts, ",", 1, false);
  215. EXPECT_TRUE(parts == expected);
  216. expected.clear(); parts.clear();
  217. expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
  218. StringRef("a,,b,c").split(parts, ",", 2, true);
  219. EXPECT_TRUE(parts == expected);
  220. expected.clear(); parts.clear();
  221. expected.push_back("a"); expected.push_back("b,c");
  222. StringRef("a,,b,c").split(parts, ",", 2, false);
  223. EXPECT_TRUE(parts == expected);
  224. expected.clear(); parts.clear();
  225. expected.push_back("a"); expected.push_back(""); expected.push_back("b");
  226. expected.push_back("c");
  227. StringRef("a,,b,c").split(parts, ",", 3, true);
  228. EXPECT_TRUE(parts == expected);
  229. expected.clear(); parts.clear();
  230. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  231. StringRef("a,,b,c").split(parts, ",", 3, false);
  232. EXPECT_TRUE(parts == expected);
  233. expected.clear(); parts.clear();
  234. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  235. StringRef("a,,b,c").split(parts, ',', 3, false);
  236. EXPECT_TRUE(parts == expected);
  237. expected.clear(); parts.clear();
  238. expected.push_back("");
  239. StringRef().split(parts, ",", 0, true);
  240. EXPECT_TRUE(parts == expected);
  241. expected.clear(); parts.clear();
  242. expected.push_back(StringRef());
  243. StringRef("").split(parts, ",", 0, true);
  244. EXPECT_TRUE(parts == expected);
  245. expected.clear(); parts.clear();
  246. StringRef("").split(parts, ",", 0, false);
  247. EXPECT_TRUE(parts == expected);
  248. StringRef().split(parts, ",", 0, false);
  249. EXPECT_TRUE(parts == expected);
  250. expected.clear(); parts.clear();
  251. expected.push_back("a");
  252. expected.push_back("");
  253. expected.push_back("b");
  254. expected.push_back("c,d");
  255. StringRef("a,,b,c,d").split(parts, ",", 3, true);
  256. EXPECT_TRUE(parts == expected);
  257. expected.clear(); parts.clear();
  258. expected.push_back("");
  259. StringRef().split(parts, ',', 0, true);
  260. EXPECT_TRUE(parts == expected);
  261. expected.clear(); parts.clear();
  262. expected.push_back(StringRef());
  263. StringRef("").split(parts, ',', 0, true);
  264. EXPECT_TRUE(parts == expected);
  265. expected.clear(); parts.clear();
  266. StringRef("").split(parts, ',', 0, false);
  267. EXPECT_TRUE(parts == expected);
  268. StringRef().split(parts, ',', 0, false);
  269. EXPECT_TRUE(parts == expected);
  270. expected.clear(); parts.clear();
  271. expected.push_back("a");
  272. expected.push_back("");
  273. expected.push_back("b");
  274. expected.push_back("c,d");
  275. StringRef("a,,b,c,d").split(parts, ',', 3, true);
  276. EXPECT_TRUE(parts == expected);
  277. }
  278. TEST(StringRefTest, Trim) {
  279. StringRef Str0("hello");
  280. StringRef Str1(" hello ");
  281. StringRef Str2(" hello ");
  282. EXPECT_EQ(StringRef("hello"), Str0.rtrim());
  283. EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
  284. EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
  285. EXPECT_EQ(StringRef("hello"), Str0.ltrim());
  286. EXPECT_EQ(StringRef("hello "), Str1.ltrim());
  287. EXPECT_EQ(StringRef("hello "), Str2.ltrim());
  288. EXPECT_EQ(StringRef("hello"), Str0.trim());
  289. EXPECT_EQ(StringRef("hello"), Str1.trim());
  290. EXPECT_EQ(StringRef("hello"), Str2.trim());
  291. EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
  292. EXPECT_EQ(StringRef(""), StringRef("").trim());
  293. EXPECT_EQ(StringRef(""), StringRef(" ").trim());
  294. EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
  295. EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
  296. EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
  297. }
  298. TEST(StringRefTest, StartsWith) {
  299. StringRef Str("hello");
  300. EXPECT_TRUE(Str.startswith(""));
  301. EXPECT_TRUE(Str.startswith("he"));
  302. EXPECT_FALSE(Str.startswith("helloworld"));
  303. EXPECT_FALSE(Str.startswith("hi"));
  304. }
  305. TEST(StringRefTest, StartsWithLower) {
  306. StringRef Str("heLLo");
  307. EXPECT_TRUE(Str.startswith_lower(""));
  308. EXPECT_TRUE(Str.startswith_lower("he"));
  309. EXPECT_TRUE(Str.startswith_lower("hell"));
  310. EXPECT_TRUE(Str.startswith_lower("HELlo"));
  311. EXPECT_FALSE(Str.startswith_lower("helloworld"));
  312. EXPECT_FALSE(Str.startswith_lower("hi"));
  313. }
  314. TEST(StringRefTest, ConsumeFront) {
  315. StringRef Str("hello");
  316. EXPECT_TRUE(Str.consume_front(""));
  317. EXPECT_EQ("hello", Str);
  318. EXPECT_TRUE(Str.consume_front("he"));
  319. EXPECT_EQ("llo", Str);
  320. EXPECT_FALSE(Str.consume_front("lloworld"));
  321. EXPECT_EQ("llo", Str);
  322. EXPECT_FALSE(Str.consume_front("lol"));
  323. EXPECT_EQ("llo", Str);
  324. EXPECT_TRUE(Str.consume_front("llo"));
  325. EXPECT_EQ("", Str);
  326. EXPECT_FALSE(Str.consume_front("o"));
  327. EXPECT_TRUE(Str.consume_front(""));
  328. }
  329. TEST(StringRefTest, EndsWith) {
  330. StringRef Str("hello");
  331. EXPECT_TRUE(Str.endswith(""));
  332. EXPECT_TRUE(Str.endswith("lo"));
  333. EXPECT_FALSE(Str.endswith("helloworld"));
  334. EXPECT_FALSE(Str.endswith("worldhello"));
  335. EXPECT_FALSE(Str.endswith("so"));
  336. }
  337. TEST(StringRefTest, EndsWithLower) {
  338. StringRef Str("heLLo");
  339. EXPECT_TRUE(Str.endswith_lower(""));
  340. EXPECT_TRUE(Str.endswith_lower("lo"));
  341. EXPECT_TRUE(Str.endswith_lower("LO"));
  342. EXPECT_TRUE(Str.endswith_lower("ELlo"));
  343. EXPECT_FALSE(Str.endswith_lower("helloworld"));
  344. EXPECT_FALSE(Str.endswith_lower("hi"));
  345. }
  346. TEST(StringRefTest, ConsumeBack) {
  347. StringRef Str("hello");
  348. EXPECT_TRUE(Str.consume_back(""));
  349. EXPECT_EQ("hello", Str);
  350. EXPECT_TRUE(Str.consume_back("lo"));
  351. EXPECT_EQ("hel", Str);
  352. EXPECT_FALSE(Str.consume_back("helhel"));
  353. EXPECT_EQ("hel", Str);
  354. EXPECT_FALSE(Str.consume_back("hle"));
  355. EXPECT_EQ("hel", Str);
  356. EXPECT_TRUE(Str.consume_back("hel"));
  357. EXPECT_EQ("", Str);
  358. EXPECT_FALSE(Str.consume_back("h"));
  359. EXPECT_TRUE(Str.consume_back(""));
  360. }
  361. TEST(StringRefTest, Find) {
  362. StringRef Str("helloHELLO");
  363. StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
  364. struct {
  365. StringRef Str;
  366. char C;
  367. std::size_t From;
  368. std::size_t Pos;
  369. std::size_t LowerPos;
  370. } CharExpectations[] = {
  371. {Str, 'h', 0U, 0U, 0U},
  372. {Str, 'e', 0U, 1U, 1U},
  373. {Str, 'l', 0U, 2U, 2U},
  374. {Str, 'l', 3U, 3U, 3U},
  375. {Str, 'o', 0U, 4U, 4U},
  376. {Str, 'L', 0U, 7U, 2U},
  377. {Str, 'z', 0U, StringRef::npos, StringRef::npos},
  378. };
  379. struct {
  380. StringRef Str;
  381. llvm::StringRef S;
  382. std::size_t From;
  383. std::size_t Pos;
  384. std::size_t LowerPos;
  385. } StrExpectations[] = {
  386. {Str, "helloword", 0, StringRef::npos, StringRef::npos},
  387. {Str, "hello", 0, 0U, 0U},
  388. {Str, "ello", 0, 1U, 1U},
  389. {Str, "zz", 0, StringRef::npos, StringRef::npos},
  390. {Str, "ll", 2U, 2U, 2U},
  391. {Str, "ll", 3U, StringRef::npos, 7U},
  392. {Str, "LL", 2U, 7U, 2U},
  393. {Str, "LL", 3U, 7U, 7U},
  394. {Str, "", 0U, 0U, 0U},
  395. {LongStr, "hello", 0U, 36U, 36U},
  396. {LongStr, "foo", 0U, 28U, 28U},
  397. {LongStr, "hell", 2U, 12U, 12U},
  398. {LongStr, "HELL", 2U, 42U, 12U},
  399. {LongStr, "", 0U, 0U, 0U}};
  400. for (auto &E : CharExpectations) {
  401. EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
  402. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
  403. EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
  404. }
  405. for (auto &E : StrExpectations) {
  406. EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
  407. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
  408. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
  409. }
  410. EXPECT_EQ(3U, Str.rfind('l'));
  411. EXPECT_EQ(StringRef::npos, Str.rfind('z'));
  412. EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
  413. EXPECT_EQ(0U, Str.rfind("hello"));
  414. EXPECT_EQ(1U, Str.rfind("ello"));
  415. EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
  416. EXPECT_EQ(8U, Str.rfind_lower('l'));
  417. EXPECT_EQ(8U, Str.rfind_lower('L'));
  418. EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
  419. EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
  420. EXPECT_EQ(5U, Str.rfind("HELLO"));
  421. EXPECT_EQ(6U, Str.rfind("ELLO"));
  422. EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
  423. EXPECT_EQ(2U, Str.find_first_of('l'));
  424. EXPECT_EQ(1U, Str.find_first_of("el"));
  425. EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
  426. Str = "hello";
  427. EXPECT_EQ(1U, Str.find_first_not_of('h'));
  428. EXPECT_EQ(4U, Str.find_first_not_of("hel"));
  429. EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
  430. EXPECT_EQ(3U, Str.find_last_not_of('o'));
  431. EXPECT_EQ(1U, Str.find_last_not_of("lo"));
  432. EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
  433. }
  434. TEST(StringRefTest, Count) {
  435. StringRef Str("hello");
  436. EXPECT_EQ(2U, Str.count('l'));
  437. EXPECT_EQ(1U, Str.count('o'));
  438. EXPECT_EQ(0U, Str.count('z'));
  439. EXPECT_EQ(0U, Str.count("helloworld"));
  440. EXPECT_EQ(1U, Str.count("hello"));
  441. EXPECT_EQ(1U, Str.count("ello"));
  442. EXPECT_EQ(0U, Str.count("zz"));
  443. }
  444. TEST(StringRefTest, EditDistance) {
  445. StringRef Hello("hello");
  446. EXPECT_EQ(2U, Hello.edit_distance("hill"));
  447. StringRef Industry("industry");
  448. EXPECT_EQ(6U, Industry.edit_distance("interest"));
  449. StringRef Soylent("soylent green is people");
  450. EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
  451. EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
  452. /* allow replacements = */ false));
  453. EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
  454. /* allow replacements = */ true,
  455. /* max edit distance = */ 8));
  456. EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
  457. "people soiled our green "
  458. "people soiled our green "));
  459. }
  460. TEST(StringRefTest, Misc) {
  461. std::string Storage;
  462. raw_string_ostream OS(Storage);
  463. OS << StringRef("hello");
  464. EXPECT_EQ("hello", OS.str());
  465. }
  466. TEST(StringRefTest, Hashing) {
  467. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
  468. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
  469. std::string S = "hello world";
  470. hash_code H = hash_value(S);
  471. EXPECT_EQ(H, hash_value(StringRef("hello world")));
  472. EXPECT_EQ(H, hash_value(StringRef(S)));
  473. EXPECT_NE(H, hash_value(StringRef("hello worl")));
  474. EXPECT_EQ(hash_value(std::string("hello worl")),
  475. hash_value(StringRef("hello worl")));
  476. EXPECT_NE(H, hash_value(StringRef("hello world ")));
  477. EXPECT_EQ(hash_value(std::string("hello world ")),
  478. hash_value(StringRef("hello world ")));
  479. EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
  480. EXPECT_NE(hash_value(std::string("ello worl")),
  481. hash_value(StringRef("hello world").slice(1, -1)));
  482. }
  483. struct UnsignedPair {
  484. const char *Str;
  485. uint64_t Expected;
  486. } Unsigned[] =
  487. { {"0", 0}
  488. , {"255", 255}
  489. , {"256", 256}
  490. , {"65535", 65535}
  491. , {"65536", 65536}
  492. , {"4294967295", 4294967295ULL}
  493. , {"4294967296", 4294967296ULL}
  494. , {"18446744073709551615", 18446744073709551615ULL}
  495. , {"042", 34}
  496. , {"0x42", 66}
  497. , {"0b101010", 42}
  498. };
  499. struct SignedPair {
  500. const char *Str;
  501. int64_t Expected;
  502. } Signed[] =
  503. { {"0", 0}
  504. , {"-0", 0}
  505. , {"127", 127}
  506. , {"128", 128}
  507. , {"-128", -128}
  508. , {"-129", -129}
  509. , {"32767", 32767}
  510. , {"32768", 32768}
  511. , {"-32768", -32768}
  512. , {"-32769", -32769}
  513. , {"2147483647", 2147483647LL}
  514. , {"2147483648", 2147483648LL}
  515. , {"-2147483648", -2147483648LL}
  516. , {"-2147483649", -2147483649LL}
  517. , {"-9223372036854775808", -(9223372036854775807LL) - 1}
  518. , {"042", 34}
  519. , {"0x42", 66}
  520. , {"0b101010", 42}
  521. , {"-042", -34}
  522. , {"-0x42", -66}
  523. , {"-0b101010", -42}
  524. };
  525. TEST(StringRefTest, getAsInteger) {
  526. uint8_t U8;
  527. uint16_t U16;
  528. uint32_t U32;
  529. uint64_t U64;
  530. for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
  531. bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
  532. if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  533. ASSERT_FALSE(U8Success);
  534. EXPECT_EQ(U8, Unsigned[i].Expected);
  535. } else {
  536. ASSERT_TRUE(U8Success);
  537. }
  538. bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
  539. if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  540. ASSERT_FALSE(U16Success);
  541. EXPECT_EQ(U16, Unsigned[i].Expected);
  542. } else {
  543. ASSERT_TRUE(U16Success);
  544. }
  545. bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
  546. if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  547. ASSERT_FALSE(U32Success);
  548. EXPECT_EQ(U32, Unsigned[i].Expected);
  549. } else {
  550. ASSERT_TRUE(U32Success);
  551. }
  552. bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
  553. if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  554. ASSERT_FALSE(U64Success);
  555. EXPECT_EQ(U64, Unsigned[i].Expected);
  556. } else {
  557. ASSERT_TRUE(U64Success);
  558. }
  559. }
  560. int8_t S8;
  561. int16_t S16;
  562. int32_t S32;
  563. int64_t S64;
  564. for (size_t i = 0; i < array_lengthof(Signed); ++i) {
  565. bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
  566. if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
  567. ASSERT_FALSE(S8Success);
  568. EXPECT_EQ(S8, Signed[i].Expected);
  569. } else {
  570. ASSERT_TRUE(S8Success);
  571. }
  572. bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
  573. if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
  574. ASSERT_FALSE(S16Success);
  575. EXPECT_EQ(S16, Signed[i].Expected);
  576. } else {
  577. ASSERT_TRUE(S16Success);
  578. }
  579. bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
  580. if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
  581. ASSERT_FALSE(S32Success);
  582. EXPECT_EQ(S32, Signed[i].Expected);
  583. } else {
  584. ASSERT_TRUE(S32Success);
  585. }
  586. bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
  587. if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
  588. ASSERT_FALSE(S64Success);
  589. EXPECT_EQ(S64, Signed[i].Expected);
  590. } else {
  591. ASSERT_TRUE(S64Success);
  592. }
  593. }
  594. }
  595. static const char* BadStrings[] = {
  596. "" // empty string
  597. , "18446744073709551617" // value just over max
  598. , "123456789012345678901" // value way too large
  599. , "4t23v" // illegal decimal characters
  600. , "0x123W56" // illegal hex characters
  601. , "0b2" // illegal bin characters
  602. , "08" // illegal oct characters
  603. , "0o8" // illegal oct characters
  604. , "-123" // negative unsigned value
  605. , "0x"
  606. , "0b"
  607. };
  608. TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
  609. unsigned long long U64;
  610. for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
  611. bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
  612. ASSERT_TRUE(IsBadNumber);
  613. }
  614. }
  615. struct ConsumeUnsignedPair {
  616. const char *Str;
  617. uint64_t Expected;
  618. const char *Leftover;
  619. } ConsumeUnsigned[] = {
  620. {"0", 0, ""},
  621. {"255", 255, ""},
  622. {"256", 256, ""},
  623. {"65535", 65535, ""},
  624. {"65536", 65536, ""},
  625. {"4294967295", 4294967295ULL, ""},
  626. {"4294967296", 4294967296ULL, ""},
  627. {"255A376", 255, "A376"},
  628. {"18446744073709551615", 18446744073709551615ULL, ""},
  629. {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
  630. {"042", 34, ""},
  631. {"0x42", 66, ""},
  632. {"0x42-0x34", 66, "-0x34"},
  633. {"0b101010", 42, ""},
  634. {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
  635. {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
  636. {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
  637. struct ConsumeSignedPair {
  638. const char *Str;
  639. int64_t Expected;
  640. const char *Leftover;
  641. } ConsumeSigned[] = {
  642. {"0", 0, ""},
  643. {"-0", 0, ""},
  644. {"0-1", 0, "-1"},
  645. {"-0-1", 0, "-1"},
  646. {"127", 127, ""},
  647. {"128", 128, ""},
  648. {"127-1", 127, "-1"},
  649. {"128-1", 128, "-1"},
  650. {"-128", -128, ""},
  651. {"-129", -129, ""},
  652. {"-128-1", -128, "-1"},
  653. {"-129-1", -129, "-1"},
  654. {"32767", 32767, ""},
  655. {"32768", 32768, ""},
  656. {"32767-1", 32767, "-1"},
  657. {"32768-1", 32768, "-1"},
  658. {"-32768", -32768, ""},
  659. {"-32769", -32769, ""},
  660. {"-32768-1", -32768, "-1"},
  661. {"-32769-1", -32769, "-1"},
  662. {"2147483647", 2147483647LL, ""},
  663. {"2147483648", 2147483648LL, ""},
  664. {"2147483647-1", 2147483647LL, "-1"},
  665. {"2147483648-1", 2147483648LL, "-1"},
  666. {"-2147483648", -2147483648LL, ""},
  667. {"-2147483649", -2147483649LL, ""},
  668. {"-2147483648-1", -2147483648LL, "-1"},
  669. {"-2147483649-1", -2147483649LL, "-1"},
  670. {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
  671. {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
  672. {"042", 34, ""},
  673. {"042-1", 34, "-1"},
  674. {"0x42", 66, ""},
  675. {"0x42-1", 66, "-1"},
  676. {"0b101010", 42, ""},
  677. {"0b101010-1", 42, "-1"},
  678. {"-042", -34, ""},
  679. {"-042-1", -34, "-1"},
  680. {"-0x42", -66, ""},
  681. {"-0x42-1", -66, "-1"},
  682. {"-0b101010", -42, ""},
  683. {"-0b101010-1", -42, "-1"}};
  684. TEST(StringRefTest, consumeIntegerUnsigned) {
  685. uint8_t U8;
  686. uint16_t U16;
  687. uint32_t U32;
  688. uint64_t U64;
  689. for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
  690. StringRef Str = ConsumeUnsigned[i].Str;
  691. bool U8Success = Str.consumeInteger(0, U8);
  692. if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
  693. ConsumeUnsigned[i].Expected) {
  694. ASSERT_FALSE(U8Success);
  695. EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
  696. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  697. } else {
  698. ASSERT_TRUE(U8Success);
  699. }
  700. Str = ConsumeUnsigned[i].Str;
  701. bool U16Success = Str.consumeInteger(0, U16);
  702. if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
  703. ConsumeUnsigned[i].Expected) {
  704. ASSERT_FALSE(U16Success);
  705. EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
  706. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  707. } else {
  708. ASSERT_TRUE(U16Success);
  709. }
  710. Str = ConsumeUnsigned[i].Str;
  711. bool U32Success = Str.consumeInteger(0, U32);
  712. if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
  713. ConsumeUnsigned[i].Expected) {
  714. ASSERT_FALSE(U32Success);
  715. EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
  716. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  717. } else {
  718. ASSERT_TRUE(U32Success);
  719. }
  720. Str = ConsumeUnsigned[i].Str;
  721. bool U64Success = Str.consumeInteger(0, U64);
  722. if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
  723. ConsumeUnsigned[i].Expected) {
  724. ASSERT_FALSE(U64Success);
  725. EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
  726. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  727. } else {
  728. ASSERT_TRUE(U64Success);
  729. }
  730. }
  731. }
  732. TEST(StringRefTest, consumeIntegerSigned) {
  733. int8_t S8;
  734. int16_t S16;
  735. int32_t S32;
  736. int64_t S64;
  737. for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
  738. StringRef Str = ConsumeSigned[i].Str;
  739. bool S8Success = Str.consumeInteger(0, S8);
  740. if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
  741. ConsumeSigned[i].Expected) {
  742. ASSERT_FALSE(S8Success);
  743. EXPECT_EQ(S8, ConsumeSigned[i].Expected);
  744. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  745. } else {
  746. ASSERT_TRUE(S8Success);
  747. }
  748. Str = ConsumeSigned[i].Str;
  749. bool S16Success = Str.consumeInteger(0, S16);
  750. if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
  751. ConsumeSigned[i].Expected) {
  752. ASSERT_FALSE(S16Success);
  753. EXPECT_EQ(S16, ConsumeSigned[i].Expected);
  754. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  755. } else {
  756. ASSERT_TRUE(S16Success);
  757. }
  758. Str = ConsumeSigned[i].Str;
  759. bool S32Success = Str.consumeInteger(0, S32);
  760. if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
  761. ConsumeSigned[i].Expected) {
  762. ASSERT_FALSE(S32Success);
  763. EXPECT_EQ(S32, ConsumeSigned[i].Expected);
  764. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  765. } else {
  766. ASSERT_TRUE(S32Success);
  767. }
  768. Str = ConsumeSigned[i].Str;
  769. bool S64Success = Str.consumeInteger(0, S64);
  770. if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
  771. ConsumeSigned[i].Expected) {
  772. ASSERT_FALSE(S64Success);
  773. EXPECT_EQ(S64, ConsumeSigned[i].Expected);
  774. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  775. } else {
  776. ASSERT_TRUE(S64Success);
  777. }
  778. }
  779. }
  780. struct GetDoubleStrings {
  781. const char *Str;
  782. bool AllowInexact;
  783. bool ShouldFail;
  784. double D;
  785. } DoubleStrings[] = {{"0", false, false, 0.0},
  786. {"0.0", false, false, 0.0},
  787. {"-0.0", false, false, -0.0},
  788. {"123.45", false, true, 123.45},
  789. {"123.45", true, false, 123.45},
  790. {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
  791. {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
  792. {"0x0.0000000000001P-1023", false, true, 0.0},
  793. {"0x0.0000000000001P-1023", true, false, 0.0},
  794. };
  795. TEST(StringRefTest, getAsDouble) {
  796. for (const auto &Entry : DoubleStrings) {
  797. double Result;
  798. StringRef S(Entry.Str);
  799. EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
  800. if (!Entry.ShouldFail) {
  801. EXPECT_EQ(Result, Entry.D);
  802. }
  803. }
  804. }
  805. static const char *join_input[] = { "a", "b", "c" };
  806. static const char join_result1[] = "a";
  807. static const char join_result2[] = "a:b:c";
  808. static const char join_result3[] = "a::b::c";
  809. TEST(StringRefTest, joinStrings) {
  810. std::vector<StringRef> v1;
  811. std::vector<std::string> v2;
  812. for (size_t i = 0; i < array_lengthof(join_input); ++i) {
  813. v1.push_back(join_input[i]);
  814. v2.push_back(join_input[i]);
  815. }
  816. bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
  817. EXPECT_TRUE(v1_join1);
  818. bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
  819. EXPECT_TRUE(v1_join2);
  820. bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
  821. EXPECT_TRUE(v1_join3);
  822. bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
  823. EXPECT_TRUE(v2_join1);
  824. bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
  825. EXPECT_TRUE(v2_join2);
  826. bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
  827. EXPECT_TRUE(v2_join3);
  828. v2_join3 = join(v2, "::") == join_result3;
  829. EXPECT_TRUE(v2_join3);
  830. }
  831. TEST(StringRefTest, AllocatorCopy) {
  832. BumpPtrAllocator Alloc;
  833. // First test empty strings. We don't want these to allocate anything on the
  834. // allocator.
  835. StringRef StrEmpty = "";
  836. StringRef StrEmptyc = StrEmpty.copy(Alloc);
  837. EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
  838. EXPECT_EQ(StrEmptyc.data(), nullptr);
  839. EXPECT_EQ(StrEmptyc.size(), 0u);
  840. EXPECT_EQ(Alloc.getTotalMemory(), 0u);
  841. StringRef Str1 = "hello";
  842. StringRef Str2 = "bye";
  843. StringRef Str1c = Str1.copy(Alloc);
  844. StringRef Str2c = Str2.copy(Alloc);
  845. EXPECT_TRUE(Str1.equals(Str1c));
  846. EXPECT_NE(Str1.data(), Str1c.data());
  847. EXPECT_TRUE(Str2.equals(Str2c));
  848. EXPECT_NE(Str2.data(), Str2c.data());
  849. }
  850. TEST(StringRefTest, Drop) {
  851. StringRef Test("StringRefTest::Drop");
  852. StringRef Dropped = Test.drop_front(5);
  853. EXPECT_EQ(Dropped, "gRefTest::Drop");
  854. Dropped = Test.drop_back(5);
  855. EXPECT_EQ(Dropped, "StringRefTest:");
  856. Dropped = Test.drop_front(0);
  857. EXPECT_EQ(Dropped, Test);
  858. Dropped = Test.drop_back(0);
  859. EXPECT_EQ(Dropped, Test);
  860. Dropped = Test.drop_front(Test.size());
  861. EXPECT_TRUE(Dropped.empty());
  862. Dropped = Test.drop_back(Test.size());
  863. EXPECT_TRUE(Dropped.empty());
  864. }
  865. TEST(StringRefTest, Take) {
  866. StringRef Test("StringRefTest::Take");
  867. StringRef Taken = Test.take_front(5);
  868. EXPECT_EQ(Taken, "Strin");
  869. Taken = Test.take_back(5);
  870. EXPECT_EQ(Taken, ":Take");
  871. Taken = Test.take_front(Test.size());
  872. EXPECT_EQ(Taken, Test);
  873. Taken = Test.take_back(Test.size());
  874. EXPECT_EQ(Taken, Test);
  875. Taken = Test.take_front(0);
  876. EXPECT_TRUE(Taken.empty());
  877. Taken = Test.take_back(0);
  878. EXPECT_TRUE(Taken.empty());
  879. }
  880. TEST(StringRefTest, FindIf) {
  881. StringRef Punct("Test.String");
  882. StringRef NoPunct("ABCDEFG");
  883. StringRef Empty;
  884. auto IsPunct = [](char c) { return ::ispunct(c); };
  885. auto IsAlpha = [](char c) { return ::isalpha(c); };
  886. EXPECT_EQ(4U, Punct.find_if(IsPunct));
  887. EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
  888. EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
  889. EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
  890. EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
  891. EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
  892. }
  893. TEST(StringRefTest, TakeWhileUntil) {
  894. StringRef Test("String With 1 Number");
  895. StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
  896. EXPECT_EQ("", Taken);
  897. Taken = Test.take_until([](char c) { return ::isdigit(c); });
  898. EXPECT_EQ("String With ", Taken);
  899. Taken = Test.take_while([](char c) { return true; });
  900. EXPECT_EQ(Test, Taken);
  901. Taken = Test.take_until([](char c) { return true; });
  902. EXPECT_EQ("", Taken);
  903. Test = "";
  904. Taken = Test.take_while([](char c) { return true; });
  905. EXPECT_EQ("", Taken);
  906. }
  907. TEST(StringRefTest, DropWhileUntil) {
  908. StringRef Test("String With 1 Number");
  909. StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
  910. EXPECT_EQ(Test, Taken);
  911. Taken = Test.drop_until([](char c) { return ::isdigit(c); });
  912. EXPECT_EQ("1 Number", Taken);
  913. Taken = Test.drop_while([](char c) { return true; });
  914. EXPECT_EQ("", Taken);
  915. Taken = Test.drop_until([](char c) { return true; });
  916. EXPECT_EQ(Test, Taken);
  917. StringRef EmptyString = "";
  918. Taken = EmptyString.drop_while([](char c) { return true; });
  919. EXPECT_EQ("", Taken);
  920. }
  921. TEST(StringRefTest, StringLiteral) {
  922. constexpr StringLiteral Strings[] = {"Foo", "Bar"};
  923. EXPECT_EQ(StringRef("Foo"), Strings[0]);
  924. EXPECT_EQ(StringRef("Bar"), Strings[1]);
  925. }
  926. // Check gtest prints StringRef as a string instead of a container of chars.
  927. // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
  928. TEST(StringRefTest, GTestPrinter) {
  929. EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
  930. }
  931. static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable");
  932. } // end anonymous namespace