1 #include <catch2/catch_all.hpp>
2 #include <catch2/matchers/catch_matchers_all.hpp>
4 using namespace Catch::Matchers;
8 #if(__cplusplus >= 201703L) || (_MSVC_LANG >= 201703L)
15 #if defined(_WIN32) && defined(_MSC_VER)
16 #define NATIVETYPE std::wstring
17 #define MAKENATIVEx(x) L##x
18 #define MAKENATIVE(x) MAKENATIVEx(x)
19 #define DELETEFILE _wremove
20 #define NATIVELENGTH(x) static_cast<std::wstring>(x).length()
22 #define NATIVETYPE std::string
23 #define MAKENATIVE(x) x
24 #define DELETEFILE std::remove
25 #define NATIVELENGTH(x) u8length(static_cast<std::string>(x).c_str())
28 #if defined(__cpp_lib_char8_t)
29 #define U8CHAR char8_t
30 #define U8STRING std::u8string
33 #define U8STRING std::string
36 #define PATH1 "C:\\dir1\\file1.txt"
37 #define PATH2 "file2.txt"
38 #define PATH3 "/dir3/dir33/file3.txt"
39 #define PATH4 u8"\u00e4\u00eb\u00ef\u00f6\u00fc\u00e1\u00e9\u00ed\u00f3\u00fa\u00df\u00c6\u002e\u010c\u011a\u0141"
40 #define PATH5 "asdf.nothing"
41 #define FILETEXT "This is a test\n"
42 #if defined(_WIN32) && defined(_MSC_VER)
43 #define LPATH4 L"\u00e4\u00eb\u00ef\u00f6\u00fc\u00e1\u00e9\u00ed\u00f3\u00fa\u00df\u00c6\u002e\u010c\u011a\u0141"
44 #define LPATH5 L"asdf.nothing"
50 for(; *str != 0; ++str) count += ((*str & 0xc0) != 0x80);
54 template <
typename U,
typename V>
56 return std::equal(a.cbegin(), a.cend(), b.cbegin(), b.cend(), [](
const typename U::value_type& first,
const typename V::value_type& second) {
57 return *reinterpret_cast<const typename U::value_type*>(&first) == *reinterpret_cast<const typename U::value_type*>(&second);
61 TEST_CASE(
"dai::Path utf-8 and native char set handling") {
64 REQUIRE(emptyString.empty());
78 U8CHAR tmp_name4[L_tmpnam];
79 REQUIRE(std::tmpnam(
reinterpret_cast<char*
>(&tmp_name4[0])) !=
nullptr);
86 REQUIRE_NOTHROW([&]() {
87 std::ofstream file(path4);
91 std::ifstream file(path4, std::ios::binary);
92 if(!file.is_open() || !file.good() || file.bad()) {
93 throw std::runtime_error(
"file not found or corrupted");
95 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
96 const char c1 = file.get();
100 auto getBlob = [](
const dai::Path& path) ->
bool {
return !
static_cast<NATIVETYPE>(path).empty(); };
101 REQUIRE(getBlob(string4));
102 REQUIRE(getBlob(
"mypath/myfile.blob"));
103 REQUIRE(getBlob([]() -> std::string {
return "mypath/rvalue.blob"; }()));
104 #if defined(_WIN32) && defined(_MSC_VER)
105 REQUIRE(getBlob(L
"mywidepath/myfile.blob"));
106 std::wstring move2ndTry(L
"move my data on the second try");
107 REQUIRE(getBlob(move2ndTry));
108 REQUIRE(getBlob(std::move(move2ndTry)));
114 REQUIRE_THROWS_AS(path4.
string(), std::range_error);
116 REQUIRE_NOTHROW(path4.
string());
119 REQUIRE(path3.
string().length() == (
sizeof(
PATH3) - 1));
121 REQUIRE(path4.
u8string() == string4);
123 auto wrapper = [&getBlob](
const dai::Path& path) ->
bool {
return getBlob(path); };
124 REQUIRE(wrapper(
"pass dai::Path across functions"));
127 #if defined(__cpp_lib_filesystem)
128 const std::filesystem::path fspath1(
PATH1);
129 const std::filesystem::path fspath2(
PATH2);
130 const std::filesystem::path fspath3(
PATH3);
131 #if defined(__cpp_lib_char8_t)
132 const auto fspath4 = std::filesystem::path(
PATH4);
134 const auto fspath4 = std::filesystem::u8path(
PATH4);
136 REQUIRE(getBlob(fspath1));
137 REQUIRE(getBlob(fspath2));
138 REQUIRE(getBlob(fspath3));
139 REQUIRE(getBlob(fspath4));
144 U8CHAR osTmpPathname[L_tmpnam];
145 REQUIRE(std::tmpnam(
reinterpret_cast<char*
>(&osTmpPathname[0])) !=
nullptr);
154 #if defined(_WIN32) && defined(_MSC_VER)
155 REQUIRE_THROWS_WITH(nn->setBlobPath(
PATH4), ContainsSubstring(
"Cannot load blob") && ContainsSubstring(
"not convertible"));
156 REQUIRE_THROWS_WITH(nn->setBlobPath(strPath), ContainsSubstring(
"Cannot load blob") && ContainsSubstring(
"not convertible"));
157 REQUIRE_THROWS_WITH(nn->setBlobPath(daiPath), ContainsSubstring(
"Cannot load blob") && ContainsSubstring(
"not convertible"));
159 REQUIRE_THROWS_WITH(nn->setBlobPath(
PATH4), ContainsSubstring(std::string(
"Cannot load blob")) && ContainsSubstring(
dai::Path(
PATH4).
string()));
160 REQUIRE_THROWS_WITH(nn->setBlobPath(strPath),
161 ContainsSubstring(
"Cannot load blob") && ContainsSubstring(std::string{
reinterpret_cast<const char*
>(strPath.c_str())}));
162 REQUIRE_THROWS_WITH(nn->setBlobPath(daiPath), ContainsSubstring(
"Cannot load blob") && ContainsSubstring(daiPath.
string()));
166 const std::string blobPath(BLOB_PATH);
170 REQUIRE_NOTHROW(nn->setBlobPath(BLOB_PATH));
171 nn->getAssetManager().remove(
"__blob");
172 REQUIRE_NOTHROW(nn->setBlobPath(blobPath));
173 nn->getAssetManager().remove(
"__blob");
174 REQUIRE_NOTHROW(nn->setBlobPath(diaPath2));
175 nn->getAssetManager().remove(
"__blob");
176 REQUIRE_NOTHROW(nn->setBlobPath(diaPath3));
177 nn->getAssetManager().remove(
"__blob");
178 REQUIRE_NOTHROW(nn->setBlobPath(diaPath4));
179 nn->getAssetManager().remove(
"__blob");
182 #if defined(__cpp_lib_filesystem)
183 std::filesystem::path stdPath(BLOB_PATH);
184 REQUIRE_NOTHROW(nn->setBlobPath(stdPath));
185 nn->getAssetManager().remove(
"__blob");
190 const char badfile[] =
PATH5;
191 const std::string strBadfile(&badfile[0]);
195 REQUIRE_NOTHROW(nn->setBlobPath(BLOB_PATH));
197 REQUIRE_THROWS_WITH(
dai::Device(pipeline, &badfile[0]), ContainsSubstring(
PATH5));
198 REQUIRE_THROWS_WITH(
dai::Device(pipeline, strBadfile), ContainsSubstring(
PATH5));
199 REQUIRE_THROWS_WITH(
dai::Device(pipeline, diaBadFile), ContainsSubstring(
PATH5));
201 #if defined(_WIN32) && defined(_MSC_VER)
202 const wchar_t wideBadfile[] = LPATH5;
203 const std::wstring wstrBadfile(LPATH5);
205 REQUIRE_THROWS_WITH(
dai::Device(pipeline, LPATH5), ContainsSubstring(
PATH5));
206 REQUIRE_THROWS_WITH(
dai::Device(pipeline, &wideBadfile[0]), ContainsSubstring(
PATH5));
207 REQUIRE_THROWS_WITH(
dai::Device(pipeline, wstrBadfile), ContainsSubstring(
PATH5));
208 REQUIRE_THROWS_WITH(
dai::Device(pipeline, diaFileFromWide), ContainsSubstring(
PATH5));
211 #if defined(__cpp_lib_filesystem)
212 std::filesystem::path stdBadfile(
PATH5);
213 REQUIRE_THROWS_WITH(
dai::Device(pipeline, stdBadfile), ContainsSubstring(
PATH5));
220 U8CHAR tmpFilename[L_tmpnam];
221 REQUIRE(std::tmpnam(
reinterpret_cast<char*
>(&tmpFilename[0])) !=
nullptr);
223 strFilename +=
PATH4;
226 CHECK_NOTHROW([&]() {
230 std::ifstream file(daiFilename, std::ios::binary);
231 if(!file.is_open() || !file.good() || file.bad()) {
232 throw std::runtime_error(
"calibration file not found or corrupted");
234 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
235 const char c1 = file.get();
242 const U8STRING strBadfile(&badfile[0]);
244 #if defined(_WIN32) && defined(_MSC_VER)
245 const wchar_t wideBadfile[] = LPATH5;
246 const std::wstring wstrBadfile(LPATH5);
254 REQUIRE_NOTHROW([&]() {
256 auto currentBlType = bl.
getType();
258 REQUIRE_NOTHROW([&]() {
260 auto currentBlType = bl.
getType();
265 auto currentBlType = bl.
getType();
267 ContainsSubstring(
"doesn't exist"));
271 auto currentBlType = bl.
getType();
273 ContainsSubstring(
"doesn't exist"));
277 auto currentBlType = bl.
getType();
279 ContainsSubstring(
"doesn't exist"));
280 #if defined(_WIN32) && defined(_MSC_VER)
284 auto currentBlType = bl.
getType();
286 ContainsSubstring(
"doesn't exist"));
290 auto currentBlType = bl.
getType();
292 ContainsSubstring(
"doesn't exist"));
296 auto currentBlType = bl.
getType();
298 ContainsSubstring(
"doesn't exist"));
300 #if defined(__cpp_lib_filesystem)
301 #if defined(__cpp_lib_char8_t)
302 const auto stdBadpath = std::filesystem::path(
PATH4);
304 const auto stdBadpath = std::filesystem::u8path(
PATH4);
309 auto currentBlType = bl.
getType();
311 ContainsSubstring(
"doesn't exist"));
314 std::cout <<
"No devices found" << std::endl;
319 U8CHAR tmp_name4[L_tmpnam];
320 REQUIRE(std::tmpnam(
reinterpret_cast<char*
>(&tmp_name4[0])) !=
nullptr);
324 REQUIRE_NOTHROW([&]() {
325 std::ofstream file(path4);
338 CHECK_NOTHROW(depth->loadMeshFiles(string4.c_str(), string4.c_str()));
339 depth->getAssetManager().remove(
"meshLeft");
340 depth->getAssetManager().remove(
"meshRight");
341 CHECK_NOTHROW(depth->loadMeshFiles(string4, string4));
342 depth->getAssetManager().remove(
"meshLeft");
343 depth->getAssetManager().remove(
"meshRight");
344 CHECK_NOTHROW(depth->loadMeshFiles(path4, path4));
345 depth->getAssetManager().remove(
"meshLeft");
346 depth->getAssetManager().remove(
"meshRight");
348 #if defined(_WIN32) && defined(_MSC_VER)
349 const std::wstring widePath4 = path4.
native();
355 CHECK_NOTHROW(depth->loadMeshFiles(widePath4.c_str(), widePath4.c_str()));
356 depth->getAssetManager().remove(
"meshLeft");
357 depth->getAssetManager().remove(
"meshRight");
358 CHECK_NOTHROW(depth->loadMeshFiles(widePath4, widePath4));
359 depth->getAssetManager().remove(
"meshLeft");
360 depth->getAssetManager().remove(
"meshRight");
363 #if defined(__cpp_lib_filesystem)
364 #if defined(__cpp_lib_char8_t)
365 const auto stdPath4 = std::filesystem::path(string4);
367 const auto stdPath4 = std::filesystem::u8path(string4);
370 CHECK_NOTHROW(depth->loadMeshFiles(stdPath4, stdPath4));
377 U8CHAR tmp_name4[L_tmpnam];
378 REQUIRE(std::tmpnam(
reinterpret_cast<char*
>(&tmp_name4[0])) !=
nullptr);
382 REQUIRE_NOTHROW([&]() {
383 std::ofstream file(path4);
390 CHECK_NOTHROW(script->setScriptPath(path4));
391 CHECK(script->getScriptPath().u8string() == string4);
393 script->getAssetManager().remove(
"__script");
395 CHECK_NOTHROW(script->setScript(
"<s>nothing</s>"));
396 CHECK(script->getScriptPath().empty());
397 CHECK(script->getScriptName() ==
"<script>");
398 script->getAssetManager().remove(
"__script");
400 CHECK_NOTHROW(script->setScript(
"<s>nothing</s>",
"myname"));
401 CHECK(script->getScriptPath().empty());
402 CHECK(script->getScriptName() ==
"myname");
403 script->getAssetManager().remove(
"__script");
405 #if defined(_WIN32) && defined(_MSC_VER)
406 const std::wstring widePath4 = path4.
native();
407 CHECK_NOTHROW(script->setScriptPath(widePath4));
408 CHECK(script->getScriptPath().native() == widePath4);
410 script->getAssetManager().remove(
"__script");
413 #if defined(__cpp_lib_filesystem)
414 #if defined(__cpp_lib_char8_t)
415 const auto stdPath4 = std::filesystem::path(string4);
417 const auto stdPath4 = std::filesystem::u8path(string4);
419 CHECK_NOTHROW(script->setScriptPath(stdPath4));
420 CHECK(script->getScriptPath().u8string() == stdPath4.u8string());
422 script->getAssetManager().remove(
"__script");