macOS support

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
2024-02-08 12:09:33 +01:00
parent b6be4f040e
commit 08a18d1519
19 changed files with 342 additions and 289 deletions

View File

@@ -58,6 +58,9 @@
F4C07F582AB08E690044EDC0 /* sgml.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sgml.cpp; sourceTree = "<group>"; };
F4C07F592AB08E690044EDC0 /* ring.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ring.cpp; sourceTree = "<group>"; };
F4C07F5A2AB08E690044EDC0 /* stream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = stream.cpp; sourceTree = "<group>"; };
F4CCA3B62B73B912007B857B /* watchdog.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = watchdog.cpp; sourceTree = "<group>"; };
F4CCA3B72B73B940007B857B /* pool.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = pool.cpp; sourceTree = "<group>"; };
F4CCA3B82B73D2E2007B857B /* string.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = string.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -110,12 +113,15 @@
F4C07F562AB08E690044EDC0 /* parser.cpp */,
F4C07F512AB059580044EDC0 /* pch.cpp */,
F4C07F502AB059580044EDC0 /* pch.hpp */,
F4CCA3B72B73B940007B857B /* pool.cpp */,
F4B7FBDD2AAF49BC00C6BE9F /* Products */,
F4C07F592AB08E690044EDC0 /* ring.cpp */,
F4C07F582AB08E690044EDC0 /* sgml.cpp */,
F4213D162ABB14AA00F72674 /* stdex */,
F4C07F5A2AB08E690044EDC0 /* stream.cpp */,
F4CCA3B82B73D2E2007B857B /* string.cpp */,
F4C07F572AB08E690044EDC0 /* unicode.cpp */,
F4CCA3B62B73B912007B857B /* watchdog.cpp */,
);
sourceTree = "<group>";
usesTabs = 1;

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -6,10 +6,12 @@
#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 <iostream>
@@ -25,6 +27,7 @@ int main(int argc, const char * argv[])
UnitTests::parser::wtest();
UnitTests::parser::sgml_test();
UnitTests::parser::http_test();
UnitTests::pool::test();
UnitTests::ring::test();
UnitTests::sgml::sgml2str();
UnitTests::sgml::str2sgml();
@@ -32,9 +35,11 @@ int main(int argc, const char * argv[])
UnitTests::stream::replicator();
UnitTests::stream::open_close();
UnitTests::stream::file_stat();
UnitTests::string::sprintf();
UnitTests::unicode::str2wstr();
UnitTests::unicode::wstr2str();
UnitTests::unicode::charset_encoder();
UnitTests::unicode::normalize();
UnitTests::watchdog::test();
std::cout << "PASS\n";
return 0;

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -6,15 +6,13 @@
#include "pch.hpp"
using namespace std;
using namespace stdex;
using namespace stdex::parser;
#ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Microsoft {
namespace VisualStudio {
namespace CppUnitTestFramework {
static std::wstring ToString(const stdex::interval<size_t>& q)
static wstring ToString(const stdex::interval<size_t>& q)
{
return stdex::sprintf(L"<%zu, %zu>", nullptr, q.start, q.end);
}
@@ -33,22 +31,22 @@ namespace UnitTests
static const wchar_t text[] = L"This is a test.\nSecond line.";
{
wnoop p;
stdex::parser::wnoop p;
Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end);
}
{
wcu p(L't');
stdex::parser::wcu p(L't');
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)1, p.interval.end);
}
{
wspace_cu p;
stdex::parser::wspace_cu p;
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 4));
Assert::AreEqual((size_t)4, p.interval.start);
@@ -56,7 +54,7 @@ namespace UnitTests
}
{
wpunct_cu p;
stdex::parser::wpunct_cu p;
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 14));
Assert::AreEqual((size_t)14, p.interval.start);
@@ -64,7 +62,7 @@ namespace UnitTests
}
{
wspace_or_punct_cu p;
stdex::parser::wspace_or_punct_cu p;
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 4));
Assert::AreEqual((size_t)4, p.interval.start);
@@ -75,7 +73,7 @@ namespace UnitTests
}
{
wbol p;
stdex::parser::wbol p;
Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end);
@@ -87,7 +85,7 @@ namespace UnitTests
}
{
weol p;
stdex::parser::weol p;
Assert::IsFalse(p.match(text));
Assert::IsFalse(p.match(text, 1));
Assert::IsTrue(p.match(text, 15));
@@ -97,14 +95,14 @@ namespace UnitTests
}
{
wcu_set p(L"abcD");
stdex::parser::wcu_set p(L"abcD");
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 8));
Assert::AreEqual((size_t)8, p.interval.start);
Assert::AreEqual((size_t)9, p.interval.end);
Assert::AreEqual((size_t)0, p.hit_offset);
Assert::IsFalse(p.match(text, 21));
Assert::IsTrue(p.match(text, 21, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 21, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)21, p.interval.start);
Assert::AreEqual((size_t)22, p.interval.end);
Assert::AreEqual((size_t)3, p.hit_offset);
@@ -113,83 +111,83 @@ namespace UnitTests
{
stdex::parser::wstring p(L"this");
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, sizeof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, sizeof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end);
}
{
wany_cu chr;
witerations p(make_shared_no_delete(&chr), 1, 5);
stdex::parser::wany_cu chr;
stdex::parser::witerations p(stdex::make_shared_no_delete(&chr), 1, 5);
Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)5, p.interval.end);
}
{
wspace_cu nospace(true);
witerations p(make_shared_no_delete(&nospace), 1);
stdex::parser::wspace_cu nospace(true);
stdex::parser::witerations p(stdex::make_shared_no_delete(&nospace), 1);
Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end);
}
{
wcu chr_t(L't'), chr_h(L'h'), chr_i(L'i'), chr_s(L's');
wspace_cu space;
wsequence p({
make_shared_no_delete(&chr_t),
make_shared_no_delete(&chr_h),
make_shared_no_delete(&chr_i),
make_shared_no_delete(&chr_s),
make_shared_no_delete(&space) });
stdex::parser::wcu chr_t(L't'), chr_h(L'h'), chr_i(L'i'), chr_s(L's');
stdex::parser::wspace_cu space;
stdex::parser::wsequence p({
stdex::make_shared_no_delete(&chr_t),
stdex::make_shared_no_delete(&chr_h),
stdex::make_shared_no_delete(&chr_i),
stdex::make_shared_no_delete(&chr_s),
stdex::make_shared_no_delete(&space) });
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)5, p.interval.end);
}
{
stdex::parser::wstring apple(L"apple"), orange(L"orange"), _this(L"this");
wspace_cu space;
wbranch p({
make_shared_no_delete(&apple),
make_shared_no_delete(&orange),
make_shared_no_delete(&_this),
make_shared_no_delete(&space) });
stdex::parser::wspace_cu space;
stdex::parser::wbranch p({
stdex::make_shared_no_delete(&apple),
stdex::make_shared_no_delete(&orange),
stdex::make_shared_no_delete(&_this),
stdex::make_shared_no_delete(&space) });
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end);
}
{
wstring_branch p(L"apple", L"orange", L"this", nullptr);
stdex::parser::wstring_branch p(L"apple", L"orange", L"this", nullptr);
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end);
}
{
wcu chr_s(L's'), chr_h(L'h'), chr_i(L'i'), chr_t(L't');
wpermutation p({
make_shared_no_delete(&chr_s),
make_shared_no_delete(&chr_h),
make_shared_no_delete(&chr_i),
make_shared_no_delete(&chr_t) });
stdex::parser::wcu chr_s(L's'), chr_h(L'h'), chr_i(L'i'), chr_t(L't');
stdex::parser::wpermutation p({
stdex::make_shared_no_delete(&chr_s),
stdex::make_shared_no_delete(&chr_h),
stdex::make_shared_no_delete(&chr_i),
stdex::make_shared_no_delete(&chr_t) });
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)4, p.interval.end);
}
{
std::locale locale_slSI("sl_SI");
wspace_cu space(false, locale_slSI);
wiban p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::wspace_cu space(false, locale_slSI);
stdex::parser::wiban p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"SI56023120015226972", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"SI", p.country);
@@ -202,7 +200,7 @@ namespace UnitTests
Assert::AreEqual(L"023120015226972", p.bban);
Assert::IsFalse(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match(L"si56 0231 2001 5226 972", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match(L"SI56 0231 2001 5226 9720", 0, SIZE_MAX));
Assert::AreEqual(stdex::interval<size_t>(0, 23), p.interval);
@@ -250,8 +248,8 @@ namespace UnitTests
{
std::locale locale_slSI("sl_SI");
wspace_cu space(false, locale_slSI);
wcreditor_reference p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::wspace_cu space(false, locale_slSI);
stdex::parser::wcreditor_reference p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"RF18539007547034", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"18", p.check_digits);
@@ -262,7 +260,7 @@ namespace UnitTests
Assert::AreEqual(L"000000000539007547034", p.reference);
Assert::IsFalse(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match(L"rf18 5390 0754 7034", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match(L"RF18 5390 0754 70340", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
@@ -274,8 +272,8 @@ namespace UnitTests
{
std::locale locale_slSI("sl_SI");
wspace_cu space(false, locale_slSI);
wsi_reference p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::wspace_cu space(false, locale_slSI);
stdex::parser::wsi_reference p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match(L"SI121234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual(L"12", p.model);
@@ -285,7 +283,7 @@ namespace UnitTests
Assert::AreEqual(L"12", p.model);
Assert::AreEqual(stdex::interval<size_t>(5, 18), p.part1.interval);
Assert::IsFalse(p.match(L"si12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.match(L"si12 1234567890120", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match(L"si12 1234567890120", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.match(L"...SI12 1234567890120...", 3, SIZE_MAX));
Assert::IsTrue(p.match(L"SI12 1234567890120", 0, SIZE_MAX)); // no-break space
}
@@ -297,30 +295,30 @@ namespace UnitTests
static const char text[] = "V ko&zcaron;u&scaron;&ccaron;ku zlobnega mizarja stopiclja fant\nin kli&ccaron;e&nbsp;1234567890.";
{
sgml_noop p;
stdex::parser::sgml_noop p;
Assert::IsTrue(p.match(text));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)0, p.interval.end);
}
{
sgml_cp p("v");
stdex::parser::sgml_cp p("v");
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 0, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 0, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)1, p.interval.end);
}
{
sgml_cp p("&Zcaron;", SIZE_MAX, false, locale_slSI);
stdex::parser::sgml_cp p("&Zcaron;", SIZE_MAX, false, locale_slSI);
Assert::IsFalse(p.match(text, 4));
Assert::IsTrue(p.match(text, 4, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 4, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)4, p.interval.start);
Assert::AreEqual((size_t)12, p.interval.end);
}
{
sgml_space_cp p(false, locale_slSI);
stdex::parser::sgml_space_cp p(false, locale_slSI);
Assert::IsFalse(p.match(text));
Assert::IsTrue(p.match(text, 1));
Assert::AreEqual((size_t)1, p.interval.start);
@@ -331,17 +329,17 @@ namespace UnitTests
}
{
sgml_string_branch p(locale_slSI, "apple", "orange", "Ko&Zcaron;u&Scaron;&ccaron;Ku", nullptr);
stdex::parser::sgml_string_branch p(locale_slSI, "apple", "orange", "Ko&Zcaron;u&Scaron;&ccaron;Ku", nullptr);
Assert::IsFalse(p.match(text, 2));
Assert::IsTrue(p.match(text, 2, _countof(text), match_case_insensitive));
Assert::IsTrue(p.match(text, 2, _countof(text), stdex::parser::match_case_insensitive));
Assert::AreEqual((size_t)2, p.hit_offset);
Assert::AreEqual((size_t)2, p.interval.start);
Assert::AreEqual((size_t)31, p.interval.end);
}
{
sgml_space_cp space(false, locale_slSI);
sgml_iban p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_iban p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("SI56023120015226972", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual("SI", p.country);
@@ -354,7 +352,7 @@ namespace UnitTests
Assert::AreEqual("023120015226972", p.bban);
Assert::IsFalse(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match("si56 0231 2001 5226 972", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match("SI56 0231 2001 5226 9720", 0, SIZE_MAX));
Assert::AreEqual(stdex::interval<size_t>(0, 23), p.interval);
@@ -366,8 +364,8 @@ namespace UnitTests
}
{
sgml_space_cp space(false, locale_slSI);
sgml_creditor_reference p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_creditor_reference p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("RF18539007547034", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual("18", p.check_digits);
@@ -378,7 +376,7 @@ namespace UnitTests
Assert::AreEqual("000000000539007547034", p.reference);
Assert::IsFalse(p.match("rf18 5390 0754 7034", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
Assert::IsTrue(p.match("rf18 5390 0754 7034", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match("rf18 5390 0754 7034", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.is_valid);
Assert::IsTrue(p.match("RF18 5390 0754 70340", 0, SIZE_MAX));
Assert::IsFalse(p.is_valid);
@@ -389,8 +387,8 @@ namespace UnitTests
}
{
sgml_space_cp space(false, locale_slSI);
sgml_si_reference p(make_shared_no_delete(&space), locale_slSI);
stdex::parser::sgml_space_cp space(false, locale_slSI);
stdex::parser::sgml_si_reference p(stdex::make_shared_no_delete(&space), locale_slSI);
Assert::IsTrue(p.match("SI121234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.is_valid);
Assert::AreEqual("12", p.model);
@@ -400,7 +398,7 @@ namespace UnitTests
Assert::AreEqual("12", p.model);
Assert::AreEqual(stdex::interval<size_t>(5, 18), p.part1.interval);
Assert::IsFalse(p.match("si12 1234567890120", 0, SIZE_MAX));
Assert::IsTrue(p.match("si12 1234567890120", 0, SIZE_MAX, match_case_insensitive));
Assert::IsTrue(p.match("si12 1234567890120", 0, SIZE_MAX, stdex::parser::match_case_insensitive));
Assert::IsTrue(p.match("...SI12 1234567890120...", 3, SIZE_MAX));
Assert::IsTrue(p.match("SI12&nbsp;1234567890120", 0, SIZE_MAX));
}
@@ -429,7 +427,7 @@ namespace UnitTests
"\r\n";
{
http_request p(locale);
stdex::parser::http_request p(locale);
Assert::IsTrue(p.match(request));
Assert::AreEqual((size_t)0, p.interval.start);
Assert::AreEqual((size_t)14, p.interval.end);
@@ -443,10 +441,10 @@ namespace UnitTests
}
{
std::list<http_header> hdrs;
list<stdex::parser::http_header> hdrs;
size_t offset = 14;
for (;;) {
http_header h;
stdex::parser::http_header h;
if (h.match(request, offset)) {
offset = h.interval.end;
hdrs.push_back(std::move(h));
@@ -455,19 +453,19 @@ namespace UnitTests
break;
}
Assert::AreEqual((size_t)15, hdrs.size());
http_weighted_collection<http_weighted_value<http_language>> langs;
stdex::parser::http_weighted_collection<stdex::parser::http_weighted_value<stdex::parser::http_language>> langs;
for (const auto& h : hdrs)
if (strnicmp(request + h.name.start, h.name.size(), "Accept-Language", SIZE_MAX, locale) == 0)
if (stdex::strnicmp(request + h.name.start, h.name.size(), "Accept-Language", SIZE_MAX, locale) == 0)
langs.insert(request, h.value.start, h.value.end);
Assert::IsTrue(!langs.empty());
{
const vector<std::string> control = {
const vector<string> control = {
"sl", "en-US", "en", "de-DE", "de"
};
auto c = control.cbegin();
auto l = langs.cbegin();
for (; c != control.cend() && l != langs.cend(); ++c, ++l)
Assert::IsTrue(strnicmp(request + l->value.interval.start, l->value.interval.size(), c->c_str(), c->size(), locale) == 0);
Assert::IsTrue(stdex::strnicmp(request + l->value.interval.start, l->value.interval.size(), c->c_str(), c->size(), locale) == 0);
Assert::IsTrue(c == control.cend());
Assert::IsTrue(l == langs.cend());
}

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -12,7 +12,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests
{
constexpr size_t capacity = 50;
constexpr size_t pool_capacity = 50;
TEST_CLASS(pool)
{
@@ -24,15 +24,15 @@ namespace UnitTests
pool_t pool;
list<thread> workers;
for (auto n = thread::hardware_concurrency(); n--; ) {
workers.push_back(std::move(thread([](_Inout_ pool_t& pool)
{
for (size_t n = 10000; n--; ) {
worker_t el = move(pool.pop());
if (!el)
el.reset(new int(1));
pool.push(move(el));
}
}, ref(pool))));
workers.push_back(thread([](_Inout_ pool_t& pool)
{
for (size_t n = 10000; n--; ) {
worker_t el = pool.pop();
if (!el)
el.reset(new int(1));
pool.push(std::move(el));
}
}, ref(pool)));
}
for (auto& w : workers)

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -12,20 +12,20 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTests
{
constexpr size_t capacity = 50;
constexpr size_t ring_capacity = 50;
TEST_CLASS(ring)
{
public:
TEST_METHOD(test)
{
using ring_t = stdex::ring<int, capacity>;
using ring_t = stdex::ring<int, ring_capacity>;
ring_t ring;
thread writer([](_Inout_ ring_t& ring)
{
int seed = 0;
for (size_t retries = 1000; retries--;) {
for (auto to_write = static_cast<size_t>(static_cast<uint64_t>(::rand()) * capacity / 5 / RAND_MAX); to_write;) {
for (auto to_write = static_cast<size_t>(static_cast<uint64_t>(::rand()) * ring_capacity / 5 / RAND_MAX); to_write;) {
int* ptr; size_t num_write;
tie(ptr, num_write) = ring.back();
if (to_write < num_write)

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -6,8 +6,6 @@
#include "pch.hpp"
using namespace std;
using namespace stdex;
using namespace stdex::stream;
#ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#endif
@@ -20,9 +18,9 @@ namespace UnitTests
TEST_METHOD(async)
{
constexpr uint32_t total = 1000;
memory_file source(mul(total, sizeof(uint32_t)));
stdex::stream::memory_file source(stdex::mul(total, sizeof(uint32_t)));
{
async_writer<70> writer(source);
stdex::stream::async_writer<70> writer(source);
for (uint32_t i = 0; i < total; ++i) {
Assert::IsTrue(writer.ok());
writer << i;
@@ -30,7 +28,7 @@ namespace UnitTests
}
Assert::AreEqual<stdex::stream::fpos_t>(0, source.seekbeg(0));
{
async_reader<50> reader(source);
stdex::stream::async_reader<50> reader(source);
uint32_t x;
for (uint32_t i = 0; i < total; ++i) {
reader >> x;
@@ -46,24 +44,24 @@ namespace UnitTests
{
constexpr uint32_t total = 1000;
memory_file f1(mul(total, sizeof(uint32_t)));
stdex::stream::memory_file f1(stdex::mul(total, sizeof(uint32_t)));
sstring filename2, filename3;
stdex::sstring filename2, filename3;
filename2 = filename3 = temp_path();
filename2 += _T("stdex-stream-replicator-2.tmp");
file f2(
stdex::stream::file f2(
filename2.c_str(),
mode_for_reading | mode_for_writing | mode_create | mode_binary);
stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary);
filename3 += _T("stdex-stream-replicator-3.tmp");
cached_file f3(
stdex::stream::cached_file f3(
filename3.c_str(),
mode_for_reading | mode_for_writing | mode_create | mode_binary,
stdex::stream::mode_for_reading | stdex::stream::mode_for_writing | stdex::stream::mode_create | stdex::stream::mode_binary,
128);
{
stdex::stream::replicator writer;
buffer f2_buf(f2, 0, 32);
stdex::stream::buffer f2_buf(f2, 0, 32);
writer.push_back(&f1);
writer.push_back(&f2_buf);
writer.push_back(&f3);
@@ -77,7 +75,7 @@ namespace UnitTests
f2.seekbeg(0);
f3.seekbeg(0);
{
buffer f2_buf(f2, 64, 0);
stdex::stream::buffer f2_buf(f2, 64, 0);
uint32_t x;
for (uint32_t i = 0; i < total; ++i) {
f1 >> x;
@@ -106,17 +104,17 @@ namespace UnitTests
TEST_METHOD(open_close)
{
cached_file dat(invalid_handle, state_t::fail, 4096);
const sstring filepath = temp_path();
stdex::stream::cached_file dat(stdex::invalid_handle, stdex::stream::state_t::fail, 4096);
const stdex::sstring filepath = temp_path();
constexpr uint32_t count = 3;
sstring filename[count];
stdex::sstring filename[count];
stdex::stream::fpos_t start[count];
for (uint32_t i = 0; i < count; ++i) {
filename[i] = filepath + sprintf(_T("stdex-stream-open_close%u.tmp"), NULL, i);
dat.open(filename[i].c_str(), mode_for_reading | mode_for_writing | share_none | mode_preserve_existing | mode_binary);
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);
Assert::IsTrue(dat.ok());
start[i] = dat.tell();
Assert::AreNotEqual(fpos_max, start[i]);
Assert::AreNotEqual(stdex::stream::fpos_max, start[i]);
for (uint32_t j = 0; j < 31 + 11 * i; ++j) {
dat << j * count + i;
Assert::IsTrue(dat.ok());
@@ -124,7 +122,7 @@ namespace UnitTests
dat.close();
}
for (uint32_t i = 0; i < count; ++i) {
dat.open(filename[i].c_str(), mode_for_reading | mode_open_existing | share_none | mode_binary);
dat.open(filename[i].c_str(), stdex::stream::mode_for_reading | stdex::stream::mode_open_existing | stdex::stream::share_none | stdex::stream::mode_binary);
Assert::IsTrue(dat.ok());
for (;;) {
uint32_t x;
@@ -141,13 +139,13 @@ namespace UnitTests
TEST_METHOD(file_stat)
{
sstring path(temp_path());
stdex::sstring path(temp_path());
Assert::IsTrue(stdex::stream::file::exists(path));
Assert::IsFalse(stdex::stream::file::readonly(path));
}
protected:
static sstring temp_path()
static stdex::sstring temp_path()
{
#ifdef _WIN32
TCHAR temp_path[MAX_PATH];

View File

@@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
@@ -17,19 +17,23 @@ namespace UnitTests
public:
TEST_METHOD(sprintf)
{
Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").c_str());
Assert::AreEqual<size_t>(15, stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").size());
Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a test").c_str());
Assert::AreEqual<size_t>(15, stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a test").size());
Assert::AreEqual("This is a test.", stdex::sprintf("This is %s.", stdex::locale_default, "a test").c_str());
Assert::AreEqual<size_t>(15, stdex::sprintf("This is %s.", stdex::locale_default, "a test").size());
// swprintf functions return EILSEQ when %ls inserts contain emoji on Mac. 😢
Assert::AreEqual(L"This is a tést.", stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a tést").c_str());
Assert::AreEqual("This is a 🐔Test🐮.", stdex::sprintf("This is %s.", stdex::locale_default, "a 🐔Test🐮").c_str());
wstring wstr;
std::string str;
for (size_t i = 0; i < 2000; i++) {
wstr += L"🐔Test🐮\r\n";
wstr += L"st\r\n";
str += "🐔Test🐮\r\n";
}
Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).c_str());
Assert::AreEqual(wstr.size(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).size());
Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%ls", stdex::locale_default, wstr.data()).c_str());
Assert::AreEqual(wstr.size(), stdex::sprintf(L"%ls", stdex::locale_default, wstr.data()).size());
Assert::AreEqual(str.c_str(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).c_str());
Assert::AreEqual(str.size(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).size());
}

View File

@@ -8,7 +8,8 @@
using namespace std;
#ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#else
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
@@ -102,6 +103,6 @@ namespace UnitTests
};
}
#ifndef _WIN32
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif