diff --git a/storm/hash/Hashkey.cpp b/storm/hash/Hashkey.cpp index 27b71ff..afd8139 100644 --- a/storm/hash/Hashkey.cpp +++ b/storm/hash/Hashkey.cpp @@ -90,3 +90,54 @@ bool HASHKEY_STRI::operator==(const char* str) const { bool HASHKEY_STRI::operator==(const HASHKEY_STRI& key) const { 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(*this) = str; + return *this; +} + +HASHKEY_CONSTSTRI& HASHKEY_CONSTSTRI::operator=(const HASHKEY_CONSTSTRI& key) { + static_cast(*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); +} diff --git a/storm/hash/Hashkey.hpp b/storm/hash/Hashkey.hpp index aed4f66..39877c7 100644 --- a/storm/hash/Hashkey.hpp +++ b/storm/hash/Hashkey.hpp @@ -49,4 +49,32 @@ class HASHKEY_STRI : public HASHKEY_STR { 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 diff --git a/test/Hash.cpp b/test/Hash.cpp index b9dd14f..58530bc 100644 --- a/test/Hash.cpp +++ b/test/Hash.cpp @@ -54,83 +54,218 @@ TEST_CASE("HASHKEY_PTR::operator==") { } TEST_CASE("HASHKEY_STR", "[hash]") { + const char* foo = "foo"; + SECTION("constructs correctly") { HASHKEY_STR key1; - REQUIRE(key1.GetString() == nullptr); + CHECK(key1.GetString() == nullptr); - HASHKEY_STR key2 = { "foo" }; - REQUIRE(SStrCmp(key2.GetString(), "foo") == 0); + HASHKEY_STR key2 = { foo }; + CHECK(key2.GetString() != foo); + CHECK(SStrCmp(key2.GetString(), "foo") == 0); } } TEST_CASE("HASHKEY_STR::operator=") { + const char* foo = "foo"; + SECTION("assigns from another string") { HASHKEY_STR key; - key = "foo"; - REQUIRE(SStrCmp(key.GetString(), "foo") == 0); + key = foo; + CHECK(key.GetString() != foo); + CHECK(SStrCmp(key.GetString(), "foo") == 0); } SECTION("assigns from another key") { HASHKEY_STR key1 = { "foo" }; HASHKEY_STR key2 = { "bar" }; - REQUIRE(!(key1 == key2)); + CHECK(!(key1 == key2)); + CHECK(key1.GetString() != key2.GetString()); key1 = key2; - REQUIRE(key1 == key2); + CHECK(key1 == key2); + CHECK(key1.GetString() != key2.GetString()); } } TEST_CASE("HASHKEY_STR::operator==") { SECTION("compares to another string") { HASHKEY_STR key = { "foo" }; - REQUIRE(key == "foo"); - REQUIRE(!(key == "FOO")); + CHECK(key == "foo"); + CHECK_FALSE(key == "FOO"); + CHECK_FALSE(key == "poop"); } SECTION("compares to another key") { HASHKEY_STR key1 = { "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]") { + const char* foo = "foo"; + SECTION("constructs correctly") { HASHKEY_STRI key1; - REQUIRE(key1.GetString() == nullptr); + CHECK(key1.GetString() == nullptr); - HASHKEY_STRI key2 = { "foo" }; - REQUIRE(SStrCmp(key2.GetString(), "foo") == 0); + HASHKEY_STRI key2 = { foo }; + CHECK(key2.GetString() != foo); + CHECK(SStrCmp(key2.GetString(), "foo") == 0); } } TEST_CASE("HASHKEY_STRI::operator=") { + const char* foo = "foo"; + SECTION("assigns from another string") { HASHKEY_STRI key; - key = "foo"; - REQUIRE(SStrCmp(key.GetString(), "foo") == 0); + key = foo; + CHECK(key.GetString() != foo); + CHECK(SStrCmp(key.GetString(), "foo") == 0); } SECTION("assigns from another key") { HASHKEY_STRI key1 = { "foo" }; HASHKEY_STRI key2 = { "bar" }; REQUIRE(!(key1 == key2)); + CHECK(key1.GetString() != key2.GetString()); key1 = key2; - REQUIRE(key1 == key2); + CHECK(key1 == key2); + CHECK(key1.GetString() != key2.GetString()); } } TEST_CASE("HASHKEY_STRI::operator==") { SECTION("compares to another string") { HASHKEY_STRI key = { "foo" }; - REQUIRE(key == "FOO"); + CHECK(key == "foo"); + CHECK(key == "FOO"); + CHECK_FALSE(key == "poop"); } SECTION("compares to another key") { HASHKEY_STRI key1 = { "foo" }; - HASHKEY_STRI key2 = { "Foo" }; - REQUIRE(key1 == key2); + HASHKEY_STRI key2 = { "foo" }; + 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); } } diff --git a/test/String.cpp b/test/String.cpp index c350681..14a4cd2 100644 --- a/test/String.cpp +++ b/test/String.cpp @@ -253,7 +253,7 @@ TEST_CASE("SStrDupA", "[string]") { #endif struct TestHash { - const char *str; + const char* str; uint32_t hash; };