129 lines
3.3 KiB
C++
129 lines
3.3 KiB
C++
#define CODE_ERROR_LOCATIONS
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <string>
|
|
#include <NBCore/DataTypes.hpp>
|
|
#include <NBCore/TypeTraits.hpp>
|
|
#include <NBCore/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<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
|
L"Jefeff"
|
|
);
|
|
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
|
ASSERT_STREQ(
|
|
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
|
L"Nafaifif"
|
|
);
|
|
|
|
tmp = find_and_replace<wchar_t>(L"aeiou", L"a", L"afa");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"e", L"efe");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"i", L"ifi");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"o", L"ofo");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"u", L"ufu");
|
|
ASSERT_STREQ(
|
|
tmp.c_str(),
|
|
L"afaefeifiofoufu"
|
|
);
|
|
|
|
tmp = find_and_replace<wchar_t>(tmp, L"afa", L"a");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"efe", L"e");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"ifi", L"i");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"ofo", L"o");
|
|
tmp = find_and_replace<wchar_t>(tmp, L"ufu", L"u");
|
|
ASSERT_STREQ(
|
|
tmp.c_str(),
|
|
L"aeiou"
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
TEST(DataTypesTests, TestNDArray) {
|
|
float* data_f = new float[9] {0,1,2,3,4,5,6,7,8};
|
|
nb::NDArray<float, 2> arr({3, 3}, data_f);
|
|
ASSERT_FLOAT_EQ(data_f[4], arr.at({1,1}));
|
|
arr.at({1,1}) = 0.5;
|
|
ASSERT_FLOAT_EQ(0.5, arr.at({1,1}));
|
|
delete[] data_f;
|
|
|
|
unsigned int* data_ui = new unsigned int[8] {0,1,2,3,4,5,6,7};
|
|
nb::NDArray<unsigned int, 3> arr3d({2,2,2}, data_ui);
|
|
ASSERT_EQ(data_ui[5], arr3d.at({1,0,1}));
|
|
arr3d.at({0,1,1}) = 23;
|
|
ASSERT_EQ(23, arr3d.at({0,1,1}));
|
|
delete[] data_ui;
|
|
|
|
|
|
}
|
|
|
|
} // namespace nb
|