feat(hash): add HASHKEY_CONSTSTR and HASHKEY_CONSTSTRI

This commit is contained in:
Adam Heinermann 2025-10-16 14:39:21 -07:00 committed by fallenoak
parent 5190c49019
commit 8c2439277c
4 changed files with 234 additions and 20 deletions

View file

@ -90,3 +90,54 @@ bool HASHKEY_STRI::operator==(const char* str) const {
bool HASHKEY_STRI::operator==(const HASHKEY_STRI& key) const { bool HASHKEY_STRI::operator==(const HASHKEY_STRI& key) const {
return this->operator==(key.m_str); return this->operator==(key.m_str);
} }
HASHKEY_CONSTSTR::HASHKEY_CONSTSTR() {
this->m_str = nullptr;
}
HASHKEY_CONSTSTR::HASHKEY_CONSTSTR(const char* str) {
this->m_str = str;
}
HASHKEY_CONSTSTR::~HASHKEY_CONSTSTR() {
}
HASHKEY_CONSTSTR& HASHKEY_CONSTSTR::operator=(const char* str) {
this->m_str = str;
return *this;
}
HASHKEY_CONSTSTR& HASHKEY_CONSTSTR::operator=(const HASHKEY_CONSTSTR& key) {
this->operator=(key.m_str);
return *this;
}
bool HASHKEY_CONSTSTR::operator==(const char* str) const {
return this->m_str == str || SStrCmp(this->m_str, str) == 0;
}
bool HASHKEY_CONSTSTR::operator==(const HASHKEY_CONSTSTR& key) const {
return this->operator==(key.m_str);
}
const char* HASHKEY_CONSTSTR::GetString() const {
return this->m_str;
}
HASHKEY_CONSTSTRI& HASHKEY_CONSTSTRI::operator=(const char* str) {
static_cast<HASHKEY_CONSTSTR&>(*this) = str;
return *this;
}
HASHKEY_CONSTSTRI& HASHKEY_CONSTSTRI::operator=(const HASHKEY_CONSTSTRI& key) {
static_cast<HASHKEY_CONSTSTR&>(*this) = key.m_str;
return *this;
}
bool HASHKEY_CONSTSTRI::operator==(const char* str) const {
return this->m_str == str || SStrCmpI(this->m_str, str) == 0;
}
bool HASHKEY_CONSTSTRI::operator==(const HASHKEY_CONSTSTRI& key) const {
return this->operator==(key.m_str);
}

View file

@ -49,4 +49,32 @@ class HASHKEY_STRI : public HASHKEY_STR {
bool operator==(const HASHKEY_STRI& key) const; bool operator==(const HASHKEY_STRI& key) const;
}; };
class HASHKEY_CONSTSTR {
public:
// Member functions
HASHKEY_CONSTSTR();
HASHKEY_CONSTSTR(const char* str);
~HASHKEY_CONSTSTR();
HASHKEY_CONSTSTR& operator=(const char* str);
HASHKEY_CONSTSTR& operator=(const HASHKEY_CONSTSTR& key);
bool operator==(const char* str) const;
bool operator==(const HASHKEY_CONSTSTR& key) const;
const char* GetString() const;
protected:
// Member variables
const char* m_str;
};
class HASHKEY_CONSTSTRI : public HASHKEY_CONSTSTR {
public:
// Member functions
HASHKEY_CONSTSTRI() : HASHKEY_CONSTSTR() {};
HASHKEY_CONSTSTRI(const char* str) : HASHKEY_CONSTSTR(str) {};
HASHKEY_CONSTSTRI& operator=(const char* str);
HASHKEY_CONSTSTRI& operator=(const HASHKEY_CONSTSTRI& key);
bool operator==(const char* str) const;
bool operator==(const HASHKEY_CONSTSTRI& key) const;
};
#endif #endif

View file

@ -54,83 +54,218 @@ TEST_CASE("HASHKEY_PTR::operator==") {
} }
TEST_CASE("HASHKEY_STR", "[hash]") { TEST_CASE("HASHKEY_STR", "[hash]") {
const char* foo = "foo";
SECTION("constructs correctly") { SECTION("constructs correctly") {
HASHKEY_STR key1; HASHKEY_STR key1;
REQUIRE(key1.GetString() == nullptr); CHECK(key1.GetString() == nullptr);
HASHKEY_STR key2 = { "foo" }; HASHKEY_STR key2 = { foo };
REQUIRE(SStrCmp(key2.GetString(), "foo") == 0); CHECK(key2.GetString() != foo);
CHECK(SStrCmp(key2.GetString(), "foo") == 0);
} }
} }
TEST_CASE("HASHKEY_STR::operator=") { TEST_CASE("HASHKEY_STR::operator=") {
const char* foo = "foo";
SECTION("assigns from another string") { SECTION("assigns from another string") {
HASHKEY_STR key; HASHKEY_STR key;
key = "foo"; key = foo;
REQUIRE(SStrCmp(key.GetString(), "foo") == 0); CHECK(key.GetString() != foo);
CHECK(SStrCmp(key.GetString(), "foo") == 0);
} }
SECTION("assigns from another key") { SECTION("assigns from another key") {
HASHKEY_STR key1 = { "foo" }; HASHKEY_STR key1 = { "foo" };
HASHKEY_STR key2 = { "bar" }; HASHKEY_STR key2 = { "bar" };
REQUIRE(!(key1 == key2)); CHECK(!(key1 == key2));
CHECK(key1.GetString() != key2.GetString());
key1 = key2; key1 = key2;
REQUIRE(key1 == key2); CHECK(key1 == key2);
CHECK(key1.GetString() != key2.GetString());
} }
} }
TEST_CASE("HASHKEY_STR::operator==") { TEST_CASE("HASHKEY_STR::operator==") {
SECTION("compares to another string") { SECTION("compares to another string") {
HASHKEY_STR key = { "foo" }; HASHKEY_STR key = { "foo" };
REQUIRE(key == "foo"); CHECK(key == "foo");
REQUIRE(!(key == "FOO")); CHECK_FALSE(key == "FOO");
CHECK_FALSE(key == "poop");
} }
SECTION("compares to another key") { SECTION("compares to another key") {
HASHKEY_STR key1 = { "foo" }; HASHKEY_STR key1 = { "foo" };
HASHKEY_STR key2 = { "foo" }; HASHKEY_STR key2 = { "foo" };
REQUIRE(key1 == key2); HASHKEY_STR key3 = { "FOO" };
HASHKEY_STR key4 = { "poop" };
CHECK(key1 == key2);
CHECK_FALSE(key1 == key3);
CHECK_FALSE(key1 == key4);
} }
} }
TEST_CASE("HASHKEY_STRI", "[hash]") { TEST_CASE("HASHKEY_STRI", "[hash]") {
const char* foo = "foo";
SECTION("constructs correctly") { SECTION("constructs correctly") {
HASHKEY_STRI key1; HASHKEY_STRI key1;
REQUIRE(key1.GetString() == nullptr); CHECK(key1.GetString() == nullptr);
HASHKEY_STRI key2 = { "foo" }; HASHKEY_STRI key2 = { foo };
REQUIRE(SStrCmp(key2.GetString(), "foo") == 0); CHECK(key2.GetString() != foo);
CHECK(SStrCmp(key2.GetString(), "foo") == 0);
} }
} }
TEST_CASE("HASHKEY_STRI::operator=") { TEST_CASE("HASHKEY_STRI::operator=") {
const char* foo = "foo";
SECTION("assigns from another string") { SECTION("assigns from another string") {
HASHKEY_STRI key; HASHKEY_STRI key;
key = "foo"; key = foo;
REQUIRE(SStrCmp(key.GetString(), "foo") == 0); CHECK(key.GetString() != foo);
CHECK(SStrCmp(key.GetString(), "foo") == 0);
} }
SECTION("assigns from another key") { SECTION("assigns from another key") {
HASHKEY_STRI key1 = { "foo" }; HASHKEY_STRI key1 = { "foo" };
HASHKEY_STRI key2 = { "bar" }; HASHKEY_STRI key2 = { "bar" };
REQUIRE(!(key1 == key2)); REQUIRE(!(key1 == key2));
CHECK(key1.GetString() != key2.GetString());
key1 = key2; key1 = key2;
REQUIRE(key1 == key2); CHECK(key1 == key2);
CHECK(key1.GetString() != key2.GetString());
} }
} }
TEST_CASE("HASHKEY_STRI::operator==") { TEST_CASE("HASHKEY_STRI::operator==") {
SECTION("compares to another string") { SECTION("compares to another string") {
HASHKEY_STRI key = { "foo" }; HASHKEY_STRI key = { "foo" };
REQUIRE(key == "FOO"); CHECK(key == "foo");
CHECK(key == "FOO");
CHECK_FALSE(key == "poop");
} }
SECTION("compares to another key") { SECTION("compares to another key") {
HASHKEY_STRI key1 = { "foo" }; HASHKEY_STRI key1 = { "foo" };
HASHKEY_STRI key2 = { "Foo" }; HASHKEY_STRI key2 = { "foo" };
REQUIRE(key1 == key2); HASHKEY_STRI key3 = { "FOO" };
HASHKEY_STRI key4 = { "poop" };
CHECK(key1 == key2);
CHECK(key1 == key3);
CHECK_FALSE(key1 == key4);
}
}
TEST_CASE("HASHKEY_CONSTSTR", "[hash]") {
const char* foo = "foo";
SECTION("constructs correctly") {
HASHKEY_CONSTSTR key1;
CHECK(key1.GetString() == nullptr);
HASHKEY_CONSTSTR key2 = { foo };
CHECK(key2.GetString() == foo);
CHECK(SStrCmp(key2.GetString(), "foo") == 0);
}
}
TEST_CASE("HASHKEY_CONSTSTR::operator=") {
const char* foo = "foo";
SECTION("assigns from another string") {
HASHKEY_CONSTSTR key;
key = foo;
CHECK(key.GetString() == foo);
CHECK(SStrCmp(key.GetString(), "foo") == 0);
}
SECTION("assigns from another key") {
HASHKEY_CONSTSTR key1 = { "foo" };
HASHKEY_CONSTSTR key2 = { "bar" };
CHECK(!(key1 == key2));
CHECK(key1.GetString() != key2.GetString());
key1 = key2;
CHECK(key1 == key2);
CHECK(key1.GetString() == key2.GetString());
}
}
TEST_CASE("HASHKEY_CONSTSTR::operator==") {
SECTION("compares to another string") {
HASHKEY_CONSTSTR key = { "foo" };
CHECK(key == "foo");
CHECK_FALSE(key == "FOO");
CHECK_FALSE(key == "poop");
}
SECTION("compares to another key") {
HASHKEY_CONSTSTR key1 = { "foo" };
HASHKEY_CONSTSTR key2 = { "foo" };
HASHKEY_CONSTSTR key3 = { "FOO" };
HASHKEY_CONSTSTR key4 = { "poop" };
CHECK(key1 == key2);
CHECK_FALSE(key1 == key3);
CHECK_FALSE(key1 == key4);
}
}
TEST_CASE("HASHKEY_CONSTSTRI", "[hash]") {
const char* foo = "foo";
SECTION("constructs correctly") {
HASHKEY_CONSTSTRI key1;
CHECK(key1.GetString() == nullptr);
HASHKEY_CONSTSTRI key2 = { foo };
CHECK(key2.GetString() == foo);
CHECK(SStrCmp(key2.GetString(), "foo") == 0);
}
}
TEST_CASE("HASHKEY_CONSTSTRI::operator=") {
const char* foo = "foo";
SECTION("assigns from another string") {
HASHKEY_CONSTSTRI key;
key = foo;
CHECK(key.GetString() == foo);
CHECK(SStrCmp(key.GetString(), "foo") == 0);
}
SECTION("assigns from another key") {
HASHKEY_CONSTSTRI key1 = { "foo" };
HASHKEY_CONSTSTRI key2 = { "bar" };
REQUIRE(!(key1 == key2));
CHECK(key1.GetString() != key2.GetString());
key1 = key2;
CHECK(key1 == key2);
CHECK(key1.GetString() == key2.GetString());
}
}
TEST_CASE("HASHKEY_CONSTSTRI::operator==") {
SECTION("compares to another string") {
HASHKEY_CONSTSTRI key = { "foo" };
CHECK(key == "foo");
CHECK(key == "FOO");
CHECK_FALSE(key == "poop");
}
SECTION("compares to another key") {
HASHKEY_CONSTSTRI key1 = { "foo" };
HASHKEY_CONSTSTRI key2 = { "foo" };
HASHKEY_CONSTSTRI key3 = { "FOO" };
HASHKEY_CONSTSTRI key4 = { "poop" };
CHECK(key1 == key2);
CHECK(key1 == key3);
CHECK_FALSE(key1 == key4);
} }
} }

View file

@ -253,7 +253,7 @@ TEST_CASE("SStrDupA", "[string]") {
#endif #endif
struct TestHash { struct TestHash {
const char *str; const char* str;
uint32_t hash; uint32_t hash;
}; };