StringRefTest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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/STLExtras.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/Support/Allocator.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. namespace llvm {
  19. std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
  20. OS << S.str();
  21. return OS;
  22. }
  23. std::ostream &operator<<(std::ostream &OS,
  24. const std::pair<StringRef, StringRef> &P) {
  25. OS << "(" << P.first << ", " << P.second << ")";
  26. return OS;
  27. }
  28. }
  29. namespace {
  30. TEST(StringRefTest, Construction) {
  31. EXPECT_EQ("", StringRef());
  32. EXPECT_EQ("hello", StringRef("hello"));
  33. EXPECT_EQ("hello", StringRef("hello world", 5));
  34. EXPECT_EQ("hello", StringRef(std::string("hello")));
  35. }
  36. TEST(StringRefTest, Iteration) {
  37. StringRef S("hello");
  38. const char *p = "hello";
  39. for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
  40. EXPECT_EQ(*it, *p);
  41. }
  42. TEST(StringRefTest, StringOps) {
  43. const char *p = "hello";
  44. EXPECT_EQ(p, StringRef(p, 0).data());
  45. EXPECT_TRUE(StringRef().empty());
  46. EXPECT_EQ((size_t) 5, StringRef("hello").size());
  47. EXPECT_EQ(-1, StringRef("aab").compare("aad"));
  48. EXPECT_EQ( 0, StringRef("aab").compare("aab"));
  49. EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
  50. EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
  51. EXPECT_EQ( 1, StringRef("aab").compare("aa"));
  52. EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
  53. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
  54. EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
  55. EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
  56. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
  57. EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
  58. EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
  59. EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
  60. EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
  61. EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
  62. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
  63. EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
  64. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
  65. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
  66. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
  67. EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
  68. EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
  69. EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
  70. EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
  71. EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
  72. EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
  73. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
  74. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
  75. EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
  76. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
  77. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
  78. EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
  79. }
  80. TEST(StringRefTest, Operators) {
  81. EXPECT_EQ("", StringRef());
  82. EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
  83. EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
  84. EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
  85. EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
  86. EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
  87. EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
  88. EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
  89. EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
  90. EXPECT_EQ(StringRef("aab"), StringRef("aab"));
  91. EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
  92. EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
  93. EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
  94. EXPECT_EQ('a', StringRef("aab")[1]);
  95. }
  96. TEST(StringRefTest, Substr) {
  97. StringRef Str("hello");
  98. EXPECT_EQ("lo", Str.substr(3));
  99. EXPECT_EQ("", Str.substr(100));
  100. EXPECT_EQ("hello", Str.substr(0, 100));
  101. EXPECT_EQ("o", Str.substr(4, 10));
  102. }
  103. TEST(StringRefTest, Slice) {
  104. StringRef Str("hello");
  105. EXPECT_EQ("l", Str.slice(2, 3));
  106. EXPECT_EQ("ell", Str.slice(1, 4));
  107. EXPECT_EQ("llo", Str.slice(2, 100));
  108. EXPECT_EQ("", Str.slice(2, 1));
  109. EXPECT_EQ("", Str.slice(10, 20));
  110. }
  111. TEST(StringRefTest, Split) {
  112. StringRef Str("hello");
  113. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  114. Str.split('X'));
  115. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  116. Str.split('e'));
  117. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  118. Str.split('h'));
  119. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
  120. Str.split('l'));
  121. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  122. Str.split('o'));
  123. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  124. Str.rsplit('X'));
  125. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  126. Str.rsplit('e'));
  127. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  128. Str.rsplit('h'));
  129. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  130. Str.rsplit('l'));
  131. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  132. Str.rsplit('o'));
  133. }
  134. TEST(StringRefTest, Split2) {
  135. SmallVector<StringRef, 5> parts;
  136. SmallVector<StringRef, 5> expected;
  137. expected.push_back("ab"); expected.push_back("c");
  138. StringRef(",ab,,c,").split(parts, ",", -1, false);
  139. EXPECT_TRUE(parts == expected);
  140. expected.clear(); parts.clear();
  141. expected.push_back(""); expected.push_back("ab"); expected.push_back("");
  142. expected.push_back("c"); expected.push_back("");
  143. StringRef(",ab,,c,").split(parts, ",", -1, true);
  144. EXPECT_TRUE(parts == expected);
  145. expected.clear(); parts.clear();
  146. expected.push_back("");
  147. StringRef("").split(parts, ",", -1, true);
  148. EXPECT_TRUE(parts == expected);
  149. expected.clear(); parts.clear();
  150. StringRef("").split(parts, ",", -1, false);
  151. EXPECT_TRUE(parts == expected);
  152. expected.clear(); parts.clear();
  153. StringRef(",").split(parts, ",", -1, false);
  154. EXPECT_TRUE(parts == expected);
  155. expected.clear(); parts.clear();
  156. expected.push_back(""); expected.push_back("");
  157. StringRef(",").split(parts, ",", -1, true);
  158. EXPECT_TRUE(parts == expected);
  159. expected.clear(); parts.clear();
  160. expected.push_back("a"); expected.push_back("b");
  161. StringRef("a,b").split(parts, ",", -1, true);
  162. EXPECT_TRUE(parts == expected);
  163. // Test MaxSplit
  164. expected.clear(); parts.clear();
  165. expected.push_back("a,,b,c");
  166. StringRef("a,,b,c").split(parts, ",", 0, true);
  167. EXPECT_TRUE(parts == expected);
  168. expected.clear(); parts.clear();
  169. expected.push_back("a,,b,c");
  170. StringRef("a,,b,c").split(parts, ",", 0, false);
  171. EXPECT_TRUE(parts == expected);
  172. expected.clear(); parts.clear();
  173. expected.push_back("a"); expected.push_back(",b,c");
  174. StringRef("a,,b,c").split(parts, ",", 1, true);
  175. EXPECT_TRUE(parts == expected);
  176. expected.clear(); parts.clear();
  177. expected.push_back("a"); expected.push_back(",b,c");
  178. StringRef("a,,b,c").split(parts, ",", 1, false);
  179. EXPECT_TRUE(parts == expected);
  180. expected.clear(); parts.clear();
  181. expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
  182. StringRef("a,,b,c").split(parts, ",", 2, true);
  183. EXPECT_TRUE(parts == expected);
  184. expected.clear(); parts.clear();
  185. expected.push_back("a"); expected.push_back("b,c");
  186. StringRef("a,,b,c").split(parts, ",", 2, false);
  187. EXPECT_TRUE(parts == expected);
  188. expected.clear(); parts.clear();
  189. expected.push_back("a"); expected.push_back(""); expected.push_back("b");
  190. expected.push_back("c");
  191. StringRef("a,,b,c").split(parts, ",", 3, true);
  192. EXPECT_TRUE(parts == expected);
  193. expected.clear(); parts.clear();
  194. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  195. StringRef("a,,b,c").split(parts, ",", 3, false);
  196. EXPECT_TRUE(parts == expected);
  197. expected.clear(); parts.clear();
  198. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  199. StringRef("a,,b,c").split(parts, ',', 3, false);
  200. EXPECT_TRUE(parts == expected);
  201. expected.clear(); parts.clear();
  202. expected.push_back("");
  203. StringRef().split(parts, ",", 0, true);
  204. EXPECT_TRUE(parts == expected);
  205. expected.clear(); parts.clear();
  206. expected.push_back(StringRef());
  207. StringRef("").split(parts, ",", 0, true);
  208. EXPECT_TRUE(parts == expected);
  209. expected.clear(); parts.clear();
  210. StringRef("").split(parts, ",", 0, false);
  211. EXPECT_TRUE(parts == expected);
  212. StringRef().split(parts, ",", 0, false);
  213. EXPECT_TRUE(parts == expected);
  214. expected.clear(); parts.clear();
  215. expected.push_back("a");
  216. expected.push_back("");
  217. expected.push_back("b");
  218. expected.push_back("c,d");
  219. StringRef("a,,b,c,d").split(parts, ",", 3, true);
  220. EXPECT_TRUE(parts == expected);
  221. expected.clear(); parts.clear();
  222. expected.push_back("");
  223. StringRef().split(parts, ',', 0, true);
  224. EXPECT_TRUE(parts == expected);
  225. expected.clear(); parts.clear();
  226. expected.push_back(StringRef());
  227. StringRef("").split(parts, ',', 0, true);
  228. EXPECT_TRUE(parts == expected);
  229. expected.clear(); parts.clear();
  230. StringRef("").split(parts, ',', 0, false);
  231. EXPECT_TRUE(parts == expected);
  232. StringRef().split(parts, ',', 0, false);
  233. EXPECT_TRUE(parts == expected);
  234. expected.clear(); parts.clear();
  235. expected.push_back("a");
  236. expected.push_back("");
  237. expected.push_back("b");
  238. expected.push_back("c,d");
  239. StringRef("a,,b,c,d").split(parts, ',', 3, true);
  240. EXPECT_TRUE(parts == expected);
  241. }
  242. TEST(StringRefTest, Trim) {
  243. StringRef Str0("hello");
  244. StringRef Str1(" hello ");
  245. StringRef Str2(" hello ");
  246. EXPECT_EQ(StringRef("hello"), Str0.rtrim());
  247. EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
  248. EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
  249. EXPECT_EQ(StringRef("hello"), Str0.ltrim());
  250. EXPECT_EQ(StringRef("hello "), Str1.ltrim());
  251. EXPECT_EQ(StringRef("hello "), Str2.ltrim());
  252. EXPECT_EQ(StringRef("hello"), Str0.trim());
  253. EXPECT_EQ(StringRef("hello"), Str1.trim());
  254. EXPECT_EQ(StringRef("hello"), Str2.trim());
  255. EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
  256. EXPECT_EQ(StringRef(""), StringRef("").trim());
  257. EXPECT_EQ(StringRef(""), StringRef(" ").trim());
  258. EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
  259. EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
  260. EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
  261. }
  262. TEST(StringRefTest, StartsWith) {
  263. StringRef Str("hello");
  264. EXPECT_TRUE(Str.startswith(""));
  265. EXPECT_TRUE(Str.startswith("he"));
  266. EXPECT_FALSE(Str.startswith("helloworld"));
  267. EXPECT_FALSE(Str.startswith("hi"));
  268. }
  269. TEST(StringRefTest, StartsWithLower) {
  270. StringRef Str("heLLo");
  271. EXPECT_TRUE(Str.startswith_lower(""));
  272. EXPECT_TRUE(Str.startswith_lower("he"));
  273. EXPECT_TRUE(Str.startswith_lower("hell"));
  274. EXPECT_TRUE(Str.startswith_lower("HELlo"));
  275. EXPECT_FALSE(Str.startswith_lower("helloworld"));
  276. EXPECT_FALSE(Str.startswith_lower("hi"));
  277. }
  278. TEST(StringRefTest, ConsumeFront) {
  279. StringRef Str("hello");
  280. EXPECT_TRUE(Str.consume_front(""));
  281. EXPECT_EQ("hello", Str);
  282. EXPECT_TRUE(Str.consume_front("he"));
  283. EXPECT_EQ("llo", Str);
  284. EXPECT_FALSE(Str.consume_front("lloworld"));
  285. EXPECT_EQ("llo", Str);
  286. EXPECT_FALSE(Str.consume_front("lol"));
  287. EXPECT_EQ("llo", Str);
  288. EXPECT_TRUE(Str.consume_front("llo"));
  289. EXPECT_EQ("", Str);
  290. EXPECT_FALSE(Str.consume_front("o"));
  291. EXPECT_TRUE(Str.consume_front(""));
  292. }
  293. TEST(StringRefTest, EndsWith) {
  294. StringRef Str("hello");
  295. EXPECT_TRUE(Str.endswith(""));
  296. EXPECT_TRUE(Str.endswith("lo"));
  297. EXPECT_FALSE(Str.endswith("helloworld"));
  298. EXPECT_FALSE(Str.endswith("worldhello"));
  299. EXPECT_FALSE(Str.endswith("so"));
  300. }
  301. TEST(StringRefTest, EndsWithLower) {
  302. StringRef Str("heLLo");
  303. EXPECT_TRUE(Str.endswith_lower(""));
  304. EXPECT_TRUE(Str.endswith_lower("lo"));
  305. EXPECT_TRUE(Str.endswith_lower("LO"));
  306. EXPECT_TRUE(Str.endswith_lower("ELlo"));
  307. EXPECT_FALSE(Str.endswith_lower("helloworld"));
  308. EXPECT_FALSE(Str.endswith_lower("hi"));
  309. }
  310. TEST(StringRefTest, ConsumeBack) {
  311. StringRef Str("hello");
  312. EXPECT_TRUE(Str.consume_back(""));
  313. EXPECT_EQ("hello", Str);
  314. EXPECT_TRUE(Str.consume_back("lo"));
  315. EXPECT_EQ("hel", Str);
  316. EXPECT_FALSE(Str.consume_back("helhel"));
  317. EXPECT_EQ("hel", Str);
  318. EXPECT_FALSE(Str.consume_back("hle"));
  319. EXPECT_EQ("hel", Str);
  320. EXPECT_TRUE(Str.consume_back("hel"));
  321. EXPECT_EQ("", Str);
  322. EXPECT_FALSE(Str.consume_back("h"));
  323. EXPECT_TRUE(Str.consume_back(""));
  324. }
  325. TEST(StringRefTest, Find) {
  326. StringRef Str("hello");
  327. EXPECT_EQ(2U, Str.find('l'));
  328. EXPECT_EQ(StringRef::npos, Str.find('z'));
  329. EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
  330. EXPECT_EQ(0U, Str.find("hello"));
  331. EXPECT_EQ(1U, Str.find("ello"));
  332. EXPECT_EQ(StringRef::npos, Str.find("zz"));
  333. EXPECT_EQ(2U, Str.find("ll", 2));
  334. EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
  335. EXPECT_EQ(0U, Str.find(""));
  336. StringRef LongStr("hellx xello hell ello world foo bar hello");
  337. EXPECT_EQ(36U, LongStr.find("hello"));
  338. EXPECT_EQ(28U, LongStr.find("foo"));
  339. EXPECT_EQ(12U, LongStr.find("hell", 2));
  340. EXPECT_EQ(0U, LongStr.find(""));
  341. EXPECT_EQ(3U, Str.rfind('l'));
  342. EXPECT_EQ(StringRef::npos, Str.rfind('z'));
  343. EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
  344. EXPECT_EQ(0U, Str.rfind("hello"));
  345. EXPECT_EQ(1U, Str.rfind("ello"));
  346. EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
  347. EXPECT_EQ(2U, Str.find_first_of('l'));
  348. EXPECT_EQ(1U, Str.find_first_of("el"));
  349. EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
  350. EXPECT_EQ(1U, Str.find_first_not_of('h'));
  351. EXPECT_EQ(4U, Str.find_first_not_of("hel"));
  352. EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
  353. EXPECT_EQ(3U, Str.find_last_not_of('o'));
  354. EXPECT_EQ(1U, Str.find_last_not_of("lo"));
  355. EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
  356. }
  357. TEST(StringRefTest, Count) {
  358. StringRef Str("hello");
  359. EXPECT_EQ(2U, Str.count('l'));
  360. EXPECT_EQ(1U, Str.count('o'));
  361. EXPECT_EQ(0U, Str.count('z'));
  362. EXPECT_EQ(0U, Str.count("helloworld"));
  363. EXPECT_EQ(1U, Str.count("hello"));
  364. EXPECT_EQ(1U, Str.count("ello"));
  365. EXPECT_EQ(0U, Str.count("zz"));
  366. }
  367. TEST(StringRefTest, EditDistance) {
  368. StringRef Str("hello");
  369. EXPECT_EQ(2U, Str.edit_distance("hill"));
  370. }
  371. TEST(StringRefTest, Misc) {
  372. std::string Storage;
  373. raw_string_ostream OS(Storage);
  374. OS << StringRef("hello");
  375. EXPECT_EQ("hello", OS.str());
  376. }
  377. TEST(StringRefTest, Hashing) {
  378. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
  379. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
  380. std::string S = "hello world";
  381. hash_code H = hash_value(S);
  382. EXPECT_EQ(H, hash_value(StringRef("hello world")));
  383. EXPECT_EQ(H, hash_value(StringRef(S)));
  384. EXPECT_NE(H, hash_value(StringRef("hello worl")));
  385. EXPECT_EQ(hash_value(std::string("hello worl")),
  386. hash_value(StringRef("hello worl")));
  387. EXPECT_NE(H, hash_value(StringRef("hello world ")));
  388. EXPECT_EQ(hash_value(std::string("hello world ")),
  389. hash_value(StringRef("hello world ")));
  390. EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
  391. EXPECT_NE(hash_value(std::string("ello worl")),
  392. hash_value(StringRef("hello world").slice(1, -1)));
  393. }
  394. struct UnsignedPair {
  395. const char *Str;
  396. uint64_t Expected;
  397. } Unsigned[] =
  398. { {"0", 0}
  399. , {"255", 255}
  400. , {"256", 256}
  401. , {"65535", 65535}
  402. , {"65536", 65536}
  403. , {"4294967295", 4294967295ULL}
  404. , {"4294967296", 4294967296ULL}
  405. , {"18446744073709551615", 18446744073709551615ULL}
  406. , {"042", 34}
  407. , {"0x42", 66}
  408. , {"0b101010", 42}
  409. };
  410. struct SignedPair {
  411. const char *Str;
  412. int64_t Expected;
  413. } Signed[] =
  414. { {"0", 0}
  415. , {"-0", 0}
  416. , {"127", 127}
  417. , {"128", 128}
  418. , {"-128", -128}
  419. , {"-129", -129}
  420. , {"32767", 32767}
  421. , {"32768", 32768}
  422. , {"-32768", -32768}
  423. , {"-32769", -32769}
  424. , {"2147483647", 2147483647LL}
  425. , {"2147483648", 2147483648LL}
  426. , {"-2147483648", -2147483648LL}
  427. , {"-2147483649", -2147483649LL}
  428. , {"-9223372036854775808", -(9223372036854775807LL) - 1}
  429. , {"042", 34}
  430. , {"0x42", 66}
  431. , {"0b101010", 42}
  432. , {"-042", -34}
  433. , {"-0x42", -66}
  434. , {"-0b101010", -42}
  435. };
  436. TEST(StringRefTest, getAsInteger) {
  437. uint8_t U8;
  438. uint16_t U16;
  439. uint32_t U32;
  440. uint64_t U64;
  441. for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
  442. bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
  443. if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  444. ASSERT_FALSE(U8Success);
  445. EXPECT_EQ(U8, Unsigned[i].Expected);
  446. } else {
  447. ASSERT_TRUE(U8Success);
  448. }
  449. bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
  450. if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  451. ASSERT_FALSE(U16Success);
  452. EXPECT_EQ(U16, Unsigned[i].Expected);
  453. } else {
  454. ASSERT_TRUE(U16Success);
  455. }
  456. bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
  457. if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  458. ASSERT_FALSE(U32Success);
  459. EXPECT_EQ(U32, Unsigned[i].Expected);
  460. } else {
  461. ASSERT_TRUE(U32Success);
  462. }
  463. bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
  464. if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  465. ASSERT_FALSE(U64Success);
  466. EXPECT_EQ(U64, Unsigned[i].Expected);
  467. } else {
  468. ASSERT_TRUE(U64Success);
  469. }
  470. }
  471. int8_t S8;
  472. int16_t S16;
  473. int32_t S32;
  474. int64_t S64;
  475. for (size_t i = 0; i < array_lengthof(Signed); ++i) {
  476. bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
  477. if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
  478. ASSERT_FALSE(S8Success);
  479. EXPECT_EQ(S8, Signed[i].Expected);
  480. } else {
  481. ASSERT_TRUE(S8Success);
  482. }
  483. bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
  484. if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
  485. ASSERT_FALSE(S16Success);
  486. EXPECT_EQ(S16, Signed[i].Expected);
  487. } else {
  488. ASSERT_TRUE(S16Success);
  489. }
  490. bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
  491. if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
  492. ASSERT_FALSE(S32Success);
  493. EXPECT_EQ(S32, Signed[i].Expected);
  494. } else {
  495. ASSERT_TRUE(S32Success);
  496. }
  497. bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
  498. if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
  499. ASSERT_FALSE(S64Success);
  500. EXPECT_EQ(S64, Signed[i].Expected);
  501. } else {
  502. ASSERT_TRUE(S64Success);
  503. }
  504. }
  505. }
  506. static const char* BadStrings[] = {
  507. "18446744073709551617" // value just over max
  508. , "123456789012345678901" // value way too large
  509. , "4t23v" // illegal decimal characters
  510. , "0x123W56" // illegal hex characters
  511. , "0b2" // illegal bin characters
  512. , "08" // illegal oct characters
  513. , "0o8" // illegal oct characters
  514. , "-123" // negative unsigned value
  515. };
  516. TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
  517. unsigned long long U64;
  518. for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
  519. bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
  520. ASSERT_TRUE(IsBadNumber);
  521. }
  522. }
  523. static const char *join_input[] = { "a", "b", "c" };
  524. static const char join_result1[] = "a";
  525. static const char join_result2[] = "a:b:c";
  526. static const char join_result3[] = "a::b::c";
  527. TEST(StringRefTest, joinStrings) {
  528. std::vector<StringRef> v1;
  529. std::vector<std::string> v2;
  530. for (size_t i = 0; i < array_lengthof(join_input); ++i) {
  531. v1.push_back(join_input[i]);
  532. v2.push_back(join_input[i]);
  533. }
  534. bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
  535. EXPECT_TRUE(v1_join1);
  536. bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
  537. EXPECT_TRUE(v1_join2);
  538. bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
  539. EXPECT_TRUE(v1_join3);
  540. bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
  541. EXPECT_TRUE(v2_join1);
  542. bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
  543. EXPECT_TRUE(v2_join2);
  544. bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
  545. EXPECT_TRUE(v2_join3);
  546. }
  547. TEST(StringRefTest, AllocatorCopy) {
  548. BumpPtrAllocator Alloc;
  549. // First test empty strings. We don't want these to allocate anything on the
  550. // allocator.
  551. StringRef StrEmpty = "";
  552. StringRef StrEmptyc = StrEmpty.copy(Alloc);
  553. EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
  554. EXPECT_EQ(StrEmptyc.data(), nullptr);
  555. EXPECT_EQ(StrEmptyc.size(), 0u);
  556. EXPECT_EQ(Alloc.getTotalMemory(), 0u);
  557. StringRef Str1 = "hello";
  558. StringRef Str2 = "bye";
  559. StringRef Str1c = Str1.copy(Alloc);
  560. StringRef Str2c = Str2.copy(Alloc);
  561. EXPECT_TRUE(Str1.equals(Str1c));
  562. EXPECT_NE(Str1.data(), Str1c.data());
  563. EXPECT_TRUE(Str2.equals(Str2c));
  564. EXPECT_NE(Str2.data(), Str2c.data());
  565. }
  566. TEST(StringRefTest, Drop) {
  567. StringRef Test("StringRefTest::Drop");
  568. StringRef Dropped = Test.drop_front(5);
  569. EXPECT_EQ(Dropped, "gRefTest::Drop");
  570. Dropped = Test.drop_back(5);
  571. EXPECT_EQ(Dropped, "StringRefTest:");
  572. Dropped = Test.drop_front(0);
  573. EXPECT_EQ(Dropped, Test);
  574. Dropped = Test.drop_back(0);
  575. EXPECT_EQ(Dropped, Test);
  576. Dropped = Test.drop_front(Test.size());
  577. EXPECT_TRUE(Dropped.empty());
  578. Dropped = Test.drop_back(Test.size());
  579. EXPECT_TRUE(Dropped.empty());
  580. }
  581. TEST(StringRefTest, Take) {
  582. StringRef Test("StringRefTest::Take");
  583. StringRef Taken = Test.take_front(5);
  584. EXPECT_EQ(Taken, "Strin");
  585. Taken = Test.take_back(5);
  586. EXPECT_EQ(Taken, ":Take");
  587. Taken = Test.take_front(Test.size());
  588. EXPECT_EQ(Taken, Test);
  589. Taken = Test.take_back(Test.size());
  590. EXPECT_EQ(Taken, Test);
  591. Taken = Test.take_front(0);
  592. EXPECT_TRUE(Taken.empty());
  593. Taken = Test.take_back(0);
  594. EXPECT_TRUE(Taken.empty());
  595. }
  596. } // end anonymous namespace