String.tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "internal/catch_stringref.h"
  2. #include "catch.hpp"
  3. #include <cstring>
  4. TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
  5. using Catch::StringRef;
  6. SECTION( "Empty string" ) {
  7. StringRef empty;
  8. REQUIRE( empty.empty() );
  9. REQUIRE( empty.size() == 0 );
  10. REQUIRE( empty.isNullTerminated() );
  11. REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 );
  12. }
  13. SECTION( "From string literal" ) {
  14. StringRef s = "hello";
  15. REQUIRE( s.empty() == false );
  16. REQUIRE( s.size() == 5 );
  17. REQUIRE( s.isNullTerminated() );
  18. auto rawChars = s.data();
  19. REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
  20. REQUIRE_NOTHROW(s.c_str());
  21. REQUIRE(s.c_str() == rawChars);
  22. REQUIRE(s.data() == rawChars);
  23. }
  24. SECTION( "From sub-string" ) {
  25. StringRef original = StringRef( "original string" ).substr(0, 8);
  26. REQUIRE( original == "original" );
  27. REQUIRE_FALSE(original.isNullTerminated());
  28. REQUIRE_THROWS(original.c_str());
  29. REQUIRE_NOTHROW(original.data());
  30. }
  31. SECTION( "Substrings" ) {
  32. StringRef s = "hello world!";
  33. StringRef ss = s.substr(0, 5);
  34. SECTION( "zero-based substring" ) {
  35. REQUIRE( ss.empty() == false );
  36. REQUIRE( ss.size() == 5 );
  37. REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 );
  38. REQUIRE( ss == "hello" );
  39. }
  40. SECTION( "non-zero-based substring") {
  41. ss = s.substr( 6, 6 );
  42. REQUIRE( ss.size() == 6 );
  43. REQUIRE( std::strcmp( ss.c_str(), "world!" ) == 0 );
  44. }
  45. SECTION( "Pointer values of full refs should match" ) {
  46. StringRef s2 = s;
  47. REQUIRE( s.data() == s2.data() );
  48. }
  49. SECTION( "Pointer values of substring refs should also match" ) {
  50. REQUIRE( s.data() == ss.data() );
  51. }
  52. SECTION("Past the end substring") {
  53. REQUIRE(s.substr(s.size() + 1, 123).empty());
  54. }
  55. SECTION("Substring off the end are trimmed") {
  56. ss = s.substr(6, 123);
  57. REQUIRE(std::strcmp(ss.c_str(), "world!") == 0);
  58. }
  59. // TODO: substring into string + size is longer than end
  60. }
  61. SECTION( "Comparisons are deep" ) {
  62. char buffer1[] = "Hello";
  63. char buffer2[] = "Hello";
  64. CHECK((char*)buffer1 != (char*)buffer2);
  65. StringRef left(buffer1), right(buffer2);
  66. REQUIRE( left == right );
  67. REQUIRE(left != left.substr(0, 3));
  68. }
  69. SECTION( "from std::string" ) {
  70. std::string stdStr = "a standard string";
  71. SECTION( "implicitly constructed" ) {
  72. StringRef sr = stdStr;
  73. REQUIRE( sr == "a standard string" );
  74. REQUIRE( sr.size() == stdStr.size() );
  75. }
  76. SECTION( "explicitly constructed" ) {
  77. StringRef sr( stdStr );
  78. REQUIRE( sr == "a standard string" );
  79. REQUIRE( sr.size() == stdStr.size() );
  80. }
  81. SECTION( "assigned" ) {
  82. StringRef sr;
  83. sr = stdStr;
  84. REQUIRE( sr == "a standard string" );
  85. REQUIRE( sr.size() == stdStr.size() );
  86. }
  87. }
  88. SECTION( "to std::string" ) {
  89. StringRef sr = "a stringref";
  90. SECTION( "explicitly constructed" ) {
  91. std::string stdStr( sr );
  92. REQUIRE( stdStr == "a stringref" );
  93. REQUIRE( stdStr.size() == sr.size() );
  94. }
  95. SECTION( "assigned" ) {
  96. std::string stdStr;
  97. stdStr = static_cast<std::string>(sr);
  98. REQUIRE( stdStr == "a stringref" );
  99. REQUIRE( stdStr.size() == sr.size() );
  100. }
  101. }
  102. }
  103. TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
  104. using Catch::StringRef;
  105. SECTION("Simple constructors") {
  106. STATIC_REQUIRE(StringRef{}.size() == 0);
  107. STATIC_REQUIRE(StringRef{ "abc", 3 }.size() == 3);
  108. STATIC_REQUIRE(StringRef{ "abc", 3 }.isNullTerminated());
  109. STATIC_REQUIRE(StringRef{ "abc", 2 }.size() == 2);
  110. STATIC_REQUIRE_FALSE(StringRef{ "abc", 2 }.isNullTerminated());
  111. }
  112. SECTION("UDL construction") {
  113. constexpr auto sr1 = "abc"_catch_sr;
  114. STATIC_REQUIRE_FALSE(sr1.empty());
  115. STATIC_REQUIRE(sr1.size() == 3);
  116. STATIC_REQUIRE(sr1.isNullTerminated());
  117. using Catch::operator"" _sr;
  118. constexpr auto sr2 = ""_sr;
  119. STATIC_REQUIRE(sr2.empty());
  120. STATIC_REQUIRE(sr2.size() == 0);
  121. STATIC_REQUIRE(sr2.isNullTerminated());
  122. }
  123. }