testString.cxx 35 KB

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