2020-11-14 17:18:49 -06:00
|
|
|
#include "storm/String.hpp"
|
2020-11-15 13:09:14 -06:00
|
|
|
#include "storm/Memory.hpp"
|
2020-11-14 17:18:49 -06:00
|
|
|
#include "test/Test.hpp"
|
|
|
|
|
|
2020-11-21 12:11:01 -06:00
|
|
|
TEST_CASE("SStrChr", "[string]") {
|
|
|
|
|
SECTION("finds first character when it exists at start of string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = 'f';
|
|
|
|
|
auto result = SStrChr(string, search);
|
|
|
|
|
REQUIRE(result == string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds first character when it exists in middle of string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = 'b';
|
|
|
|
|
auto result = SStrChr(string, search);
|
|
|
|
|
REQUIRE(result == string + 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds first character when it exists at end of string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = 'r';
|
|
|
|
|
auto result = SStrChr(string, search);
|
|
|
|
|
REQUIRE(result == string + 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("returns nullptr when character does not exist in string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = 'z';
|
|
|
|
|
auto result = SStrChr(string, search);
|
|
|
|
|
REQUIRE(result == nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("returns nullptr when string is empty") {
|
|
|
|
|
auto string = "";
|
|
|
|
|
auto search = 'z';
|
|
|
|
|
auto result = SStrChr(string, search);
|
|
|
|
|
REQUIRE(result == nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 12:49:47 -06:00
|
|
|
TEST_CASE("SStrChrR", "[string]") {
|
|
|
|
|
SECTION("finds last character when it exists at start of string") {
|
|
|
|
|
auto string = "ffoobbaarr";
|
|
|
|
|
auto search = 'f';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == string + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds last character when it exists in middle of string") {
|
|
|
|
|
auto string = "ffoobbaarr";
|
|
|
|
|
auto search = 'b';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == string + 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds last character when it exists at end of string") {
|
|
|
|
|
auto string = "ffoobbaarr";
|
|
|
|
|
auto search = 'r';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == string + 9);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds last character when it exists at start and end of string") {
|
|
|
|
|
auto string = "ffoobbaarrff";
|
|
|
|
|
auto search = 'f';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == string + 11);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("returns nullptr when character does not exist in string") {
|
|
|
|
|
auto string = "ffoobbaarr";
|
|
|
|
|
auto search = 'z';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("returns nullptr when string is empty") {
|
|
|
|
|
auto string = "";
|
|
|
|
|
auto search = 'z';
|
|
|
|
|
auto result = SStrChrR(string, search);
|
|
|
|
|
REQUIRE(result == nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 17:58:34 -06:00
|
|
|
TEST_CASE("SStrCmp", "[string]") {
|
|
|
|
|
SECTION("compares two strings that exactly match correctly") {
|
|
|
|
|
auto compare = SStrCmp("foo", "foo", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("compares two strings that partially match correctly") {
|
|
|
|
|
auto compare1 = SStrCmp("bar", "foobar", STORM_MAX_STR);
|
|
|
|
|
auto compare2 = SStrCmp("foobar", "bar", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare1 < 0);
|
|
|
|
|
REQUIRE(compare2 > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("compares two strings that do not match correctly") {
|
|
|
|
|
auto compare = SStrCmp("bar", "xyzzy", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare < 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 12:47:41 -06:00
|
|
|
TEST_CASE("SStrCmpI", "[string]") {
|
|
|
|
|
SECTION("compares two strings that exactly match correctly") {
|
|
|
|
|
auto compare = SStrCmpI("foo", "foo", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("compares two strings that match with differing case correctly") {
|
|
|
|
|
auto compare = SStrCmpI("foo", "foO", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("compares two strings that do not match correctly") {
|
|
|
|
|
auto compare = SStrCmpI("bar", "xyzzy", STORM_MAX_STR);
|
|
|
|
|
REQUIRE(compare < 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 13:09:14 -06:00
|
|
|
TEST_CASE("SStrDupA", "[string]") {
|
|
|
|
|
SECTION("duplicates string correctly") {
|
|
|
|
|
auto string1 = "foo bar";
|
|
|
|
|
auto string2 = SStrDupA(string1, __FILE__, __LINE__);
|
|
|
|
|
auto compare = SStrCmp(string1, string2, STORM_MAX_STR);
|
|
|
|
|
auto newPtr = string1 != string2;
|
|
|
|
|
SMemFree(string2);
|
|
|
|
|
|
|
|
|
|
REQUIRE(compare == 0);
|
|
|
|
|
REQUIRE(newPtr == true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 17:18:49 -06:00
|
|
|
TEST_CASE("SStrHashHT", "[string]") {
|
|
|
|
|
SECTION("hashes simple string correctly") {
|
|
|
|
|
auto hash = SStrHashHT("foo");
|
|
|
|
|
REQUIRE(hash == 1371562358u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("hashes string with forward slash correctly") {
|
|
|
|
|
auto hash = SStrHashHT("foo/bar");
|
|
|
|
|
REQUIRE(hash == 2270424393u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("hashes string with forward slash equivalent to back slash") {
|
|
|
|
|
auto hashForwardSlash = SStrHashHT("foo/bar");
|
|
|
|
|
auto hashBackSlash = SStrHashHT("foo\\bar");
|
|
|
|
|
REQUIRE(hashForwardSlash == hashBackSlash);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-15 12:57:32 -06:00
|
|
|
|
|
|
|
|
TEST_CASE("SStrLen", "[string]") {
|
|
|
|
|
SECTION("calculates string length correctly") {
|
|
|
|
|
auto length = SStrLen("foo");
|
|
|
|
|
REQUIRE(length == 3);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-16 17:44:08 -06:00
|
|
|
|
2020-11-16 18:41:53 -06:00
|
|
|
TEST_CASE("SStrLower", "[string]") {
|
|
|
|
|
SECTION("rewrites uppercase string to lowercase correctly") {
|
|
|
|
|
auto upper = "FOOBAR";
|
|
|
|
|
char* lower = static_cast<char*>(SMemAlloc(SStrLen(upper) + 1, __FILE__, __LINE__, 0x0));
|
|
|
|
|
SStrCopy(lower, upper, STORM_MAX_STR);
|
|
|
|
|
SStrLower(lower);
|
|
|
|
|
auto compare = SStrCmp(lower, "foobar", SStrLen(lower));
|
|
|
|
|
SMemFree(lower);
|
|
|
|
|
|
|
|
|
|
REQUIRE(!compare);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("rewrites lowercase string to lowercase correctly") {
|
|
|
|
|
auto lower1 = "foobar";
|
|
|
|
|
char* lower2 = static_cast<char*>(SMemAlloc(SStrLen(lower1) + 1, __FILE__, __LINE__, 0x0));
|
|
|
|
|
SStrCopy(lower2, lower1, STORM_MAX_STR);
|
|
|
|
|
SStrLower(lower2);
|
|
|
|
|
auto compare = SStrCmp(lower2, "foobar", SStrLen(lower2));
|
|
|
|
|
SMemFree(lower2);
|
|
|
|
|
|
|
|
|
|
REQUIRE(!compare);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 17:44:08 -06:00
|
|
|
TEST_CASE("SStrStr", "[string]") {
|
|
|
|
|
SECTION("finds substring when it exists at end of string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = "bar";
|
|
|
|
|
auto substring = SStrStr(string, search);
|
|
|
|
|
REQUIRE(!SStrCmp(search, substring, SStrLen(search)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("finds substring when it exists at start of string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = "foo";
|
|
|
|
|
auto substring = SStrStr(string, search);
|
|
|
|
|
REQUIRE(!SStrCmp(search, substring, SStrLen(search)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 17:54:56 -06:00
|
|
|
SECTION("finds substring when search is empty") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = "";
|
|
|
|
|
auto substring = SStrStr(string, search);
|
|
|
|
|
REQUIRE(!SStrCmp(string, substring, SStrLen(string)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SECTION("returns nullptr when search does not exist in string") {
|
|
|
|
|
auto string = "foobar";
|
|
|
|
|
auto search = "xyzzy";
|
|
|
|
|
auto substring = SStrStr(string, search);
|
|
|
|
|
REQUIRE(substring == nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 17:44:08 -06:00
|
|
|
SECTION("returns nullptr when given empty string") {
|
|
|
|
|
auto string = "";
|
|
|
|
|
auto search = "bar";
|
|
|
|
|
auto substring = SStrStr(string, search);
|
|
|
|
|
REQUIRE(substring == nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|