StringRefTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/Hashing.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "gtest/gtest.h"
  14. using namespace llvm;
  15. namespace llvm {
  16. std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
  17. OS << S.str();
  18. return OS;
  19. }
  20. std::ostream &operator<<(std::ostream &OS,
  21. const std::pair<StringRef, StringRef> &P) {
  22. OS << "(" << P.first << ", " << P.second << ")";
  23. return OS;
  24. }
  25. }
  26. namespace {
  27. TEST(StringRefTest, Construction) {
  28. EXPECT_EQ("", StringRef());
  29. EXPECT_EQ("hello", StringRef("hello"));
  30. EXPECT_EQ("hello", StringRef("hello world", 5));
  31. EXPECT_EQ("hello", StringRef(std::string("hello")));
  32. }
  33. TEST(StringRefTest, Iteration) {
  34. StringRef S("hello");
  35. const char *p = "hello";
  36. for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
  37. EXPECT_EQ(*it, *p);
  38. }
  39. TEST(StringRefTest, StringOps) {
  40. const char *p = "hello";
  41. EXPECT_EQ(p, StringRef(p, 0).data());
  42. EXPECT_TRUE(StringRef().empty());
  43. EXPECT_EQ((size_t) 5, StringRef("hello").size());
  44. EXPECT_EQ(-1, StringRef("aab").compare("aad"));
  45. EXPECT_EQ( 0, StringRef("aab").compare("aab"));
  46. EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
  47. EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
  48. EXPECT_EQ( 1, StringRef("aab").compare("aa"));
  49. EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
  50. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
  51. EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
  52. EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
  53. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
  54. EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
  55. EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
  56. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
  57. EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
  58. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
  59. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
  60. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
  61. EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
  62. EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
  63. EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
  64. EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
  65. EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
  66. EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
  67. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
  68. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
  69. EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
  70. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
  71. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
  72. EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
  73. }
  74. TEST(StringRefTest, Operators) {
  75. EXPECT_EQ("", StringRef());
  76. EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
  77. EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
  78. EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
  79. EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
  80. EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
  81. EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
  82. EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
  83. EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
  84. EXPECT_EQ(StringRef("aab"), StringRef("aab"));
  85. EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
  86. EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
  87. EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
  88. EXPECT_EQ('a', StringRef("aab")[1]);
  89. }
  90. TEST(StringRefTest, Substr) {
  91. StringRef Str("hello");
  92. EXPECT_EQ("lo", Str.substr(3));
  93. EXPECT_EQ("", Str.substr(100));
  94. EXPECT_EQ("hello", Str.substr(0, 100));
  95. EXPECT_EQ("o", Str.substr(4, 10));
  96. }
  97. TEST(StringRefTest, Slice) {
  98. StringRef Str("hello");
  99. EXPECT_EQ("l", Str.slice(2, 3));
  100. EXPECT_EQ("ell", Str.slice(1, 4));
  101. EXPECT_EQ("llo", Str.slice(2, 100));
  102. EXPECT_EQ("", Str.slice(2, 1));
  103. EXPECT_EQ("", Str.slice(10, 20));
  104. }
  105. TEST(StringRefTest, Split) {
  106. StringRef Str("hello");
  107. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  108. Str.split('X'));
  109. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  110. Str.split('e'));
  111. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  112. Str.split('h'));
  113. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
  114. Str.split('l'));
  115. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  116. Str.split('o'));
  117. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  118. Str.rsplit('X'));
  119. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  120. Str.rsplit('e'));
  121. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  122. Str.rsplit('h'));
  123. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  124. Str.rsplit('l'));
  125. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  126. Str.rsplit('o'));
  127. }
  128. TEST(StringRefTest, Split2) {
  129. SmallVector<StringRef, 5> parts;
  130. SmallVector<StringRef, 5> expected;
  131. expected.push_back("ab"); expected.push_back("c");
  132. StringRef(",ab,,c,").split(parts, ",", -1, false);
  133. EXPECT_TRUE(parts == expected);
  134. expected.clear(); parts.clear();
  135. expected.push_back(""); expected.push_back("ab"); expected.push_back("");
  136. expected.push_back("c"); expected.push_back("");
  137. StringRef(",ab,,c,").split(parts, ",", -1, true);
  138. EXPECT_TRUE(parts == expected);
  139. expected.clear(); parts.clear();
  140. expected.push_back("");
  141. StringRef("").split(parts, ",", -1, true);
  142. EXPECT_TRUE(parts == expected);
  143. expected.clear(); parts.clear();
  144. StringRef("").split(parts, ",", -1, false);
  145. EXPECT_TRUE(parts == expected);
  146. expected.clear(); parts.clear();
  147. StringRef(",").split(parts, ",", -1, false);
  148. EXPECT_TRUE(parts == expected);
  149. expected.clear(); parts.clear();
  150. expected.push_back(""); expected.push_back("");
  151. StringRef(",").split(parts, ",", -1, true);
  152. EXPECT_TRUE(parts == expected);
  153. expected.clear(); parts.clear();
  154. expected.push_back("a"); expected.push_back("b");
  155. StringRef("a,b").split(parts, ",", -1, true);
  156. EXPECT_TRUE(parts == expected);
  157. // Test MaxSplit
  158. expected.clear(); parts.clear();
  159. expected.push_back("a,,b,c");
  160. StringRef("a,,b,c").split(parts, ",", 0, true);
  161. EXPECT_TRUE(parts == expected);
  162. expected.clear(); parts.clear();
  163. expected.push_back("a,,b,c");
  164. StringRef("a,,b,c").split(parts, ",", 0, false);
  165. EXPECT_TRUE(parts == expected);
  166. expected.clear(); parts.clear();
  167. expected.push_back("a"); expected.push_back(",b,c");
  168. StringRef("a,,b,c").split(parts, ",", 1, true);
  169. EXPECT_TRUE(parts == expected);
  170. expected.clear(); parts.clear();
  171. expected.push_back("a"); expected.push_back(",b,c");
  172. StringRef("a,,b,c").split(parts, ",", 1, false);
  173. EXPECT_TRUE(parts == expected);
  174. expected.clear(); parts.clear();
  175. expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
  176. StringRef("a,,b,c").split(parts, ",", 2, true);
  177. EXPECT_TRUE(parts == expected);
  178. expected.clear(); parts.clear();
  179. expected.push_back("a"); expected.push_back("b,c");
  180. StringRef("a,,b,c").split(parts, ",", 2, false);
  181. EXPECT_TRUE(parts == expected);
  182. expected.clear(); parts.clear();
  183. expected.push_back("a"); expected.push_back(""); expected.push_back("b");
  184. expected.push_back("c");
  185. StringRef("a,,b,c").split(parts, ",", 3, true);
  186. EXPECT_TRUE(parts == expected);
  187. expected.clear(); parts.clear();
  188. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  189. StringRef("a,,b,c").split(parts, ",", 3, false);
  190. EXPECT_TRUE(parts == expected);
  191. }
  192. TEST(StringRefTest, Trim) {
  193. StringRef Str0("hello");
  194. StringRef Str1(" hello ");
  195. StringRef Str2(" hello ");
  196. EXPECT_EQ(StringRef("hello"), Str0.rtrim());
  197. EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
  198. EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
  199. EXPECT_EQ(StringRef("hello"), Str0.ltrim());
  200. EXPECT_EQ(StringRef("hello "), Str1.ltrim());
  201. EXPECT_EQ(StringRef("hello "), Str2.ltrim());
  202. EXPECT_EQ(StringRef("hello"), Str0.trim());
  203. EXPECT_EQ(StringRef("hello"), Str1.trim());
  204. EXPECT_EQ(StringRef("hello"), Str2.trim());
  205. EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
  206. EXPECT_EQ(StringRef(""), StringRef("").trim());
  207. EXPECT_EQ(StringRef(""), StringRef(" ").trim());
  208. EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
  209. EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
  210. EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
  211. }
  212. TEST(StringRefTest, StartsWith) {
  213. StringRef Str("hello");
  214. EXPECT_TRUE(Str.startswith("he"));
  215. EXPECT_FALSE(Str.startswith("helloworld"));
  216. EXPECT_FALSE(Str.startswith("hi"));
  217. }
  218. TEST(StringRefTest, EndsWith) {
  219. StringRef Str("hello");
  220. EXPECT_TRUE(Str.endswith("lo"));
  221. EXPECT_FALSE(Str.endswith("helloworld"));
  222. EXPECT_FALSE(Str.endswith("worldhello"));
  223. EXPECT_FALSE(Str.endswith("so"));
  224. }
  225. TEST(StringRefTest, Find) {
  226. StringRef Str("hello");
  227. EXPECT_EQ(2U, Str.find('l'));
  228. EXPECT_EQ(StringRef::npos, Str.find('z'));
  229. EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
  230. EXPECT_EQ(0U, Str.find("hello"));
  231. EXPECT_EQ(1U, Str.find("ello"));
  232. EXPECT_EQ(StringRef::npos, Str.find("zz"));
  233. EXPECT_EQ(2U, Str.find("ll", 2));
  234. EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
  235. EXPECT_EQ(0U, Str.find(""));
  236. StringRef LongStr("hellx xello hell ello world foo bar hello");
  237. EXPECT_EQ(36U, LongStr.find("hello"));
  238. EXPECT_EQ(28U, LongStr.find("foo"));
  239. EXPECT_EQ(12U, LongStr.find("hell", 2));
  240. EXPECT_EQ(0U, LongStr.find(""));
  241. EXPECT_EQ(3U, Str.rfind('l'));
  242. EXPECT_EQ(StringRef::npos, Str.rfind('z'));
  243. EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
  244. EXPECT_EQ(0U, Str.rfind("hello"));
  245. EXPECT_EQ(1U, Str.rfind("ello"));
  246. EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
  247. EXPECT_EQ(2U, Str.find_first_of('l'));
  248. EXPECT_EQ(1U, Str.find_first_of("el"));
  249. EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
  250. EXPECT_EQ(1U, Str.find_first_not_of('h'));
  251. EXPECT_EQ(4U, Str.find_first_not_of("hel"));
  252. EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
  253. EXPECT_EQ(3U, Str.find_last_not_of('o'));
  254. EXPECT_EQ(1U, Str.find_last_not_of("lo"));
  255. EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
  256. }
  257. TEST(StringRefTest, Count) {
  258. StringRef Str("hello");
  259. EXPECT_EQ(2U, Str.count('l'));
  260. EXPECT_EQ(1U, Str.count('o'));
  261. EXPECT_EQ(0U, Str.count('z'));
  262. EXPECT_EQ(0U, Str.count("helloworld"));
  263. EXPECT_EQ(1U, Str.count("hello"));
  264. EXPECT_EQ(1U, Str.count("ello"));
  265. EXPECT_EQ(0U, Str.count("zz"));
  266. }
  267. TEST(StringRefTest, EditDistance) {
  268. StringRef Str("hello");
  269. EXPECT_EQ(2U, Str.edit_distance("hill"));
  270. }
  271. TEST(StringRefTest, Misc) {
  272. std::string Storage;
  273. raw_string_ostream OS(Storage);
  274. OS << StringRef("hello");
  275. EXPECT_EQ("hello", OS.str());
  276. }
  277. TEST(StringRefTest, Hashing) {
  278. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
  279. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
  280. std::string S = "hello world";
  281. hash_code H = hash_value(S);
  282. EXPECT_EQ(H, hash_value(StringRef("hello world")));
  283. EXPECT_EQ(H, hash_value(StringRef(S)));
  284. EXPECT_NE(H, hash_value(StringRef("hello worl")));
  285. EXPECT_EQ(hash_value(std::string("hello worl")),
  286. hash_value(StringRef("hello worl")));
  287. EXPECT_NE(H, hash_value(StringRef("hello world ")));
  288. EXPECT_EQ(hash_value(std::string("hello world ")),
  289. hash_value(StringRef("hello world ")));
  290. EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
  291. EXPECT_NE(hash_value(std::string("ello worl")),
  292. hash_value(StringRef("hello world").slice(1, -1)));
  293. }
  294. struct UnsignedPair {
  295. const char *Str;
  296. uint64_t Expected;
  297. } Unsigned[] =
  298. { {"0", 0}
  299. , {"255", 255}
  300. , {"256", 256}
  301. , {"65535", 65535}
  302. , {"65536", 65536}
  303. , {"4294967295", 4294967295ULL}
  304. , {"4294967296", 4294967296ULL}
  305. , {"18446744073709551615", 18446744073709551615ULL}
  306. , {"042", 34}
  307. , {"0x42", 66}
  308. , {"0b101010", 42}
  309. };
  310. struct SignedPair {
  311. const char *Str;
  312. int64_t Expected;
  313. } Signed[] =
  314. { {"0", 0}
  315. , {"-0", 0}
  316. , {"127", 127}
  317. , {"128", 128}
  318. , {"-128", -128}
  319. , {"-129", -129}
  320. , {"32767", 32767}
  321. , {"32768", 32768}
  322. , {"-32768", -32768}
  323. , {"-32769", -32769}
  324. , {"2147483647", 2147483647LL}
  325. , {"2147483648", 2147483648LL}
  326. , {"-2147483648", -2147483648LL}
  327. , {"-2147483649", -2147483649LL}
  328. , {"-9223372036854775808", -(9223372036854775807LL) - 1}
  329. , {"042", 34}
  330. , {"0x42", 66}
  331. , {"0b101010", 42}
  332. , {"-042", -34}
  333. , {"-0x42", -66}
  334. , {"-0b101010", -42}
  335. };
  336. TEST(StringRefTest, getAsInteger) {
  337. uint8_t U8;
  338. uint16_t U16;
  339. uint32_t U32;
  340. uint64_t U64;
  341. for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
  342. bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
  343. if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  344. ASSERT_FALSE(U8Success);
  345. EXPECT_EQ(U8, Unsigned[i].Expected);
  346. } else {
  347. ASSERT_TRUE(U8Success);
  348. }
  349. bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
  350. if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  351. ASSERT_FALSE(U16Success);
  352. EXPECT_EQ(U16, Unsigned[i].Expected);
  353. } else {
  354. ASSERT_TRUE(U16Success);
  355. }
  356. bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
  357. if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  358. ASSERT_FALSE(U32Success);
  359. EXPECT_EQ(U32, Unsigned[i].Expected);
  360. } else {
  361. ASSERT_TRUE(U32Success);
  362. }
  363. bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
  364. if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  365. ASSERT_FALSE(U64Success);
  366. EXPECT_EQ(U64, Unsigned[i].Expected);
  367. } else {
  368. ASSERT_TRUE(U64Success);
  369. }
  370. }
  371. int8_t S8;
  372. int16_t S16;
  373. int32_t S32;
  374. int64_t S64;
  375. for (size_t i = 0; i < array_lengthof(Signed); ++i) {
  376. bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
  377. if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
  378. ASSERT_FALSE(S8Success);
  379. EXPECT_EQ(S8, Signed[i].Expected);
  380. } else {
  381. ASSERT_TRUE(S8Success);
  382. }
  383. bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
  384. if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
  385. ASSERT_FALSE(S16Success);
  386. EXPECT_EQ(S16, Signed[i].Expected);
  387. } else {
  388. ASSERT_TRUE(S16Success);
  389. }
  390. bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
  391. if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
  392. ASSERT_FALSE(S32Success);
  393. EXPECT_EQ(S32, Signed[i].Expected);
  394. } else {
  395. ASSERT_TRUE(S32Success);
  396. }
  397. bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
  398. if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
  399. ASSERT_FALSE(S64Success);
  400. EXPECT_EQ(S64, Signed[i].Expected);
  401. } else {
  402. ASSERT_TRUE(S64Success);
  403. }
  404. }
  405. }
  406. static const char* BadStrings[] = {
  407. "18446744073709551617" // value just over max
  408. , "123456789012345678901" // value way too large
  409. , "4t23v" // illegal decimal characters
  410. , "0x123W56" // illegal hex characters
  411. , "0b2" // illegal bin characters
  412. , "08" // illegal oct characters
  413. , "0o8" // illegal oct characters
  414. , "-123" // negative unsigned value
  415. };
  416. TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
  417. unsigned long long U64;
  418. for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
  419. bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
  420. ASSERT_TRUE(IsBadNumber);
  421. }
  422. }
  423. } // end anonymous namespace