#undef NDEBUG #include"Ev/map.hpp" #include"Ev/start.hpp" #include"Ev/yield.hpp" #include"Util/make_unique.hpp" #include #include #include #include int main() { auto flag = false; /* Functions. */ auto pi_to_pd = [](std::unique_ptr pi) { auto ret = Util::make_unique(*pi); return Ev::lift(std::move(ret)); }; auto fail_if_odd = [](int i) { if (i % 2) throw std::runtime_error("already odd"); return Ev::lift(i + 1); }; auto set_flag_if_called = [&flag](int i) { flag = true; return Ev::lift(i); }; auto is_odd = [](int i) { if (i % 2) return Ev::lift(true); return Ev::lift(false); }; auto code = Ev::lift().then([&]() { /* Check move-only requirement. */ auto vec = std::vector>(); vec.push_back(Util::make_unique(0)); vec.push_back(Util::make_unique(1)); vec.push_back(Util::make_unique(2)); return Ev::map(pi_to_pd, std::move(vec)); }).then([&](std::vector> ovec) { assert(ovec.size() == 3); assert(*ovec[0] == 0.0); assert(*ovec[1] == 1.0); assert(*ovec[2] == 2.0); /* Check exception handling. */ flag = false; auto vec = std::vector(); vec.push_back(0); vec.push_back(1); vec.push_back(2); vec.push_back(3); return Ev::map(fail_if_odd, std::move(vec) ).catching< std::runtime_error >([&](std::runtime_error const& _) { flag = true; /* Return dummy vector. */ return Ev::lift(std::vector()); }); }).then([&](std::vector ovec) { assert(flag); /* Check empty vector. */ flag = false; auto vec = std::vector(); return Ev::map(set_flag_if_called, std::move(vec)); }).then([&](std::vector ovec) { assert(ovec.size() == 0); /* Nothing to process, so it should not have been called. */ assert(!flag); /* Check vector of boolean. */ auto vec = std::vector(); vec.push_back(0); vec.push_back(1); vec.push_back(43); vec.push_back(42); return Ev::map(is_odd, std::move(vec)); }).then([&](std::vector ovec) { assert(ovec.size() == 4); assert(ovec[0] == false); assert(ovec[1] == true); assert(ovec[2] == true); assert(ovec[3] == false); return Ev::lift(0); }); return Ev::start(code); }