mirror of
https://github.com/catchorg/Catch2.git
synced 2025-05-24 13:39:24 +00:00
Added string classes
This commit is contained in:
parent
cb0a5194af
commit
78e7994435
13 changed files with 908 additions and 0 deletions
76
projects/SelfTest/StringBuilder.tests.cpp
Normal file
76
projects/SelfTest/StringBuilder.tests.cpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include "internal/catch_stringbuilder.h"
|
||||
#include "../include/internal/catch_stringref.h"
|
||||
#include "../include/internal/catch_string.h"
|
||||
|
||||
#include "catch.hpp"
|
||||
|
||||
TEST_CASE( "StringBuilder", "[Strings]" ) {
|
||||
|
||||
using Catch::StringBuilder;
|
||||
using Catch::String;
|
||||
|
||||
StringBuilder sb;
|
||||
|
||||
SECTION( "basic" ) {
|
||||
REQUIRE( sb.capacity() == 0 );
|
||||
REQUIRE( sb.size() == 0 );
|
||||
|
||||
sb.reserve( 32 );
|
||||
REQUIRE( sb.capacity() == 32 );
|
||||
REQUIRE( sb.size() == 0 );
|
||||
|
||||
sb.append( "hello" );
|
||||
REQUIRE( sb.capacity() == 32 );
|
||||
REQUIRE( sb.size() == 5 );
|
||||
|
||||
String s = std::move( sb );
|
||||
REQUIRE( s == "hello" );
|
||||
REQUIRE( s.size() == 5 );
|
||||
}
|
||||
|
||||
SECTION( "concatenation" ) {
|
||||
sb << "hello" << " " << "world";
|
||||
String s = std::move( sb );
|
||||
REQUIRE( s == "hello world" );
|
||||
}
|
||||
|
||||
SECTION( "concat & move" ) {
|
||||
String s = StringBuilder() << "hello" << " " << "world";
|
||||
REQUIRE( s == "hello world" );
|
||||
}
|
||||
|
||||
SECTION( "reserved" ) {
|
||||
StringBuilder sb16( 16 );
|
||||
REQUIRE( sb16.capacity() == 16 );
|
||||
sb16 << "hello" << " " << "world";
|
||||
REQUIRE( sb16.capacity() == 16 );
|
||||
String s = std::move( sb16 );
|
||||
REQUIRE( s == "hello world" );
|
||||
}
|
||||
|
||||
SECTION( "from String" ) {
|
||||
String s = "hello";
|
||||
|
||||
SECTION( "copy" ) {
|
||||
StringBuilder sb2 = s;
|
||||
String s2( std::move(sb2) );
|
||||
REQUIRE( s2 == s );
|
||||
REQUIRE( s2.c_str() != s.c_str() );
|
||||
}
|
||||
SECTION( "move from uniquely owned string" ) {
|
||||
auto originalPointer = s.c_str();
|
||||
StringBuilder sb2( std::move( s ) );
|
||||
String s2( std::move(sb2) );
|
||||
REQUIRE( s2 == "hello" );
|
||||
REQUIRE( s2.c_str() == originalPointer );
|
||||
}
|
||||
SECTION( "move from shared string (copies)" ) {
|
||||
auto originalPointer = s.c_str();
|
||||
String keepAlive = s;
|
||||
StringBuilder sb2( std::move( s ) );
|
||||
String s2( std::move(sb2) );
|
||||
REQUIRE( s2 == "hello" );
|
||||
REQUIRE( s2.c_str() != originalPointer );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue