UnitTest: redesing to avoid #include ".cpp" in Xcode

Each unit test .cpp file is not a separate compilation unit in Xcode project
like it is in Visual Studio. Hopefully, Visual Studio test tool still likes
this arrangement.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2024-02-08 15:18:42 +01:00
parent 1e627e1c6b
commit 9acd185d44
14 changed files with 951 additions and 902 deletions

View File

@ -17,6 +17,17 @@
F421D4802B750EAE004ECBB0 /* trees.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D4792B750EAE004ECBB0 /* trees.c */; settings = {COMPILER_FLAGS = "-w"; }; }; F421D4802B750EAE004ECBB0 /* trees.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D4792B750EAE004ECBB0 /* trees.c */; settings = {COMPILER_FLAGS = "-w"; }; };
F421D4812B750EAE004ECBB0 /* inftrees.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D47A2B750EAE004ECBB0 /* inftrees.c */; settings = {COMPILER_FLAGS = "-w"; }; }; F421D4812B750EAE004ECBB0 /* inftrees.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D47A2B750EAE004ECBB0 /* inftrees.c */; settings = {COMPILER_FLAGS = "-w"; }; };
F421D4822B750EAE004ECBB0 /* uncompr.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D47B2B750EAE004ECBB0 /* uncompr.c */; settings = {COMPILER_FLAGS = "-w"; }; }; F421D4822B750EAE004ECBB0 /* uncompr.c in Sources */ = {isa = PBXBuildFile; fileRef = F421D47B2B750EAE004ECBB0 /* uncompr.c */; settings = {COMPILER_FLAGS = "-w"; }; };
F421D4832B7511FA004ECBB0 /* hash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F437AA902AC1BB64001E2230 /* hash.cpp */; };
F421D4842B7514B5004ECBB0 /* math.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F4E2AB059300044EDC0 /* math.cpp */; };
F421D4852B751551004ECBB0 /* parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F562AB08E690044EDC0 /* parser.cpp */; };
F421D4862B751707004ECBB0 /* pool.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4CCA3B72B73B940007B857B /* pool.cpp */; };
F421D4872B751739004ECBB0 /* ring.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F592AB08E690044EDC0 /* ring.cpp */; };
F421D4882B751774004ECBB0 /* sgml.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F582AB08E690044EDC0 /* sgml.cpp */; };
F421D4892B751777004ECBB0 /* stream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F5A2AB08E690044EDC0 /* stream.cpp */; };
F421D48A2B75177B004ECBB0 /* string.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4CCA3B82B73D2E2007B857B /* string.cpp */; };
F421D48B2B75177E004ECBB0 /* unicode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F572AB08E690044EDC0 /* unicode.cpp */; };
F421D48C2B751780004ECBB0 /* watchdog.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4CCA3B62B73B912007B857B /* watchdog.cpp */; };
F421D48D2B751783004ECBB0 /* zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F421D46B2B750BFD004ECBB0 /* zlib.cpp */; };
F4C07F522AB059580044EDC0 /* pch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F512AB059580044EDC0 /* pch.cpp */; }; F4C07F522AB059580044EDC0 /* pch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F512AB059580044EDC0 /* pch.cpp */; };
F4C07F552AB05B5B0044EDC0 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F542AB05B5B0044EDC0 /* main.cpp */; }; F4C07F552AB05B5B0044EDC0 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C07F542AB05B5B0044EDC0 /* main.cpp */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@ -231,17 +242,28 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
F421D4892B751777004ECBB0 /* stream.cpp in Sources */,
F421D48B2B75177E004ECBB0 /* unicode.cpp in Sources */,
F421D47F2B750EAE004ECBB0 /* zutil.c in Sources */, F421D47F2B750EAE004ECBB0 /* zutil.c in Sources */,
F4C07F552AB05B5B0044EDC0 /* main.cpp in Sources */, F4C07F552AB05B5B0044EDC0 /* main.cpp in Sources */,
F421D4882B751774004ECBB0 /* sgml.cpp in Sources */,
F4C07F522AB059580044EDC0 /* pch.cpp in Sources */, F4C07F522AB059580044EDC0 /* pch.cpp in Sources */,
F421D4862B751707004ECBB0 /* pool.cpp in Sources */,
F421D4802B750EAE004ECBB0 /* trees.c in Sources */, F421D4802B750EAE004ECBB0 /* trees.c in Sources */,
F421D48D2B751783004ECBB0 /* zlib.cpp in Sources */,
F421D47D2B750EAE004ECBB0 /* deflate.c in Sources */, F421D47D2B750EAE004ECBB0 /* deflate.c in Sources */,
F421D4702B750E0F004ECBB0 /* adler32.c in Sources */, F421D4702B750E0F004ECBB0 /* adler32.c in Sources */,
F421D4822B750EAE004ECBB0 /* uncompr.c in Sources */, F421D4822B750EAE004ECBB0 /* uncompr.c in Sources */,
F421D4742B750E21004ECBB0 /* crc32.c in Sources */, F421D4742B750E21004ECBB0 /* crc32.c in Sources */,
F421D47E2B750EAE004ECBB0 /* inffast.c in Sources */, F421D47E2B750EAE004ECBB0 /* inffast.c in Sources */,
F421D4722B750E18004ECBB0 /* compress.c in Sources */, F421D4722B750E18004ECBB0 /* compress.c in Sources */,
F421D4872B751739004ECBB0 /* ring.cpp in Sources */,
F421D4852B751551004ECBB0 /* parser.cpp in Sources */,
F421D4812B750EAE004ECBB0 /* inftrees.c in Sources */, F421D4812B750EAE004ECBB0 /* inftrees.c in Sources */,
F421D4842B7514B5004ECBB0 /* math.cpp in Sources */,
F421D4832B7511FA004ECBB0 /* hash.cpp in Sources */,
F421D48A2B75177B004ECBB0 /* string.cpp in Sources */,
F421D48C2B751780004ECBB0 /* watchdog.cpp in Sources */,
F421D47C2B750EAE004ECBB0 /* inflate.c in Sources */, F421D47C2B750EAE004ECBB0 /* inflate.c in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -34,10 +34,7 @@ namespace Microsoft {
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(hash) void hash::crc32()
{
public:
TEST_METHOD(crc32)
{ {
stdex::crc32_hash h; stdex::crc32_hash h;
static const char data[] = "This is a test."; static const char data[] = "This is a test.";
@ -46,7 +43,7 @@ namespace UnitTests
Assert::AreEqual<stdex::crc32_t>(0xc6c3c95d, h); Assert::AreEqual<stdex::crc32_t>(0xc6c3c95d, h);
} }
TEST_METHOD(md5) void hash::md5()
{ {
stdex::md5_hash h; stdex::md5_hash h;
static const char data[] = "This is a test."; static const char data[] = "This is a test.";
@ -55,7 +52,7 @@ namespace UnitTests
Assert::AreEqual<stdex::md5_t>({0x12,0x0e,0xa8,0xa2,0x5e,0x5d,0x48,0x7b,0xf6,0x8b,0x5f,0x70,0x96,0x44,0x00,0x19}, h); Assert::AreEqual<stdex::md5_t>({0x12,0x0e,0xa8,0xa2,0x5e,0x5d,0x48,0x7b,0xf6,0x8b,0x5f,0x70,0x96,0x44,0x00,0x19}, h);
} }
TEST_METHOD(sha1) void hash::sha1()
{ {
stdex::sha1_hash h; stdex::sha1_hash h;
static const char data[] = "This is a test."; static const char data[] = "This is a test.";
@ -63,5 +60,4 @@ namespace UnitTests
h.finalize(); h.finalize();
Assert::AreEqual<stdex::sha1_t>({0xaf,0xa6,0xc8,0xb3,0xa2,0xfa,0xe9,0x57,0x85,0xdc,0x7d,0x96,0x85,0xa5,0x78,0x35,0xd7,0x03,0xac,0x88}, h); Assert::AreEqual<stdex::sha1_t>({0xaf,0xa6,0xc8,0xb3,0xa2,0xfa,0xe9,0x57,0x85,0xdc,0x7d,0x96,0x85,0xa5,0x78,0x35,0xd7,0x03,0xac,0x88}, h);
} }
};
} }

View File

@ -4,17 +4,6 @@
*/ */
#include "pch.hpp" #include "pch.hpp"
#include "hash.cpp"
#include "math.cpp"
#include "pool.cpp"
#include "parser.cpp"
#include "ring.cpp"
#include "sgml.cpp"
#include "stream.cpp"
#include "string.cpp"
#include "unicode.cpp"
#include "watchdog.cpp"
#include "zlib.cpp"
#include <iostream> #include <iostream>
int main(int argc, const char * argv[]) int main(int argc, const char * argv[])

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -12,10 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(math) void math::mul()
{
public:
TEST_METHOD(mul)
{ {
Assert::AreEqual<size_t>(10, stdex::mul(2, 5)); Assert::AreEqual<size_t>(10, stdex::mul(2, 5));
Assert::AreEqual<size_t>(10, stdex::mul(5, 2)); Assert::AreEqual<size_t>(10, stdex::mul(5, 2));
@ -29,7 +26,7 @@ namespace UnitTests
Assert::ExpectException<std::invalid_argument>([] { stdex::mul(2, SIZE_MAX); }); Assert::ExpectException<std::invalid_argument>([] { stdex::mul(2, SIZE_MAX); });
} }
TEST_METHOD(add) void math::add()
{ {
Assert::AreEqual<size_t>(7, stdex::add(2, 5)); Assert::AreEqual<size_t>(7, stdex::add(2, 5));
Assert::AreEqual<size_t>(7, stdex::add(5, 2)); Assert::AreEqual<size_t>(7, stdex::add(5, 2));
@ -40,5 +37,4 @@ namespace UnitTests
Assert::ExpectException<std::invalid_argument>([] { stdex::add(SIZE_MAX, 1); }); Assert::ExpectException<std::invalid_argument>([] { stdex::add(SIZE_MAX, 1); });
Assert::ExpectException<std::invalid_argument>([] { stdex::add(1, SIZE_MAX); }); Assert::ExpectException<std::invalid_argument>([] { stdex::add(1, SIZE_MAX); });
} }
};
} }

View File

@ -6,15 +6,17 @@
#include "pch.hpp" #include "pch.hpp"
using namespace std; using namespace std;
using namespace stdex;
using namespace parser;
#ifdef _WIN32 #ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Microsoft { namespace Microsoft {
namespace VisualStudio { namespace VisualStudio {
namespace CppUnitTestFramework { namespace CppUnitTestFramework {
static wstring ToString(const stdex::interval<size_t>& q) static wstring ToString(const interval<size_t>& q)
{ {
return stdex::sprintf(L"<%zu, %zu>", nullptr, q.start, q.end); return sprintf(L"<%zu, %zu>", nullptr, q.start, q.end);
} }
} }
} }
@ -23,30 +25,27 @@ namespace Microsoft {
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(parser) void parser::wtest()
{
public:
TEST_METHOD(wtest)
{ {
static const wchar_t text[] = L"This is a test.\nSecond line."; static const wchar_t text[] = L"This is a test.\nSecond line.";
{ {
stdex::parser::wnoop p; wnoop p;
Assert::IsTrue(p.match(text)); Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end); Assert::AreEqual((size_t)0, p.interval.end);
} }
{ {
stdex::parser::wcu p(L't'); wcu p(L't');
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)1, p.interval.end); Assert::AreEqual((size_t)1, p.interval.end);
} }
{ {
stdex::parser::wspace_cu p; wspace_cu p;
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 4)); Assert::IsTrue(p.match(text, 4));
Assert::AreEqual((size_t)4, p.interval.start); Assert::AreEqual((size_t)4, p.interval.start);
@ -54,7 +53,7 @@ namespace UnitTests
} }
{ {
stdex::parser::wpunct_cu p; wpunct_cu p;
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 14)); Assert::IsTrue(p.match(text, 14));
Assert::AreEqual((size_t)14, p.interval.start); Assert::AreEqual((size_t)14, p.interval.start);
@ -62,7 +61,7 @@ namespace UnitTests
} }
{ {
stdex::parser::wspace_or_punct_cu p; wspace_or_punct_cu p;
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 4)); Assert::IsTrue(p.match(text, 4));
Assert::AreEqual((size_t)4, p.interval.start); Assert::AreEqual((size_t)4, p.interval.start);
@ -73,7 +72,7 @@ namespace UnitTests
} }
{ {
stdex::parser::wbol p; wbol p;
Assert::IsTrue(p.match(text)); Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end); Assert::AreEqual((size_t)0, p.interval.end);
@ -85,7 +84,7 @@ namespace UnitTests
} }
{ {
stdex::parser::weol p; weol p;
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsFalse(p.match(text, 1)); Assert::IsFalse(p.match(text, 1));
Assert::IsTrue(p.match(text, 15)); Assert::IsTrue(p.match(text, 15));
@ -95,14 +94,14 @@ namespace UnitTests
} }
{ {
stdex::parser::wcu_set p(L"abcD"); wcu_set p(L"abcD");
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 8)); Assert::IsTrue(p.match(text, 8));
Assert::AreEqual((size_t)8, p.interval.start); Assert::AreEqual((size_t)8, p.interval.start);
Assert::AreEqual((size_t)9, p.interval.end); Assert::AreEqual((size_t)9, p.interval.end);
Assert::AreEqual((size_t)0, p.hit_offset); Assert::AreEqual((size_t)0, p.hit_offset);
Assert::IsFalse(p.match(text, 21)); Assert::IsFalse(p.match(text, 21));
Assert::IsTrue(p.match(text, 21, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 21, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)21, p.interval.start); Assert::AreEqual((size_t)21, p.interval.start);
Assert::AreEqual((size_t)22, p.interval.end); Assert::AreEqual((size_t)22, p.interval.end);
Assert::AreEqual((size_t)3, p.hit_offset); Assert::AreEqual((size_t)3, p.hit_offset);
@ -111,83 +110,83 @@ namespace UnitTests
{ {
stdex::parser::wstring p(L"this"); stdex::parser::wstring p(L"this");
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, sizeof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, sizeof(text), match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end); Assert::AreEqual((size_t)4, p.interval.end);
} }
{ {
stdex::parser::wany_cu chr; wany_cu chr;
stdex::parser::witerations p(stdex::make_shared_no_delete(&chr), 1, 5); witerations p(make_shared_no_delete(&chr), 1, 5);
Assert::IsTrue(p.match(text)); Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)5, p.interval.end); Assert::AreEqual((size_t)5, p.interval.end);
} }
{ {
stdex::parser::wspace_cu nospace(true); wspace_cu nospace(true);
stdex::parser::witerations p(stdex::make_shared_no_delete(&nospace), 1); witerations p(make_shared_no_delete(&nospace), 1);
Assert::IsTrue(p.match(text)); Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end); Assert::AreEqual((size_t)4, p.interval.end);
} }
{ {
stdex::parser::wcu chr_t(L't'), chr_h(L'h'), chr_i(L'i'), chr_s(L's'); wcu chr_t(L't'), chr_h(L'h'), chr_i(L'i'), chr_s(L's');
stdex::parser::wspace_cu space; wspace_cu space;
stdex::parser::wsequence p({ wsequence p({
stdex::make_shared_no_delete(&chr_t), make_shared_no_delete(&chr_t),
stdex::make_shared_no_delete(&chr_h), make_shared_no_delete(&chr_h),
stdex::make_shared_no_delete(&chr_i), make_shared_no_delete(&chr_i),
stdex::make_shared_no_delete(&chr_s), make_shared_no_delete(&chr_s),
stdex::make_shared_no_delete(&space) }); make_shared_no_delete(&space) });
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)5, p.interval.end); Assert::AreEqual((size_t)5, p.interval.end);
} }
{ {
stdex::parser::wstring apple(L"apple"), orange(L"orange"), _this(L"this"); stdex::parser::wstring apple(L"apple"), orange(L"orange"), _this(L"this");
stdex::parser::wspace_cu space; wspace_cu space;
stdex::parser::wbranch p({ wbranch p({
stdex::make_shared_no_delete(&apple), make_shared_no_delete(&apple),
stdex::make_shared_no_delete(&orange), make_shared_no_delete(&orange),
stdex::make_shared_no_delete(&_this), make_shared_no_delete(&_this),
stdex::make_shared_no_delete(&space) }); make_shared_no_delete(&space) });
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset); Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end); Assert::AreEqual((size_t)4, p.interval.end);
} }
{ {
stdex::parser::wstring_branch p(L"apple", L"orange", L"this", nullptr); wstring_branch p(L"apple", L"orange", L"this", nullptr);
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset); Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end); Assert::AreEqual((size_t)4, p.interval.end);
} }
{ {
stdex::parser::wcu chr_s(L's'), chr_h(L'h'), chr_i(L'i'), chr_t(L't'); wcu chr_s(L's'), chr_h(L'h'), chr_i(L'i'), chr_t(L't');
stdex::parser::wpermutation p({ wpermutation p({
stdex::make_shared_no_delete(&chr_s), make_shared_no_delete(&chr_s),
stdex::make_shared_no_delete(&chr_h), make_shared_no_delete(&chr_h),
stdex::make_shared_no_delete(&chr_i), make_shared_no_delete(&chr_i),
stdex::make_shared_no_delete(&chr_t) }); make_shared_no_delete(&chr_t) });
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end); Assert::AreEqual((size_t)4, p.interval.end);
} }
{ {
std::locale locale_slSI("sl_SI"); std::locale locale_slSI("sl_SI");
stdex::parser::wspace_cu space(false, locale_slSI); wspace_cu space(false, locale_slSI);
stdex::parser::wiban p(stdex::make_shared_no_delete(&space), locale_slSI); wiban p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"SI56023120015226972", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"SI56023120015226972", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"SI", p.country); Assert::AreEqual(L"SI", p.country);
@ -200,10 +199,10 @@ namespace UnitTests
Assert::AreEqual(L"023120015226972", p.bban); Assert::AreEqual(L"023120015226972", p.bban);
Assert::IsFalse(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX)); Assert::IsFalse(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match(L"SI56 0231 2001 5226 9720", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"SI56 0231 2001 5226 9720", 0, SIZE_MAX));
Assert::AreEqual(stdex::interval<size_t>(0, 23), p.interval); Assert::AreEqual(interval<size_t>(0, 23), p.interval);
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match(L"...SI56 0231 2001 5226 972...", 3, SIZE_MAX)); Assert::IsTrue(p.match(L"...SI56 0231 2001 5226 972...", 3, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
@ -248,8 +247,8 @@ namespace UnitTests
{ {
std::locale locale_slSI("sl_SI"); std::locale locale_slSI("sl_SI");
stdex::parser::wspace_cu space(false, locale_slSI); wspace_cu space(false, locale_slSI);
stdex::parser::wcreditor_reference p(stdex::make_shared_no_delete(&space), locale_slSI); wcreditor_reference p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"RF18539007547034", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"RF18539007547034", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"18", p.check_digits); Assert::AreEqual(L"18", p.check_digits);
@ -260,7 +259,7 @@ namespace UnitTests
Assert::AreEqual(L"000000000539007547034", p.reference); Assert::AreEqual(L"000000000539007547034", p.reference);
Assert::IsFalse(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX)); Assert::IsFalse(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match(L"RF18 5390 0754 70340", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"RF18 5390 0754 70340", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
@ -272,53 +271,53 @@ namespace UnitTests
{ {
std::locale locale_slSI("sl_SI"); std::locale locale_slSI("sl_SI");
stdex::parser::wspace_cu space(false, locale_slSI); wspace_cu space(false, locale_slSI);
stdex::parser::wsi_reference p(stdex::make_shared_no_delete(&space), locale_slSI); wsi_reference p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"SI121234567890120", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"SI121234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"12", p.model); Assert::AreEqual(L"12", p.model);
Assert::AreEqual(stdex::interval<size_t>(4, 17), p.part1.interval); Assert::AreEqual(interval<size_t>(4, 17), p.part1.interval);
Assert::IsTrue(p.match(L"SI12 1234567890120", 0, SIZE_MAX)); Assert::IsTrue(p.match(L"SI12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"12", p.model); Assert::AreEqual(L"12", p.model);
Assert::AreEqual(stdex::interval<size_t>(5, 18), p.part1.interval); Assert::AreEqual(interval<size_t>(5, 18), p.part1.interval);
Assert::IsFalse(p.match(L"si12 1234567890120", 0, SIZE_MAX)); Assert::IsFalse(p.match(L"si12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.match(L"si12 1234567890120", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(L"si12 1234567890120", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match(L"...SI12 1234567890120...", 3, SIZE_MAX)); Assert::IsTrue(p.match(L"...SI12 1234567890120...", 3, SIZE_MAX));
Assert::IsTrue(p.match(L"SI12 1234567890120", 0, SIZE_MAX)); // no-break space Assert::IsTrue(p.match(L"SI12 1234567890120", 0, SIZE_MAX)); // no-break space
} }
} }
TEST_METHOD(sgml_test) void parser::sgml_test()
{ {
std::locale locale_slSI("sl_SI"); std::locale locale_slSI("sl_SI");
static const char text[] = "V ko&zcaron;u&scaron;&ccaron;ku zlobnega mizarja stopiclja fant\nin kli&ccaron;e&nbsp;1234567890."; static const char text[] = "V ko&zcaron;u&scaron;&ccaron;ku zlobnega mizarja stopiclja fant\nin kli&ccaron;e&nbsp;1234567890.";
{ {
stdex::parser::sgml_noop p; sgml_noop p;
Assert::IsTrue(p.match(text)); Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end); Assert::AreEqual((size_t)0, p.interval.end);
} }
{ {
stdex::parser::sgml_cp p("v"); sgml_cp p("v");
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)1, p.interval.end); Assert::AreEqual((size_t)1, p.interval.end);
} }
{ {
stdex::parser::sgml_cp p("&Zcaron;", SIZE_MAX, false, locale_slSI); sgml_cp p("&Zcaron;", SIZE_MAX, false, locale_slSI);
Assert::IsFalse(p.match(text, 4)); Assert::IsFalse(p.match(text, 4));
Assert::IsTrue(p.match(text, 4, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 4, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)4, p.interval.start); Assert::AreEqual((size_t)4, p.interval.start);
Assert::AreEqual((size_t)12, p.interval.end); Assert::AreEqual((size_t)12, p.interval.end);
} }
{ {
stdex::parser::sgml_space_cp p(false, locale_slSI); sgml_space_cp p(false, locale_slSI);
Assert::IsFalse(p.match(text)); Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 1)); Assert::IsTrue(p.match(text, 1));
Assert::AreEqual((size_t)1, p.interval.start); Assert::AreEqual((size_t)1, p.interval.start);
@ -329,17 +328,17 @@ namespace UnitTests
} }
{ {
stdex::parser::sgml_string_branch p(locale_slSI, "apple", "orange", "Ko&Zcaron;u&Scaron;&ccaron;Ku", nullptr); sgml_string_branch p(locale_slSI, "apple", "orange", "Ko&Zcaron;u&Scaron;&ccaron;Ku", nullptr);
Assert::IsFalse(p.match(text, 2)); Assert::IsFalse(p.match(text, 2));
Assert::IsTrue(p.match(text, 2, _countof(text), stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match(text, 2, _countof(text), match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset); Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)2, p.interval.start); Assert::AreEqual((size_t)2, p.interval.start);
Assert::AreEqual((size_t)31, p.interval.end); Assert::AreEqual((size_t)31, p.interval.end);
} }
{ {
stdex::parser::sgml_space_cp space(false, locale_slSI); sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_iban p(stdex::make_shared_no_delete(&space), locale_slSI); sgml_iban p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("SI56023120015226972", 0, SIZE_MAX)); Assert::IsTrue(p.match("SI56023120015226972", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual("SI", p.country); Assert::AreEqual("SI", p.country);
@ -352,10 +351,10 @@ namespace UnitTests
Assert::AreEqual("023120015226972", p.bban); Assert::AreEqual("023120015226972", p.bban);
Assert::IsFalse(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX)); Assert::IsFalse(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match("SI56 0231 2001 5226 9720", 0, SIZE_MAX)); Assert::IsTrue(p.match("SI56 0231 2001 5226 9720", 0, SIZE_MAX));
Assert::AreEqual(stdex::interval<size_t>(0, 23), p.interval); Assert::AreEqual(interval<size_t>(0, 23), p.interval);
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match("...SI56 0231 2001 5226 972...", 3, SIZE_MAX)); Assert::IsTrue(p.match("...SI56 0231 2001 5226 972...", 3, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
@ -364,8 +363,8 @@ namespace UnitTests
} }
{ {
stdex::parser::sgml_space_cp space(false, locale_slSI); sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_creditor_reference p(stdex::make_shared_no_delete(&space), locale_slSI); sgml_creditor_reference p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("RF18539007547034", 0, SIZE_MAX)); Assert::IsTrue(p.match("RF18539007547034", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual("18", p.check_digits); Assert::AreEqual("18", p.check_digits);
@ -376,7 +375,7 @@ namespace UnitTests
Assert::AreEqual("000000000539007547034", p.reference); Assert::AreEqual("000000000539007547034", p.reference);
Assert::IsFalse(p.match("rf18 5390 0754 7034", 0, SIZE_MAX)); Assert::IsFalse(p.match("rf18 5390 0754 7034", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match("rf18 5390 0754 7034", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match("rf18 5390 0754 7034", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match("RF18 5390 0754 70340", 0, SIZE_MAX)); Assert::IsTrue(p.match("RF18 5390 0754 70340", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid); Assert::IsFalse(p.is_valid);
@ -387,24 +386,24 @@ namespace UnitTests
} }
{ {
stdex::parser::sgml_space_cp space(false, locale_slSI); sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_si_reference p(stdex::make_shared_no_delete(&space), locale_slSI); sgml_si_reference p(make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("SI121234567890120", 0, SIZE_MAX)); Assert::IsTrue(p.match("SI121234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual("12", p.model); Assert::AreEqual("12", p.model);
Assert::AreEqual(stdex::interval<size_t>(4, 17), p.part1.interval); Assert::AreEqual(interval<size_t>(4, 17), p.part1.interval);
Assert::IsTrue(p.match("SI12 1234567890120", 0, SIZE_MAX)); Assert::IsTrue(p.match("SI12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid); Assert::IsTrue(p.is_valid);
Assert::AreEqual("12", p.model); Assert::AreEqual("12", p.model);
Assert::AreEqual(stdex::interval<size_t>(5, 18), p.part1.interval); Assert::AreEqual(interval<size_t>(5, 18), p.part1.interval);
Assert::IsFalse(p.match("si12 1234567890120", 0, SIZE_MAX)); Assert::IsFalse(p.match("si12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.match("si12 1234567890120", 0, SIZE_MAX, stdex::parser::match_case_insensitive)); Assert::IsTrue(p.match("si12 1234567890120", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match("...SI12 1234567890120...", 3, SIZE_MAX)); Assert::IsTrue(p.match("...SI12 1234567890120...", 3, SIZE_MAX));
Assert::IsTrue(p.match("SI12&nbsp;1234567890120", 0, SIZE_MAX)); Assert::IsTrue(p.match("SI12&nbsp;1234567890120", 0, SIZE_MAX));
} }
} }
TEST_METHOD(http_test) void parser::http_test()
{ {
static const std::locale locale("en_US.UTF-8"); static const std::locale locale("en_US.UTF-8");
static const char request[] = static const char request[] =
@ -427,7 +426,7 @@ namespace UnitTests
"\r\n"; "\r\n";
{ {
stdex::parser::http_request p(locale); http_request p(locale);
Assert::IsTrue(p.match(request)); Assert::IsTrue(p.match(request));
Assert::AreEqual((size_t)0, p.interval.start); Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)14, p.interval.end); Assert::AreEqual((size_t)14, p.interval.end);
@ -441,10 +440,10 @@ namespace UnitTests
} }
{ {
list<stdex::parser::http_header> hdrs; list<http_header> hdrs;
size_t offset = 14; size_t offset = 14;
for (;;) { for (;;) {
stdex::parser::http_header h; http_header h;
if (h.match(request, offset)) { if (h.match(request, offset)) {
offset = h.interval.end; offset = h.interval.end;
hdrs.push_back(std::move(h)); hdrs.push_back(std::move(h));
@ -453,19 +452,19 @@ namespace UnitTests
break; break;
} }
Assert::AreEqual((size_t)15, hdrs.size()); Assert::AreEqual((size_t)15, hdrs.size());
stdex::parser::http_weighted_collection<stdex::parser::http_weighted_value<stdex::parser::http_language>> langs; http_weighted_collection<http_weighted_value<http_language>> langs;
for (const auto& h : hdrs) for (const auto& h : hdrs)
if (stdex::strnicmp(request + h.name.start, h.name.size(), "Accept-Language", SIZE_MAX, locale) == 0) if (strnicmp(request + h.name.start, h.name.size(), "Accept-Language", SIZE_MAX, locale) == 0)
langs.insert(request, h.value.start, h.value.end); langs.insert(request, h.value.start, h.value.end);
Assert::IsTrue(!langs.empty()); Assert::IsTrue(!langs.empty());
{ {
const vector<string> control = { const vector<std::string> control = {
"sl", "en-US", "en", "de-DE", "de" "sl", "en-US", "en", "de-DE", "de"
}; };
auto c = control.cbegin(); auto c = control.cbegin();
auto l = langs.cbegin(); auto l = langs.cbegin();
for (; c != control.cend() && l != langs.cend(); ++c, ++l) for (; c != control.cend() && l != langs.cend(); ++c, ++l)
Assert::IsTrue(stdex::strnicmp(request + l->value.interval.start, l->value.interval.size(), c->c_str(), c->size(), locale) == 0); Assert::IsTrue(strnicmp(request + l->value.interval.start, l->value.interval.size(), c->c_str(), c->size(), locale) == 0);
Assert::IsTrue(c == control.cend()); Assert::IsTrue(c == control.cend());
Assert::IsTrue(l == langs.cend()); Assert::IsTrue(l == langs.cend());
} }
@ -495,5 +494,4 @@ namespace UnitTests
// "X-Firefox-Spdy: h2\r\n" // "X-Firefox-Spdy: h2\r\n"
// "\r\n"; // "\r\n";
} }
};
} }

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -39,3 +39,84 @@
#include <filesystem> #include <filesystem>
#include <list> #include <list>
#include <thread> #include <thread>
namespace UnitTests
{
TEST_CLASS(hash)
{
public:
TEST_METHOD(crc32);
TEST_METHOD(md5);
TEST_METHOD(sha1);
};
TEST_CLASS(math)
{
public:
TEST_METHOD(mul);
TEST_METHOD(add);
};
TEST_CLASS(parser)
{
public:
TEST_METHOD(wtest);
TEST_METHOD(sgml_test);
TEST_METHOD(http_test);
};
TEST_CLASS(pool)
{
public:
TEST_METHOD(test);
};
TEST_CLASS(ring)
{
public:
TEST_METHOD(test);
};
TEST_CLASS(sgml)
{
public:
TEST_METHOD(sgml2str);
TEST_METHOD(str2sgml);
};
TEST_CLASS(stream)
{
public:
TEST_METHOD(async);
TEST_METHOD(replicator);
TEST_METHOD(open_close);
TEST_METHOD(file_stat);
};
TEST_CLASS(string)
{
public:
TEST_METHOD(sprintf);
};
TEST_CLASS(unicode)
{
public:
TEST_METHOD(str2wstr);
TEST_METHOD(wstr2str);
TEST_METHOD(charset_encoder);
TEST_METHOD(normalize);
};
TEST_CLASS(watchdog)
{
public:
TEST_METHOD(test);
};
TEST_CLASS(zlib)
{
public:
TEST_METHOD(test);
};
}

View File

@ -12,12 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
constexpr size_t pool_capacity = 50; void pool::test()
TEST_CLASS(pool)
{
public:
TEST_METHOD(test)
{ {
using worker_t = unique_ptr<int>; using worker_t = unique_ptr<int>;
using pool_t = stdex::pool<worker_t>; using pool_t = stdex::pool<worker_t>;
@ -38,5 +33,4 @@ namespace UnitTests
for (auto& w : workers) for (auto& w : workers)
w.join(); w.join();
} }
};
} }

View File

@ -14,10 +14,7 @@ namespace UnitTests
{ {
constexpr size_t ring_capacity = 50; constexpr size_t ring_capacity = 50;
TEST_CLASS(ring) void ring::test()
{
public:
TEST_METHOD(test)
{ {
using ring_t = stdex::ring<int, ring_capacity>; using ring_t = stdex::ring<int, ring_capacity>;
ring_t ring; ring_t ring;
@ -53,5 +50,4 @@ namespace UnitTests
} }
writer.join(); writer.join();
} }
};
} }

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -12,10 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(sgml) void sgml::sgml2str()
{
public:
TEST_METHOD(sgml2str)
{ {
Assert::AreEqual(L"This is a test.", stdex::sgml2str("This is a test.", SIZE_MAX).c_str()); Assert::AreEqual(L"This is a test.", stdex::sgml2str("This is a test.", SIZE_MAX).c_str());
Assert::AreEqual(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", stdex::sgml2str("Th&iacute;&scaron; i&sdot; &#97; te&smacr;t.&unknown;&#x1F600;&#X1f605;", SIZE_MAX).c_str()); Assert::AreEqual(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", stdex::sgml2str("Th&iacute;&scaron; i&sdot; &#97; te&smacr;t.&unknown;&#x1F600;&#X1f605;", SIZE_MAX).c_str());
@ -52,7 +49,7 @@ namespace UnitTests
} == map); } == map);
} }
TEST_METHOD(str2sgml) void sgml::str2sgml()
{ {
Assert::AreEqual("This is a test.", stdex::str2sgml(L"This is a test.", SIZE_MAX).c_str()); Assert::AreEqual("This is a test.", stdex::str2sgml(L"This is a test.", SIZE_MAX).c_str());
Assert::AreEqual("Th&iacute;&scaron; i&sdot; a te&smacr;t.&amp;unknown;&#x1f600;&#x1f605;", stdex::str2sgml(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", SIZE_MAX).c_str()); Assert::AreEqual("Th&iacute;&scaron; i&sdot; a te&smacr;t.&amp;unknown;&#x1f600;&#x1f605;", stdex::str2sgml(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", SIZE_MAX).c_str());
@ -63,5 +60,4 @@ namespace UnitTests
Assert::AreEqual("$\"<>&amp;", stdex::str2sgml(L"$\"<>&", SIZE_MAX).c_str()); Assert::AreEqual("$\"<>&amp;", stdex::str2sgml(L"$\"<>&", SIZE_MAX).c_str());
Assert::AreEqual("$&quot;<>&amp;", stdex::str2sgml(L"$\"<>&", SIZE_MAX, stdex::sgml_c).c_str()); Assert::AreEqual("$&quot;<>&amp;", stdex::str2sgml(L"$\"<>&", SIZE_MAX, stdex::sgml_c).c_str());
} }
};
} }

View File

@ -6,21 +6,31 @@
#include "pch.hpp" #include "pch.hpp"
using namespace std; using namespace std;
using namespace stdex;
using namespace stdex::stream;
#ifdef _WIN32 #ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#endif #endif
static stdex::sstring temp_path()
{
#ifdef _WIN32
TCHAR temp_path[MAX_PATH];
Assert::IsTrue(ExpandEnvironmentStrings(_T("%TEMP%\\"), temp_path, _countof(temp_path)) < MAX_PATH);
return temp_path;
#else
return "/tmp/";
#endif
}
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(stream) void stream::async()
{
public:
TEST_METHOD(async)
{ {
constexpr uint32_t total = 1000; constexpr uint32_t total = 1000;
stdex::stream::memory_file source(stdex::mul(total, sizeof(uint32_t))); memory_file source(stdex::mul(total, sizeof(uint32_t)));
{ {
stdex::stream::async_writer<70> writer(source); async_writer<70> writer(source);
for (uint32_t i = 0; i < total; ++i) { for (uint32_t i = 0; i < total; ++i) {
Assert::IsTrue(writer.ok()); Assert::IsTrue(writer.ok());
writer << i; writer << i;
@ -28,7 +38,7 @@ namespace UnitTests
} }
Assert::AreEqual<stdex::stream::fpos_t>(0, source.seekbeg(0)); Assert::AreEqual<stdex::stream::fpos_t>(0, source.seekbeg(0));
{ {
stdex::stream::async_reader<50> reader(source); async_reader<50> reader(source);
uint32_t x; uint32_t x;
for (uint32_t i = 0; i < total; ++i) { for (uint32_t i = 0; i < total; ++i) {
reader >> x; reader >> x;
@ -40,28 +50,28 @@ namespace UnitTests
} }
} }
TEST_METHOD(replicator) void stream::replicator()
{ {
constexpr uint32_t total = 1000; constexpr uint32_t total = 1000;
stdex::stream::memory_file f1(stdex::mul(total, sizeof(uint32_t))); memory_file f1(stdex::mul(total, sizeof(uint32_t)));
stdex::sstring filename2, filename3; stdex::sstring filename2, filename3;
filename2 = filename3 = temp_path(); filename2 = filename3 = temp_path();
filename2 += _T("stdex-stream-replicator-2.tmp"); filename2 += _T("stdex-stream-replicator-2.tmp");
stdex::stream::file f2( file f2(
filename2.c_str(), filename2.c_str(),
stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary); mode_for_reading | mode_for_writing | mode_create | mode_binary);
filename3 += _T("stdex-stream-replicator-3.tmp"); filename3 += _T("stdex-stream-replicator-3.tmp");
stdex::stream::cached_file f3( cached_file f3(
filename3.c_str(), filename3.c_str(),
stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary, mode_for_reading | mode_for_writing | mode_create | mode_binary,
128); 128);
{ {
stdex::stream::replicator writer; stdex::stream::replicator writer;
stdex::stream::buffer f2_buf(f2, 0, 32); buffer f2_buf(f2, 0, 32);
writer.push_back(&f1); writer.push_back(&f1);
writer.push_back(&f2_buf); writer.push_back(&f2_buf);
writer.push_back(&f3); writer.push_back(&f3);
@ -75,7 +85,7 @@ namespace UnitTests
f2.seekbeg(0); f2.seekbeg(0);
f3.seekbeg(0); f3.seekbeg(0);
{ {
stdex::stream::buffer f2_buf(f2, 64, 0); buffer f2_buf(f2, 64, 0);
uint32_t x; uint32_t x;
for (uint32_t i = 0; i < total; ++i) { for (uint32_t i = 0; i < total; ++i) {
f1 >> x; f1 >> x;
@ -102,19 +112,19 @@ namespace UnitTests
std::filesystem::remove(filename3); std::filesystem::remove(filename3);
} }
TEST_METHOD(open_close) void stream::open_close()
{ {
stdex::stream::cached_file dat(stdex::invalid_handle, stdex::stream::state_t::fail, 4096); cached_file dat(stdex::invalid_handle, state_t::fail, 4096);
const stdex::sstring filepath = temp_path(); const stdex::sstring filepath = temp_path();
constexpr uint32_t count = 3; constexpr uint32_t count = 3;
stdex::sstring filename[count]; stdex::sstring filename[count];
stdex::stream::fpos_t start[count]; stdex::stream::fpos_t start[count];
for (uint32_t i = 0; i < count; ++i) { for (uint32_t i = 0; i < count; ++i) {
filename[i] = filepath + stdex::sprintf(_T("stdex-stream-open_close%u.tmp"), NULL, i); filename[i] = filepath + stdex::sprintf(_T("stdex-stream-open_close%u.tmp"), NULL, i);
dat.open(filename[i].c_str(), stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::share_none | stdex::stream::mode_preserve_existing | stdex::stream::mode_binary); dat.open(filename[i].c_str(), mode_for_reading | mode_for_writing | share_none | mode_preserve_existing | mode_binary);
Assert::IsTrue(dat.ok()); Assert::IsTrue(dat.ok());
start[i] = dat.tell(); start[i] = dat.tell();
Assert::AreNotEqual(stdex::stream::fpos_max, start[i]); Assert::AreNotEqual(fpos_max, start[i]);
for (uint32_t j = 0; j < 31 + 11 * i; ++j) { for (uint32_t j = 0; j < 31 + 11 * i; ++j) {
dat << j * count + i; dat << j * count + i;
Assert::IsTrue(dat.ok()); Assert::IsTrue(dat.ok());
@ -122,7 +132,7 @@ namespace UnitTests
dat.close(); dat.close();
} }
for (uint32_t i = 0; i < count; ++i) { for (uint32_t i = 0; i < count; ++i) {
dat.open(filename[i].c_str(), stdex::stream::mode_for_reading | stdex::stream::mode_open_existing | stdex::stream::share_none | stdex::stream::mode_binary); dat.open(filename[i].c_str(), mode_for_reading | mode_open_existing | share_none | mode_binary);
Assert::IsTrue(dat.ok()); Assert::IsTrue(dat.ok());
for (;;) { for (;;) {
uint32_t x; uint32_t x;
@ -137,23 +147,10 @@ namespace UnitTests
std::filesystem::remove(filename[i]); std::filesystem::remove(filename[i]);
} }
TEST_METHOD(file_stat) void stream::file_stat()
{ {
stdex::sstring path(temp_path()); stdex::sstring path(temp_path());
Assert::IsTrue(stdex::stream::file::exists(path)); Assert::IsTrue(file::exists(path));
Assert::IsFalse(stdex::stream::file::readonly(path)); Assert::IsFalse(file::readonly(path));
} }
protected:
static stdex::sstring temp_path()
{
#ifdef _WIN32
TCHAR temp_path[MAX_PATH];
Assert::IsTrue(ExpandEnvironmentStrings(_T("%TEMP%\\"), temp_path, _countof(temp_path)) < MAX_PATH);
return temp_path;
#else
return "/tmp/";
#endif
}
};
} }

View File

@ -12,10 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(string) void string::sprintf()
{
public:
TEST_METHOD(sprintf)
{ {
stdex::locale locale(stdex::create_locale(LC_ALL, "en_US.UTF-8")); stdex::locale locale(stdex::create_locale(LC_ALL, "en_US.UTF-8"));
@ -38,5 +35,4 @@ namespace UnitTests
Assert::AreEqual(str.c_str(), stdex::sprintf("%s", locale, str.data()).c_str()); Assert::AreEqual(str.c_str(), stdex::sprintf("%s", locale, str.data()).c_str());
Assert::AreEqual(str.size(), stdex::sprintf("%s", locale, str.data()).size()); Assert::AreEqual(str.size(), stdex::sprintf("%s", locale, str.data()).size());
} }
};
} }

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -16,10 +16,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(unicode) void unicode::str2wstr()
{
public:
TEST_METHOD(str2wstr)
{ {
Assert::AreEqual( Assert::AreEqual(
L"This is a test.", L"This is a test.",
@ -42,7 +39,7 @@ namespace UnitTests
stdex::str2wstr(nullptr, 0, stdex::charset_id::utf8).c_str()); stdex::str2wstr(nullptr, 0, stdex::charset_id::utf8).c_str());
} }
TEST_METHOD(wstr2str) void unicode::wstr2str()
{ {
Assert::AreEqual( Assert::AreEqual(
"This is a test.", "This is a test.",
@ -65,7 +62,7 @@ namespace UnitTests
stdex::wstr2str(nullptr, 0, stdex::charset_id::utf8).c_str()); stdex::wstr2str(nullptr, 0, stdex::charset_id::utf8).c_str());
} }
TEST_METHOD(charset_encoder) void unicode::charset_encoder()
{ {
stdex::charset_encoder<char, char> win1250_to_utf8(stdex::charset_id::windows1250, stdex::charset_id::utf8); stdex::charset_encoder<char, char> win1250_to_utf8(stdex::charset_id::windows1250, stdex::charset_id::utf8);
@ -89,7 +86,7 @@ namespace UnitTests
win1250_to_utf8.convert(nullptr, 0).c_str()); win1250_to_utf8.convert(nullptr, 0).c_str());
} }
TEST_METHOD(normalize) void unicode::normalize()
{ {
#ifdef _WIN32 #ifdef _WIN32
Assert::AreEqual( Assert::AreEqual(
@ -100,7 +97,6 @@ namespace UnitTests
stdex::normalize(nullptr, 0).c_str()); stdex::normalize(nullptr, 0).c_str());
#endif #endif
} }
};
} }
#ifdef __GNUC__ #ifdef __GNUC__

View File

@ -1,4 +1,4 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis Copyright © 2023-2024 Amebis
*/ */
@ -12,10 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(watchdog) void watchdog::test()
{
public:
TEST_METHOD(test)
{ {
volatile bool wd_called = false; volatile bool wd_called = false;
stdex::watchdog<std::chrono::steady_clock> wd( stdex::watchdog<std::chrono::steady_clock> wd(
@ -28,5 +25,4 @@ namespace UnitTests
std::this_thread::sleep_for(std::chrono::milliseconds(300)); std::this_thread::sleep_for(std::chrono::milliseconds(300));
Assert::IsTrue(wd_called); Assert::IsTrue(wd_called);
} }
};
} }

View File

@ -12,10 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests namespace UnitTests
{ {
TEST_CLASS(zlib) void zlib::test()
{
public:
TEST_METHOD(test)
{ {
static const char inflated[] = "This is a test."; static const char inflated[] = "This is a test.";
stdex::stream::memory_file dat_deflated; stdex::stream::memory_file dat_deflated;
@ -36,5 +33,4 @@ namespace UnitTests
Assert::AreEqual<size_t>(sizeof(inflated) - sizeof(*inflated), dat_inflated.size()); Assert::AreEqual<size_t>(sizeof(inflated) - sizeof(*inflated), dat_inflated.size());
Assert::AreEqual(0, memcmp(inflated, dat_inflated.data(), sizeof(inflated) - sizeof(*inflated))); Assert::AreEqual(0, memcmp(inflated, dat_inflated.data(), sizeof(inflated) - sizeof(*inflated)));
} }
};
} }