From 6e19b57d85c8b7a1b147da1ae892d4e1e0a13206 Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 28 Aug 2017 18:35:52 +0100 Subject: [PATCH] Add unit tests for string_utils.cpp --- renderdoc/serialise/string_utils.cpp | 133 ++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) diff --git a/renderdoc/serialise/string_utils.cpp b/renderdoc/serialise/string_utils.cpp index 0837235dc..8f3fef66a 100644 --- a/renderdoc/serialise/string_utils.cpp +++ b/renderdoc/serialise/string_utils.cpp @@ -120,4 +120,135 @@ std::string removeFromEnd(const std::string &value, const std::string &ending) // If pattern not found, just return original string return value; -} \ No newline at end of file +} + +#if ENABLED(ENABLE_UNIT_TESTS) +#include "3rdparty/catch/catch.hpp" + +TEST_CASE("String hashing", "[string]") +{ + SECTION("Same value returns the same hash") + { + REQUIRE(strhash("foobar") == strhash("foobar")); + REQUIRE(strhash("blah") == strhash("blah")); + REQUIRE(strhash("test of a long string for strhash") == + strhash("test of a long string for strhash")); + }; + + SECTION("Different inputs have different hashes") + { + REQUIRE(strhash("foobar") != strhash("blah")); + REQUIRE(strhash("test thing") != strhash("test test test")); + REQUIRE(strhash("test1") != strhash("test2")); + REQUIRE(strhash("test1") != strhash("test3")); + }; + + SECTION("Same input with different seeds have different hashes") + { + REQUIRE(strhash("foobar", 1) != strhash("foobar", 2)); + REQUIRE(strhash("foobar", 100) != strhash("foobar", 256)); + REQUIRE(strhash("foobar", 1024) != strhash("foobar", 2048)); + }; + + SECTION("Incremental hashing") + { + int complete = strhash("test of a long string for strhash"); + + int partial = strhash("test of"); + partial = strhash(" a long", partial); + partial = strhash(" string", partial); + partial = strhash(" for ", partial); + partial = strhash("strhash", partial); + + REQUIRE(partial == complete); + }; +}; + +TEST_CASE("String manipulation", "[string]") +{ + SECTION("strlower") + { + REQUIRE(strlower("foobar") == "foobar"); + REQUIRE(strlower("Foobar") == "foobar"); + REQUIRE(strlower("FOOBAR") == "foobar"); + }; + + SECTION("strupper") + { + REQUIRE(strupper("foobar") == "FOOBAR"); + REQUIRE(strupper("Foobar") == "FOOBAR"); + REQUIRE(strupper("FOOBAR") == "FOOBAR"); + }; + + SECTION("trim") + { + REQUIRE(trim(" foo bar ") == "foo bar"); + REQUIRE(trim(" Foo bar") == "Foo bar"); + REQUIRE(trim(" Foo\nbar") == "Foo\nbar"); + REQUIRE(trim("FOO BAR ") == "FOO BAR"); + REQUIRE(trim("FOO BAR \t\n") == "FOO BAR"); + }; + + SECTION("endswith / removeFromEnd") + { + REQUIRE(endswith("foobar", "bar")); + REQUIRE_FALSE(endswith("foobar", "foo")); + REQUIRE(endswith("foobar", "")); + + REQUIRE(removeFromEnd("test/foobar", "") == "test/foobar"); + REQUIRE(removeFromEnd("test/foobar", "foo") == "test/"); + REQUIRE(removeFromEnd("test/foobar", "bar") == "test/foo"); + }; + + SECTION("split by comma") + { + std::vector vec; + + split(std::string("foo,bar, blah,test"), vec, ','); + + REQUIRE(vec.size() == 4); + REQUIRE(vec[0] == "foo"); + REQUIRE(vec[1] == "bar"); + REQUIRE(vec[2] == " blah"); + REQUIRE(vec[3] == "test"); + }; + + SECTION("split by space") + { + std::vector vec; + + split(std::string("this is a test string for splitting! "), vec, ' '); + + REQUIRE(vec.size() == 9); + REQUIRE(vec[0] == "this"); + REQUIRE(vec[1] == "is"); + REQUIRE(vec[2] == "a"); + REQUIRE(vec[3] == "test"); + REQUIRE(vec[4] == "string"); + REQUIRE(vec[5] == "for"); + REQUIRE(vec[6] == ""); + REQUIRE(vec[7] == ""); + REQUIRE(vec[8] == "splitting!"); + }; + + SECTION("merge") + { + std::vector vec; + std::string str; + + merge(vec, str, ' '); + REQUIRE(str == ""); + + vec.push_back("Hello"); + + merge(vec, str, ' '); + REQUIRE(str == "Hello"); + + vec.push_back("World"); + + merge(vec, str, ' '); + REQUIRE(str == "Hello World"); + }; +}; + +#endif // ENABLED(ENABLE_UNIT_TESTS)