2020-02-27 12:42:26 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <cstring>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include "../extern/catch.hpp"
|
|
|
|
#include "../src/ConstString.hpp"
|
|
|
|
|
|
|
|
TEST_CASE("Use const string in unordered_map", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
std::unordered_map<ArbUt::ConstString, int32_t> map;
|
2020-02-27 12:42:26 +00:00
|
|
|
map.insert({"foo"_c, 1});
|
|
|
|
map.insert({"bar"_c, 5});
|
|
|
|
|
|
|
|
CHECK(map["bar"_c] == 5);
|
|
|
|
CHECK(map["foo"_c] == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Use const string in switch case", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
auto val = ArbUt::ConstString("foobar");
|
2020-04-28 14:55:00 +00:00
|
|
|
switch (val) {
|
2020-02-27 12:42:26 +00:00
|
|
|
case "foo"_c: FAIL(); break;
|
|
|
|
case "bar"_c: FAIL(); break;
|
|
|
|
case "foobar"_c: SUCCEED(); break;
|
|
|
|
default: FAIL(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:18:54 +00:00
|
|
|
TEST_CASE("Use insensitive const string in unordered_map", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
std::unordered_map<ArbUt::CaseInsensitiveConstString, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Use case insensitive const string in switch case", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
auto val = ArbUt::CaseInsensitiveConstString("foobar");
|
2020-04-28 14:55:00 +00:00
|
|
|
switch (val) {
|
2020-02-27 14:18:54 +00:00
|
|
|
case "foo"_cnc: FAIL(); break;
|
|
|
|
case "bar"_cnc: FAIL(); break;
|
|
|
|
case "FOObAr"_cnc: SUCCEED(); break;
|
|
|
|
default: FAIL(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:04:28 +00:00
|
|
|
#ifndef WINDOWS
|
2020-04-07 16:54:23 +00:00
|
|
|
__attribute__((optnone))
|
2020-04-09 16:04:28 +00:00
|
|
|
#endif
|
2020-05-26 15:21:08 +00:00
|
|
|
static ArbUt::CaseInsensitiveConstString
|
2020-04-28 14:55:00 +00:00
|
|
|
TestCreateConstString() {
|
|
|
|
char originalVal[7];
|
2020-04-07 16:54:23 +00:00
|
|
|
originalVal[0] = 'f';
|
|
|
|
originalVal[1] = 'o';
|
|
|
|
originalVal[2] = 'o';
|
|
|
|
originalVal[3] = 'b';
|
|
|
|
originalVal[4] = 'a';
|
|
|
|
originalVal[5] = 'r';
|
2020-04-07 16:57:11 +00:00
|
|
|
originalVal[6] = '\0';
|
2020-05-26 15:21:08 +00:00
|
|
|
return ArbUt::CaseInsensitiveConstString(originalVal);
|
2020-04-07 16:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
ArbUt::CaseInsensitiveConstString val = TestCreateConstString();
|
2020-04-07 16:54:23 +00:00
|
|
|
INFO(val.c_str());
|
|
|
|
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:38:01 +00:00
|
|
|
TEST_CASE("Literal conststring to non literal, then use", "[Utilities]") {
|
2020-05-26 15:21:08 +00:00
|
|
|
ArbUt::CaseInsensitiveConstString 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-02-27 12:42:26 +00:00
|
|
|
#endif
|