NBEngine/engine/NBCore/tests/testUtils.cpp
2026-04-19 18:08:58 -05:00

148 lines
3.6 KiB
C++

#define CODE_ERROR_LOCATIONS
#include <gtest/gtest.h>
#include <string>
#include "TypeTraits.hpp"
#include "StringUtils.hpp"
namespace nb {
TEST(UtilsTest, TestFindAndReplace) {
ASSERT_STREQ(
find_and_replace("Jeff", "e", "efe").c_str(),
"Jefeff"
);
std::string tmp = find_and_replace("Naif", "a", "afa");
ASSERT_STREQ(
find_and_replace(tmp, "i", "ifi").c_str(),
"Nafaifif"
);
tmp = find_and_replace("aeiou", "a", "afa");
tmp = find_and_replace(tmp, "e", "efe");
tmp = find_and_replace(tmp, "i", "ifi");
tmp = find_and_replace(tmp, "o", "ofo");
tmp = find_and_replace(tmp, "u", "ufu");
ASSERT_STREQ(
tmp.c_str(),
"afaefeifiofoufu"
);
tmp = find_and_replace(tmp, "afa", "a");
tmp = find_and_replace(tmp, "efe", "e");
tmp = find_and_replace(tmp, "ifi", "i");
tmp = find_and_replace(tmp, "ofo", "o");
tmp = find_and_replace(tmp, "ufu", "u");
ASSERT_STREQ(
tmp.c_str(),
"aeiou"
);
}
TEST(UtilsTest, TestFindAndReplaceWstr) {
ASSERT_STREQ(
find_and_replace<std::wstring>(L"Jeff", L"e", L"efe").c_str(),
L"Jefeff"
);
std::wstring tmp = find_and_replace<std::wstring>(L"Naif", L"a", L"afa");
ASSERT_STREQ(
find_and_replace<std::wstring>(tmp, L"i", L"ifi").c_str(),
L"Nafaifif"
);
tmp = find_and_replace<std::wstring>(L"aeiou", L"a", L"afa");
tmp = find_and_replace<std::wstring>(tmp, L"e", L"efe");
tmp = find_and_replace<std::wstring>(tmp, L"i", L"ifi");
tmp = find_and_replace<std::wstring>(tmp, L"o", L"ofo");
tmp = find_and_replace<std::wstring>(tmp, L"u", L"ufu");
ASSERT_STREQ(
tmp.c_str(),
L"afaefeifiofoufu"
);
tmp = find_and_replace<std::wstring>(tmp, L"afa", L"a");
tmp = find_and_replace<std::wstring>(tmp, L"efe", L"e");
tmp = find_and_replace<std::wstring>(tmp, L"ifi", L"i");
tmp = find_and_replace<std::wstring>(tmp, L"ofo", L"o");
tmp = find_and_replace<std::wstring>(tmp, L"ufu", L"u");
ASSERT_STREQ(
tmp.c_str(),
L"aeiou"
);
}
TEST(UtilsTest, TestWstrToStr) {
ASSERT_STREQ(
nb::wstr_to_str(L"Hi!").c_str(),
"Hi!"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif").c_str(),
"Naif"
);
ASSERT_STREQ(
nb::wstr_to_str(L"\r\r\r\n").c_str(),
"\r\r\r\n"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
"Naif\ttalks\r\ra\t\nlot"
);
}
TEST(UtilsTest, TestStrToWstr) {
ASSERT_STREQ(
nb::str_to_wstr("Hi!").c_str(),
L"Hi!"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif").c_str(),
L"Naif"
);
ASSERT_STREQ(
nb::str_to_wstr("\r\r\r\n").c_str(),
L"\r\r\r\n"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
L"Naif\ttalks\r\ra\t\nlot"
);
}
struct A { bool x(); };
struct B { bool y(); };
struct C { bool x(); bool y; };
struct D : C {};
template <typename T>
using has_x = decltype(T::x);
template <typename T>
using has_y = decltype(T::y);
TEST(UtilsTest, TestIsDetected) {
auto ret = is_detected<has_x, A>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, A>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_y, B>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, B>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_x, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, D>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, D>::value;
ASSERT_TRUE(ret);
}
} // namespace nb