testString.cxx 35 KB

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