String.tests.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/internal/catch_stringref.hpp>
  8. #include <cstring>
  9. TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
  10. using Catch::StringRef;
  11. SECTION( "Empty string" ) {
  12. StringRef empty;
  13. REQUIRE( empty.empty() );
  14. REQUIRE( empty.size() == 0 );
  15. REQUIRE( std::strcmp( empty.data(), "" ) == 0 );
  16. }
  17. SECTION( "From string literal" ) {
  18. StringRef s = "hello";
  19. REQUIRE( s.empty() == false );
  20. REQUIRE( s.size() == 5 );
  21. auto rawChars = s.data();
  22. REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
  23. REQUIRE(s.data() == rawChars);
  24. }
  25. SECTION( "From sub-string" ) {
  26. StringRef original = StringRef( "original string" ).substr(0, 8);
  27. REQUIRE( original == "original" );
  28. REQUIRE_NOTHROW(original.data());
  29. }
  30. SECTION( "Copy construction is shallow" ) {
  31. StringRef original = StringRef( "original string" );
  32. StringRef copy = original;
  33. REQUIRE(original.begin() == copy.begin());
  34. }
  35. SECTION( "Copy assignment is shallow" ) {
  36. StringRef original = StringRef( "original string" );
  37. StringRef copy;
  38. copy = original;
  39. REQUIRE(original.begin() == copy.begin());
  40. }
  41. SECTION( "Substrings" ) {
  42. StringRef s = "hello world!";
  43. StringRef ss = s.substr(0, 5);
  44. SECTION( "zero-based substring" ) {
  45. REQUIRE( ss.empty() == false );
  46. REQUIRE( ss.size() == 5 );
  47. REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 );
  48. REQUIRE( ss == "hello" );
  49. }
  50. SECTION( "non-zero-based substring") {
  51. ss = s.substr( 6, 6 );
  52. REQUIRE( ss.size() == 6 );
  53. REQUIRE( std::strcmp( ss.data(), "world!" ) == 0 );
  54. }
  55. SECTION( "Pointer values of full refs should match" ) {
  56. StringRef s2 = s;
  57. REQUIRE( s.data() == s2.data() );
  58. }
  59. SECTION( "Pointer values of substring refs should also match" ) {
  60. REQUIRE( s.data() == ss.data() );
  61. }
  62. SECTION("Past the end substring") {
  63. REQUIRE(s.substr(s.size() + 1, 123).empty());
  64. }
  65. SECTION("Substring off the end are trimmed") {
  66. ss = s.substr(6, 123);
  67. REQUIRE(std::strcmp(ss.data(), "world!") == 0);
  68. }
  69. SECTION("substring start after the end is empty") {
  70. REQUIRE(s.substr(1'000'000, 1).empty());
  71. }
  72. }
  73. SECTION( "Comparisons are deep" ) {
  74. char buffer1[] = "Hello";
  75. char buffer2[] = "Hello";
  76. CHECK(reinterpret_cast<char*>(buffer1) != reinterpret_cast<char*>(buffer2));
  77. StringRef left(buffer1), right(buffer2);
  78. REQUIRE( left == right );
  79. REQUIRE(left != left.substr(0, 3));
  80. }
  81. SECTION( "from std::string" ) {
  82. std::string stdStr = "a standard string";
  83. SECTION( "implicitly constructed" ) {
  84. StringRef sr = stdStr;
  85. REQUIRE( sr == "a standard string" );
  86. REQUIRE( sr.size() == stdStr.size() );
  87. }
  88. SECTION( "explicitly constructed" ) {
  89. StringRef sr( stdStr );
  90. REQUIRE( sr == "a standard string" );
  91. REQUIRE( sr.size() == stdStr.size() );
  92. }
  93. SECTION( "assigned" ) {
  94. StringRef sr;
  95. sr = stdStr;
  96. REQUIRE( sr == "a standard string" );
  97. REQUIRE( sr.size() == stdStr.size() );
  98. }
  99. }
  100. SECTION( "to std::string" ) {
  101. StringRef sr = "a stringref";
  102. SECTION( "explicitly constructed" ) {
  103. std::string stdStr( sr );
  104. REQUIRE( stdStr == "a stringref" );
  105. REQUIRE( stdStr.size() == sr.size() );
  106. }
  107. SECTION( "assigned" ) {
  108. std::string stdStr;
  109. stdStr = static_cast<std::string>(sr);
  110. REQUIRE( stdStr == "a stringref" );
  111. REQUIRE( stdStr.size() == sr.size() );
  112. }
  113. }
  114. SECTION("std::string += StringRef") {
  115. StringRef sr = "the stringref contents";
  116. std::string lhs("some string += ");
  117. lhs += sr;
  118. REQUIRE(lhs == "some string += the stringref contents");
  119. }
  120. SECTION("StringRef + StringRef") {
  121. StringRef sr1 = "abraka", sr2 = "dabra";
  122. std::string together = sr1 + sr2;
  123. REQUIRE(together == "abrakadabra");
  124. }
  125. }
  126. TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
  127. using Catch::StringRef;
  128. SECTION("Simple constructors") {
  129. constexpr StringRef empty{};
  130. STATIC_REQUIRE(empty.size() == 0);
  131. STATIC_REQUIRE(empty.begin() == empty.end());
  132. constexpr char const* const abc = "abc";
  133. constexpr StringRef stringref(abc, 3);
  134. STATIC_REQUIRE(stringref.size() == 3);
  135. STATIC_REQUIRE(stringref.data() == abc);
  136. STATIC_REQUIRE(stringref.begin() == abc);
  137. STATIC_REQUIRE(stringref.begin() != stringref.end());
  138. STATIC_REQUIRE(stringref.substr(10, 0).empty());
  139. STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
  140. STATIC_REQUIRE(stringref[1] == 'b');
  141. constexpr StringRef shortened(abc, 2);
  142. STATIC_REQUIRE(shortened.size() == 2);
  143. STATIC_REQUIRE(shortened.data() == abc);
  144. STATIC_REQUIRE(shortened.begin() != shortened.end());
  145. }
  146. SECTION("UDL construction") {
  147. constexpr auto sr1 = "abc"_catch_sr;
  148. STATIC_REQUIRE_FALSE(sr1.empty());
  149. STATIC_REQUIRE(sr1.size() == 3);
  150. using Catch::operator"" _sr;
  151. constexpr auto sr2 = ""_sr;
  152. STATIC_REQUIRE(sr2.empty());
  153. STATIC_REQUIRE(sr2.size() == 0);
  154. }
  155. }
  156. TEST_CASE("StringRef::compare", "[Strings][StringRef][approvals]") {
  157. using Catch::StringRef;
  158. SECTION("Same length on both sides") {
  159. StringRef sr1("abcdc");
  160. StringRef sr2("abcdd");
  161. StringRef sr3("abcdc");
  162. REQUIRE(sr1.compare(sr2) < 0);
  163. REQUIRE(sr2.compare(sr1) > 0);
  164. REQUIRE(sr1.compare(sr3) == 0);
  165. REQUIRE(sr3.compare(sr1) == 0);
  166. }
  167. SECTION("Different lengths") {
  168. StringRef sr1("def");
  169. StringRef sr2("deff");
  170. StringRef sr3("ab");
  171. REQUIRE(sr1.compare(sr2) < 0);
  172. REQUIRE(sr2.compare(sr1) > 0);
  173. REQUIRE(sr1.compare(sr3) > 0);
  174. REQUIRE(sr2.compare(sr3) > 0);
  175. REQUIRE(sr3.compare(sr1) < 0);
  176. REQUIRE(sr3.compare(sr2) < 0);
  177. }
  178. }