mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
e1e6872c4c
This is both a really big and a really small commit. It is small in that it only contains renaming, moving and modification of include directives caused by this. It is really big in the obvious way of touching something like 200 files. The new rules for naming files is simple: headers use the `.hpp` extension. The rules for physical file layout is still kinda in progress, but the basics are also simple: * Significant parts of functionality get their own subfolder * Benchmarking is in `catch2/benchmark` * Matchers are in `catch2/matchers` * Generators are in `catch2/generators` * Reporters are in `catch2/reporters` * Baseline testing facilities are in `catch2/` * Various top level folders also contain `internal` subfolder, with files that users probably do not want to include directly, at least not until they have to write something like their own reporter. * The exact files in these subfolders is likely to change later on Note that while some includes were cleaned up in this commit, it is only the low hanging fruit and further cleanup using automatic tooling will happen later. Also note that various include guards, copyright notices and file headers will also be standardized later, rather than in this commit.
169 lines
5.2 KiB
C++
169 lines
5.2 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/catch_stringref.hpp>
|
|
|
|
#include <cstring>
|
|
|
|
TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
|
|
|
|
using Catch::StringRef;
|
|
|
|
SECTION( "Empty string" ) {
|
|
StringRef empty;
|
|
REQUIRE( empty.empty() );
|
|
REQUIRE( empty.size() == 0 );
|
|
REQUIRE( empty.isNullTerminated() );
|
|
REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 );
|
|
}
|
|
|
|
SECTION( "From string literal" ) {
|
|
StringRef s = "hello";
|
|
REQUIRE( s.empty() == false );
|
|
REQUIRE( s.size() == 5 );
|
|
REQUIRE( s.isNullTerminated() );
|
|
|
|
auto rawChars = s.data();
|
|
REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
|
|
|
|
REQUIRE_NOTHROW(s.c_str());
|
|
REQUIRE(s.c_str() == rawChars);
|
|
REQUIRE(s.data() == rawChars);
|
|
}
|
|
SECTION( "From sub-string" ) {
|
|
StringRef original = StringRef( "original string" ).substr(0, 8);
|
|
REQUIRE( original == "original" );
|
|
|
|
REQUIRE_FALSE(original.isNullTerminated());
|
|
REQUIRE_THROWS(original.c_str());
|
|
REQUIRE_NOTHROW(original.data());
|
|
}
|
|
|
|
|
|
SECTION( "Substrings" ) {
|
|
StringRef s = "hello world!";
|
|
StringRef ss = s.substr(0, 5);
|
|
|
|
SECTION( "zero-based substring" ) {
|
|
REQUIRE( ss.empty() == false );
|
|
REQUIRE( ss.size() == 5 );
|
|
REQUIRE( std::strncmp( ss.data(), "hello", 5 ) == 0 );
|
|
REQUIRE( ss == "hello" );
|
|
}
|
|
|
|
SECTION( "non-zero-based substring") {
|
|
ss = s.substr( 6, 6 );
|
|
REQUIRE( ss.size() == 6 );
|
|
REQUIRE( std::strcmp( ss.c_str(), "world!" ) == 0 );
|
|
}
|
|
|
|
SECTION( "Pointer values of full refs should match" ) {
|
|
StringRef s2 = s;
|
|
REQUIRE( s.data() == s2.data() );
|
|
}
|
|
|
|
SECTION( "Pointer values of substring refs should also match" ) {
|
|
REQUIRE( s.data() == ss.data() );
|
|
}
|
|
|
|
SECTION("Past the end substring") {
|
|
REQUIRE(s.substr(s.size() + 1, 123).empty());
|
|
}
|
|
|
|
SECTION("Substring off the end are trimmed") {
|
|
ss = s.substr(6, 123);
|
|
REQUIRE(std::strcmp(ss.c_str(), "world!") == 0);
|
|
}
|
|
SECTION("substring start after the end is empty") {
|
|
REQUIRE(s.substr(1'000'000, 1).empty());
|
|
}
|
|
}
|
|
|
|
SECTION( "Comparisons are deep" ) {
|
|
char buffer1[] = "Hello";
|
|
char buffer2[] = "Hello";
|
|
CHECK((char*)buffer1 != (char*)buffer2);
|
|
|
|
StringRef left(buffer1), right(buffer2);
|
|
REQUIRE( left == right );
|
|
REQUIRE(left != left.substr(0, 3));
|
|
}
|
|
|
|
SECTION( "from std::string" ) {
|
|
std::string stdStr = "a standard string";
|
|
|
|
SECTION( "implicitly constructed" ) {
|
|
StringRef sr = stdStr;
|
|
REQUIRE( sr == "a standard string" );
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
}
|
|
SECTION( "explicitly constructed" ) {
|
|
StringRef sr( stdStr );
|
|
REQUIRE( sr == "a standard string" );
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
}
|
|
SECTION( "assigned" ) {
|
|
StringRef sr;
|
|
sr = stdStr;
|
|
REQUIRE( sr == "a standard string" );
|
|
REQUIRE( sr.size() == stdStr.size() );
|
|
}
|
|
}
|
|
|
|
SECTION( "to std::string" ) {
|
|
StringRef sr = "a stringref";
|
|
|
|
SECTION( "explicitly constructed" ) {
|
|
std::string stdStr( sr );
|
|
REQUIRE( stdStr == "a stringref" );
|
|
REQUIRE( stdStr.size() == sr.size() );
|
|
}
|
|
SECTION( "assigned" ) {
|
|
std::string stdStr;
|
|
stdStr = static_cast<std::string>(sr);
|
|
REQUIRE( stdStr == "a stringref" );
|
|
REQUIRE( stdStr.size() == sr.size() );
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
|
|
//TODO:
|
|
// * substr
|
|
using Catch::StringRef;
|
|
SECTION("Simple constructors") {
|
|
constexpr StringRef empty{};
|
|
STATIC_REQUIRE(empty.size() == 0);
|
|
STATIC_REQUIRE(empty.begin() == empty.end());
|
|
|
|
constexpr char const* const abc = "abc";
|
|
|
|
constexpr StringRef stringref(abc, 3);
|
|
STATIC_REQUIRE(stringref.size() == 3);
|
|
STATIC_REQUIRE(stringref.isNullTerminated());
|
|
STATIC_REQUIRE(stringref.data() == abc);
|
|
STATIC_REQUIRE(stringref.begin() == abc);
|
|
STATIC_REQUIRE(stringref.begin() != stringref.end());
|
|
STATIC_REQUIRE(stringref.substr(10, 0).empty());
|
|
STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
|
|
|
|
|
|
constexpr StringRef shortened(abc, 2);
|
|
STATIC_REQUIRE(shortened.size() == 2);
|
|
STATIC_REQUIRE(shortened.data() == abc);
|
|
STATIC_REQUIRE(shortened.begin() != shortened.end());
|
|
STATIC_REQUIRE_FALSE(shortened.isNullTerminated());
|
|
STATIC_REQUIRE_FALSE(shortened.substr(1, 3).isNullTerminated());
|
|
}
|
|
SECTION("UDL construction") {
|
|
constexpr auto sr1 = "abc"_catch_sr;
|
|
STATIC_REQUIRE_FALSE(sr1.empty());
|
|
STATIC_REQUIRE(sr1.size() == 3);
|
|
STATIC_REQUIRE(sr1.isNullTerminated());
|
|
|
|
using Catch::operator"" _sr;
|
|
constexpr auto sr2 = ""_sr;
|
|
STATIC_REQUIRE(sr2.empty());
|
|
STATIC_REQUIRE(sr2.size() == 0);
|
|
STATIC_REQUIRE(sr2.isNullTerminated());
|
|
}
|
|
}
|