Skip to content

Commit

Permalink
Making tests more portable
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Aug 8, 2024
1 parent daadbd1 commit 167731d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 58 deletions.
2 changes: 2 additions & 0 deletions test/BlockUtilTestCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "doctest.h"

#include <string>

TEST_SUITE_BEGIN( "BlockUtilTestsCpp" );

namespace {
Expand Down
29 changes: 8 additions & 21 deletions test/CoDispatchTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -698,27 +698,14 @@ static auto checkIO() -> DispatchTask<> {
co_await resumeOnMainQueue();
}

static DispatchTask<> runTests(bool & shouldKeepRunning) {

co_await checkReturnPropagation();
co_await checkDispatchToDifferentQueue();
co_await checkMakeAwaitable();
co_await checkTasks();
co_await checkGenerator();
co_await checkIO();

shouldKeepRunning = false;
}


TEST_CASE("CoDispatchTests") {

bool shouldKeepRunning = true;

runTests(shouldKeepRunning);

NSRunLoop * theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

[]() -> DispatchTask<> {
co_await checkReturnPropagation();
co_await checkDispatchToDifferentQueue();
co_await checkMakeAwaitable();
co_await checkTasks();
co_await checkGenerator();
co_await checkIO();
}();
}

31 changes: 15 additions & 16 deletions test/CoDispatchTestsCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@

#include "doctest.h"

#include <CoreFoundation/CoreFoundation.h>
#if (defined(__APPLE__) && defined(__MACH__))
#include <mach-o/dyld.h>
#endif

#include <filesystem>
#include <vector>

static auto checkIO() -> DispatchTask<> {

auto conq = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0);
auto conq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

co_await resumeOn(conq);

#if (defined(__APPLE__) && defined(__MACH__))
char buf[1024];
uint32_t size = sizeof(buf);
REQUIRE(_NSGetExecutablePath(buf, &size) == 0);
std::filesystem::path path(buf);
#else
std::filesystem::path selfLink = "/proc/" + std::to_string(getpid()) + "/exe";
std::error_code ec;
std::filesystem::path path = read_symlink(selfLink, ec);
REQUIRE(!ec);
#endif

path = path.parent_path() / "test.txt";

dispatch_data_t hello = dispatch_data_create("hello", 5, conq, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
Expand Down Expand Up @@ -99,9 +109,9 @@ static auto checkIO() -> DispatchTask<> {
co_await resumeOnMainQueue();
}

static DispatchTask<> runTests(bool & shouldKeepRunning) {
static DispatchTask<> runTests() {

auto conq = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0);
auto conq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

int i = co_await co_dispatch([&]() {
return 7;
Expand Down Expand Up @@ -139,20 +149,9 @@ static DispatchTask<> runTests(bool & shouldKeepRunning) {
}

co_await checkIO();

shouldKeepRunning = false;

}

TEST_CASE("CoDispatchTestsCpp") {

bool shouldKeepRunning = true;

runTests(shouldKeepRunning);

while (shouldKeepRunning) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, std::numeric_limits<CFTimeInterval>::max(), true);
}

runTests();
}

26 changes: 6 additions & 20 deletions test/CoDispatchTestsNoexcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@

#include "doctest.h"

#include <CoreFoundation/CoreFoundation.h>


static DispatchTask<> runTests(bool & shouldKeepRunning) {

auto i = co_await co_dispatch([]() {
return 7;
});
CHECK(i == 7);

shouldKeepRunning = false;
}


TEST_CASE("CoDispatchTestsNoExcept") {

bool shouldKeepRunning = true;

runTests(shouldKeepRunning);

while (shouldKeepRunning) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, std::numeric_limits<CFTimeInterval>::max(), true);
}

[]() -> DispatchTask<> {
auto i = co_await co_dispatch([]() {
return 7;
});
CHECK(i == 7);
}();
}
10 changes: 9 additions & 1 deletion test/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"

#include <dispatch/dispatch.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
return doctest::Context(argc, argv).run();
dispatch_async(dispatch_get_main_queue(), ^ {
@autoreleasepool {
auto ret = doctest::Context(argc, argv).run();
exit(ret);
}
});
dispatch_main();
}
}

0 comments on commit 167731d

Please sign in to comment.