1
0

testString.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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> // IWYU pragma: keep
  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 <cmext/string_view>
  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. #ifndef __clang_analyzer__ /* cplusplus.Move */
  291. ASSERT_TRUE(s1.data() == nullptr);
  292. ASSERT_TRUE(s1.size() == 0);
  293. ASSERT_TRUE(s2.size() == 3);
  294. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  295. ASSERT_TRUE(s1.is_stable());
  296. ASSERT_TRUE(s2.is_stable());
  297. #endif
  298. return true;
  299. }
  300. static bool testAssignCopy()
  301. {
  302. std::cout << "testAssignCopy()\n";
  303. cm::String s1 = std::string("abc");
  304. cm::String s2;
  305. s2 = s1;
  306. ASSERT_TRUE(s1.data() == s2.data());
  307. ASSERT_TRUE(s1.size() == 3);
  308. ASSERT_TRUE(s2.size() == 3);
  309. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  310. ASSERT_TRUE(s1.is_stable());
  311. ASSERT_TRUE(s2.is_stable());
  312. return true;
  313. }
  314. static bool testAssignMove()
  315. {
  316. std::cout << "testAssignMove()\n";
  317. cm::String s1 = std::string("abc");
  318. cm::String s2;
  319. s2 = std::move(s1);
  320. #ifndef __clang_analyzer__ /* cplusplus.Move */
  321. ASSERT_TRUE(s1.data() == nullptr);
  322. ASSERT_TRUE(s1.size() == 0);
  323. ASSERT_TRUE(s2.size() == 3);
  324. ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0);
  325. ASSERT_TRUE(s1.is_stable());
  326. ASSERT_TRUE(s2.is_stable());
  327. #endif
  328. return true;
  329. }
  330. static bool testOperatorBool()
  331. {
  332. std::cout << "testOperatorBool()\n";
  333. cm::String str;
  334. ASSERT_TRUE(!str);
  335. str = "";
  336. ASSERT_TRUE(str);
  337. str = static_cast<const char*>(nullptr);
  338. ASSERT_TRUE(!str);
  339. str = std::string();
  340. ASSERT_TRUE(str);
  341. str = nullptr;
  342. ASSERT_TRUE(!str);
  343. return true;
  344. }
  345. static bool testOperatorIndex()
  346. {
  347. std::cout << "testOperatorIndex()\n";
  348. cm::String str = "abc";
  349. ASSERT_TRUE(str[0] == 'a');
  350. ASSERT_TRUE(str[1] == 'b');
  351. ASSERT_TRUE(str[2] == 'c');
  352. return true;
  353. }
  354. static bool testOperatorPlusEqual()
  355. {
  356. std::cout << "testOperatorPlusEqual()\n";
  357. cm::String str = "a";
  358. str += "b";
  359. {
  360. const char* c = "c";
  361. str += c;
  362. }
  363. str += 'd';
  364. str += std::string("e");
  365. str += cm::string_view("f", 1);
  366. str += cm::String("g");
  367. ASSERT_TRUE(str.size() == 7);
  368. ASSERT_TRUE(std::strncmp(str.data(), "abcdefg", 7) == 0);
  369. ASSERT_TRUE(str.is_stable());
  370. return true;
  371. }
  372. static bool testOperatorCompare()
  373. {
  374. std::cout << "testOperatorCompare()\n";
  375. cm::String str = "b";
  376. {
  377. ASSERT_TRUE(str == "b");
  378. ASSERT_TRUE("b" == str);
  379. ASSERT_TRUE(str != "a");
  380. ASSERT_TRUE("a" != str);
  381. ASSERT_TRUE(str < "c");
  382. ASSERT_TRUE("a" < str);
  383. ASSERT_TRUE(str > "a");
  384. ASSERT_TRUE("c" > str);
  385. ASSERT_TRUE(str <= "b");
  386. ASSERT_TRUE("b" <= str);
  387. ASSERT_TRUE(str >= "b");
  388. ASSERT_TRUE("b" >= str);
  389. }
  390. {
  391. const char* a = "a";
  392. const char* b = "b";
  393. const char* c = "c";
  394. ASSERT_TRUE(str == b);
  395. ASSERT_TRUE(b == str);
  396. ASSERT_TRUE(str != a);
  397. ASSERT_TRUE(a != str);
  398. ASSERT_TRUE(str < c);
  399. ASSERT_TRUE(a < str);
  400. ASSERT_TRUE(str > a);
  401. ASSERT_TRUE(c > str);
  402. ASSERT_TRUE(str <= b);
  403. ASSERT_TRUE(b <= str);
  404. ASSERT_TRUE(str >= b);
  405. ASSERT_TRUE(b >= str);
  406. }
  407. {
  408. ASSERT_TRUE(str == 'b');
  409. ASSERT_TRUE('b' == str);
  410. ASSERT_TRUE(str != 'a');
  411. ASSERT_TRUE('a' != str);
  412. ASSERT_TRUE(str < 'c');
  413. ASSERT_TRUE('a' < str);
  414. ASSERT_TRUE(str > 'a');
  415. ASSERT_TRUE('c' > str);
  416. ASSERT_TRUE(str <= 'b');
  417. ASSERT_TRUE('b' <= str);
  418. ASSERT_TRUE(str >= 'b');
  419. ASSERT_TRUE('b' >= str);
  420. }
  421. {
  422. std::string const a = "a";
  423. std::string const b = "b";
  424. std::string const c = "c";
  425. ASSERT_TRUE(str == b);
  426. ASSERT_TRUE(b == str);
  427. ASSERT_TRUE(str != a);
  428. ASSERT_TRUE(a != str);
  429. ASSERT_TRUE(str < c);
  430. ASSERT_TRUE(a < str);
  431. ASSERT_TRUE(str > a);
  432. ASSERT_TRUE(c > str);
  433. ASSERT_TRUE(str <= b);
  434. ASSERT_TRUE(b <= str);
  435. ASSERT_TRUE(str >= b);
  436. ASSERT_TRUE(b >= str);
  437. }
  438. {
  439. cm::string_view const a("a", 1);
  440. cm::string_view const b("b", 1);
  441. cm::string_view const c("c", 1);
  442. ASSERT_TRUE(str == b);
  443. ASSERT_TRUE(b == str);
  444. ASSERT_TRUE(str != a);
  445. ASSERT_TRUE(a != str);
  446. ASSERT_TRUE(str < c);
  447. ASSERT_TRUE(a < str);
  448. ASSERT_TRUE(str > a);
  449. ASSERT_TRUE(c > str);
  450. ASSERT_TRUE(str <= b);
  451. ASSERT_TRUE(b <= str);
  452. ASSERT_TRUE(str >= b);
  453. ASSERT_TRUE(b >= str);
  454. }
  455. {
  456. cm::String const a("a");
  457. cm::String const b("b");
  458. cm::String const c("c");
  459. ASSERT_TRUE(str == b);
  460. ASSERT_TRUE(b == str);
  461. ASSERT_TRUE(str != a);
  462. ASSERT_TRUE(a != str);
  463. ASSERT_TRUE(str < c);
  464. ASSERT_TRUE(a < str);
  465. ASSERT_TRUE(str > a);
  466. ASSERT_TRUE(c > str);
  467. ASSERT_TRUE(str <= b);
  468. ASSERT_TRUE(b <= str);
  469. ASSERT_TRUE(str >= b);
  470. ASSERT_TRUE(b >= str);
  471. }
  472. return true;
  473. }
  474. static bool testOperatorStream()
  475. {
  476. std::cout << "testOperatorStream()\n";
  477. std::ostringstream ss;
  478. ss << "a" << cm::String("b") << 'c';
  479. ASSERT_TRUE(ss.str() == "abc");
  480. return true;
  481. }
  482. static bool testOperatorStdStringPlusEqual()
  483. {
  484. std::cout << "testOperatorStdStringPlusEqual()\n";
  485. std::string s = "a";
  486. s += cm::String("b");
  487. ASSERT_TRUE(s == "ab");
  488. return true;
  489. }
  490. static bool testMethod_borrow()
  491. {
  492. std::cout << "testMethod_borrow()\n";
  493. std::string s = "abc";
  494. cm::String str = cm::String::borrow(s);
  495. ASSERT_TRUE(str.data() == s.data());
  496. ASSERT_TRUE(str.size() == s.size());
  497. ASSERT_TRUE(str == s);
  498. return true;
  499. }
  500. static bool testMethod_view()
  501. {
  502. std::cout << "testMethod_view()\n";
  503. cm::String str;
  504. ASSERT_TRUE(str.view().data() == nullptr);
  505. ASSERT_TRUE(str.view().size() == 0);
  506. str = charArray;
  507. ASSERT_TRUE(str.view().data() != charArray);
  508. ASSERT_TRUE(str.view().size() == sizeof(charArray) - 1);
  509. str = std::string("abc");
  510. ASSERT_TRUE(str.view().size() == 3);
  511. ASSERT_TRUE(strncmp(str.view().data(), "abc", 3) == 0);
  512. return true;
  513. }
  514. static bool testMethod_empty()
  515. {
  516. std::cout << "testMethod_empty()\n";
  517. cm::String str;
  518. ASSERT_TRUE(str.empty());
  519. str = "";
  520. ASSERT_TRUE(str.empty());
  521. str = "abc";
  522. ASSERT_TRUE(!str.empty());
  523. str = std::string();
  524. ASSERT_TRUE(str.empty());
  525. str = std::string("abc");
  526. ASSERT_TRUE(!str.empty());
  527. return true;
  528. }
  529. static bool testMethod_length()
  530. {
  531. std::cout << "testMethod_length()\n";
  532. cm::String str;
  533. ASSERT_TRUE(str.length() == 0);
  534. str = "";
  535. ASSERT_TRUE(str.length() == 0);
  536. str = "abc";
  537. ASSERT_TRUE(str.length() == 3);
  538. str = std::string();
  539. ASSERT_TRUE(str.length() == 0);
  540. str = std::string("abc");
  541. ASSERT_TRUE(str.length() == 3);
  542. return true;
  543. }
  544. static bool testMethod_at()
  545. {
  546. std::cout << "testMethod_at()\n";
  547. cm::String str = "abc";
  548. ASSERT_TRUE(str.at(0) == 'a');
  549. ASSERT_TRUE(str.at(1) == 'b');
  550. ASSERT_TRUE(str.at(2) == 'c');
  551. bool except_out_of_range = false;
  552. try {
  553. str.at(3);
  554. } catch (std::out_of_range&) {
  555. except_out_of_range = true;
  556. }
  557. ASSERT_TRUE(except_out_of_range);
  558. return true;
  559. }
  560. static bool testMethod_front_back()
  561. {
  562. std::cout << "testMethod_front_back()\n";
  563. cm::String str = "abc";
  564. ASSERT_TRUE(str.front() == 'a');
  565. ASSERT_TRUE(str.back() == 'c');
  566. return true;
  567. }
  568. static bool testMethodIterators()
  569. {
  570. std::cout << "testMethodIterators()\n";
  571. cm::String str = "abc";
  572. ASSERT_TRUE(*str.begin() == 'a');
  573. ASSERT_TRUE(*(str.end() - 1) == 'c');
  574. ASSERT_TRUE(str.end() - str.begin() == 3);
  575. ASSERT_TRUE(*str.cbegin() == 'a');
  576. ASSERT_TRUE(*(str.cend() - 1) == 'c');
  577. ASSERT_TRUE(str.cend() - str.cbegin() == 3);
  578. ASSERT_TRUE(*str.rbegin() == 'c');
  579. ASSERT_TRUE(*(str.rend() - 1) == 'a');
  580. ASSERT_TRUE(str.rend() - str.rbegin() == 3);
  581. ASSERT_TRUE(*str.crbegin() == 'c');
  582. ASSERT_TRUE(*(str.crend() - 1) == 'a');
  583. ASSERT_TRUE(str.crend() - str.crbegin() == 3);
  584. return true;
  585. }
  586. static bool testMethod_clear()
  587. {
  588. std::cout << "testMethod_clear()\n";
  589. cm::String str = "abc";
  590. ASSERT_TRUE(!str.empty());
  591. str.clear();
  592. ASSERT_TRUE(str.empty());
  593. return true;
  594. }
  595. static bool testMethod_insert()
  596. {
  597. std::cout << "testMethod_insert()\n";
  598. cm::String str = "abc";
  599. str.insert(1, 2, 'd').insert(0, 1, '_');
  600. ASSERT_TRUE(str.size() == 6);
  601. ASSERT_TRUE(std::strncmp(str.data(), "_addbc", 6) == 0);
  602. return true;
  603. }
  604. static bool testMethod_erase()
  605. {
  606. std::cout << "testMethod_erase()\n";
  607. cm::String str = "abcdefg";
  608. str.erase(5).erase(1, 2);
  609. ASSERT_TRUE(str.size() == 3);
  610. ASSERT_TRUE(std::strncmp(str.data(), "ade", 3) == 0);
  611. return true;
  612. }
  613. static bool testMethod_push_back()
  614. {
  615. std::cout << "testMethod_push_back()\n";
  616. cm::String str = "abc";
  617. str.push_back('d');
  618. ASSERT_TRUE(str == "abcd");
  619. return true;
  620. }
  621. static bool testMethod_pop_back()
  622. {
  623. std::cout << "testMethod_pop_back()\n";
  624. cm::String str = "abc";
  625. str.pop_back();
  626. ASSERT_TRUE(str == "ab");
  627. return true;
  628. }
  629. static bool testMethod_replace()
  630. {
  631. std::cout << "testMethod_replace()\n";
  632. {
  633. cm::String str = "abcd";
  634. const char* bc = "bc";
  635. ASSERT_TRUE(str.replace(1, 2, "BC") == "aBCd");
  636. ASSERT_TRUE(str.replace(1, 2, bc) == "abcd");
  637. ASSERT_TRUE(str.replace(1, 2, 'x') == "axd");
  638. ASSERT_TRUE(str.replace(1, 1, std::string("bc")) == "abcd");
  639. ASSERT_TRUE(str.replace(1, 2, cm::string_view("BC", 2)) == "aBCd");
  640. ASSERT_TRUE(str.replace(1, 2, cm::String("bc")) == "abcd");
  641. }
  642. {
  643. cm::String str = "abcd";
  644. const char* bc = "bc";
  645. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, "BC") == "aBCd");
  646. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, bc) == "abcd");
  647. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 'x') == "axd");
  648. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 2,
  649. std::string("bc")) == "abcd");
  650. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3,
  651. cm::string_view("BC", 2)) == "aBCd");
  652. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3,
  653. cm::String("bc")) == "abcd");
  654. }
  655. {
  656. cm::String str = "abcd";
  657. const char* bc = "_bc";
  658. ASSERT_TRUE(str.replace(1, 2, "_BC_", 1, 2) == "aBCd");
  659. ASSERT_TRUE(str.replace(1, 2, bc, 1) == "abcd");
  660. ASSERT_TRUE(str.replace(1, 2, 'x', 0) == "axd");
  661. ASSERT_TRUE(str.replace(1, 1, std::string("_bc_"), 1, 2) == "abcd");
  662. ASSERT_TRUE(str.replace(1, 2, cm::string_view("_BC", 3), 1) == "aBCd");
  663. ASSERT_TRUE(str.replace(1, 2, cm::String("_bc_"), 1, 2) == "abcd");
  664. }
  665. {
  666. cm::String str = "abcd";
  667. const char* bc = "_bc_";
  668. ASSERT_TRUE(str.replace(1, 2, 2, 'x') == "axxd");
  669. ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 2, 'y') ==
  670. "ayyd");
  671. ASSERT_TRUE(
  672. str.replace(str.begin() + 1, str.begin() + 3, bc + 1, bc + 3) == "abcd");
  673. }
  674. return true;
  675. }
  676. static bool testMethod_copy()
  677. {
  678. std::cout << "testMethod_copy()\n";
  679. cm::String str = "abc";
  680. char dest[2];
  681. cm::String::size_type n = str.copy(dest, 2, 1);
  682. ASSERT_TRUE(n == 2);
  683. ASSERT_TRUE(std::strncmp(dest, "bc", 2) == 0);
  684. n = str.copy(dest, 2);
  685. ASSERT_TRUE(n == 2);
  686. ASSERT_TRUE(std::strncmp(dest, "ab", 2) == 0);
  687. return true;
  688. }
  689. static bool testMethod_resize()
  690. {
  691. std::cout << "testMethod_resize()\n";
  692. cm::String str = "abc";
  693. str.resize(3);
  694. ASSERT_TRUE(str == "abc");
  695. str.resize(2);
  696. ASSERT_TRUE(str == "ab");
  697. str.resize(3, 'c');
  698. ASSERT_TRUE(str == "abc");
  699. return true;
  700. }
  701. static bool testMethod_swap()
  702. {
  703. std::cout << "testMethod_swap()\n";
  704. cm::String str1 = std::string("1");
  705. cm::String str2 = std::string("2");
  706. str1.swap(str2);
  707. ASSERT_TRUE(str1 == "2");
  708. ASSERT_TRUE(str2 == "1");
  709. return true;
  710. }
  711. static bool testMethod_substr_AtEnd(cm::String str)
  712. {
  713. cm::String substr = str.substr(1);
  714. ASSERT_TRUE(substr.data() == str.data() + 1);
  715. ASSERT_TRUE(substr.size() == 2);
  716. ASSERT_TRUE(!substr.is_stable());
  717. // c_str() at the end of the buffer does not internally mutate.
  718. ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0);
  719. ASSERT_TRUE(substr.c_str() == str.data() + 1);
  720. ASSERT_TRUE(substr.data() == str.data() + 1);
  721. ASSERT_TRUE(substr.size() == 2);
  722. ASSERT_TRUE(!substr.is_stable());
  723. // str() internally mutates.
  724. ASSERT_TRUE(substr.str() == "bc");
  725. ASSERT_TRUE(substr.is_stable());
  726. ASSERT_TRUE(substr.data() != str.data() + 1);
  727. ASSERT_TRUE(substr.size() == 2);
  728. ASSERT_TRUE(substr.c_str() != str.data() + 1);
  729. ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0);
  730. return true;
  731. }
  732. static bool testMethod_substr_AtEndBorrowed()
  733. {
  734. std::cout << "testMethod_substr_AtEndBorrowed()\n";
  735. return testMethod_substr_AtEnd(cm::String("abc"_s));
  736. }
  737. static bool testMethod_substr_AtEndOwned()
  738. {
  739. std::cout << "testMethod_substr_AtEndOwned()\n";
  740. return testMethod_substr_AtEnd(std::string("abc"));
  741. }
  742. static bool testMethod_substr_AtStart(cm::String str)
  743. {
  744. {
  745. cm::String substr = str.substr(0, 2);
  746. ASSERT_TRUE(substr.data() == str.data());
  747. ASSERT_TRUE(substr.size() == 2);
  748. // c_str() not at the end of the buffer internally mutates.
  749. const char* substr_c = substr.c_str();
  750. ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0);
  751. ASSERT_TRUE(substr_c != str.data());
  752. ASSERT_TRUE(substr.data() != str.data());
  753. ASSERT_TRUE(substr.size() == 2);
  754. ASSERT_TRUE(substr.is_stable());
  755. // str() does not need to internally mutate after c_str() did so
  756. ASSERT_TRUE(substr.str() == "ab");
  757. ASSERT_TRUE(substr.is_stable());
  758. ASSERT_TRUE(substr.data() == substr_c);
  759. ASSERT_TRUE(substr.size() == 2);
  760. ASSERT_TRUE(substr.c_str() == substr_c);
  761. }
  762. {
  763. cm::String substr = str.substr(0, 2);
  764. ASSERT_TRUE(substr.data() == str.data());
  765. ASSERT_TRUE(substr.size() == 2);
  766. ASSERT_TRUE(!substr.is_stable());
  767. // str() internally mutates.
  768. ASSERT_TRUE(substr.str() == "ab");
  769. ASSERT_TRUE(substr.is_stable());
  770. ASSERT_TRUE(substr.data() != str.data());
  771. ASSERT_TRUE(substr.size() == 2);
  772. ASSERT_TRUE(substr.c_str() != str.data());
  773. // c_str() does not internally after str() did so
  774. const char* substr_c = substr.c_str();
  775. ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0);
  776. ASSERT_TRUE(substr_c == substr.data());
  777. ASSERT_TRUE(substr.size() == 2);
  778. ASSERT_TRUE(substr.is_stable());
  779. }
  780. return true;
  781. }
  782. static bool testMethod_substr_AtStartBorrowed()
  783. {
  784. std::cout << "testMethod_substr_AtStartBorrowed()\n";
  785. return testMethod_substr_AtStart(cm::String("abc"_s));
  786. }
  787. static bool testMethod_substr_AtStartOwned()
  788. {
  789. std::cout << "testMethod_substr_AtStartOwned()\n";
  790. return testMethod_substr_AtStart(std::string("abc"));
  791. }
  792. static bool testMethod_compare()
  793. {
  794. std::cout << "testMethod_compare()\n";
  795. cm::String str = "b";
  796. ASSERT_TRUE(str.compare("a") > 0);
  797. ASSERT_TRUE(str.compare("b") == 0);
  798. ASSERT_TRUE(str.compare("c") < 0);
  799. {
  800. const char* a = "a";
  801. const char* b = "b";
  802. const char* c = "c";
  803. ASSERT_TRUE(str.compare(a) > 0);
  804. ASSERT_TRUE(str.compare(b) == 0);
  805. ASSERT_TRUE(str.compare(c) < 0);
  806. }
  807. ASSERT_TRUE(str.compare('a') > 0);
  808. ASSERT_TRUE(str.compare('b') == 0);
  809. ASSERT_TRUE(str.compare('c') < 0);
  810. ASSERT_TRUE(str.compare(std::string("a")) > 0);
  811. ASSERT_TRUE(str.compare(std::string("b")) == 0);
  812. ASSERT_TRUE(str.compare(std::string("c")) < 0);
  813. ASSERT_TRUE(str.compare(cm::string_view("a_", 1)) > 0);
  814. ASSERT_TRUE(str.compare(cm::string_view("b_", 1)) == 0);
  815. ASSERT_TRUE(str.compare(cm::string_view("c_", 1)) < 0);
  816. ASSERT_TRUE(str.compare(cm::String("a")) > 0);
  817. ASSERT_TRUE(str.compare(cm::String("b")) == 0);
  818. ASSERT_TRUE(str.compare(cm::String("c")) < 0);
  819. ASSERT_TRUE(str.compare(0, 1, cm::string_view("a", 1)) > 0);
  820. ASSERT_TRUE(str.compare(1, 0, cm::string_view("", 0)) == 0);
  821. ASSERT_TRUE(str.compare(0, 1, cm::string_view("ac", 2), 1, 1) < 0);
  822. ASSERT_TRUE(str.compare(1, 0, "") == 0);
  823. ASSERT_TRUE(str.compare(1, 0, "_", 0) == 0);
  824. return true;
  825. }
  826. static bool testMethod_find()
  827. {
  828. std::cout << "testMethod_find()\n";
  829. cm::String str = "abcabc";
  830. ASSERT_TRUE(str.find("a") == 0);
  831. ASSERT_TRUE(str.find("a", 1) == 3);
  832. {
  833. const char* a = "a";
  834. ASSERT_TRUE(str.find(a) == 0);
  835. ASSERT_TRUE(str.find(a, 1) == 3);
  836. }
  837. ASSERT_TRUE(str.find('a') == 0);
  838. ASSERT_TRUE(str.find('a', 1) == 3);
  839. ASSERT_TRUE(str.find(std::string("a")) == 0);
  840. ASSERT_TRUE(str.find(std::string("a"), 1) == 3);
  841. ASSERT_TRUE(str.find(cm::string_view("a_", 1)) == 0);
  842. ASSERT_TRUE(str.find(cm::string_view("a_", 1), 1) == 3);
  843. ASSERT_TRUE(str.find(cm::String("a")) == 0);
  844. ASSERT_TRUE(str.find(cm::String("a"), 1) == 3);
  845. ASSERT_TRUE(str.find("ab_", 1, 2) == 3);
  846. return true;
  847. }
  848. static bool testMethod_rfind()
  849. {
  850. std::cout << "testMethod_rfind()\n";
  851. cm::String str = "abcabc";
  852. ASSERT_TRUE(str.rfind("a") == 3);
  853. ASSERT_TRUE(str.rfind("a", 1) == 0);
  854. {
  855. const char* a = "a";
  856. ASSERT_TRUE(str.rfind(a) == 3);
  857. ASSERT_TRUE(str.rfind(a, 1) == 0);
  858. }
  859. ASSERT_TRUE(str.rfind('a') == 3);
  860. ASSERT_TRUE(str.rfind('a', 1) == 0);
  861. ASSERT_TRUE(str.rfind(std::string("a")) == 3);
  862. ASSERT_TRUE(str.rfind(std::string("a"), 1) == 0);
  863. ASSERT_TRUE(str.rfind(cm::string_view("a_", 1)) == 3);
  864. ASSERT_TRUE(str.rfind(cm::string_view("a_", 1), 1) == 0);
  865. ASSERT_TRUE(str.rfind(cm::String("a")) == 3);
  866. ASSERT_TRUE(str.rfind(cm::String("a"), 1) == 0);
  867. ASSERT_TRUE(str.rfind("ab_", 1, 2) == 0);
  868. return true;
  869. }
  870. static bool testMethod_find_first_of()
  871. {
  872. std::cout << "testMethod_find_first_of()\n";
  873. cm::String str = "abcabc";
  874. ASSERT_TRUE(str.find_first_of("_a") == 0);
  875. ASSERT_TRUE(str.find_first_of("_a", 1) == 3);
  876. {
  877. const char* a = "_a";
  878. ASSERT_TRUE(str.find_first_of(a) == 0);
  879. ASSERT_TRUE(str.find_first_of(a, 1) == 3);
  880. }
  881. ASSERT_TRUE(str.find_first_of('a') == 0);
  882. ASSERT_TRUE(str.find_first_of('a', 1) == 3);
  883. ASSERT_TRUE(str.find_first_of(std::string("_a")) == 0);
  884. ASSERT_TRUE(str.find_first_of(std::string("_a"), 1) == 3);
  885. ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1)) == 1);
  886. ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1), 2) == 4);
  887. ASSERT_TRUE(str.find_first_of(cm::String("ab")) == 0);
  888. ASSERT_TRUE(str.find_first_of(cm::String("ab"), 2) == 3);
  889. ASSERT_TRUE(str.find_first_of("_ab", 1, 2) == 3);
  890. return true;
  891. }
  892. static bool testMethod_find_first_not_of()
  893. {
  894. std::cout << "testMethod_find_first_not_of()\n";
  895. cm::String str = "abcabc";
  896. ASSERT_TRUE(str.find_first_not_of("_a") == 1);
  897. ASSERT_TRUE(str.find_first_not_of("_a", 2) == 2);
  898. {
  899. const char* a = "_a";
  900. ASSERT_TRUE(str.find_first_not_of(a) == 1);
  901. ASSERT_TRUE(str.find_first_not_of(a, 2) == 2);
  902. }
  903. ASSERT_TRUE(str.find_first_not_of('a') == 1);
  904. ASSERT_TRUE(str.find_first_not_of('a', 2) == 2);
  905. ASSERT_TRUE(str.find_first_not_of(std::string("_a")) == 1);
  906. ASSERT_TRUE(str.find_first_not_of(std::string("_a"), 2) == 2);
  907. ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1)) == 0);
  908. ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1), 1) == 2);
  909. ASSERT_TRUE(str.find_first_not_of(cm::String("_a")) == 1);
  910. ASSERT_TRUE(str.find_first_not_of(cm::String("_a"), 2) == 2);
  911. ASSERT_TRUE(str.find_first_not_of("_bca", 1, 3) == 3);
  912. return true;
  913. }
  914. static bool testMethod_find_last_of()
  915. {
  916. std::cout << "testMethod_find_last_of()\n";
  917. cm::String str = "abcabc";
  918. ASSERT_TRUE(str.find_last_of("_a") == 3);
  919. ASSERT_TRUE(str.find_last_of("_a", 1) == 0);
  920. {
  921. const char* a = "_a";
  922. ASSERT_TRUE(str.find_last_of(a) == 3);
  923. ASSERT_TRUE(str.find_last_of(a, 1) == 0);
  924. }
  925. ASSERT_TRUE(str.find_last_of('a') == 3);
  926. ASSERT_TRUE(str.find_last_of('a', 1) == 0);
  927. ASSERT_TRUE(str.find_last_of(std::string("_a")) == 3);
  928. ASSERT_TRUE(str.find_last_of(std::string("_a"), 1) == 0);
  929. ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1)) == 4);
  930. ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1), 2) == 1);
  931. ASSERT_TRUE(str.find_last_of(cm::String("ab")) == 4);
  932. ASSERT_TRUE(str.find_last_of(cm::String("ab"), 2) == 1);
  933. ASSERT_TRUE(str.find_last_of("_ab", 1, 2) == 0);
  934. return true;
  935. }
  936. static bool testMethod_find_last_not_of()
  937. {
  938. std::cout << "testMethod_find_last_not_of()\n";
  939. cm::String str = "abcabc";
  940. ASSERT_TRUE(str.find_last_not_of("_a") == 5);
  941. ASSERT_TRUE(str.find_last_not_of("_a", 1) == 1);
  942. {
  943. const char* a = "_a";
  944. ASSERT_TRUE(str.find_last_not_of(a) == 5);
  945. ASSERT_TRUE(str.find_last_not_of(a, 1) == 1);
  946. }
  947. ASSERT_TRUE(str.find_last_not_of('a') == 5);
  948. ASSERT_TRUE(str.find_last_not_of('a', 1) == 1);
  949. ASSERT_TRUE(str.find_last_not_of(std::string("_a")) == 5);
  950. ASSERT_TRUE(str.find_last_not_of(std::string("_a"), 1) == 1);
  951. ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1)) == 4);
  952. ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1), 2) == 1);
  953. ASSERT_TRUE(str.find_last_not_of(cm::String("_a")) == 5);
  954. ASSERT_TRUE(str.find_last_not_of(cm::String("_a"), 1) == 1);
  955. ASSERT_TRUE(str.find_last_not_of("cb_", 2, 2) == 0);
  956. return true;
  957. }
  958. static bool testAddition()
  959. {
  960. std::cout << "testAddition()\n";
  961. {
  962. ASSERT_TRUE(cm::String("a") + "b" == "ab");
  963. ASSERT_TRUE("ab" == "a" + cm::String("b"));
  964. ASSERT_TRUE("a" + cm::String("b") + "c" == "abc");
  965. ASSERT_TRUE("abc" == "a" + cm::String("b") + "c");
  966. ASSERT_TRUE("a" + (cm::String("b") + "c") + "d" == "abcd");
  967. ASSERT_TRUE("abcd" == "a" + (cm::String("b") + "c") + "d");
  968. }
  969. {
  970. ASSERT_TRUE(cm::String("a"_s) + "b"_s == "ab"_s);
  971. ASSERT_TRUE("ab"_s == "a"_s + cm::String("b"_s));
  972. ASSERT_TRUE("a"_s + cm::String("b"_s) + "c"_s == "abc"_s);
  973. ASSERT_TRUE("abc"_s == "a"_s + cm::String("b"_s) + "c"_s);
  974. ASSERT_TRUE("a"_s + (cm::String("b"_s) + "c"_s) + "d"_s == "abcd"_s);
  975. ASSERT_TRUE("abcd"_s == "a"_s + (cm::String("b"_s) + "c"_s) + "d"_s);
  976. }
  977. {
  978. const char* a = "a";
  979. const char* b = "b";
  980. const char* ab = "ab";
  981. ASSERT_TRUE(cm::String(a) + b == ab);
  982. ASSERT_TRUE(ab == a + cm::String(b));
  983. const char* c = "c";
  984. const char* abc = "abc";
  985. ASSERT_TRUE(a + cm::String(b) + c == abc);
  986. ASSERT_TRUE(abc == a + cm::String(b) + c);
  987. const char* d = "d";
  988. const char* abcd = "abcd";
  989. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  990. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  991. }
  992. {
  993. ASSERT_TRUE(cm::String('a') + 'b' == "ab");
  994. ASSERT_TRUE("ab" == 'a' + cm::String('b'));
  995. ASSERT_TRUE('a' + cm::String('b') + 'c' == "abc");
  996. ASSERT_TRUE("abc" == 'a' + cm::String('b') + 'c');
  997. ASSERT_TRUE('a' + (cm::String('b') + 'c') + 'd' == "abcd");
  998. ASSERT_TRUE("abcd" == 'a' + (cm::String('b') + 'c') + 'd');
  999. }
  1000. {
  1001. std::string a = "a";
  1002. std::string b = "b";
  1003. std::string ab = "ab";
  1004. ASSERT_TRUE(cm::String(a) + b == ab);
  1005. ASSERT_TRUE(ab == a + cm::String(b));
  1006. std::string c = "c";
  1007. std::string abc = "abc";
  1008. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1009. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1010. std::string d = "d";
  1011. std::string abcd = "abcd";
  1012. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1013. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1014. }
  1015. {
  1016. cm::string_view a("a", 1);
  1017. cm::string_view b("b", 1);
  1018. cm::string_view ab("ab", 2);
  1019. ASSERT_TRUE(cm::String(a) + b == ab);
  1020. ASSERT_TRUE(ab == a + cm::String(b));
  1021. cm::string_view c("c", 1);
  1022. cm::string_view abc("abc", 3);
  1023. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1024. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1025. cm::string_view d("d", 1);
  1026. cm::string_view abcd("abcd", 4);
  1027. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1028. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1029. }
  1030. {
  1031. cm::String a = "a";
  1032. cm::String b = "b";
  1033. cm::String ab = "ab";
  1034. ASSERT_TRUE(a + b == ab);
  1035. ASSERT_TRUE(ab == a + b);
  1036. cm::String c = "c";
  1037. cm::String abc = "abc";
  1038. ASSERT_TRUE(a + cm::String(b) + c == abc);
  1039. ASSERT_TRUE(abc == a + cm::String(b) + c);
  1040. cm::String d = "d";
  1041. cm::String abcd = "abcd";
  1042. ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd);
  1043. ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d);
  1044. }
  1045. {
  1046. cm::String str;
  1047. str += "a" + cm::String("b") + 'c';
  1048. ASSERT_TRUE(str == "abc");
  1049. ASSERT_TRUE(str.is_stable());
  1050. }
  1051. {
  1052. std::string s;
  1053. s += "a" + cm::String("b") + 'c';
  1054. ASSERT_TRUE(s == "abc");
  1055. }
  1056. {
  1057. std::ostringstream ss;
  1058. ss << ("a" + cm::String("b") + 'c');
  1059. ASSERT_TRUE(ss.str() == "abc");
  1060. }
  1061. return true;
  1062. }
  1063. static bool testStability()
  1064. {
  1065. std::cout << "testStability()\n";
  1066. cm::String str("abc"_s);
  1067. ASSERT_TRUE(!str.is_stable());
  1068. ASSERT_TRUE(str.str_if_stable() == nullptr);
  1069. str.stabilize();
  1070. ASSERT_TRUE(str.is_stable());
  1071. std::string const* str_if_stable = str.str_if_stable();
  1072. ASSERT_TRUE(str_if_stable != nullptr);
  1073. ASSERT_TRUE(*str_if_stable == "abc");
  1074. str.stabilize();
  1075. ASSERT_TRUE(str.is_stable());
  1076. ASSERT_TRUE(str.str_if_stable() == str_if_stable);
  1077. return true;
  1078. }
  1079. int testString(int /*unused*/, char* /*unused*/ [])
  1080. {
  1081. if (!testConstructDefault()) {
  1082. return 1;
  1083. }
  1084. if (!testConstructFromNullPtr()) {
  1085. return 1;
  1086. }
  1087. if (!testConstructFromCStrNull()) {
  1088. return 1;
  1089. }
  1090. if (!testConstructFromCharArray()) {
  1091. return 1;
  1092. }
  1093. if (!testConstructFromCStr()) {
  1094. return 1;
  1095. }
  1096. if (!testConstructFromStdString()) {
  1097. return 1;
  1098. }
  1099. if (!testConstructFromView()) {
  1100. return 1;
  1101. }
  1102. if (!testConstructFromChar()) {
  1103. return 1;
  1104. }
  1105. if (!testConstructFromInitList()) {
  1106. return 1;
  1107. }
  1108. if (!testConstructFromBuffer()) {
  1109. return 1;
  1110. }
  1111. if (!testConstructFromInputIterator()) {
  1112. return 1;
  1113. }
  1114. if (!testConstructFromN()) {
  1115. return 1;
  1116. }
  1117. if (!testConstructFromStaticStringView()) {
  1118. return 1;
  1119. }
  1120. if (!testConstructCopy()) {
  1121. return 1;
  1122. }
  1123. if (!testConstructMove()) {
  1124. return 1;
  1125. }
  1126. if (!testAssignCopy()) {
  1127. return 1;
  1128. }
  1129. if (!testAssignMove()) {
  1130. return 1;
  1131. }
  1132. if (!testAssignFromChar()) {
  1133. return 1;
  1134. }
  1135. if (!testAssignFromView()) {
  1136. return 1;
  1137. }
  1138. if (!testAssignFromStdString()) {
  1139. return 1;
  1140. }
  1141. if (!testAssignFromCStr()) {
  1142. return 1;
  1143. }
  1144. if (!testAssignFromCharArray()) {
  1145. return 1;
  1146. }
  1147. if (!testAssignFromCStrNull()) {
  1148. return 1;
  1149. }
  1150. if (!testAssignFromNullPtr()) {
  1151. return 1;
  1152. }
  1153. if (!testAssignFromInitList()) {
  1154. return 1;
  1155. }
  1156. if (!testAssignFromStaticStringView()) {
  1157. return 1;
  1158. }
  1159. if (!testOperatorBool()) {
  1160. return 1;
  1161. }
  1162. if (!testOperatorIndex()) {
  1163. return 1;
  1164. }
  1165. if (!testOperatorPlusEqual()) {
  1166. return 1;
  1167. }
  1168. if (!testOperatorCompare()) {
  1169. return 1;
  1170. }
  1171. if (!testOperatorStream()) {
  1172. return 1;
  1173. }
  1174. if (!testOperatorStdStringPlusEqual()) {
  1175. return 1;
  1176. }
  1177. if (!testMethod_borrow()) {
  1178. return 1;
  1179. }
  1180. if (!testMethod_view()) {
  1181. return 1;
  1182. }
  1183. if (!testMethod_empty()) {
  1184. return 1;
  1185. }
  1186. if (!testMethod_length()) {
  1187. return 1;
  1188. }
  1189. if (!testMethod_at()) {
  1190. return 1;
  1191. }
  1192. if (!testMethod_front_back()) {
  1193. return 1;
  1194. }
  1195. if (!testMethod_clear()) {
  1196. return 1;
  1197. }
  1198. if (!testMethod_insert()) {
  1199. return 1;
  1200. }
  1201. if (!testMethod_erase()) {
  1202. return 1;
  1203. }
  1204. if (!testMethod_push_back()) {
  1205. return 1;
  1206. }
  1207. if (!testMethod_pop_back()) {
  1208. return 1;
  1209. }
  1210. if (!testMethod_replace()) {
  1211. return 1;
  1212. }
  1213. if (!testMethod_copy()) {
  1214. return 1;
  1215. }
  1216. if (!testMethod_resize()) {
  1217. return 1;
  1218. }
  1219. if (!testMethod_swap()) {
  1220. return 1;
  1221. }
  1222. if (!testMethodIterators()) {
  1223. return 1;
  1224. }
  1225. if (!testMethod_substr_AtEndBorrowed()) {
  1226. return 1;
  1227. }
  1228. if (!testMethod_substr_AtEndOwned()) {
  1229. return 1;
  1230. }
  1231. if (!testMethod_substr_AtStartBorrowed()) {
  1232. return 1;
  1233. }
  1234. if (!testMethod_substr_AtStartOwned()) {
  1235. return 1;
  1236. }
  1237. if (!testMethod_compare()) {
  1238. return 1;
  1239. }
  1240. if (!testMethod_find()) {
  1241. return 1;
  1242. }
  1243. if (!testMethod_rfind()) {
  1244. return 1;
  1245. }
  1246. if (!testMethod_find_first_of()) {
  1247. return 1;
  1248. }
  1249. if (!testMethod_find_first_not_of()) {
  1250. return 1;
  1251. }
  1252. if (!testMethod_find_last_of()) {
  1253. return 1;
  1254. }
  1255. if (!testMethod_find_last_not_of()) {
  1256. return 1;
  1257. }
  1258. if (!testAddition()) {
  1259. return 1;
  1260. }
  1261. if (!testStability()) {
  1262. return 1;
  1263. }
  1264. return 0;
  1265. }