From 8c22ae32af3eaa905da5769fb46b2d44d3498255 Mon Sep 17 00:00:00 2001 From: Ivan Pinezhaninov Date: Mon, 25 Dec 2023 17:53:12 +0300 Subject: [PATCH] Class method call for all task added --- include/async_promise.hpp | 537 ++++++++++++++++++++++------ tests/CMakeLists.txt | 1 + tests/src/all.cpp | 173 ++++++++- tests/src/any.cpp | 16 +- tests/src/common.h | 5 +- tests/src/constants.h | 25 ++ tests/src/fail.cpp | 18 +- tests/src/finally.cpp | 10 +- tests/src/initial.cpp | 18 +- tests/src/make_promise.cpp | 18 +- tests/src/make_promise_all.cpp | 152 +++++++- tests/src/make_promise_any.cpp | 20 +- tests/src/make_promise_race.cpp | 20 +- tests/src/make_rejected_promise.cpp | 4 +- tests/src/race.cpp | 20 +- tests/src/settled.cpp | 44 +-- tests/src/test_funcs.cpp | 15 +- tests/src/test_struct.cpp | 41 ++- tests/src/test_struct.h | 22 +- tests/src/then.cpp | 28 +- 20 files changed, 908 insertions(+), 279 deletions(-) create mode 100644 tests/src/constants.h diff --git a/include/async_promise.hpp b/include/async_promise.hpp index b8c2261..1449610 100644 --- a/include/async_promise.hpp +++ b/include/async_promise.hpp @@ -237,8 +237,9 @@ auto apply(Func&& func, Tuple&& tuple) -> decltype(apply(std::forward(func template -struct is_invocable : - std::is_constructible, std::reference_wrapper::type>> +struct is_invocable + : std::is_constructible + , std::reference_wrapper::type>> {}; #else @@ -303,6 +304,14 @@ struct promise_helper struct vector_helper { + template + static std::vector create_vector(std::size_t reserve_size) + { + std::vector v; + v.reserve(reserve_size); + return v; + } + template static void reserve(T&, std::size_t) {} @@ -319,7 +328,7 @@ template struct class_method_call_helper { template - T call(Method&& method, Class* obj, Tuple&& tpl) + static T call(Method&& method, Class* obj, Tuple&& tpl) { using tuple_t = typename std::decay::type; using impl_t = impl::value, std::tuple_size::value>; @@ -348,7 +357,7 @@ struct class_method_call_helper template -class initial_class_task final : public task, public class_method_call_helper +class initial_class_task final : public task { public: template @@ -360,7 +369,7 @@ class initial_class_task final : public task, public class_method_call_h Result run() final { - return this->call(m_method, m_obj, m_args); + return class_method_call_helper::call(m_method, m_obj, m_args); } private: @@ -371,7 +380,7 @@ class initial_class_task final : public task, public class_method_call_h template -class initial_class_task final : public task, public class_method_call_helper +class initial_class_task final : public task { public: template @@ -383,7 +392,7 @@ class initial_class_task final : public task void run() final { - this->call(m_method, m_obj, m_args); + class_method_call_helper::call(m_method, m_obj, m_args); } private: @@ -702,29 +711,165 @@ class finally_func_task final : public next_task }; +template class Container, + typename Method, typename Allocator, typename Class> +class all_class_task final : public next_task +{ + public: + all_class_task(task_ptr parent, Container methods, Class* obj) + : next_task{std::move(parent)} + , m_methods{std::move(methods)} + , m_obj{obj} + {} + + Result run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + auto rv = this->m_parent->run(); + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, std::move(method), m_obj, rv)); + + Result result; + vector_helper::reserve(result, m_methods.size()); + for (auto& future : futures) + result.push_back(future.get()); + + return result; + } + + private: + Container m_methods; + Class* const m_obj; +}; + + +template class Container, + typename Method, typename Allocator, typename Class> +class all_class_task final + : public next_task +{ + public: + all_class_task(task_ptr parent, Container methods, Class* obj) + : next_task{std::move(parent)} + , m_methods{std::move(methods)} + , m_obj{obj} + {} + + void run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + auto rv = this->m_parent->run(); + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, std::move(method), m_obj, rv)); + + for (auto& future : futures) + { + try + { + future.get(); + } + catch(...) + { + std::rethrow_exception(std::current_exception()); + } + } + } + + private: + Container m_methods; + Class* const m_obj; +}; + + +template class Container, typename Method, typename Allocator, typename Class> +class all_class_task_void final : public next_task +{ + public: + all_class_task_void(task_ptr parent, Container methods, Class* obj) + : next_task{std::move(parent)} + , m_methods{std::move(methods)} + , m_obj{obj} + {} + + Result run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + this->m_parent->run(); + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, std::move(method), m_obj)); + + Result result; + vector_helper::reserve(result, m_methods.size()); + for (auto& future : futures) + result.push_back(future.get()); + + return result; + } + + private: + Container m_methods; + Class* const m_obj; +}; + + +template class Container, + typename Method, typename Allocator, typename Class> +class all_class_task_void final + : public next_task +{ + public: + all_class_task_void(task_ptr parent, Container methods, Class* obj) + : next_task{std::move(parent)} + , m_methods{std::move(methods)} + , m_obj{obj} + {} + + void run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + this->m_parent->run(); + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, std::move(method), m_obj)); + + for (auto& future : futures) + { + try + { + future.get(); + } + catch(...) + { + std::rethrow_exception(std::current_exception()); + } + } + } + + private: + Container m_methods; + Class* const m_obj; +}; + + template class Container, typename Func, typename Allocator> -class all_task final : public next_task +class all_func_task final : public next_task { public: - all_task(task_ptr parent, Container funcs) + all_func_task(task_ptr parent, Container funcs) : next_task{std::move(parent)} , m_funcs{std::move(funcs)} {} Result run() final { - using value_type = typename Result::value_type; - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); auto rv = this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func), rv)); Result result; vector_helper::reserve(result, m_funcs.size()); - for (auto& future : futures) result.push_back(future.get()); @@ -737,19 +882,17 @@ class all_task final : public next_task template class Container, typename Func, typename Allocator> -class all_task final : public next_task +class all_func_task final : public next_task { public: - all_task(task_ptr parent, Container funcs) + all_func_task(task_ptr parent, Container funcs) : next_task{std::move(parent)} , m_funcs{std::move(funcs)} {} void run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); auto rv = this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func), rv)); @@ -774,27 +917,23 @@ class all_task final : public ne template class Container, typename Func, typename Allocator> -class all_task_void final : public next_task +class all_func_task_void final : public next_task { public: - all_task_void(task_ptr parent, Container funcs) + all_func_task_void(task_ptr parent, Container funcs) : next_task{std::move(parent)} , m_funcs{std::move(funcs)} {} Result run() final { - using value_type = typename Result::value_type; - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); this->m_parent->run(); - for (auto&& func : m_funcs) + for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func))); Result result; vector_helper::reserve(result, m_funcs.size()); - for (auto& future : futures) result.push_back(future.get()); @@ -807,21 +946,19 @@ class all_task_void final : public next_task template class Container, typename Func, typename Allocator> -class all_task_void final : public next_task +class all_func_task_void final : public next_task { public: - all_task_void(task_ptr parent, Container funcs) + all_func_task_void(task_ptr parent, Container funcs) : next_task{std::move(parent)} , m_funcs{std::move(funcs)} {} void run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); this->m_parent->run(); - for (auto&& func : m_funcs) + for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func))); for (auto& future : futures) @@ -854,9 +991,7 @@ class all_settled_task final : public next_task Result run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); auto rv = this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func), rv)); @@ -897,9 +1032,7 @@ class all_settled_task f Result run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); auto rv = this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func), rv)); @@ -940,9 +1073,7 @@ class all_settled_task_void final : public next_task Result run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func))); @@ -983,9 +1114,7 @@ class all_settled_task_void> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); this->m_parent->run(); for (auto func : m_funcs) futures.push_back(std::async(std::launch::async, std::move(func))); @@ -1027,8 +1156,7 @@ class any_task_base : public next_task Result run() final { auto future = m_promise.get_future(); - std::vector> futures; - futures.reserve(m_funcs.size()); + auto futures = vector_helper::create_vector>(m_funcs.size()); m_errors.reserve(m_funcs.size()); async_run(futures); @@ -1080,7 +1208,7 @@ class any_task final : public any_task_basem_parent->run(); using task = any_task; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func), arg))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func), arg)); } void call(Func func, ParentResult arg) @@ -1112,7 +1240,7 @@ class any_task final auto arg = this->m_parent->run(); using task = any_task; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func), arg))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func), arg)); } void call(Func func, ParentResult arg) @@ -1145,7 +1273,7 @@ class any_task_void final : public any_task_basem_parent->run(); using task = any_task_void; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); } void call(Func func) @@ -1177,7 +1305,7 @@ class any_task_void final this->m_parent->run(); using task = any_task_void; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); } void call(Func func) @@ -1208,8 +1336,7 @@ class race_task_base : public next_task Result run() final { auto future = m_promise.get_future(); - std::vector> futures; - futures.reserve(m_funcs.size()); + auto futures = vector_helper::create_vector>(m_funcs.size()); async_run(futures); try @@ -1246,7 +1373,7 @@ class race_task final : public race_task_basem_parent->run(); using task = race_task; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func), arg))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func), arg)); } void call(Func func, ParentResult arg) @@ -1278,7 +1405,7 @@ class race_task final auto arg = this->m_parent->run(); using task = race_task; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func), arg))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func), arg)); } void call(Func func, ParentResult arg) @@ -1311,7 +1438,7 @@ class race_task_void final : public race_task_basem_parent->run(); using task = race_task_void; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); } void call(Func func) @@ -1343,7 +1470,7 @@ class race_task_void final this->m_parent->run(); using task = race_task_void; for (auto func : this->m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); } void call(Func func) @@ -1361,30 +1488,109 @@ class race_task_void final }; +template class Container, + typename Method, typename Allocator, typename Class, typename... Args> +class make_all_class_task final : public task +{ + public: + template + make_all_class_task(Container methods, Class* obj, Args_&&... args) + : m_methods{std::move(methods)} + , m_obj{obj} + , m_args{std::forward(args)...} + {} + + Result run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + using task = make_all_class_task; + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(method))); + + Result result; + vector_helper::reserve(result, m_methods.size()); + for (auto& future : futures) + result.push_back(future.get()); + + return result; + } + + private: + typename Result::value_type call(Method method) const + { + return class_method_call_helper::call(std::move(method), m_obj, m_args); + } + + Container m_methods; + Class* const m_obj; + std::tuple m_args; +}; + + +template class Container, typename Method, + typename Allocator, typename Class, typename... Args> +class make_all_class_task final : public task +{ + public: + template + make_all_class_task(Container methods, Class* obj, Args_&&... args) + : m_methods{std::move(methods)} + , m_obj{obj} + , m_args{std::forward(args)...} + {} + + void run() final + { + auto futures = vector_helper::create_vector>(m_methods.size()); + using task = make_all_class_task; + for (auto method : m_methods) + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(method))); + + for (auto& future : futures) + { + try + { + future.get(); + } + catch(...) + { + std::rethrow_exception(std::current_exception()); + } + } + } + + private: + void call(Method method) const + { + class_method_call_helper::call(std::move(method), m_obj, m_args); + } + + Container m_methods; + Class* const m_obj; + std::tuple m_args; +}; + + template class Container, typename Func, typename Allocator, typename... Args> -class make_all_task final : public task +class make_all_func_task final : public task { public: template - explicit make_all_task(Container funcs, Args_&&... args) + explicit make_all_func_task(Container funcs, Args_&&... args) : m_funcs{std::move(funcs)} , m_args{std::forward(args)...} {} Result run() final { - using value_type = typename Result::value_type; - std::vector> futures; - futures.reserve(m_funcs.size()); - - using task = make_all_task; + auto futures = vector_helper::create_vector>(m_funcs.size()); + using task = make_all_func_task; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); Result result; vector_helper::reserve(result, m_funcs.size()); - for (auto& future : futures) result.push_back(future.get()); @@ -1403,23 +1609,21 @@ class make_all_task final : public task template class Container, typename Func, typename Allocator, typename... Args> -class make_all_task final : public task +class make_all_func_task final : public task { public: template - explicit make_all_task(Container funcs, Args_&&... args) + explicit make_all_func_task(Container funcs, Args_&&... args) : m_funcs{std::move(funcs)} , m_args{std::forward(args)...} {} void run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - - using task = make_all_task; + auto futures = vector_helper::create_vector>(m_funcs.size()); + using task = make_all_func_task; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); for (auto& future : futures) { @@ -1458,12 +1662,10 @@ class make_all_settled_task final : public task Result run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); using task = make_all_settled_task; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); Result result; vector_helper::reserve(result, m_funcs.size()); @@ -1507,12 +1709,10 @@ class make_all_settled_task f Result run() final { - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); using task = make_all_settled_task; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); Result result; vector_helper::reserve(result, m_funcs.size()); @@ -1555,13 +1755,11 @@ class any_task_static_base : public task Result run() final { auto future = m_promise.get_future(); - std::vector> futures; - futures.reserve(m_funcs.size()); + auto futures = vector_helper::create_vector>(m_funcs.size()); m_errors.reserve(m_funcs.size()); - using task = any_task_static_base; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); try { @@ -1666,12 +1864,10 @@ class race_task_static_base : public task Result run() final { auto future = m_promise.get_future(); - std::vector> futures; - futures.reserve(m_funcs.size()); - + auto futures = vector_helper::create_vector>(m_funcs.size()); using task = race_task_static_base; for (auto func : m_funcs) - futures.push_back(std::async(std::launch::async, std::bind(&task::call, this, std::move(func)))); + futures.push_back(std::async(std::launch::async, &task::call, this, std::move(func))); try { @@ -1999,9 +2195,81 @@ class promise } + /** + * @brief Add an iterable of class methods to be called next. + * Return either an iterable of results or the first rejection reason. + * @param methods - Methods that receives the result of the previous function. + * @param obj - Object containing the required methods. + * @return Promise object. + */ + template class Container, typename Func, typename Allocator, typename Class, + typename Arg = T, typename FuncResult = typename std::result_of::type, + typename Result = Container>, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type> + promise all(Container methods, Class* obj) const + { + using task = internal::all_class_task; + return promise{std::make_shared(m_task, std::move(methods), obj)}; + } + + + /** + * @brief Add an iterable of class methods to be called next. + * Return either an iterable of results or the first rejection reason. + * @param methods - Methods that not receives any result of the previous function. + * @param obj - Object containing the required methods. + * @return Promise object. + */ + template class Container, typename Func, typename Allocator, typename Class, + typename FuncResult = typename std::result_of::type, + typename Result = Container>, + typename = typename std::enable_if::value>::type> + promise all(Container methods, Class* obj) const + { + using task = internal::all_class_task_void; + return promise{std::make_shared(m_task, std::move(methods), obj)}; + } + + + /** + * @brief Add an iterable of class methods to be called next. + * Return either an iterable of results or the first rejection reason. + * @param methods - Methods that receives the result of the previous function. + * @param obj - Object containing the required methods. + * @return Promise object. + */ + template class Container, typename Func, typename Allocator, typename Class, + typename Arg = T, typename FuncResult = typename std::result_of::type, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type> + promise all(Container methods, Class* obj) const + { + using task = internal::all_class_task; + return promise{std::make_shared(m_task, std::move(methods), obj)}; + } + + + /** + * @brief Add an iterable of class methods to be called next. + * Return either an iterable of results or the first rejection reason. + * @param methods - Methods that not receives any result of the previous function. + * @param obj - Object containing the required methods. + * @return Promise object. + */ + template class Container, typename Func, typename Allocator, typename Class, + typename FuncResult = typename std::result_of::type, + typename = typename std::enable_if::value>::type> + promise all(Container methods, Class* obj) const + { + using task = internal::all_class_task_void; + return promise{std::make_shared(m_task, std::move(methods), obj)}; + } + + /** * @brief Add an iterable of functions to be called next. - * Return an iterable of results or first rejection reason. + * Return either an iterable of results or the first rejection reason. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2012,14 +2280,14 @@ class promise typename = typename std::enable_if::value>::type> promise all(Container funcs) const { - using task = internal::all_task; + using task = internal::all_func_task; return promise{std::make_shared(m_task, std::move(funcs))}; } /** * @brief Add an iterable of functions to be called next. - * Return an iterable of results or first rejection reason. + * Return either an iterable of results or the first rejection reason. * @param funcs - Functions that not receives any result of the previous function. * @return Promise object. */ @@ -2029,14 +2297,14 @@ class promise typename = typename std::enable_if::value>::type> promise all(Container funcs) const { - using task = internal::all_task_void; + using task = internal::all_func_task_void; return promise{std::make_shared(m_task, std::move(funcs))}; } /** * @brief Add an iterable of functions to be called next. - * Return an iterable of results or first rejection reason. + * Return either an iterable of results or the first rejection reason. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2046,14 +2314,14 @@ class promise typename = typename std::enable_if::value>::type> promise all(Container funcs) const { - using task = internal::all_task; + using task = internal::all_func_task; return promise{std::make_shared(m_task, std::move(funcs))}; } /** * @brief Add an iterable of functions to be called next. - * Return an iterable of results or first rejection reason. + * Return either an iterable of results or the first rejection reason. * @param funcs - Functions that not receives any result of the previous function. * @return Promise object. */ @@ -2062,14 +2330,14 @@ class promise typename = typename std::enable_if::value>::type> promise all(Container funcs) const { - using task = internal::all_task_void; + using task = internal::all_func_task_void; return promise{std::make_shared(m_task, std::move(funcs))}; } /** * @brief Add an iterable of functions to be called next. - * Return an iterable of @ref settled objects with a result or an error. + * Return an iterable of @ref settled objects with either a result or an error. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2087,7 +2355,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return an iterable of @ref settled objects with a result or an error. + * Return an iterable of @ref settled objects with either a result or an error. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2105,7 +2373,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return an iterable of @ref settled objects with a result or an error. + * Return an iterable of @ref settled objects with either a result or an error. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2124,7 +2392,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return an iterable of @ref settled objects with a result or an error. + * Return an iterable of @ref settled objects with either a result or an error. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2141,7 +2409,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return first resolved result. + * Return the first resolved result. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2157,7 +2425,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return first resolved result. + * Return the first resolved result. * @param funcs - Functions that not receives any result of the previous function. * @return Promise object. */ @@ -2172,7 +2440,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return first resolved or rejected result. + * Return either the first resolved or rejected result. * @param funcs - Functions that receives the result of the previous function. * @return Promise object. */ @@ -2188,7 +2456,7 @@ class promise /** * @brief Add an iterable of functions to be called next. - * Return first resolved or rejected result. + * Return either the first resolved or rejected result. * @param funcs - Functions that not receives any result of the previous function. * @return Promise object. */ @@ -2207,7 +2475,7 @@ class promise */ std::future run() const { - return std::async(std::launch::async, std::bind(&promise::run_impl, this, m_task)); + return std::async(std::launch::async, &promise::run_impl, this, m_task); } private: @@ -2238,7 +2506,7 @@ static promise make_promise(Method&& method, Class* obj, Args&&... args) /** * @brief Make a promise object with an initial function. - * @param func - Function or class method for call. + * @param func - Function for call. * @param args - Optional arguments. * @return Promise object. */ @@ -2250,9 +2518,50 @@ static promise make_promise(Func&& func, Args&&... args) } +/** + * @brief Make promise with an iterable of class methods to be called. + * Return promise object with either an iterable of results or the first rejection reason. + * @param methods - Methods. + * @param obj - Object containing the required methods. + * @param args - Optional arguments. + * @return Promise object. + */ +template class Container, typename Method, + typename Allocator, typename Class, typename... Args, + typename FuncResult = typename std::result_of::type, + typename Result = Container>, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type> +static promise make_promise_all(Container methods, Class* obj, Args&&... args) +{ + using task = internal::make_all_class_task; + return promise{std::make_shared(std::move(methods), obj, std::forward(args)...)}; +} + + +/** + * @brief Make promise with an iterable of class methods to be called. + * Return promise object with either a void value or the first rejection reason. + * @param methods - Methods. + * @param obj - Object containing the required methods. + * @param args - Optional arguments. + * @return Promise object. + */ +template class Container, typename Method, + typename Allocator, typename Class, typename... Args, + typename FuncResult = typename std::result_of::type, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type> +static promise make_promise_all(Container methods, Class* obj, Args&&... args) +{ + using task = internal::make_all_class_task; + return promise{std::make_shared(std::move(methods), obj, std::forward(args)...)}; +} + + /** * @brief Make promise with an iterable of functions to be called. - * Return promise object with an iterable of results or first rejection reason. + * Return promise object with either an iterable of results or the first rejection reason. * @param funcs - Functions. * @param args - Function arguments. * @return Promise object. @@ -2263,14 +2572,14 @@ template class Container, typename Func, typename A typename = typename std::enable_if::value>::type> static promise make_promise_all(Container funcs, Args&&... args) { - using task = internal::make_all_task; + using task = internal::make_all_func_task; return promise{std::make_shared(std::move(funcs), std::forward(args)...)}; } /** * @brief Make promise with an iterable of functions to be called. - * Return promise object with void or first rejection reason. + * Return promise object with either a void value or the first rejection reason. * @param funcs - Functions. * @param args - Function arguments. * @return Promise object. @@ -2280,14 +2589,14 @@ template class Container, typename Func, typename A typename = typename std::enable_if::value>::type> static promise make_promise_all(Container funcs, Args&&... args) { - using task = internal::make_all_task; + using task = internal::make_all_func_task; return promise{std::make_shared(std::move(funcs), std::forward(args)...)}; } /** * @brief Make promise with an iterable of functions to be called. - * Return an iterable of @ref settled objects with a result or an error. + * Return an iterable of @ref settled objects with either a result or an error. * @param funcs - Functions. * @param args - Function arguments. * @return Promise object. @@ -2304,7 +2613,7 @@ static promise make_promise_all_settled(Container funcs /** * @brief Make promise with an iterable of functions to be called. - * Return first resolved result. + * Return the first resolved result. * @param funcs - Functions. * @param args - Function arguments. * @return Promise object. @@ -2320,7 +2629,7 @@ static promise make_promise_any(Container funcs, Args&& /** * @brief Make promise with an iterable of functions to be called. - * Return first resolved or rejected result. + * Return either the first resolved or rejected result. * @param funcs - Functions. * @param args - Function arguments. * @return Promise object. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a8cb9b0..d5201fe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,6 +36,7 @@ include(Catch) set(HEADERS src/common.h + src/constants.h src/test_funcs.h src/test_struct.h ) diff --git a/tests/src/all.cpp b/tests/src/all.cpp index 9db6a2e..2a4437c 100644 --- a/tests/src/all.cpp +++ b/tests/src/all.cpp @@ -27,7 +27,162 @@ #include "common.h" -TEST_CASE("All void void", "[all]") +TEST_CASE("All with class method void void", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_void, + &test_struct::void_void, + }; + + auto future = async::make_resolved_promise().all(methods, &test).run(); + + REQUIRE_NOTHROW(future.get()); +} + + +TEST_CASE("All with class method error void void", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_void, + &test_struct::error_void_void, + }; + + auto future = async::make_resolved_promise().all(methods, &test).run(); + + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); +} + + +TEST_CASE("All with class method void string", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_string, + &test_struct::void_string, + }; + + auto future = async::make_resolved_promise(str1).all(methods, &test).run(); + + REQUIRE_NOTHROW(future.get()); +} + + +TEST_CASE("All with class method error void string", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_string, + &test_struct::error_void_string, + }; + + auto future = async::make_resolved_promise(str1).all(methods, &test).run(); + + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); +} + + +TEST_CASE("All with class method string void", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_void1, + &test_struct::string_void2, + }; + + auto future = async::make_resolved_promise().all(methods, &test).run(); + + std::vector res; + REQUIRE_NOTHROW(res = future.get()); + REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector{str1, str2})); +} + + +TEST_CASE("All with class method string void ignore arg", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_void1, + &test_struct::string_void2, + }; + + auto future = async::make_resolved_promise(str1).all(methods, &test).run(); + + std::vector res; + REQUIRE_NOTHROW(res = future.get()); + REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector{str1, str2})); +} + + +TEST_CASE("All with class method error string void", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_void1, + &test_struct::error_string_void, + }; + + auto future = async::make_resolved_promise().all(methods, &test).run(); + + std::vector res; + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); + REQUIRE(res.empty()); +} + + +TEST_CASE("All with class method string string", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_string, + &test_struct::string_string, + }; + + auto future = async::make_resolved_promise(str1).all(methods, &test).run(); + + std::vector res; + REQUIRE_NOTHROW(res = future.get()); + REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector{str1, str1})); +} + + +TEST_CASE("All with class method error string string", "[all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_string, + &test_struct::error_string_string + }; + + auto future = async::make_resolved_promise(str1).all(methods, &test).run(); + + std::vector res; + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); + REQUIRE(res.empty()); +} + + + +TEST_CASE("All with function void void", "[all]") { std::vector funcs { @@ -41,7 +196,7 @@ TEST_CASE("All void void", "[all]") } -TEST_CASE("All error void void", "[all]") +TEST_CASE("All with function error void void", "[all]") { std::vector funcs { @@ -55,7 +210,7 @@ TEST_CASE("All error void void", "[all]") } -TEST_CASE("All void string", "[all]") +TEST_CASE("All with function void string", "[all]") { std::vector funcs { @@ -69,7 +224,7 @@ TEST_CASE("All void string", "[all]") } -TEST_CASE("All error void string", "[all]") +TEST_CASE("All with function error void string", "[all]") { std::vector funcs { @@ -83,7 +238,7 @@ TEST_CASE("All error void string", "[all]") } -TEST_CASE("All string void", "[all]") +TEST_CASE("All with function string void", "[all]") { std::vector funcs { @@ -99,7 +254,7 @@ TEST_CASE("All string void", "[all]") } -TEST_CASE("All string void ignore arg", "[all]") +TEST_CASE("All with function string void ignore arg", "[all]") { std::vector funcs { @@ -115,7 +270,7 @@ TEST_CASE("All string void ignore arg", "[all]") } -TEST_CASE("All error string void", "[all]") +TEST_CASE("All with function error string void", "[all]") { std::vector funcs { @@ -131,7 +286,7 @@ TEST_CASE("All error string void", "[all]") } -TEST_CASE("All string string", "[all]") +TEST_CASE("All with function string string", "[all]") { std::vector funcs { @@ -147,7 +302,7 @@ TEST_CASE("All string string", "[all]") } -TEST_CASE("All error string string", "[all]") +TEST_CASE("All with function error string string", "[all]") { std::vector funcs { diff --git a/tests/src/any.cpp b/tests/src/any.cpp index b8d4024..498fc7d 100644 --- a/tests/src/any.cpp +++ b/tests/src/any.cpp @@ -45,7 +45,7 @@ TEST_CASE("Any error void void", "[any]") { std::vector funcs { - [] () { throw std::runtime_error{str1}; }, + [] () { throw std::runtime_error{str2}; }, [] () {}, }; @@ -87,7 +87,7 @@ TEST_CASE("Any error void string", "[any]") { std::vector funcs { - [] (std::string) { throw std::runtime_error{str1}; }, + [] (std::string) { throw std::runtime_error{str2}; }, [] (std::string) {}, }; @@ -147,15 +147,15 @@ TEST_CASE("Any error string void", "[any]") { std::vector funcs { - [] () -> std::string { throw std::runtime_error{str1}; }, - [] () { return std::string{str2}; }, + [] () -> std::string { throw std::runtime_error{str2}; }, + [] () { return std::string{str1}; }, }; auto future = async::make_resolved_promise().any(funcs).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); - REQUIRE(res == str2); + REQUIRE(res == str1); } @@ -192,15 +192,15 @@ TEST_CASE("Any error string string", "[any]") { std::vector funcs { - [] (std::string str) -> std::string { throw std::runtime_error{str1}; }, - [] (std::string str) { return std::string{str2}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str2}; }, + [] (std::string str) { return std::string{str1}; }, }; auto future = async::make_resolved_promise(str1).any(funcs).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); - REQUIRE(res == str2); + REQUIRE(res == str1); } diff --git a/tests/src/common.h b/tests/src/common.h index 24b56a7..54f499b 100644 --- a/tests/src/common.h +++ b/tests/src/common.h @@ -18,11 +18,8 @@ #ifndef COMMON_H #define COMMON_H +#include "constants.h" #include "test_funcs.h" #include "test_struct.h" -static constexpr auto aggregate_error_message = "All functions rejected"; -static constexpr auto str1 = "Hello World!"; -static constexpr auto str2 = "Hello Universe!"; - #endif // COMMON_H diff --git a/tests/src/constants.h b/tests/src/constants.h new file mode 100644 index 0000000..62646f6 --- /dev/null +++ b/tests/src/constants.h @@ -0,0 +1,25 @@ +/****************************************************************************** +** +** Copyright (C) 2023 Ivan Pinezhaninov +** +** This file is part of the async_promise - which can be found at +** https://github.com/IvanPinezhaninov/async_promise/. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +** OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR +** THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +******************************************************************************/ + +#ifndef CONSTANTS_H +#define CONSTANTS_H + +static constexpr auto aggregate_error_message = "All functions rejected"; +static constexpr auto str1 = "Hello World!"; +static constexpr auto str2 = "Hello Universe!"; + +#endif // CONSTANTS_H diff --git a/tests/src/fail.cpp b/tests/src/fail.cpp index da68bbf..04fc299 100644 --- a/tests/src/fail.cpp +++ b/tests/src/fail.cpp @@ -28,7 +28,7 @@ TEST_CASE("Fail with class method void void", "[fail]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}) + auto future = async::make_rejected_promise(std::runtime_error{str2}) .fail(&test_struct::void_void, &test).run(); REQUIRE_NOTHROW(future.get()); @@ -38,7 +38,7 @@ TEST_CASE("Fail with class method void void", "[fail]") TEST_CASE("Fail with class method void exception", "[fail]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}) + auto future = async::make_rejected_promise(std::runtime_error{str2}) .fail(&test_struct::void_exception, &test).run(); REQUIRE_NOTHROW(future.get()); @@ -48,8 +48,8 @@ TEST_CASE("Fail with class method void exception", "[fail]") TEST_CASE("Fail with class method string void", "[fail]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}) - .fail(&test_struct::string_void, &test).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}) + .fail(&test_struct::string_void1, &test).run(); REQUIRE_NOTHROW(future.get()); } @@ -58,7 +58,7 @@ TEST_CASE("Fail with class method string void", "[fail]") TEST_CASE("Fail with class method string exception", "[fail]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}) + auto future = async::make_rejected_promise(std::runtime_error{str2}) .fail(&test_struct::string_exception, &test).run(); REQUIRE_NOTHROW(future.get()); @@ -67,7 +67,7 @@ TEST_CASE("Fail with class method string exception", "[fail]") TEST_CASE("Fail with func void void", "[fail]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).fail(void_void).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).fail(void_void).run(); REQUIRE_NOTHROW(future.get()); } @@ -75,7 +75,7 @@ TEST_CASE("Fail with func void void", "[fail]") TEST_CASE("Fail with func void exception", "[fail]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).fail(void_exception).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).fail(void_exception).run(); REQUIRE_NOTHROW(future.get()); } @@ -83,7 +83,7 @@ TEST_CASE("Fail with func void exception", "[fail]") TEST_CASE("Fail with func string void", "[fail]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).fail(string_void).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).fail(string_void).run(); REQUIRE_NOTHROW(future.get()); } @@ -91,7 +91,7 @@ TEST_CASE("Fail with func string void", "[fail]") TEST_CASE("Fail with func string exception", "[fail]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).fail(string_exception).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).fail(string_exception).run(); REQUIRE_NOTHROW(future.get()); } diff --git a/tests/src/finally.cpp b/tests/src/finally.cpp index 63b7f4c..0e976f3 100644 --- a/tests/src/finally.cpp +++ b/tests/src/finally.cpp @@ -37,7 +37,7 @@ TEST_CASE("Finally with class method resolve void void", "[finally]") TEST_CASE("Finally with class method resolve string void", "[finally]") { test_struct test; - auto future = async::make_resolved_promise(str1).finally(&test_struct::string_void, &test).run(); + auto future = async::make_resolved_promise(str1).finally(&test_struct::string_void1, &test).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -48,7 +48,7 @@ TEST_CASE("Finally with class method resolve string void", "[finally]") TEST_CASE("Finally with class method reject void void", "[finally]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}).finally(&test_struct::void_void, &test).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).finally(&test_struct::void_void, &test).run(); REQUIRE_NOTHROW(future.get()); } @@ -57,7 +57,7 @@ TEST_CASE("Finally with class method reject void void", "[finally]") TEST_CASE("Finally with class method reject string void", "[finally]") { test_struct test; - auto future = async::make_rejected_promise(std::runtime_error{str1}).finally(&test_struct::string_void, &test).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).finally(&test_struct::string_void1, &test).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -85,7 +85,7 @@ TEST_CASE("Finally with func resolve string void", "[finally]") TEST_CASE("Finally with func reject void void", "[finally]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).finally(void_void).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).finally(void_void).run(); REQUIRE_NOTHROW(future.get()); } @@ -93,7 +93,7 @@ TEST_CASE("Finally with func reject void void", "[finally]") TEST_CASE("Finally with func reject string void", "[finally]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).finally(string_void).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).finally(string_void).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); diff --git a/tests/src/initial.cpp b/tests/src/initial.cpp index 8d9065e..b5e25e7 100644 --- a/tests/src/initial.cpp +++ b/tests/src/initial.cpp @@ -40,7 +40,7 @@ TEST_CASE("Initial with class method error void void", "[initial]") test_struct test; auto future = async::promise{&test_struct::error_void_void, &test}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -58,14 +58,14 @@ TEST_CASE("Initial with class method error void string", "[initial]") test_struct test; auto future = async::promise{&test_struct::error_void_string, &test, str1}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Initial with class method string void", "[initial]") { test_struct test; - auto future = async::promise{&test_struct::string_void, &test}.run(); + auto future = async::promise{&test_struct::string_void1, &test}.run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -78,7 +78,7 @@ TEST_CASE("Initial with class method error string void", "[initial]") test_struct test; auto future = async::promise{&test_struct::error_string_void, &test}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -98,7 +98,7 @@ TEST_CASE("Initial with class method error string string", "[initial]") test_struct test; auto future = async::promise{&test_struct::error_string_string, &test, str1}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -114,7 +114,7 @@ TEST_CASE("Initial with func error void void", "[initial]") { auto future = async::promise{error_void_void}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -130,7 +130,7 @@ TEST_CASE("Initial with func error void string", "[initial]") { auto future = async::promise{error_void_string, str1}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -148,7 +148,7 @@ TEST_CASE("Initial with func error string void", "[initial]") { auto future = async::promise{error_string_void}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -166,6 +166,6 @@ TEST_CASE("Initial with func error string string", "[initial]") { auto future = async::promise{error_string_string, str1}.run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } diff --git a/tests/src/make_promise.cpp b/tests/src/make_promise.cpp index 1d94364..7a1019d 100644 --- a/tests/src/make_promise.cpp +++ b/tests/src/make_promise.cpp @@ -40,7 +40,7 @@ TEST_CASE("Make promise with class method error void void", "[make promise]") test_struct test; auto future = async::make_promise(&test_struct::error_void_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -58,14 +58,14 @@ TEST_CASE("Make promise with class method error void string", "[make promise]") test_struct test; auto future = async::make_promise(&test_struct::error_void_string, &test, str1).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Make promise with class method string void", "[make promise]") { test_struct test; - auto future = async::make_promise(&test_struct::string_void, &test).run(); + auto future = async::make_promise(&test_struct::string_void1, &test).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -78,7 +78,7 @@ TEST_CASE("Make promise with class method error string void", "[make promise]") test_struct test; auto future = async::make_promise(&test_struct::error_string_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -98,7 +98,7 @@ TEST_CASE("Make promise with class method error string string", "[make promise]" test_struct test; auto future = async::make_promise(&test_struct::error_string_string, &test, str1).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -114,7 +114,7 @@ TEST_CASE("Make promise with func error void void", "[make promise]") { auto future = async::make_promise(error_void_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -130,7 +130,7 @@ TEST_CASE("Make promise with func error void string", "[make promise]") { auto future = async::make_promise(error_void_string, str1).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -148,7 +148,7 @@ TEST_CASE("Make promise with func error string void", "[make promise]") { auto future = async::make_promise(error_string_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -166,5 +166,5 @@ TEST_CASE("Make promise with func error string string", "[make promise]") { auto future = async::make_promise(error_string_string, str1).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } diff --git a/tests/src/make_promise_all.cpp b/tests/src/make_promise_all.cpp index d2d0b67..0cad792 100644 --- a/tests/src/make_promise_all.cpp +++ b/tests/src/make_promise_all.cpp @@ -27,7 +27,143 @@ #include "common.h" -TEST_CASE("Make all void void", "[make promise all]") +TEST_CASE("Make all with class methods void void", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_void, + &test_struct::void_void, + }; + + auto future = async::make_promise_all(methods, &test).run(); + + REQUIRE_NOTHROW(future.get()); +} + + +TEST_CASE("Make all with class methods error void void", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_void, + &test_struct::error_void_void, + }; + + auto future = async::make_promise_all(methods, &test).run(); + + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); +} + + +TEST_CASE("Make all with class methods void string", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_string, + &test_struct::void_string, + }; + + auto future = async::make_promise_all(methods, &test, str1).run(); + + REQUIRE_NOTHROW(future.get()); +} + + +TEST_CASE("Make all with class methods error void string", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::void_string, + &test_struct::error_void_string, + }; + + auto future = async::make_promise_all(methods, &test, str1).run(); + + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); +} + + +TEST_CASE("Make all with class methods string void", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_void1, + &test_struct::string_void2, + }; + + auto future = async::make_promise_all(methods, &test).run(); + + std::vector res; + REQUIRE_NOTHROW(res = future.get()); + REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector{str1, str2})); +} + + +TEST_CASE("Make all with class methods error string void", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_void1, + &test_struct::error_string_void, + }; + + auto future = async::make_promise_all(methods, &test).run(); + + std::vector res; + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); + REQUIRE(res.empty()); +} + + +TEST_CASE("Make all with class methods string string", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_string, + &test_struct::string_string, + }; + + auto future = async::make_promise_all(methods, &test, str1).run(); + + std::vector res; + REQUIRE_NOTHROW(res = future.get()); + REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector{str1, str1})); +} + + +TEST_CASE("Make all with class methods error string string", "[make promise all]") +{ + test_struct test; + + std::vector methods + { + &test_struct::string_string, + &test_struct::error_string_string + }; + + auto future = async::make_promise_all(methods, &test, str1).run(); + + std::vector res; + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); + REQUIRE(res.empty()); +} + + +TEST_CASE("Make all with funcs void void", "[make promise all]") { std::vector funcs { @@ -41,7 +177,7 @@ TEST_CASE("Make all void void", "[make promise all]") } -TEST_CASE("Make all error void void", "[make promise all]") +TEST_CASE("Make all with funcs error void void", "[make promise all]") { std::vector funcs { @@ -55,7 +191,7 @@ TEST_CASE("Make all error void void", "[make promise all]") } -TEST_CASE("Make all void string", "[make promise all]") +TEST_CASE("Make all with funcs void string", "[make promise all]") { std::vector funcs { @@ -69,7 +205,7 @@ TEST_CASE("Make all void string", "[make promise all]") } -TEST_CASE("Make all error void string", "[make promise all]") +TEST_CASE("Make all with funcs error void string", "[make promise all]") { std::vector funcs { @@ -83,7 +219,7 @@ TEST_CASE("Make all error void string", "[make promise all]") } -TEST_CASE("Make all string void", "[make promise all]") +TEST_CASE("Make all with funcs string void", "[make promise all]") { std::vector funcs { @@ -99,7 +235,7 @@ TEST_CASE("Make all string void", "[make promise all]") } -TEST_CASE("Make all error string void", "[make promise all]") +TEST_CASE("Make all with funcs error string void", "[make promise all]") { std::vector funcs { @@ -115,7 +251,7 @@ TEST_CASE("Make all error string void", "[make promise all]") } -TEST_CASE("Make all string string", "[make promise all]") +TEST_CASE("Make all with funcs string string", "[make promise all]") { std::vector funcs { @@ -131,7 +267,7 @@ TEST_CASE("Make all string string", "[make promise all]") } -TEST_CASE("Make all error string string", "[make promise all]") +TEST_CASE("Make all with funcs error string string", "[make promise all]") { std::vector funcs { diff --git a/tests/src/make_promise_any.cpp b/tests/src/make_promise_any.cpp index 860bd2b..ee3834b 100644 --- a/tests/src/make_promise_any.cpp +++ b/tests/src/make_promise_any.cpp @@ -47,7 +47,7 @@ TEST_CASE("Make any error void void", "[make promise any]") { std::vector funcs { - [] () { throw std::runtime_error{str1}; }, + [] () { throw std::runtime_error{str2}; }, [] () {}, }; @@ -89,7 +89,7 @@ TEST_CASE("Make any error void string", "[make promise any]") { std::vector funcs { - [] (std::string) { throw std::runtime_error{str1}; }, + [] (std::string) { throw std::runtime_error{str2}; }, [] (std::string) {}, }; @@ -133,15 +133,15 @@ TEST_CASE("Make any error string void", "[make promise any]") { std::vector funcs { - [] () -> std::string { throw std::runtime_error{str1}; }, - [] () { return std::string{str2}; }, + [] () -> std::string { throw std::runtime_error{str2}; }, + [] () { return std::string{str1}; }, }; auto future = async::make_promise_any(funcs).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); - REQUIRE(res == str2); + REQUIRE(res == str1); } @@ -178,15 +178,15 @@ TEST_CASE("Make any error string string", "[make promise any]") { std::vector funcs { - [] (std::string str) -> std::string { throw std::runtime_error{str}; }, - [] (std::string str) { return std::string{str2}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str2}; }, + [] (std::string str) { return std::string{str1}; }, }; auto future = async::make_promise_any(funcs, str1).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); - REQUIRE(res == str2); + REQUIRE(res == str1); } @@ -194,8 +194,8 @@ TEST_CASE("Make any all error string string", "[make promise any]") { std::vector funcs { - [] (std::string str) -> std::string { throw std::runtime_error{str}; }, - [] (std::string str) -> std::string { throw std::runtime_error{str}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str1}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str2}; }, }; auto future = async::make_promise_any(funcs, str1).run(); diff --git a/tests/src/make_promise_race.cpp b/tests/src/make_promise_race.cpp index 5528acb..75f02c7 100644 --- a/tests/src/make_promise_race.cpp +++ b/tests/src/make_promise_race.cpp @@ -49,13 +49,13 @@ TEST_CASE("Make race error won void void", "[make promise race]") { std::vector void_void { - [] () { throw std::runtime_error{str1}; }, + [] () { throw std::runtime_error{str2}; }, [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); }, }; auto future = async::make_promise_race(void_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -91,13 +91,13 @@ TEST_CASE("Make race error won void string", "[make promise race]") { std::vector void_str { - [] (std::string) { throw std::runtime_error{str1}; }, + [] (std::string) { throw std::runtime_error{str2}; }, [] (std::string) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); }, }; auto future = async::make_promise_race(void_str, str1).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -135,14 +135,14 @@ TEST_CASE("Make race error won string void", "[make promise race]") { std::vector str_void { - [] () -> std::string { throw std::runtime_error{str1}; }, - [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str2}; }, + [] () -> std::string { throw std::runtime_error{str2}; }, + [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str1}; }, }; auto future = async::make_promise_race(str_void).run(); std::string res; - REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); REQUIRE(res.empty()); } @@ -183,14 +183,14 @@ TEST_CASE("Make race error won string string", "[make promise race]") { std::vector str_str { - [] (std::string str) -> std::string { throw std::runtime_error{str1}; }, - [] (std::string str) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str2}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str2}; }, + [] (std::string str) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str1}; }, }; auto future = async::make_promise_race(str_str, str1).run(); std::string res; - REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); REQUIRE(res.empty()); } diff --git a/tests/src/make_rejected_promise.cpp b/tests/src/make_rejected_promise.cpp index 041fe1b..42af2c7 100644 --- a/tests/src/make_rejected_promise.cpp +++ b/tests/src/make_rejected_promise.cpp @@ -28,7 +28,7 @@ TEST_CASE("Make rejected", "[make rejected promise]") { - auto future = async::make_rejected_promise(std::runtime_error{str1}).run(); + auto future = async::make_rejected_promise(std::runtime_error{str2}).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } diff --git a/tests/src/race.cpp b/tests/src/race.cpp index f8b6ebb..9ca7549 100644 --- a/tests/src/race.cpp +++ b/tests/src/race.cpp @@ -49,13 +49,13 @@ TEST_CASE("Race error won void void", "[race]") { std::vector void_void { - [] () { throw std::runtime_error{str1}; }, + [] () { throw std::runtime_error{str2}; }, [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); }, }; auto future = async::make_resolved_promise().race(void_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -91,13 +91,13 @@ TEST_CASE("Race error won void string", "[race]") { std::vector void_str { - [] (std::string) { throw std::runtime_error{str1}; }, + [] (std::string) { throw std::runtime_error{str2}; }, [] (std::string) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); }, }; auto future = async::make_resolved_promise(str1).race(void_str).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -151,14 +151,14 @@ TEST_CASE("Race error won string void", "[race]") { std::vector str_void { - [] () -> std::string { throw std::runtime_error{str1}; }, - [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str2}; }, + [] () -> std::string { throw std::runtime_error{str2}; }, + [] () { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str1}; }, }; auto future = async::make_resolved_promise().race(str_void).run(); std::string res; - REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); REQUIRE(res.empty()); } @@ -199,14 +199,14 @@ TEST_CASE("Race error won string string", "[race]") { std::vector str_str { - [] (std::string str) -> std::string { throw std::runtime_error{str1}; }, - [] (std::string str) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str2}; }, + [] (std::string str) -> std::string { throw std::runtime_error{str2}; }, + [] (std::string str) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return std::string{str1}; }, }; auto future = async::make_resolved_promise(str1).race(str_str).run(); std::string res; - REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2)); REQUIRE(res.empty()); } diff --git a/tests/src/settled.cpp b/tests/src/settled.cpp index bc05b95..1918737 100644 --- a/tests/src/settled.cpp +++ b/tests/src/settled.cpp @@ -88,33 +88,33 @@ TEST_CASE("Resolved settled object move operator", "[settled]") TEST_CASE("Rejected settled object copy constructor", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); auto obj2 = obj1; REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Rejected settled object move constructor", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); auto obj2 = std::move(obj1); REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Rejected settled object copy operator", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); async::settled obj2{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj2.type == async::settle_type::rejected); @@ -128,9 +128,9 @@ TEST_CASE("Rejected settled object copy operator", "[settled]") TEST_CASE("Rejected settled object move operator", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); async::settled obj2{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj2.type == async::settle_type::rejected); @@ -138,7 +138,7 @@ TEST_CASE("Rejected settled object move operator", "[settled]") obj2 = std::move(obj1); REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -212,33 +212,33 @@ TEST_CASE("Resolved settled void object move operator", "[settled]") TEST_CASE("Rejected settled void object copy constructor", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); auto obj2 = obj1; REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Rejected settled void object move constructor", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); auto obj2 = std::move(obj1); REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Rejected settled void object copy operator", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); async::settled obj2{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj2.type == async::settle_type::rejected); @@ -252,9 +252,9 @@ TEST_CASE("Rejected settled void object copy operator", "[settled]") TEST_CASE("Rejected settled void object move operator", "[settled]") { - async::settled obj1{std::make_exception_ptr(std::runtime_error{str1})}; + async::settled obj1{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj1.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj1.error), std::runtime_error, Catch::Matchers::Message(str2)); async::settled obj2{std::make_exception_ptr(std::runtime_error{str2})}; REQUIRE(obj2.type == async::settle_type::rejected); @@ -262,7 +262,7 @@ TEST_CASE("Rejected settled void object move operator", "[settled]") obj2 = std::move(obj1); REQUIRE(obj2.type == async::settle_type::rejected); - REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(std::rethrow_exception(obj2.error), std::runtime_error, Catch::Matchers::Message(str2)); } diff --git a/tests/src/test_funcs.cpp b/tests/src/test_funcs.cpp index 8fb2306..ee0a7a6 100644 --- a/tests/src/test_funcs.cpp +++ b/tests/src/test_funcs.cpp @@ -2,10 +2,9 @@ #include // local +#include "constants.h" #include "test_funcs.h" -static constexpr auto str = "Hello World!"; - void void_void() {} @@ -13,7 +12,7 @@ void void_void() void error_void_void() { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } @@ -23,7 +22,7 @@ void void_string(std::string str) void error_void_string(std::string str) { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } @@ -33,13 +32,13 @@ void void_exception(std::exception_ptr e) std::string string_void() { - return str; + return str1; } std::string error_string_void() { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } @@ -51,7 +50,7 @@ std::string string_string(std::string str) std::string error_string_string(std::string str) { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } @@ -67,6 +66,6 @@ std::string string_exception(std::exception_ptr e) } catch(...) { - return str; + return str1; } } diff --git a/tests/src/test_struct.cpp b/tests/src/test_struct.cpp index 64b9038..7047651 100644 --- a/tests/src/test_struct.cpp +++ b/tests/src/test_struct.cpp @@ -2,60 +2,65 @@ #include // local +#include "constants.h" #include "test_struct.h" -static constexpr auto str = "Hello World!"; - -void test_struct::void_void() +void test_struct::void_void() const {} -void test_struct::error_void_void() +void test_struct::error_void_void() const { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } -void test_struct::void_string(std::string str) +void test_struct::void_string(std::string str) const {} -void test_struct::error_void_string(std::string str) +void test_struct::error_void_string(std::string str) const { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } -void test_struct::void_exception(std::exception_ptr e) +void test_struct::void_exception(std::exception_ptr e) const {} -std::string test_struct::string_void() +std::string test_struct::string_void1() const { - return str; + return str1; +} + + +std::string test_struct::string_void2() const +{ + return str2; } -std::string test_struct::error_string_void() +std::string test_struct::error_string_void() const { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } -std::string test_struct::string_string(std::string str) +std::string test_struct::string_string(std::string str) const { return str; } -std::string test_struct::error_string_string(std::string str) +std::string test_struct::error_string_string(std::string str) const { - throw std::runtime_error{str}; + throw std::runtime_error{str2}; } -std::string test_struct::string_exception(std::exception_ptr e) +std::string test_struct::string_exception(std::exception_ptr e) const { try { @@ -67,6 +72,6 @@ std::string test_struct::string_exception(std::exception_ptr e) } catch(...) { - return str; + return str1; } } diff --git a/tests/src/test_struct.h b/tests/src/test_struct.h index 3a3833d..7145050 100644 --- a/tests/src/test_struct.h +++ b/tests/src/test_struct.h @@ -24,25 +24,27 @@ struct test_struct final { - void void_void(); + void void_void() const; - void error_void_void(); + void error_void_void() const; - void void_string(std::string str); + void void_string(std::string str) const; - void error_void_string(std::string str); + void error_void_string(std::string str) const; - void void_exception(std::exception_ptr e); + void void_exception(std::exception_ptr e) const; - std::string string_void(); + std::string string_void1() const; - std::string error_string_void(); + std::string string_void2() const; - std::string string_string(std::string str); + std::string error_string_void() const; - std::string error_string_string(std::string str); + std::string string_string(std::string str) const; - std::string string_exception(std::exception_ptr e); + std::string error_string_string(std::string str) const; + + std::string string_exception(std::exception_ptr e) const; }; #endif // TEST_STRUCT_H diff --git a/tests/src/then.cpp b/tests/src/then.cpp index 5ed5f20..eca9e48 100644 --- a/tests/src/then.cpp +++ b/tests/src/then.cpp @@ -40,7 +40,7 @@ TEST_CASE("Then with class method error void void", "[then]") test_struct test; auto future = async::make_resolved_promise().then(&test_struct::error_void_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -58,7 +58,7 @@ TEST_CASE("Then with class method error void void ignore arg", "[then]") test_struct test; auto future = async::make_resolved_promise(str1).then(&test_struct::error_void_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -76,14 +76,14 @@ TEST_CASE("Then with class method error void string", "[then]") test_struct test; auto future = async::make_resolved_promise(str1).then(&test_struct::error_void_string, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Then with class method string void", "[then]") { test_struct test; - auto future = async::make_resolved_promise().then(&test_struct::string_void, &test).run(); + auto future = async::make_resolved_promise().then(&test_struct::string_void1, &test).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -96,14 +96,14 @@ TEST_CASE("Then with class method error string void", "[then]") test_struct test; auto future = async::make_resolved_promise().then(&test_struct::error_string_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } TEST_CASE("Then with class method string void ignore arg", "[then]") { test_struct test; - auto future = async::make_resolved_promise(str1).then(&test_struct::string_void, &test).run(); + auto future = async::make_resolved_promise(str1).then(&test_struct::string_void1, &test).run(); std::string res; REQUIRE_NOTHROW(res = future.get()); @@ -116,7 +116,7 @@ TEST_CASE("Then with class method error string void ignore arg", "[then]") test_struct test; auto future = async::make_resolved_promise(str1).then(&test_struct::error_string_void, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -136,7 +136,7 @@ TEST_CASE("Then with class method error string string", "[then]") test_struct test; auto future = async::make_resolved_promise(str1).then(&test_struct::error_string_string, &test).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -152,7 +152,7 @@ TEST_CASE("Then with func error void void", "[then]") { auto future = async::make_resolved_promise().then(error_void_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -168,7 +168,7 @@ TEST_CASE("Then with func error void void ignore arg", "[then]") { auto future = async::make_resolved_promise(str1).then(error_void_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -184,7 +184,7 @@ TEST_CASE("Then with func error void string", "[then]") { auto future = async::make_resolved_promise(str1).then(error_void_string).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -202,7 +202,7 @@ TEST_CASE("Then with func error string void", "[then]") { auto future = async::make_resolved_promise().then(error_string_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -220,7 +220,7 @@ TEST_CASE("Then with func error string void ignore arg", "[then]") { auto future = async::make_resolved_promise(str1).then(error_string_void).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); } @@ -238,5 +238,5 @@ TEST_CASE("Then with func error string string", "[then]") { auto future = async::make_resolved_promise(str1).then(error_string_string).run(); - REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str1)); + REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2)); }