Skip to content

Commit

Permalink
test: add sync_wait tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Mar 1, 2024
1 parent 636109a commit 1ea1cec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/coro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_gap_test(test-gap-coro
generator.cpp
recursive_generator.cpp
shared_task.cpp
sync_wait.cpp
task.cpp
when_all_ready.cpp
)
Expand Down
68 changes: 68 additions & 0 deletions test/coro/sync_wait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2021-present, Trail of Bits, Inc.

///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
// This file is a modified version of cppcoro/test/sync_wait.cpp from
// the cppcoro project. The original file is licenced under the MIT license and
// the original license is included above.
///////////////////////////////////////////////////////////////////////////////

#ifdef GAP_ENABLE_COROUTINES

#include <doctest/doctest.h>
#include <gap/coro/sync_wait.hpp>

#include <gap/core/config.hpp>
#include <gap/core/on_scope_exit.hpp>

#include <gap/coro/task.hpp>
#include <gap/coro/shared_task.hpp>

#include <string>
#include <type_traits>

using namespace gap::coro;

namespace gap::test
{
TEST_SUITE_BEGIN("sync_wait");

static_assert(std::is_same<
decltype(gap::coro::sync_wait(std::declval<gap::coro::task<std::string>>())),
std::string&&>::value);
static_assert(std::is_same<
decltype(gap::coro::sync_wait(std::declval<gap::coro::task<std::string>&>())),
std::string&>::value);

TEST_CASE("sync_wait(task<T>)")
{
auto makeTask = []() -> gap::coro::task<std::string>
{
co_return "foo";
};

auto task = makeTask();
CHECK(gap::coro::sync_wait(task) == "foo");

CHECK(gap::coro::sync_wait(makeTask()) == "foo");
}

TEST_CASE("sync_wait(shared_task<T>)")
{
auto makeTask = []() -> gap::coro::shared_task<std::string>
{
co_return "foo";
};

auto task = makeTask();

CHECK(gap::coro::sync_wait(task) == "foo");
CHECK(gap::coro::sync_wait(makeTask()) == "foo");
}

TEST_SUITE_END();

} // namespace gap::test
#endif

0 comments on commit 1ea1cec

Please sign in to comment.