2020-02-27 12:42:26 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <cstring>
|
2022-03-20 11:02:25 +00:00
|
|
|
#include <doctest.h>
|
2020-09-21 12:01:19 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
2020-02-27 12:42:26 +00:00
|
|
|
#include <unordered_map>
|
2020-09-21 12:01:19 +00:00
|
|
|
#include "../src/String/BasicStringView.hpp"
|
|
|
|
#include "../src/String/StringView.hpp"
|
|
|
|
#include "../src/String/StringViewLiteral.hpp"
|
2020-02-27 12:42:26 +00:00
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Initialize compile time") {
|
2020-06-26 13:56:00 +00:00
|
|
|
static_assert("foo"_cnc.Length() == 3);
|
|
|
|
static_assert("bar"_cnc.Length() == 3);
|
|
|
|
}
|
2020-02-27 12:42:26 +00:00
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Compare compile time") { static_assert("foo"_cnc != "bar"_cnc); }
|
2020-02-27 12:42:26 +00:00
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Compare compile time with CaseInsensitiveStringview") {
|
2020-06-26 13:56:00 +00:00
|
|
|
static_assert("foo"_cnc == ArbUt::StringViewLiteral("foo"));
|
2020-02-27 12:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Use insensitive const string in unordered_map") {
|
2020-06-26 13:56:00 +00:00
|
|
|
std::unordered_map<ArbUt::StringView, int32_t> map;
|
2020-02-27 14:18:54 +00:00
|
|
|
map.insert({"foO"_cnc, 1});
|
|
|
|
map.insert({"bAR"_cnc, 5});
|
|
|
|
|
|
|
|
CHECK(map["bar"_cnc] == 5);
|
|
|
|
CHECK(map["foo"_cnc] == 1);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Use case insensitive const string in switch case") {
|
2020-06-26 13:56:00 +00:00
|
|
|
auto val = ArbUt::StringView("foobar");
|
2020-04-28 14:55:00 +00:00
|
|
|
switch (val) {
|
2020-09-25 09:50:53 +00:00
|
|
|
case "foo"_cnc: FAIL("Wrong case"); break;
|
|
|
|
case "bar"_cnc: FAIL("Wrong case"); break;
|
|
|
|
case "FOObAr"_cnc: break;
|
|
|
|
default: FAIL("Wrong case"); break;
|
2020-02-27 14:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Literal stringview to non literal, then use") {
|
2020-06-26 13:56:00 +00:00
|
|
|
ArbUt::StringView val;
|
2020-04-28 14:55:00 +00:00
|
|
|
{ val = "foobar"_cnc; }
|
2020-04-10 12:38:01 +00:00
|
|
|
INFO(val.c_str());
|
|
|
|
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Pipe stringview into stream") {
|
2020-07-08 11:28:43 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:16:15 +00:00
|
|
|
#ifndef WINDOWS
|
|
|
|
__attribute__((optnone))
|
|
|
|
#endif
|
|
|
|
static ArbUt::StringView
|
2020-07-08 11:28:43 +00:00
|
|
|
TestCreateStringview() {
|
2020-06-27 12:16:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Out of scope char* doesn't lose reference") {
|
2020-07-08 11:28:43 +00:00
|
|
|
ArbUt::StringView val = TestCreateStringview();
|
2020-06-27 12:16:15 +00:00
|
|
|
INFO(val.c_str());
|
|
|
|
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Get Empty String") { REQUIRE(ArbUt::StringView::EmptyString() == ""_cnc); }
|
2020-08-08 09:26:46 +00:00
|
|
|
|
2020-02-27 12:42:26 +00:00
|
|
|
#endif
|