Skip to content

Commit

Permalink
added tests for poly
Browse files Browse the repository at this point in the history
  • Loading branch information
dietmarkuehl committed Jan 17, 2025
1 parent 28708d0 commit ad682dc
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BUILDDIR = build
default: test

compile: config
cmake --build $(BUILDDIR)
cmake --build $(BUILDDIR) -j

format:
git clang-format main
Expand Down
8 changes: 4 additions & 4 deletions include/beman/lazy/detail/lazy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ struct lazy {
::beman::execution26::set_stopped_t()>;

struct state_base {
virtual void complete(lazy_promise_base<std::remove_cvref_t<T>>::result_t&) = 0;
virtual stop_token_type get_stop_token() = 0;
virtual C& get_context() = 0;
virtual void complete(typename lazy_promise_base<std::remove_cvref_t<T>>::result_t&) = 0;
virtual stop_token_type get_stop_token() = 0;
virtual C& get_context() = 0;

protected:
virtual ~state_base() = default;
Expand Down Expand Up @@ -251,7 +251,7 @@ struct lazy {
handle.promise().state = this;
handle.resume();
}
void complete(lazy_promise_base<std::remove_cvref_t<T>>::result_t& result) override {
void complete(typename lazy_promise_base<std::remove_cvref_t<T>>::result_t& result) override {
switch (result.index()) {
case 0: // set_stopped
this->reset_handle();
Expand Down
49 changes: 44 additions & 5 deletions include/beman/lazy/detail/poly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,62 @@
// ----------------------------------------------------------------------------

namespace beman::lazy::detail {
template <typename Base, std::size_t Size>
/*!
* \brief Utility providing small object optimization and type erasure.
* \headerfile beman/lazy/lazy.hpp <beman/lazy/lazy.hpp>
* \internal
*/
template <typename Base, std::size_t Size = 4u * sizeof(void*)>
class alignas(sizeof(double)) poly {
private:
std::array<std::byte, Size> buf{};
Base* pointer() { return static_cast<Base*>(static_cast<void*>(buf.data())); }

Base* pointer() { return static_cast<Base*>(static_cast<void*>(buf.data())); }
const Base* pointer() const { return static_cast<const Base*>(static_cast<const void*>(buf.data())); }

public:
template <typename T, typename... Args>
requires(sizeof(T) <= Size)
poly(T*, Args&&... args) {
new (this->buf.data()) T(::std::forward<Args>(args)...);
static_assert(sizeof(T) <= Size);
}
poly(poly&& other) { other.pointer()->move(this->buf.data()); }
poly(const poly& other) { other.pointer()->clone(this->buf.data()); }
poly(poly&& other)
requires requires(Base* b, void* t) { b->move(t); }
{
other.pointer()->move(this->buf.data());
}
poly& operator=(poly&& other)
requires requires(Base* b, void* t) { b->move(t); }
{
if (this != &other) {
this->pointer()->~Base();
other.pointer()->move(this->buf.data());
}
return *this;
}
poly& operator=(const poly& other)
requires requires(Base* b, void* t) { b->clone(t); }
{
if (this != &other) {
this->pointer()->~Base();
other.pointer()->clone(this->buf.data());
}
return *this;
}
poly(const poly& other)
requires requires(Base* b, void* t) { b->clone(t); }
{
other.pointer()->clone(this->buf.data());
}
~poly() { this->pointer()->~Base(); }
bool operator==(const poly& other) const { return other.pointer()->equals(this->pointer()); }
bool operator==(const poly& other) const
requires requires(const Base& b) {
{ b.equals(&b) } -> std::same_as<bool>;
}
{
return other.pointer()->equals(this->pointer());
}
Base* operator->() { return this->pointer(); }
};
} // namespace beman::lazy::detail
Expand Down
18 changes: 11 additions & 7 deletions tests/beman/lazy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_executable(beman.lazy.tests.lazy)
target_sources(beman.lazy.tests.lazy PRIVATE lazy.test.cpp)
target_link_libraries(beman.lazy.tests.lazy PRIVATE beman::lazy)
add_test(
NAME beman.lazy.tests.lazy
COMMAND $<TARGET_FILE:beman.lazy.tests.lazy>
)
list(APPEND lazy_tests poly lazy)

foreach(test ${lazy_tests})
add_executable(beman.lazy.tests.${test})
target_sources(beman.lazy.tests.${test} PRIVATE ${test}.test.cpp)
target_link_libraries(beman.lazy.tests.${test} PRIVATE beman::lazy)
add_test(
NAME beman.lazy.tests.${test}
COMMAND $<TARGET_FILE:beman.lazy.tests.${test}>
)
endforeach()
11 changes: 10 additions & 1 deletion tests/beman/lazy/lazy.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/lazy/lazy.hpp>
#include <beman/execution26/execution.hpp>
#include <cassert>

namespace ex = beman::execution26;

// ----------------------------------------------------------------------------

int main() {}
int main() {
auto rc = ex::sync_wait([]() -> ex::lazy<int> { co_return 17; }());
assert(rc);
auto [value] = rc.value_or(std::tuple{0});
assert(value == 17);
}
Loading

0 comments on commit ad682dc

Please sign in to comment.