Last active
November 7, 2017 02:21
-
-
Save yayj/8aa00e55bc85d030c7205b54112c14e7 to your computer and use it in GitHub Desktop.
test cases for ASIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <asio/io_service.hpp> | |
#include <asio/spawn.hpp> | |
#include <asio/strand.hpp> | |
#include <asio/system_executor.hpp> | |
#include <iostream> | |
#include <type_traits> | |
using namespace std; | |
static asio::io_service io; | |
static asio::system_executor executor; | |
using DefaultParam = asio::detail::default_param; | |
#ifdef BOOST_COROUTINES_V2 | |
static auto const default_size = DefaultParam::traits_type::default_size(); | |
class GenericParam : public DefaultParam { | |
public: | |
GenericParam(size_t size = default_size) : DefaultParam{size} {} | |
GenericParam(GenericParam const&) = default; | |
GenericParam(GenericParam&&) = default; | |
}; | |
class CopyOnlyParam : public DefaultParam { | |
public: | |
CopyOnlyParam(size_t size = default_size) : DefaultParam{size} {} | |
CopyOnlyParam(CopyOnlyParam const&) = default; | |
CopyOnlyParam(CopyOnlyParam&&) = delete; | |
}; | |
static void verify() | |
{ | |
auto function = [](auto) {}; | |
auto handler = []() {}; | |
GenericParam gp{}; | |
CopyOnlyParam cp{}; | |
asio::spawn(function); | |
asio::spawn(function, gp); | |
asio::spawn(function, move(gp)); | |
asio::spawn(function, cp); | |
asio::spawn(handler, function); | |
asio::spawn(handler, function, gp); | |
asio::spawn(handler, function, move(gp)); | |
asio::spawn(handler, function, cp); | |
asio::spawn(io, [=](auto yield) { | |
asio::spawn(yield, function); | |
asio::spawn(yield, function, gp); | |
asio::spawn(yield, function, move(gp)); | |
asio::spawn(yield, function, cp); | |
}); | |
asio::spawn(executor, function); | |
asio::spawn(executor, function, gp); | |
asio::spawn(executor, function, move(gp)); | |
asio::spawn(executor, function, cp); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function, gp); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function, move(gp)); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function, cp); | |
asio::spawn(asio::io_service::strand{io}, function); | |
asio::spawn(asio::io_service::strand{io}, function, gp); | |
asio::spawn(asio::io_service::strand{io}, function, move(gp)); | |
asio::spawn(asio::io_service::strand{io}, function, cp); | |
asio::spawn(io, function); | |
asio::spawn(io, function, gp); | |
asio::spawn(io, function, move(gp)); | |
asio::spawn(io, function, cp); | |
} | |
#else | |
static void verify() | |
{ | |
auto function = [](auto) {}; | |
auto handler = []() {}; | |
auto param = DefaultParam{}; | |
asio::spawn(function); | |
asio::spawn(function, param); | |
asio::spawn(function, move(param)); | |
asio::spawn(handler, function); | |
asio::spawn(handler, function, param); | |
asio::spawn(handler, function, move(param)); | |
asio::spawn(io, [=](auto yield) { | |
asio::spawn(yield, function); | |
asio::spawn(yield, function, param); | |
asio::spawn(yield, function, move(param)); | |
}); | |
asio::spawn(executor, function); | |
asio::spawn(executor, function, param); | |
asio::spawn(executor, function, move(param)); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function, param); | |
asio::spawn(asio::strand<decltype(executor)>{executor}, function, move(param)); | |
asio::spawn(asio::io_service::strand{io}, function); | |
asio::spawn(asio::io_service::strand{io}, function, param); | |
asio::spawn(asio::io_service::strand{io}, function, move(param)); | |
asio::spawn(io, function); | |
asio::spawn(io, function, param); | |
asio::spawn(io, function, move(param)); | |
} | |
#endif | |
int main(int argc, char const* argv[]) | |
{ | |
verify(); | |
io.run(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <asio/io_service.hpp> | |
#include <asio/spawn.hpp> | |
#include <memory> | |
using namespace std; | |
static void handlerFunction() {} | |
struct HandlerFunctor { | |
void operator()() {} | |
}; | |
static void copyableHandler() | |
{ | |
auto function = [](auto) {}; | |
{ | |
// raw function | |
asio::spawn(handlerFunction, function); | |
asio::spawn(&handlerFunction, function); | |
} | |
{ | |
// lambda | |
auto handler = []() {}; | |
asio::spawn(handler, function); // compilation error | |
asio::spawn([]() {}, function); | |
} | |
{ | |
// functor | |
auto handler = HandlerFunctor{}; | |
asio::spawn(handler, function); // compilation error | |
asio::spawn(HandlerFunctor{}, function); | |
} | |
} | |
static void temporaryFunction() | |
{ | |
asio::io_service io; | |
asio::spawn(io, *make_unique<void(*)(asio::yield_context)>([](auto) {})); | |
io.run(); // runtime error | |
} | |
int main(int argc, char const* argv[]) | |
{ | |
copyableHandler(); | |
temporaryFunction(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment