StringRefTest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. }
  198. TEST(StringRefTest, Trim) {
  199. StringRef Str0("hello");
  200. StringRef Str1(" hello ");
  201. StringRef Str2(" hello ");
  202. EXPECT_EQ(StringRef("hello"), Str0.rtrim());
  203. EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
  204. EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
  205. EXPECT_EQ(StringRef("hello"), Str0.ltrim());
  206. EXPECT_EQ(StringRef("hello "), Str1.ltrim());
  207. EXPECT_EQ(StringRef("hello "), Str2.ltrim());
  208. EXPECT_EQ(StringRef("hello"), Str0.trim());
  209. EXPECT_EQ(StringRef("hello"), Str1.trim());
  210. EXPECT_EQ(StringRef("hello"), Str2.trim());
  211. EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
  212. EXPECT_EQ(StringRef(""), StringRef("").trim());
  213. EXPECT_EQ(StringRef(""), StringRef(" ").trim());
  214. EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
  215. EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
  216. EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
  217. }
  218. TEST(StringRefTest, StartsWith) {
  219. StringRef Str("hello");
  220. EXPECT_TRUE(Str.startswith(""));
  221. EXPECT_TRUE(Str.startswith("he"));
  222. EXPECT_FALSE(Str.startswith("helloworld"));
  223. EXPECT_FALSE(Str.startswith("hi"));
  224. }
  225. TEST(StringRefTest, StartsWithLower) {
  226. StringRef Str("heLLo");
  227. EXPECT_TRUE(Str.startswith_lower(""));
  228. EXPECT_TRUE(Str.startswith_lower("he"));
  229. EXPECT_TRUE(Str.startswith_lower("hell"));
  230. EXPECT_TRUE(Str.startswith_lower("HELlo"));
  231. EXPECT_FALSE(Str.startswith_lower("helloworld"));
  232. EXPECT_FALSE(Str.startswith_lower("hi"));
  233. }
  234. TEST(StringRefTest, EndsWith) {
  235. StringRef Str("hello");
  236. EXPECT_TRUE(Str.endswith(""));
  237. EXPECT_TRUE(Str.endswith("lo"));
  238. EXPECT_FALSE(Str.endswith("helloworld"));
  239. EXPECT_FALSE(Str.endswith("worldhello"));
  240. EXPECT_FALSE(Str.endswith("so"));
  241. }
  242. TEST(StringRefTest, EndsWithLower) {
  243. StringRef Str("heLLo");
  244. EXPECT_TRUE(Str.endswith_lower(""));
  245. EXPECT_TRUE(Str.endswith_lower("lo"));
  246. EXPECT_TRUE(Str.endswith_lower("LO"));
  247. EXPECT_TRUE(Str.endswith_lower("ELlo"));
  248. EXPECT_FALSE(Str.endswith_lower("helloworld"));
  249. EXPECT_FALSE(Str.endswith_lower("hi"));
  250. }
  251. TEST(StringRefTest, Find) {
  252. StringRef Str("hello");
  253. EXPECT_EQ(2U, Str.find('l'));
  254. EXPECT_EQ(StringRef::npos, Str.find('z'));
  255. EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
  256. EXPECT_EQ(0U, Str.find("hello"));
  257. EXPECT_EQ(1U, Str.find("ello"));
  258. EXPECT_EQ(StringRef::npos, Str.find("zz"));
  259. EXPECT_EQ(2U, Str.find("ll", 2));
  260. EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
  261. EXPECT_EQ(0U, Str.find(""));
  262. StringRef LongStr("hellx xello hell ello world foo bar hello");
  263. EXPECT_EQ(36U, LongStr.find("hello"));
  264. EXPECT_EQ(28U, LongStr.find("foo"));
  265. EXPECT_EQ(12U, LongStr.find("hell", 2));
  266. EXPECT_EQ(0U, LongStr.find(""));
  267. EXPECT_EQ(3U, Str.rfind('l'));
  268. EXPECT_EQ(StringRef::npos, Str.rfind('z'));
  269. EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
  270. EXPECT_EQ(0U, Str.rfind("hello"));
  271. EXPECT_EQ(1U, Str.rfind("ello"));
  272. EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
  273. EXPECT_EQ(2U, Str.find_first_of('l'));
  274. EXPECT_EQ(1U, Str.find_first_of("el"));
  275. EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
  276. EXPECT_EQ(1U, Str.find_first_not_of('h'));
  277. EXPECT_EQ(4U, Str.find_first_not_of("hel"));
  278. EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
  279. EXPECT_EQ(3U, Str.find_last_not_of('o'));
  280. EXPECT_EQ(1U, Str.find_last_not_of("lo"));
  281. EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
  282. }
  283. TEST(StringRefTest, Count) {
  284. StringRef Str("hello");
  285. EXPECT_EQ(2U, Str.count('l'));
  286. EXPECT_EQ(1U, Str.count('o'));
  287. EXPECT_EQ(0U, Str.count('z'));
  288. EXPECT_EQ(0U, Str.count("helloworld"));
  289. EXPECT_EQ(1U, Str.count("hello"));
  290. EXPECT_EQ(1U, Str.count("ello"));
  291. EXPECT_EQ(0U, Str.count("zz"));
  292. }
  293. TEST(StringRefTest, EditDistance) {
  294. StringRef Str("hello");
  295. EXPECT_EQ(2U, Str.edit_distance("hill"));
  296. }
  297. TEST(StringRefTest, Misc) {
  298. std::string Storage;
  299. raw_string_ostream OS(Storage);
  300. OS << StringRef("hello");
  301. EXPECT_EQ("hello", OS.str());
  302. }
  303. TEST(StringRefTest, Hashing) {
  304. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
  305. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
  306. std::string S = "hello world";
  307. hash_code H = hash_value(S);
  308. EXPECT_EQ(H, hash_value(StringRef("hello world")));
  309. EXPECT_EQ(H, hash_value(StringRef(S)));
  310. EXPECT_NE(H, hash_value(StringRef("hello worl")));
  311. EXPECT_EQ(hash_value(std::string("hello worl")),
  312. hash_value(StringRef("hello worl")));
  313. EXPECT_NE(H, hash_value(StringRef("hello world ")));
  314. EXPECT_EQ(hash_value(std::string("hello world ")),
  315. hash_value(StringRef("hello world ")));
  316. EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
  317. EXPECT_NE(hash_value(std::string("ello worl")),
  318. hash_value(StringRef("hello world").slice(1, -1)));
  319. }
  320. struct UnsignedPair {
  321. const char *Str;
  322. uint64_t Expected;
  323. } Unsigned[] =
  324. { {"0", 0}
  325. , {"255", 255}
  326. , {"256", 256}
  327. , {"65535", 65535}
  328. , {"65536", 65536}
  329. , {"4294967295", 4294967295ULL}
  330. , {"4294967296", 4294967296ULL}
  331. , {"18446744073709551615", 18446744073709551615ULL}
  332. , {"042", 34}
  333. , {"0x42", 66}
  334. , {"0b101010", 42}
  335. };
  336. struct SignedPair {
  337. const char *Str;
  338. int64_t Expected;
  339. } Signed[] =
  340. { {"0", 0}
  341. , {"-0", 0}
  342. , {"127", 127}
  343. , {"128", 128}
  344. , {"-128", -128}
  345. , {"-129", -129}
  346. , {"32767", 32767}
  347. , {"32768", 32768}
  348. , {"-32768", -32768}
  349. , {"-32769", -32769}
  350. , {"2147483647", 2147483647LL}
  351. , {"2147483648", 2147483648LL}
  352. , {"-2147483648", -2147483648LL}
  353. , {"-2147483649", -2147483649LL}
  354. , {"-9223372036854775808", -(9223372036854775807LL) - 1}
  355. , {"042", 34}
  356. , {"0x42", 66}
  357. , {"0b101010", 42}
  358. , {"-042", -34}
  359. , {"-0x42", -66}
  360. , {"-0b101010", -42}
  361. };
  362. TEST(StringRefTest, getAsInteger) {
  363. uint8_t U8;
  364. uint16_t U16;
  365. uint32_t U32;
  366. uint64_t U64;
  367. for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
  368. bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
  369. if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  370. ASSERT_FALSE(U8Success);
  371. EXPECT_EQ(U8, Unsigned[i].Expected);
  372. } else {
  373. ASSERT_TRUE(U8Success);
  374. }
  375. bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
  376. if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  377. ASSERT_FALSE(U16Success);
  378. EXPECT_EQ(U16, Unsigned[i].Expected);
  379. } else {
  380. ASSERT_TRUE(U16Success);
  381. }
  382. bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
  383. if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  384. ASSERT_FALSE(U32Success);
  385. EXPECT_EQ(U32, Unsigned[i].Expected);
  386. } else {
  387. ASSERT_TRUE(U32Success);
  388. }
  389. bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
  390. if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  391. ASSERT_FALSE(U64Success);
  392. EXPECT_EQ(U64, Unsigned[i].Expected);
  393. } else {
  394. ASSERT_TRUE(U64Success);
  395. }
  396. }
  397. int8_t S8;
  398. int16_t S16;
  399. int32_t S32;
  400. int64_t S64;
  401. for (size_t i = 0; i < array_lengthof(Signed); ++i) {
  402. bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
  403. if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
  404. ASSERT_FALSE(S8Success);
  405. EXPECT_EQ(S8, Signed[i].Expected);
  406. } else {
  407. ASSERT_TRUE(S8Success);
  408. }
  409. bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
  410. if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
  411. ASSERT_FALSE(S16Success);
  412. EXPECT_EQ(S16, Signed[i].Expected);
  413. } else {
  414. ASSERT_TRUE(S16Success);
  415. }
  416. bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
  417. if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
  418. ASSERT_FALSE(S32Success);
  419. EXPECT_EQ(S32, Signed[i].Expected);
  420. } else {
  421. ASSERT_TRUE(S32Success);
  422. }
  423. bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
  424. if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
  425. ASSERT_FALSE(S64Success);
  426. EXPECT_EQ(S64, Signed[i].Expected);
  427. } else {
  428. ASSERT_TRUE(S64Success);
  429. }
  430. }
  431. }
  432. static const char* BadStrings[] = {
  433. "18446744073709551617" // value just over max
  434. , "123456789012345678901" // value way too large
  435. , "4t23v" // illegal decimal characters
  436. , "0x123W56" // illegal hex characters
  437. , "0b2" // illegal bin characters
  438. , "08" // illegal oct characters
  439. , "0o8" // illegal oct characters
  440. , "-123" // negative unsigned value
  441. };
  442. TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
  443. unsigned long long U64;
  444. for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
  445. bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
  446. ASSERT_TRUE(IsBadNumber);
  447. }
  448. }
  449. static const char *join_input[] = { "a", "b", "c" };
  450. static const char join_result1[] = "a";
  451. static const char join_result2[] = "a:b:c";
  452. static const char join_result3[] = "a::b::c";
  453. TEST(StringRefTest, joinStrings) {
  454. std::vector<StringRef> v1;
  455. std::vector<std::string> v2;
  456. for (size_t i = 0; i < array_lengthof(join_input); ++i) {
  457. v1.push_back(join_input[i]);
  458. v2.push_back(join_input[i]);
  459. }
  460. bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
  461. EXPECT_TRUE(v1_join1);
  462. bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
  463. EXPECT_TRUE(v1_join2);
  464. bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
  465. EXPECT_TRUE(v1_join3);
  466. bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
  467. EXPECT_TRUE(v2_join1);
  468. bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
  469. EXPECT_TRUE(v2_join2);
  470. bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
  471. EXPECT_TRUE(v2_join3);
  472. }
  473. TEST(StringRefTest, AllocatorCopy) {
  474. BumpPtrAllocator Alloc;
  475. StringRef Str1 = "hello";
  476. StringRef Str2 = "bye";
  477. StringRef Str1c = Str1.copy(Alloc);
  478. StringRef Str2c = Str2.copy(Alloc);
  479. EXPECT_TRUE(Str1.equals(Str1c));
  480. EXPECT_NE(Str1.data(), Str1c.data());
  481. EXPECT_TRUE(Str2.equals(Str2c));
  482. EXPECT_NE(Str2.data(), Str2c.data());
  483. }
  484. } // end anonymous namespace