#include #include #include struct fake_delta {}; template struct fake_process: entt::process, Delta> { using process_type = entt::process, Delta>; using delta_type = typename process_type::delta_type; fake_process() : init_invoked{false}, update_invoked{false}, succeeded_invoked{false}, failed_invoked{false}, aborted_invoked{false} {} void succeed() noexcept { process_type::succeed(); } void fail() noexcept { process_type::fail(); } void pause() noexcept { process_type::pause(); } void unpause() noexcept { process_type::unpause(); } void init() { init_invoked = true; } void succeeded() { succeeded_invoked = true; } void failed() { failed_invoked = true; } void aborted() { aborted_invoked = true; } void update(typename entt::process, Delta>::delta_type, void *data) { if(data) { (*static_cast(data))++; } update_invoked = true; } bool init_invoked; bool update_invoked; bool succeeded_invoked; bool failed_invoked; bool aborted_invoked; }; TEST(Process, Basics) { fake_process process{}; ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); process.succeed(); process.fail(); process.abort(); process.pause(); process.unpause(); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); process.tick(0); ASSERT_TRUE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); process.pause(); ASSERT_TRUE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_TRUE(process.paused()); ASSERT_FALSE(process.rejected()); process.unpause(); ASSERT_TRUE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); process.fail(); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); process.tick(0); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_TRUE(process.rejected()); } TEST(Process, Succeeded) { fake_process process{}; process.tick({}); process.tick({}); process.succeed(); process.tick({}); ASSERT_FALSE(process.alive()); ASSERT_TRUE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); ASSERT_TRUE(process.init_invoked); ASSERT_TRUE(process.update_invoked); ASSERT_TRUE(process.succeeded_invoked); ASSERT_FALSE(process.failed_invoked); ASSERT_FALSE(process.aborted_invoked); } TEST(Process, Fail) { fake_process process{}; process.tick(0); process.tick(0); process.fail(); process.tick(0); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_TRUE(process.rejected()); ASSERT_TRUE(process.init_invoked); ASSERT_TRUE(process.update_invoked); ASSERT_FALSE(process.succeeded_invoked); ASSERT_TRUE(process.failed_invoked); ASSERT_FALSE(process.aborted_invoked); } TEST(Process, Data) { fake_process process{}; int value = 0; process.tick({}); process.tick({}, &value); process.succeed(); process.tick({}, &value); ASSERT_FALSE(process.alive()); ASSERT_TRUE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_FALSE(process.rejected()); ASSERT_EQ(value, 1); ASSERT_TRUE(process.init_invoked); ASSERT_TRUE(process.update_invoked); ASSERT_TRUE(process.succeeded_invoked); ASSERT_FALSE(process.failed_invoked); ASSERT_FALSE(process.aborted_invoked); } TEST(Process, AbortNextTick) { fake_process process{}; process.tick(0); process.abort(); process.tick(0); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_TRUE(process.rejected()); ASSERT_TRUE(process.init_invoked); ASSERT_FALSE(process.update_invoked); ASSERT_FALSE(process.succeeded_invoked); ASSERT_FALSE(process.failed_invoked); ASSERT_TRUE(process.aborted_invoked); } TEST(Process, AbortImmediately) { fake_process process{}; process.tick({}); process.abort(true); ASSERT_FALSE(process.alive()); ASSERT_FALSE(process.finished()); ASSERT_FALSE(process.paused()); ASSERT_TRUE(process.rejected()); ASSERT_TRUE(process.init_invoked); ASSERT_FALSE(process.update_invoked); ASSERT_FALSE(process.succeeded_invoked); ASSERT_FALSE(process.failed_invoked); ASSERT_TRUE(process.aborted_invoked); } TEST(ProcessAdaptor, Resolved) { bool updated = false; auto lambda = [&updated](std::uint64_t, void *, auto resolve, auto) { ASSERT_FALSE(updated); updated = true; resolve(); }; auto process = entt::process_adaptor{lambda}; process.tick(0); process.tick(0); ASSERT_TRUE(process.finished()); ASSERT_TRUE(updated); } TEST(ProcessAdaptor, Rejected) { bool updated = false; auto lambda = [&updated](std::uint64_t, void *, auto, auto rejected) { ASSERT_FALSE(updated); updated = true; rejected(); }; auto process = entt::process_adaptor{lambda}; process.tick(0); process.tick(0); ASSERT_TRUE(process.rejected()); ASSERT_TRUE(updated); } TEST(ProcessAdaptor, Data) { int value = 0; auto lambda = [](std::uint64_t, void *data, auto resolve, auto) { *static_cast(data) = 42; resolve(); }; auto process = entt::process_adaptor{lambda}; process.tick(0); process.tick(0, &value); ASSERT_TRUE(process.finished()); ASSERT_EQ(value, 42); }