testString.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmString.hxx"
  4. #include "cm_static_string_view.hxx"
  5. #include "cm_string_view.hxx"
  6. #include <cstring>
  7. #include <iostream>
  8. #include <iterator>
  9. #include <sstream>
  10. #include <stdexcept>
  11. #include <string>
  12. #include <type_traits>
  13. #include <utility>
  14. #define ASSERT_TRUE(x) \
  15. do { \
  16. if (!(x)) { \
  17. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
  18. return false; \
  19. } \
  20. } while (false)
  21. static bool testConstructDefault()
  22. {
  23. std::cout << "testConstructDefault()\n";
  24. cm::String str;
  25. cm::String const& str_const = str;
  26. ASSERT_TRUE(bool(str_const) == false);
  27. ASSERT_TRUE(str_const.data() == nullptr);
  28. ASSERT_TRUE(str_const.size() == 0);
  29. ASSERT_TRUE(str_const.empty());
  30. ASSERT_TRUE(str_const.is_stable());
  31. ASSERT_TRUE(str.c_str() == nullptr);
  32. ASSERT_TRUE(str.str().empty());
  33. ASSERT_TRUE(str.is_stable());
  34. return true;
  35. }
  36. static bool testFromNullPtr(cm::String str)
  37. {
  38. cm::String const& str_const = str;
  39. ASSERT_TRUE(bool(str_const) == false);
  40. ASSERT_TRUE(str_const.data() == nullptr);
  41. ASSERT_TRUE(str_const.size() == 0);
  42. ASSERT_TRUE(str_const.empty());
  43. ASSERT_TRUE(str_const.is_stable());
  44. ASSERT_TRUE(str.c_str() == nullptr);
  45. ASSERT_TRUE(str.str().empty());
  46. ASSERT_TRUE(str.is_stable());
  47. return true;
  48. }
  49. static bool testConstructFromNullPtr()
  50. {
  51. std::cout << "testConstructFromNullPtr()\n";
  52. return testFromNullPtr(nullptr);
  53. }
  54. static bool testAssignFromNullPtr()
  55. {
  56. std::cout << "testAssignFromNullPtr()\n";
  57. cm::String str;
  58. str = nullptr;
  59. return testFromNullPtr(str);
  60. }
  61. static bool testFromCStrNull(cm::String str)
  62. {
  63. cm::String const& str_const = str;
  64. ASSERT_TRUE(bool(str_const) == false);
  65. ASSERT_TRUE(str_const.data() == nullptr);
  66. ASSERT_TRUE(str_const.size() == 0);
  67. ASSERT_TRUE(str_const.empty());
  68. ASSERT_TRUE(str_const.is_stable());
  69. ASSERT_TRUE(str.c_str() == nullptr);
  70. ASSERT_TRUE(str.str().empty());
  71. ASSERT_TRUE(str.is_stable());
  72. return true;
  73. }
  74. static bool testConstructFromCStrNull()
  75. {
  76. std::cout << "testConstructFromCStrNull()\n";
  77. const char* null = nullptr;
  78. return testFromCStrNull(null);
  79. }
  80. static bool testAssignFromCStrNull()
  81. {
  82. std::cout << "testAssignFromCStrNull()\n";
  83. const char* null = nullptr;
  84. cm::String str;
  85. str = null;
  86. return testFromCStrNull(str);
  87. }
  88. static char const charArray[] = "abc";
  89. static bool testFromCharArray(cm::String str)
  90. {
  91. cm::String const& str_const = str;
  92. ASSERT_TRUE(str_const.data() != charArray);
  93. ASSERT_TRUE(str_const.size() == sizeof(charArray) - 1);
  94. ASSERT_TRUE(str_const.is_stable());
  95. ASSERT_TRUE(str.c_str() != charArray);
  96. ASSERT_TRUE(str.is_stable());
  97. cm::String substr = str.substr(1);
  98. cm::String const& substr_const = substr;
  99. ASSERT_TRUE(substr_const.data() != &charArray[1]);
  100. ASSERT_TRUE(substr_const.size() == 2);
  101. ASSERT_TRUE(!substr_const.is_stable());
  102. ASSERT_TRUE(substr.c_str() != &charArray[1]);
  103. ASSERT_TRUE(!substr.is_stable());
  104. return true;
  105. }
  106. static bool testConstructFromCharArray()
  107. {
  108. std::cout << "testConstructFromCharArray()\n";
  109. return testFromCharArray(charArray);
  110. }
  111. static bool testAssignFromCharArray()
  112. {
  113. std::cout << "testAssignFromCharArray()\n";
  114. cm::String str;
  115. str = charArray;
  116. return testFromCharArray(str);
  117. }
  118. static const char* cstr = "abc";
  119. static bool testFromCStr(cm::String const& str)
  120. {
  121. ASSERT_TRUE(str.data() != cstr);
  122. ASSERT_TRUE(str.size() == 3);
  123. ASSERT_TRUE(std::strncmp(str.data(), cstr, 3) == 0);
  124. ASSERT_TRUE(str.is_stable());
  125. return true;
  126. }
  127. static bool testConstructFromCStr()
  128. {
  129. std::cout << "testConstructFromCStr()\n";
  130. return testFromCStr(cstr);
  131. }
  132. static bool testAssignFromCStr()
  133. {
  134. std::cout << "testAssignFromCStr()\n";
  135. cm::String str;
  136. str = cstr;
  137. return testFromCStr(str);
  138. }
  139. static const std::string stdstr = "abc";
  140. static bool testFromStdString(cm::String const& str)
  141. {
  142. #if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI
  143. // It would be nice to check this everywhere, but several platforms
  144. // still use a CoW implementation even in C++11.
  145. ASSERT_TRUE(str.data() != stdstr.data());
  146. #endif
  147. ASSERT_TRUE(str.size() == 3);
  148. ASSERT_TRUE(std::strncmp(str.data(), stdstr.data(), 3) == 0);
  149. ASSERT_TRUE(str.is_stable());
  150. return true;
  151. }
  152. static bool testConstructFromStdString()
  153. {
  154. std::cout << "testConstructFromStdString()\n";
  155. return testFromStdString(stdstr);
  156. }
  157. static bool testAssignFromStdString()
  158. {
  159. std::cout << "testAssignFromStdString()\n";
  160. cm::String str;
  161. str = stdstr;
  162. return testFromStdString(str);
  163. }
  164. static bool testConstructFromView()
  165. {
  166. std::cout << "testConstructFromView()\n";
  167. cm::string_view view = cstr;
  168. return testFromCStr(view);
  169. }
  170. static bool testAssignFromView()
  171. {
  172. std::cout << "testAssignFromView()\n";
  173. cm::string_view view = cstr;
  174. cm::String str;
  175. str = view;
  176. return testFromCStr(str);
  177. }
  178. static bool testFromChar(cm::String const& str)
  179. {
  180. ASSERT_TRUE(str.size() == 1);
  181. ASSERT_TRUE(std::strncmp(str.data(), "a", 1) == 0);
  182. ASSERT_TRUE(str.is_stable());
  183. return true;
  184. }
  185. static bool testConstructFromChar()
  186. {
  187. std::cout << "testConstructFromChar()\n";
  188. return testFromChar('a');
  189. }
  190. static bool testAssignFromChar()
  191. {
  192. std::cout << "testAssignFromChar()\n";
  193. cm::String str;
  194. str = 'a';
  195. return testFromChar(str);
  196. }
  197. static bool testConstructFromInitList()
  198. {
  199. std::cout << "testConstructFromInitList()\n";
  200. cm::String const str{ 'a', 'b', 'c' };
  201. ASSERT_TRUE(str.size() == 3);
  202. ASSERT_TRUE(std::strncmp(str.data(), "abc", 3) == 0);
  203. ASSERT_TRUE(str.is_stable());
  204. return true;
  205. }
  206. static bool testAssignFromInitList()
  207. {
  208. std::cout << "testAssignFromInitList()\n";
  209. cm::String str;
  210. str = { 'a', 'b', 'c' };
  211. ASSERT_TRUE(str.size() == 3);
  212. ASSERT_TRUE(std::strncmp(str.data(), "abc", 3) == 0);
  213. ASSERT_TRUE(str.is_stable());
  214. return true;
  215. }
  216. static bool testConstructFromBuffer()
  217. {
  218. std::cout << "testConstructFromBuffer()\n";
  219. cm::String const str(cstr, 3);
  220. return testFromCStr(str);
  221. }
  222. static bool testConstructFromInputIterator()
  223. {
  224. std::cout << "testConstructFromInputIterator()\n";
  225. cm::String const str(cstr, cstr + 3);
  226. ASSERT_TRUE(str.data() != cstr);
  227. ASSERT_TRUE(str.size() == 3);
  228. ASSERT_TRUE(std::strncmp(str.data(), cstr, 3) == 0);
  229. ASSERT_TRUE(str.is_stable());
  230. return true;
  231. }
  232. static bool testConstructFromN()
  233. {
  234. std::cout << "testConstructFromN()\n";
  235. cm::String const str(3, 'a');
  236. ASSERT_TRUE(str.size() == 3);
  237. ASSERT_TRUE(std::strncmp(str.data(), "aaa", 3) == 0);
  238. ASSERT_TRUE(str.is_stable());
  239. return true;
  240. }
  241. static const auto staticStringView = "abc"_s;
  242. static bool testFromStaticStringView(cm::String str)
  243. {
  244. cm::String const& str_const = str;
  245. ASSERT_TRUE(str_const.data() == staticStringView.data());
  246. ASSERT_TRUE(str_const.size() == staticStringView.size());
  247. ASSERT_TRUE(!str_const.is_stable());
  248. ASSERT_TRUE(str.c_str() == staticStringView);
  249. ASSERT_TRUE(!str.is_stable());
  250. cm::String substr = str.substr(1);
  251. cm::String const& substr_const = substr;
  252. ASSERT_TRUE(substr_const.data() == &staticStringView[1]);
  253. ASSERT_TRUE(substr_const.size() == 2);
  254. ASSERT_TRUE(!substr_const.is_stable());
  255. ASSERT_TRUE(substr.c_str() == &staticStringView[1]);
  256. ASSERT_TRUE(!substr.is_stable());
  257. return true;
  258. }
  259. static bool testConstructFromStaticStringView()
  260. {
  261. std::cout << "testConstructFromStaticStringView()\n";
  262. return testFromStaticStringView(staticStringView);
  263. }
  264. static bool testAssignFromStaticStringView()
  265. {
  266. std::cout << "testAssignFromStaticStringView()\n";
  267. cm::String str;
  268. str = staticStringView;
  269. return testFromStaticStringView(str);
  270. }
  271. static bool testConstructCopy()
  272. {
  273. std::cout << "testConstructCopy()\n";
  274. cm::String s1 = std::string("abc");
  275. cm::String s2 = s1;
  276. ASSERT_TRUE(s1.data() == s2.data());
  277. ASSERT_TRUE(s1.size() == 3);
  278. ASSERT_TRUE(s2.size() == 3);
  279. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  280. ASSERT_TRUE(s1.is_stable());
  281. ASSERT_TRUE(s2.is_stable());
  282. return true;
  283. }
  284. static bool testConstructMove()
  285. {
  286. std::cout << "testConstructMove()\n";
  287. cm::String s1 = std::string("abc");
  288. cm::String s2 = std::move(s1);
  289. ASSERT_TRUE(s1.data() == nullptr);
  290. ASSERT_TRUE(s1.size() == 0);
  291. ASSERT_TRUE(s2.size() == 3);
  292. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  293. ASSERT_TRUE(s1.is_stable());
  294. ASSERT_TRUE(s2.is_stable());
  295. return true;
  296. }
  297. static bool testAssignCopy()
  298. {
  299. std::cout << "testAssignCopy()\n";
  300. cm::String s1 = std::string("abc");
  301. cm::String s2;
  302. s2 = s1;
  303. ASSERT_TRUE(s1.data() == s2.data());
  304. ASSERT_TRUE(s1.size() == 3);
  305. ASSERT_TRUE(s2.size() == 3);
  306. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  307. ASSERT_TRUE(s1.is_stable());
  308. ASSERT_TRUE(s2.is_stable());
  309. return true;
  310. }
  311. static bool testAssignMove()
  312. {
  313. std::cout << "testAssignMove()\n";
  314. cm::String s1 = std::string("abc");
  315. cm::String s2;
  316. s2 = std::move(s1);
  317. ASSERT_TRUE(s1.data() == nullptr);
  318. ASSERT_TRUE(s1.size() == 0);
  319. ASSERT_TRUE(s2.size() == 3);
  320. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  321. ASSERT_TRUE(s1.is_stable());
  322. ASSERT_TRUE(s2.is_stable());
  323. return true;
  324. }
  325. static bool testOperatorBool()
  326. {
  327. std::cout << "testOperatorBool()\n";
  328. cm::String str;
  329. ASSERT_TRUE(!str);
  330. str = "";
  331. ASSERT_TRUE(str);
  332. str = static_cast<const char*>(nullptr);
  333. ASSERT_TRUE(!str);
  334. str = std::string();
  335. ASSERT_TRUE(str);
  336. str = nullptr;
  337. ASSERT_TRUE(!str);
  338. return true;
  339. }
  340. static bool testOperatorIndex()
  341. {
  342. std::cout << "testOperatorIndex()\n";
  343. cm::String str = "abc";
  344. ASSERT_TRUE(str[0] == 'a');
  345. ASSERT_TRUE(str[1] == 'b');
  346. ASSERT_TRUE(str[2] == 'c');
  347. return true;
  348. }
  349. static bool testOperatorPlusEqual()
  350. {
  351. std::cout << "testOperatorPlusEqual()\n";
  352. cm::String str = "a";
  353. str += "b";
  354. {
  355. const char* c = "c";
  356. str += c;
  357. }
  358. str += 'd';
  359. str += std::string("e");
  360. str += cm::string_view("f", 1);
  361. str += cm::String("g");
  362. ASSERT_TRUE(str.size() == 7);
  363. ASSERT_TRUE(std::strncmp(str.data(), "abcdefg", 7) == 0);
  364. ASSERT_TRUE(str.is_stable());
  365. return true;
  366. }
  367. static bool testOperatorCompare()
  368. {
  369. std::cout << "testOperatorCompare()\n";
  370. cm::String str = "b";
  371. {
  372. ASSERT_TRUE(str == "b");
  373. ASSERT_TRUE("b" == str);
  374. ASSERT_TRUE(str != "a");
  375. ASSERT_TRUE("a" != str);
  376. ASSERT_TRUE(str < "c");
  377. ASSERT_TRUE("a" < str);
  378. ASSERT_TRUE(str > "a");
  379. ASSERT_TRUE("c" > str);
  380. ASSERT_TRUE(str <= "b");
  381. ASSERT_TRUE("b" <= str);
  382. ASSERT_TRUE(str >= "b");
  383. ASSERT_TRUE("b" >= str);
  384. }
  385. {
  386. const char* a = "a";
  387. const char* b = "b";
  388. const char* c = "c";
  389. ASSERT_TRUE(str == b);
  390. ASSERT_TRUE(b == str);
  391. ASSERT_TRUE(str != a);
  392. ASSERT_TRUE(a != str);
  393. ASSERT_TRUE(str < c);
  394. ASSERT_TRUE(a < str);
  395. ASSERT_TRUE(str > a);
  396. ASSERT_TRUE(c > str);
  397. ASSERT_TRUE(str <= b);
  398. ASSERT_TRUE(b <= str);
  399. ASSERT_TRUE(str >= b);
  400. ASSERT_TRUE(b >= str);
  401. }
  402. {
  403. ASSERT_TRUE(str == 'b');
  404. ASSERT_TRUE('b' == str);
  405. ASSERT_TRUE(str != 'a');
  406. ASSERT_TRUE('a' != str);
  407. ASSERT_TRUE(str < 'c');
  408. ASSERT_TRUE('a' < str);
  409. ASSERT_TRUE(str > 'a');
  410. ASSERT_TRUE('c' > str);
  411. ASSERT_TRUE(str <= 'b');
  412. ASSERT_TRUE('b' <= str);
  413. ASSERT_TRUE(str >= 'b');
  414. ASSERT_TRUE('b' >= str);
  415. }
  416. {
  417. std::string const a = "a";
  418. std::string const b = "b";
  419. std::string const c = "c";
  420. ASSERT_TRUE(str == b);
  421. ASSERT_TRUE(b == str);
  422. ASSERT_TRUE(str != a);
  423. ASSERT_TRUE(a != str);
  424. ASSERT_TRUE(str < c);
  425. ASSERT_TRUE(a < str);
  426. ASSERT_TRUE(str > a);
  427. ASSERT_TRUE(c > str);
  428. ASSERT_TRUE(str <= b);
  429. ASSERT_TRUE(b <= str);
  430. ASSERT_TRUE(str >= b);
  431. ASSERT_TRUE(b >= str);
  432. }
  433. {
  434. cm::string_view const a("a", 1);
  435. cm::string_view const b("b", 1);
  436. cm::string_view const c("c", 1);
  437. ASSERT_TRUE(str == b);
  438. ASSERT_TRUE(b == str);
  439. ASSERT_TRUE(str != a);
  440. ASSERT_TRUE(a != str);
  441. ASSERT_TRUE(str < c);
  442. ASSERT_TRUE(a < str);
  443. ASSERT_TRUE(str > a);
  444. ASSERT_TRUE(c > str);
  445. ASSERT_TRUE(str <= b);
  446. ASSERT_TRUE(b <= str);
  447. ASSERT_TRUE(str >= b);
  448. ASSERT_TRUE(b >= str);
  449. }
  450. {
  451. cm::String const a("a");
  452. cm::String const b("b");
  453. cm::String const c("c");
  454. ASSERT_TRUE(str == b);
  455. ASSERT_TRUE(b == str);
  456. ASSERT_TRUE(str != a);
  457. ASSERT_TRUE(a != str);
  458. ASSERT_TRUE(str < c);
  459. ASSERT_TRUE(a < str);
  460. ASSERT_TRUE(str > a);
  461. ASSERT_TRUE(c > str);
  462. ASSERT_TRUE(str <= b);
  463. ASSERT_TRUE(b <= str);
  464. ASSERT_TRUE(str >= b);
  465. ASSERT_TRUE(b >= str);
  466. }
  467. return true;
  468. }
  469. static bool testOperatorStream()
  470. {
  471. std::cout << "testOperatorStream()\n";
  472. std::ostringstream ss;
  473. ss << "a" << cm::String("b") << 'c';
  474. ASSERT_TRUE(ss.str() == "abc");
  475. return true;
  476. }
  477. static bool testOperatorStdStringPlusEqual()
  478. {
  479. std::cout << "testOperatorStdStringPlusEqual()\n";
  480. std::string s = "a";
  481. s += cm::String("b");
  482. ASSERT_TRUE(s == "ab");
  483. return true;
  484. }
  485. static bool testMethod_borrow()
  486. {
  487. std::cout << "testMethod_borrow()\n";
  488. std::string s = "abc";
  489. cm::String str = cm::String::borrow(s);
  490. ASSERT_TRUE(str.data() == s.data());
  491. ASSERT_TRUE(str.size() == s.size());
  492. ASSERT_TRUE(str == s);
  493. return true;
  494. }
  495. static bool testMethod_view()
  496. {
  497. std::cout << "testMethod_view()\n";
  498. cm::String str;
  499. ASSERT_TRUE(str.view().data() == nullptr);
  500. ASSERT_TRUE(str.view().size() == 0);
  501. str = charArray;
  502. ASSERT_TRUE(str.view().data() != charArray);
  503. ASSERT_TRUE(str.view().size() == sizeof(charArray) - 1);
  504. str = std::string("abc");
  505. ASSERT_TRUE(str.view().size() == 3);
  506. ASSERT_TRUE(strncmp(str.view().data(), "abc", 3) == 0);
  507. return true;
  508. }
  509. static bool testMethod_empty()
  510. {
  511. std::cout << "testMethod_empty()\n";
  512. cm::String str;
  513. ASSERT_TRUE(str.empty());
  514. str = "";
  515. ASSERT_TRUE(str.empty());
  516. str = "abc";
  517. ASSERT_TRUE(!str.empty());
  518. str = std::string();
  519. ASSERT_TRUE(str.empty());
  520. str = std::string("abc");
  521. ASSERT_TRUE(!str.empty());
  522. return true;
  523. }
  524. static bool testMethod_length()
  525. {
  526. std::cout << "testMethod_length()\n";
  527. cm::String str;
  528. ASSERT_TRUE(str.length() == 0);
  529. str = "";
  530. ASSERT_TRUE(str.length() == 0);
  531. str = "abc";
  532. ASSERT_TRUE(str.length() == 3);
  533. str = std::string();
  534. ASSERT_TRUE(str.length() == 0);
  535. str = std::string("abc");
  536. ASSERT_TRUE(str.length() == 3);
  537. return true;
  538. }
  539. static bool testMethod_at()
  540. {
  541. std::cout << "testMethod_at()\n";
  542. cm::String str = "abc";
  543. ASSERT_TRUE(str.at(0) == 'a');
  544. ASSERT_TRUE(str.at(1) == 'b');
  545. ASSERT_TRUE(str.at(2) == 'c');
  546. bool except_out_of_range = false;
  547. try {
  548. str.at(3);
  549. } catch (std::out_of_range&) {
  550. except_out_of_range = true;
  551. }
  552. ASSERT_TRUE(except_out_of_range);
  553. return true;
  554. }
  555. static bool testMethod_front_back()
  556. {
  557. std::cout << "testMethod_front_back()\n";
  558. cm::String str = "abc";
  559. ASSERT_TRUE(str.front() == 'a');
  560. ASSERT_TRUE(str.back() == 'c');
  561. return true;
  562. }
  563. static bool testMethodIterators()
  564. {
  565. std::cout << "testMethodIterators()\n";
  566. cm::String str = "abc";
  567. ASSERT_TRUE(*str.begin() == 'a');
  568. ASSERT_TRUE(*(str.end() - 1) == 'c');
  569. ASSERT_TRUE(str.end() - str.begin() == 3);
  570. ASSERT_TRUE(*str.cbegin() == 'a');
  571. ASSERT_TRUE(*(str.cend() - 1) == 'c');
  572. ASSERT_TRUE(str.cend() - str.cbegin() == 3);
  573. ASSERT_TRUE(*str.rbegin() == 'c');
  574. ASSERT_TRUE(*(str.rend() - 1) == 'a');
  575. ASSERT_TRUE(str.rend() - str.rbegin() == 3);
  576. ASSERT_TRUE(*str.crbegin() == 'c');
  577. ASSERT_TRUE(*(str.crend() - 1) == 'a');
  578. ASSERT_TRUE(str.crend() - str.crbegin() == 3);
  579. return true;
  580. }
  581. static bool testMethod_clear()
  582. {
  583. std::cout << "testMethod_clear()\n";
  584. cm::String str = "abc";
  585. ASSERT_TRUE(!str.empty());
  586. str.clear();
  587. ASSERT_TRUE(str.empty());
  588. return true;
  589. }
  590. static bool testMethod_insert()
  591. {
  592. std::cout << "testMethod_insert()\n";
  593. cm::String str = "abc";
  594. str.insert(1, 2, 'd').insert(0, 1, '_');
  595. ASSERT_TRUE(str.size() == 6);
  596. ASSERT_TRUE(std::strncmp(str.data(), "_addbc", 6) == 0);
  597. return true;
  598. }
  599. static bool testMethod_erase()
  600. {
  601. std::cout << "testMethod_erase()\n";
  602. cm::String str = "abcdefg";
  603. str.erase(5).erase(1, 2);
  604. ASSERT_TRUE(str.size() == 3);
  605. ASSERT_TRUE(std::strncmp(str.data(), "ade", 3) == 0);
  606. return true;
  607. }
  608. static bool testMethod_push_back()
  609. {
  610. std::cout << "testMethod_push_back()\n";
  611. cm::String str = "abc";
  612. str.push_back('d');
  613. ASSERT_TRUE(str == "abcd");
  614. return true;
  615. }
  616. static bool testMethod_pop_back()
  617. {
  618. std::cout << "testMethod_pop_back()\n";
  619. cm::String str = "abc";
  620. str.pop_back();
  621. ASSERT_TRUE(str == "ab");
  622. return true;
  623. }
  624. static bool testMethod_replace()
  625. {
  626. std::cout << "testMethod_replace()\n";
  627. {
  628. cm::String str = "abcd";
  629. const char* bc = "bc";
  630. ASSERT_TRUE(str.replace(1, 2, "BC") == "aBCd");
  631. ASSERT_TRUE(str.replace(1, 2, bc) == "abcd");
  632. ASSERT_TRUE(str.replace(1, 2, 'x') == "axd");
  633. ASSERT_TRUE(str.replace(1, 1, std::string("bc")) == "abcd");
  634. ASSERT_TRUE(str.replace(1, 2, cm::string_view("BC", 2)) == "aBCd");
  635. ASSERT_TRUE(str.replace(1, 2, cm::String("bc")) == "abcd");
  636. }
  637. {
  638. cm::String str = "abcd";
  639. const char* bc = "bc";
  640. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, "BC") == "aBCd");
  641. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, bc) == "abcd");
  642. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 'x') == "axd");
  643. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 2,
  644. std::string("bc")) == "abcd");
  645. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3,
  646. cm::string_view("BC", 2)) == "aBCd");
  647. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3,
  648. cm::String("bc")) == "abcd");
  649. }
  650. {
  651. cm::String str = "abcd";
  652. const char* bc = "_bc";
  653. ASSERT_TRUE(str.replace(1, 2, "_BC_", 1, 2) == "aBCd");
  654. ASSERT_TRUE(str.replace(1, 2, bc, 1) == "abcd");
  655. ASSERT_TRUE(str.replace(1, 2, 'x', 0) == "axd");
  656. ASSERT_TRUE(str.replace(1, 1, std::string("_bc_"), 1, 2) == "abcd");
  657. ASSERT_TRUE(str.replace(1, 2, cm::string_view("_BC", 3), 1) == "aBCd");
  658. ASSERT_TRUE(str.replace(1, 2, cm::String("_bc_"), 1, 2) == "abcd");
  659. }
  660. {
  661. cm::String str = "abcd";
  662. const char* bc = "_bc_";
  663. ASSERT_TRUE(str.replace(1, 2, 2, 'x') == "axxd");
  664. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 2, 'y') ==
  665. "ayyd");
  666. ASSERT_TRUE(
  667. str.replace(str.begin() + 1, str.begin() + 3, bc + 1, bc + 3) == "abcd");
  668. }
  669. return true;
  670. }
  671. static bool testMethod_copy()
  672. {
  673. std::cout << "testMethod_copy()\n";
  674. cm::String str = "abc";
  675. char dest[2];
  676. cm::String::size_type n = str.copy(dest, 2, 1);
  677. ASSERT_TRUE(n == 2);
  678. ASSERT_TRUE(std::strncmp(dest, "bc", 2) == 0);
  679. n = str.copy(dest, 2);
  680. ASSERT_TRUE(n == 2);
  681. ASSERT_TRUE(std::strncmp(dest, "ab", 2) == 0);
  682. return true;
  683. }
  684. static bool testMethod_resize()
  685. {
  686. std::cout << "testMethod_resize()\n";
  687. cm::String str = "abc";
  688. str.resize(3);
  689. ASSERT_TRUE(str == "abc");
  690. str.resize(2);
  691. ASSERT_TRUE(str == "ab");
  692. str.resize(3, 'c');
  693. ASSERT_TRUE(str == "abc");
  694. return true;
  695. }
  696. static bool testMethod_swap()
  697. {
  698. std::cout << "testMethod_swap()\n";
  699. cm::String str1 = std::string("1");
  700. cm::String str2 = std::string("2");
  701. str1.swap(str2);
  702. ASSERT_TRUE(str1 == "2");
  703. ASSERT_TRUE(str2 == "1");
  704. return true;
  705. }
  706. static bool testMethod_substr_AtEnd(cm::String str)
  707. {
  708. cm::String substr = str.substr(1);
  709. ASSERT_TRUE(substr.data() == str.data() + 1);
  710. ASSERT_TRUE(substr.size() == 2);
  711. ASSERT_TRUE(!substr.is_stable());
  712. // c_str() at the end of the buffer does not internally mutate.
  713. ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0);
  714. ASSERT_TRUE(substr.c_str() == str.data() + 1);
  715. ASSERT_TRUE(substr.data() == str.data() + 1);
  716. ASSERT_TRUE(substr.size() == 2);
  717. ASSERT_TRUE(!substr.is_stable());
  718. // str() internally mutates.
  719. ASSERT_TRUE(substr.str() == "bc");
  720. ASSERT_TRUE(substr.is_stable());
  721. ASSERT_TRUE(substr.data() != str.data() + 1);
  722. ASSERT_TRUE(substr.size() == 2);
  723. ASSERT_TRUE(substr.c_str() != str.data() + 1);
  724. ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0);
  725. return true;
  726. }
  727. static bool testMethod_substr_AtEndBorrowed()
  728. {
  729. std::cout << "testMethod_substr_AtEndBorrowed()\n";
  730. return testMethod_substr_AtEnd("abc"_s);
  731. }
  732. static bool testMethod_substr_AtEndOwned()
  733. {
  734. std::cout << "testMethod_substr_AtEndOwned()\n";
  735. return testMethod_substr_AtEnd(std::string("abc"));
  736. }
  737. static bool testMethod_substr_AtStart(cm::String str)
  738. {
  739. {
  740. cm::String substr = str.substr(0, 2);
  741. ASSERT_TRUE(substr.data() == str.data());
  742. ASSERT_TRUE(substr.size() == 2);
  743. // c_str() not at the end of the buffer internally mutates.
  744. const char* substr_c = substr.c_str();
  745. ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0);
  746. ASSERT_TRUE(substr_c != str.data());
  747. ASSERT_TRUE(substr.data() != str.data());
  748. ASSERT_TRUE(substr.size() == 2);
  749. ASSERT_TRUE(substr.is_stable());
  750. // str() does not need to internally mutate after c_str() did so
  751. ASSERT_TRUE(substr.str() == "ab");
  752. ASSERT_TRUE(substr.is_stable());
  753. ASSERT_TRUE(substr.data() == substr_c);
  754. ASSERT_TRUE(substr.size() == 2);
  755. ASSERT_TRUE(substr.c_str() == substr_c);
  756. }
  757. {
  758. cm::String substr = str.substr(0, 2);
  759. ASSERT_TRUE(substr.data() == str.data());
  760. ASSERT_TRUE(substr.size() == 2);
  761. ASSERT_TRUE(!substr.is_stable());
  762. // str() internally mutates.
  763. ASSERT_TRUE(substr.str() == "ab");
  764. ASSERT_TRUE(substr.is_stable());
  765. ASSERT_TRUE(substr.data() != str.data());
  766. ASSERT_TRUE(substr.size() == 2);
  767. ASSERT_TRUE(substr.c_str() != str.data());
  768. // c_str() does not internally after str() did so
  769. const char* substr_c = substr.c_str();
  770. ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0);
  771. ASSERT_TRUE(substr_c == substr.data());
  772. ASSERT_TRUE(substr.size() == 2);
  773. ASSERT_TRUE(substr.is_stable());
  774. }
  775. return true;
  776. }
  777. static bool testMethod_substr_AtStartBorrowed()
  778. {
  779. std::cout << "testMethod_substr_AtStartBorrowed()\n";
  780. return testMethod_substr_AtStart("abc"_s);
  781. }
  782. static bool testMethod_substr_AtStartOwned()
  783. {
  784. std::cout << "testMethod_substr_AtStartOwned()\n";
  785. return testMethod_substr_AtStart(std::string("abc"));
  786. }
  787. static bool testMethod_compare()
  788. {
  789. std::cout << "testMethod_compare()\n";
  790. cm::String str = "b";
  791. ASSERT_TRUE(str.compare("a") > 0);
  792. ASSERT_TRUE(str.compare("b") == 0);
  793. ASSERT_TRUE(str.compare("c") < 0);
  794. {
  795. const char* a = "a";
  796. const char* b = "b";
  797. const char* c = "c";
  798. ASSERT_TRUE(str.compare(a) > 0);
  799. ASSERT_TRUE(str.compare(b) == 0);
  800. ASSERT_TRUE(str.compare(c) < 0);
  801. }
  802. ASSERT_TRUE(str.compare('a') > 0);
  803. ASSERT_TRUE(str.compare('b') == 0);
  804. ASSERT_TRUE(str.compare('c') < 0);
  805. ASSERT_TRUE(str.compare(std::string("a")) > 0);
  806. ASSERT_TRUE(str.compare(std::string("b")) == 0);
  807. ASSERT_TRUE(str.compare(std::string("c")) < 0);
  808. ASSERT_TRUE(str.compare(cm::string_view("a_", 1)) > 0);
  809. ASSERT_TRUE(str.compare(cm::string_view("b_", 1)) == 0);
  810. ASSERT_TRUE(str.compare(cm::string_view("c_", 1)) < 0);
  811. ASSERT_TRUE(str.compare(cm::String("a")) > 0);
  812. ASSERT_TRUE(str.compare(cm::String("b")) == 0);
  813. ASSERT_TRUE(str.compare(cm::String("c")) < 0);
  814. ASSERT_TRUE(str.compare(0, 1, cm::string_view("a", 1)) > 0);
  815. ASSERT_TRUE(str.compare(1, 0, cm::string_view("", 0)) == 0);
  816. ASSERT_TRUE(str.compare(0, 1, cm::string_view("ac", 2), 1, 1) < 0);
  817. ASSERT_TRUE(str.compare(1, 0, "") == 0);
  818. ASSERT_TRUE(str.compare(1, 0, "_", 0) == 0);
  819. return true;
  820. }
  821. static bool testMethod_find()
  822. {
  823. std::cout << "testMethod_find()\n";
  824. cm::String str = "abcabc";
  825. ASSERT_TRUE(str.find("a") == 0);
  826. ASSERT_TRUE(str.find("a", 1) == 3);
  827. {
  828. const char* a = "a";
  829. ASSERT_TRUE(str.find(a) == 0);
  830. ASSERT_TRUE(str.find(a, 1) == 3);
  831. }
  832. ASSERT_TRUE(str.find('a') == 0);
  833. ASSERT_TRUE(str.find('a', 1) == 3);
  834. ASSERT_TRUE(str.find(std::string("a")) == 0);
  835. ASSERT_TRUE(str.find(std::string("a"), 1) == 3);
  836. ASSERT_TRUE(str.find(cm::string_view("a_", 1)) == 0);
  837. ASSERT_TRUE(str.find(cm::string_view("a_", 1), 1) == 3);
  838. ASSERT_TRUE(str.find(cm::String("a")) == 0);
  839. ASSERT_TRUE(str.find(cm::String("a"), 1) == 3);
  840. ASSERT_TRUE(str.find("ab_", 1, 2) == 3);
  841. return true;
  842. }
  843. static bool testMethod_rfind()
  844. {
  845. std::cout << "testMethod_rfind()\n";
  846. cm::String str = "abcabc";
  847. ASSERT_TRUE(str.rfind("a") == 3);
  848. ASSERT_TRUE(str.rfind("a", 1) == 0);
  849. {
  850. const char* a = "a";
  851. ASSERT_TRUE(str.rfind(a) == 3);
  852. ASSERT_TRUE(str.rfind(a, 1) == 0);
  853. }
  854. ASSERT_TRUE(str.rfind('a') == 3);
  855. ASSERT_TRUE(str.rfind('a', 1) == 0);
  856. ASSERT_TRUE(str.rfind(std::string("a")) == 3);
  857. ASSERT_TRUE(str.rfind(std::string("a"), 1) == 0);
  858. ASSERT_TRUE(str.rfind(cm::string_view("a_", 1)) == 3);
  859. ASSERT_TRUE(str.rfind(cm::string_view("a_", 1), 1) == 0);
  860. ASSERT_TRUE(str.rfind(cm::String("a")) == 3);
  861. ASSERT_TRUE(str.rfind(cm::String("a"), 1) == 0);
  862. ASSERT_TRUE(str.rfind("ab_", 1, 2) == 0);
  863. return true;
  864. }
  865. static bool testMethod_find_first_of()
  866. {
  867. std::cout << "testMethod_find_first_of()\n";
  868. cm::String str = "abcabc";
  869. ASSERT_TRUE(str.find_first_of("_a") == 0);
  870. ASSERT_TRUE(str.find_first_of("_a", 1) == 3);
  871. {
  872. const char* a = "_a";
  873. ASSERT_TRUE(str.find_first_of(a) == 0);
  874. ASSERT_TRUE(str.find_first_of(a, 1) == 3);
  875. }
  876. ASSERT_TRUE(str.find_first_of('a') == 0);
  877. ASSERT_TRUE(str.find_first_of('a', 1) == 3);
  878. ASSERT_TRUE(str.find_first_of(std::string("_a")) == 0);
  879. ASSERT_TRUE(str.find_first_of(std::string("_a"), 1) == 3);
  880. ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1)) == 1);
  881. ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1), 2) == 4);
  882. ASSERT_TRUE(str.find_first_of(cm::String("ab")) == 0);
  883. ASSERT_TRUE(str.find_first_of(cm::String("ab"), 2) == 3);
  884. ASSERT_TRUE(str.find_first_of("_ab", 1, 2) == 3);
  885. return true;
  886. }
  887. static bool testMethod_find_first_not_of()
  888. {
  889. std::cout << "testMethod_find_first_not_of()\n";
  890. cm::String str = "abcabc";
  891. ASSERT_TRUE(str.find_first_not_of("_a") == 1);
  892. ASSERT_TRUE(str.find_first_not_of("_a", 2) == 2);
  893. {
  894. const char* a = "_a";
  895. ASSERT_TRUE(str.find_first_not_of(a) == 1);
  896. ASSERT_TRUE(str.find_first_not_of(a, 2) == 2);
  897. }
  898. ASSERT_TRUE(str.find_first_not_of('a') == 1);
  899. ASSERT_TRUE(str.find_first_not_of('a', 2) == 2);
  900. ASSERT_TRUE(str.find_first_not_of(std::string("_a")) == 1);
  901. ASSERT_TRUE(str.find_first_not_of(std::string("_a"), 2) == 2);
  902. ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1)) == 0);
  903. ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1), 1) == 2);
  904. ASSERT_TRUE(str.find_first_not_of(cm::String("_a")) == 1);
  905. ASSERT_TRUE(str.find_first_not_of(cm::String("_a"), 2) == 2);
  906. ASSERT_TRUE(str.find_first_not_of("_bca", 1, 3) == 3);
  907. return true;
  908. }
  909. static bool testMethod_find_last_of()
  910. {
  911. std::cout << "testMethod_find_last_of()\n";
  912. cm::String str = "abcabc";
  913. ASSERT_TRUE(str.find_last_of("_a") == 3);
  914. ASSERT_TRUE(str.find_last_of("_a", 1) == 0);
  915. {
  916. const char* a = "_a";
  917. ASSERT_TRUE(str.find_last_of(a) == 3);
  918. ASSERT_TRUE(str.find_last_of(a, 1) == 0);
  919. }
  920. ASSERT_TRUE(str.find_last_of('a') == 3);
  921. ASSERT_TRUE(str.find_last_of('a', 1) == 0);
  922. ASSERT_TRUE(str.find_last_of(std::string("_a")) == 3);
  923. ASSERT_TRUE(str.find_last_of(std::string("_a"), 1) == 0);
  924. ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1)) == 4);
  925. ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1), 2) == 1);
  926. ASSERT_TRUE(str.find_last_of(cm::String("ab")) == 4);
  927. ASSERT_TRUE(str.find_last_of(cm::String("ab"), 2) == 1);
  928. ASSERT_TRUE(str.find_last_of("_ab", 1, 2) == 0);
  929. return true;
  930. }
  931. static bool testMethod_find_last_not_of()
  932. {
  933. std::cout << "testMethod_find_last_not_of()\n";
  934. cm::String str = "abcabc";
  935. ASSERT_TRUE(str.find_last_not_of("_a") == 5);
  936. ASSERT_TRUE(str.find_last_not_of("_a", 1) == 1);
  937. {
  938. const char* a = "_a";
  939. ASSERT_TRUE(str.find_last_not_of(a) == 5);
  940. ASSERT_TRUE(str.find_last_not_of(a, 1) == 1);
  941. }
  942. ASSERT_TRUE(str.find_last_not_of('a') == 5);
  943. ASSERT_TRUE(str.find_last_not_of('a', 1) == 1);
  944. ASSERT_TRUE(str.find_last_not_of(std::string("_a")) == 5);
  945. ASSERT_TRUE(str.find_last_not_of(std::string("_a"), 1) == 1);
  946. ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1)) == 4);
  947. ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1), 2) == 1);
  948. ASSERT_TRUE(str.find_last_not_of(cm::String("_a")) == 5);
  949. ASSERT_TRUE(str.find_last_not_of(cm::String("_a"), 1) == 1);
  950. ASSERT_TRUE(str.find_last_not_of("cb_", 2, 2) == 0);
  951. return true;
  952. }
  953. static bool testAddition()
  954. {
  955. std::cout << "testAddition()\n";
  956. {
  957. ASSERT_TRUE(cm::String("a") + "b" == "ab");
  958. ASSERT_TRUE("ab" == "a" + cm::String("b"));
  959. ASSERT_TRUE("a" + cm::String("b") + "c" == "abc");
  960. ASSERT_TRUE("abc" == "a" + cm::String("b") + "c");
  961. ASSERT_TRUE("a" + (cm::String("b") + "c") + "d" == "abcd");
  962. ASSERT_TRUE("abcd" == "a" + (cm::String("b") + "c") + "d");
  963. }
  964. {
  965. ASSERT_TRUE(cm::String("a"_s) + "b"_s == "ab"_s);
  966. ASSERT_TRUE("ab"_s == "a"_s + cm::String("b"_s));
  967. ASSERT_TRUE("a"_s + cm::String("b"_s) + "c"_s == "abc"_s);
  968. ASSERT_TRUE("abc"_s == "a"_s + cm::String("b"_s) + "c"_s);
  969. ASSERT_TRUE("a"_s + (cm::String("b"_s) + "c"_s) + "d"_s == "abcd"_s);
  970. ASSERT_TRUE("abcd"_s == "a"_s + (cm::String("b"_s) + "c"_s) + "d"_s);
  971. }
  972. {
  973. const char* a = "a";
  974. const char* b = "b";
  975. const char* ab = "ab";
  976. ASSERT_TRUE(cm::String(a) + b == ab);
  977. ASSERT_TRUE(ab == a + cm::String(b));
  978. const char* c = "c";
  979. const char* abc = "abc";
  980. ASSERT_TRUE(a + cm::String(b) + c == abc);
  981. ASSERT_TRUE(abc == a + cm::String(b) + c);
  982. const char* d = "d";
  983. const char* abcd = "abcd";
  984. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  985. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  986. }
  987. {
  988. ASSERT_TRUE(cm::String('a') + 'b' == "ab");
  989. ASSERT_TRUE("ab" == 'a' + cm::String('b'));
  990. ASSERT_TRUE('a' + cm::String('b') + 'c' == "abc");
  991. ASSERT_TRUE("abc" == 'a' + cm::String('b') + 'c');
  992. ASSERT_TRUE('a' + (cm::String('b') + 'c') + 'd' == "abcd");
  993. ASSERT_TRUE("abcd" == 'a' + (cm::String('b') + 'c') + 'd');
  994. }
  995. {
  996. std::string a = "a";
  997. std::string b = "b";
  998. std::string ab = "ab";
  999. ASSERT_TRUE(cm::String(a) + b == ab);
  1000. ASSERT_TRUE(ab == a + cm::String(b));
  1001. std::string c = "c";
  1002. std::string abc = "abc";
  1003. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1004. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1005. std::string d = "d";
  1006. std::string abcd = "abcd";
  1007. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1008. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1009. }
  1010. {
  1011. cm::string_view a("a", 1);
  1012. cm::string_view b("b", 1);
  1013. cm::string_view ab("ab", 2);
  1014. ASSERT_TRUE(cm::String(a) + b == ab);
  1015. ASSERT_TRUE(ab == a + cm::String(b));
  1016. cm::string_view c("c", 1);
  1017. cm::string_view abc("abc", 3);
  1018. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1019. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1020. cm::string_view d("d", 1);
  1021. cm::string_view abcd("abcd", 4);
  1022. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1023. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1024. }
  1025. {
  1026. cm::String a = "a";
  1027. cm::String b = "b";
  1028. cm::String ab = "ab";
  1029. ASSERT_TRUE(a + b == ab);
  1030. ASSERT_TRUE(ab == a + b);
  1031. cm::String c = "c";
  1032. cm::String abc = "abc";
  1033. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1034. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1035. cm::String d = "d";
  1036. cm::String abcd = "abcd";
  1037. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1038. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1039. }
  1040. {
  1041. cm::String str;
  1042. str += "a" + cm::String("b") + 'c';
  1043. ASSERT_TRUE(str == "abc");
  1044. ASSERT_TRUE(str.is_stable());
  1045. }
  1046. {
  1047. std::string s;
  1048. s += "a" + cm::String("b") + 'c';
  1049. ASSERT_TRUE(s == "abc");
  1050. }
  1051. {
  1052. std::ostringstream ss;
  1053. ss << ("a" + cm::String("b") + 'c');
  1054. ASSERT_TRUE(ss.str() == "abc");
  1055. }
  1056. return true;
  1057. }
  1058. static bool testStability()
  1059. {
  1060. std::cout << "testStability()\n";
  1061. cm::String str = "abc"_s;
  1062. ASSERT_TRUE(!str.is_stable());
  1063. ASSERT_TRUE(str.str_if_stable() == nullptr);
  1064. str.stabilize();
  1065. ASSERT_TRUE(str.is_stable());
  1066. std::string const* str_if_stable = str.str_if_stable();
  1067. ASSERT_TRUE(str_if_stable != nullptr);
  1068. ASSERT_TRUE(*str_if_stable == "abc");
  1069. str.stabilize();
  1070. ASSERT_TRUE(str.is_stable());
  1071. ASSERT_TRUE(str.str_if_stable() == str_if_stable);
  1072. return true;
  1073. }
  1074. int testString(int /*unused*/, char* /*unused*/ [])
  1075. {
  1076. if (!testConstructDefault()) {
  1077. return 1;
  1078. }
  1079. if (!testConstructFromNullPtr()) {
  1080. return 1;
  1081. }
  1082. if (!testConstructFromCStrNull()) {
  1083. return 1;
  1084. }
  1085. if (!testConstructFromCharArray()) {
  1086. return 1;
  1087. }
  1088. if (!testConstructFromCStr()) {
  1089. return 1;
  1090. }
  1091. if (!testConstructFromStdString()) {
  1092. return 1;
  1093. }
  1094. if (!testConstructFromView()) {
  1095. return 1;
  1096. }
  1097. if (!testConstructFromChar()) {
  1098. return 1;
  1099. }
  1100. if (!testConstructFromInitList()) {
  1101. return 1;
  1102. }
  1103. if (!testConstructFromBuffer()) {
  1104. return 1;
  1105. }
  1106. if (!testConstructFromInputIterator()) {
  1107. return 1;
  1108. }
  1109. if (!testConstructFromN()) {
  1110. return 1;
  1111. }
  1112. if (!testConstructFromStaticStringView()) {
  1113. return 1;
  1114. }
  1115. if (!testConstructCopy()) {
  1116. return 1;
  1117. }
  1118. if (!testConstructMove()) {
  1119. return 1;
  1120. }
  1121. if (!testAssignCopy()) {
  1122. return 1;
  1123. }
  1124. if (!testAssignMove()) {
  1125. return 1;
  1126. }
  1127. if (!testAssignFromChar()) {
  1128. return 1;
  1129. }
  1130. if (!testAssignFromView()) {
  1131. return 1;
  1132. }
  1133. if (!testAssignFromStdString()) {
  1134. return 1;
  1135. }
  1136. if (!testAssignFromCStr()) {
  1137. return 1;
  1138. }
  1139. if (!testAssignFromCharArray()) {
  1140. return 1;
  1141. }
  1142. if (!testAssignFromCStrNull()) {
  1143. return 1;
  1144. }
  1145. if (!testAssignFromNullPtr()) {
  1146. return 1;
  1147. }
  1148. if (!testAssignFromInitList()) {
  1149. return 1;
  1150. }
  1151. if (!testAssignFromStaticStringView()) {
  1152. return 1;
  1153. }
  1154. if (!testOperatorBool()) {
  1155. return 1;
  1156. }
  1157. if (!testOperatorIndex()) {
  1158. return 1;
  1159. }
  1160. if (!testOperatorPlusEqual()) {
  1161. return 1;
  1162. }
  1163. if (!testOperatorCompare()) {
  1164. return 1;
  1165. }
  1166. if (!testOperatorStream()) {
  1167. return 1;
  1168. }
  1169. if (!testOperatorStdStringPlusEqual()) {
  1170. return 1;
  1171. }
  1172. if (!testMethod_borrow()) {
  1173. return 1;
  1174. }
  1175. if (!testMethod_view()) {
  1176. return 1;
  1177. }
  1178. if (!testMethod_empty()) {
  1179. return 1;
  1180. }
  1181. if (!testMethod_length()) {
  1182. return 1;
  1183. }
  1184. if (!testMethod_at()) {
  1185. return 1;
  1186. }
  1187. if (!testMethod_front_back()) {
  1188. return 1;
  1189. }
  1190. if (!testMethod_clear()) {
  1191. return 1;
  1192. }
  1193. if (!testMethod_insert()) {
  1194. return 1;
  1195. }
  1196. if (!testMethod_erase()) {
  1197. return 1;
  1198. }
  1199. if (!testMethod_push_back()) {
  1200. return 1;
  1201. }
  1202. if (!testMethod_pop_back()) {
  1203. return 1;
  1204. }
  1205. if (!testMethod_replace()) {
  1206. return 1;
  1207. }
  1208. if (!testMethod_copy()) {
  1209. return 1;
  1210. }
  1211. if (!testMethod_resize()) {
  1212. return 1;
  1213. }
  1214. if (!testMethod_swap()) {
  1215. return 1;
  1216. }
  1217. if (!testMethodIterators()) {
  1218. return 1;
  1219. }
  1220. if (!testMethod_substr_AtEndBorrowed()) {
  1221. return 1;
  1222. }
  1223. if (!testMethod_substr_AtEndOwned()) {
  1224. return 1;
  1225. }
  1226. if (!testMethod_substr_AtStartBorrowed()) {
  1227. return 1;
  1228. }
  1229. if (!testMethod_substr_AtStartOwned()) {
  1230. return 1;
  1231. }
  1232. if (!testMethod_compare()) {
  1233. return 1;
  1234. }
  1235. if (!testMethod_find()) {
  1236. return 1;
  1237. }
  1238. if (!testMethod_rfind()) {
  1239. return 1;
  1240. }
  1241. if (!testMethod_find_first_of()) {
  1242. return 1;
  1243. }
  1244. if (!testMethod_find_first_not_of()) {
  1245. return 1;
  1246. }
  1247. if (!testMethod_find_last_of()) {
  1248. return 1;
  1249. }
  1250. if (!testMethod_find_last_not_of()) {
  1251. return 1;
  1252. }
  1253. if (!testAddition()) {
  1254. return 1;
  1255. }
  1256. if (!testStability()) {
  1257. return 1;
  1258. }
  1259. return 0;
  1260. }