Arbutils/tests/StringViewTests.cpp

85 lines
2.2 KiB
C++

#ifdef TESTS_BUILD
#include <cstring>
#include <sstream>
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <doctest.h>
#include "../src/String/BasicStringView.hpp"
#include "../src/String/StringView.hpp"
#include "../src/String/StringViewLiteral.hpp"
TEST_CASE("Initialize compile time") {
static_assert("foo"_cnc.Length() == 3);
static_assert("bar"_cnc.Length() == 3);
}
TEST_CASE("Compare compile time") { static_assert("foo"_cnc != "bar"_cnc); }
TEST_CASE("Compare compile time with CaseInsensitiveStringview") {
static_assert("foo"_cnc == ArbUt::StringViewLiteral("foo"));
}
TEST_CASE("Use insensitive const string in unordered_map") {
std::unordered_map<ArbUt::StringView, int32_t> map;
map.insert({"foO"_cnc, 1});
map.insert({"bAR"_cnc, 5});
CHECK(map["bar"_cnc] == 5);
CHECK(map["foo"_cnc] == 1);
}
TEST_CASE("Use case insensitive const string in switch case") {
auto val = ArbUt::StringView("foobar");
switch (val) {
case "foo"_cnc: FAIL("Wrong case"); break;
case "bar"_cnc: FAIL("Wrong case"); break;
case "FOObAr"_cnc: break;
default: FAIL("Wrong case"); break;
}
}
TEST_CASE("Literal stringview to non literal, then use") {
ArbUt::StringView val;
{ val = "foobar"_cnc; }
INFO(val.c_str());
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
}
TEST_CASE("Pipe stringview into stream") {
std::stringstream ss;
ArbUt::StringView val = "foo";
ss << val;
REQUIRE(ss.str() == "foo");
ss << val;
REQUIRE(ss.str() == "foofoo");
ArbUt::StringView val2 = "bar";
ss << val2;
REQUIRE(ss.str() == "foofoobar");
}
#ifndef WINDOWS
__attribute__((optnone))
#endif
static ArbUt::StringView
TestCreateStringview() {
char originalVal[7];
originalVal[0] = 'f';
originalVal[1] = 'o';
originalVal[2] = 'o';
originalVal[3] = 'b';
originalVal[4] = 'a';
originalVal[5] = 'r';
originalVal[6] = '\0';
return ArbUt::StringView(originalVal);
}
TEST_CASE("Out of scope char* doesn't lose reference") {
ArbUt::StringView val = TestCreateStringview();
INFO(val.c_str());
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
}
TEST_CASE("Get Empty String") { REQUIRE(ArbUt::StringView::EmptyString() == ""_cnc); }
#endif