bonuses: jobber

This commit is contained in:
2018-12-12 20:07:41 +07:00
parent 05561ad511
commit be72e90532
2 changed files with 634 additions and 4 deletions

266
tests.cpp
View File

@@ -7,12 +7,15 @@
#define CATCH_CONFIG_FAST_COMPILE
#include "catch.hpp"
#include "promise.hpp"
namespace pr = promise_hpp;
#include <thread>
#include <cmath>
#include <numeric>
#include <cstring>
#include "jobber.hpp"
#include "promise.hpp"
namespace jb = jobber_hpp;
namespace pr = promise_hpp;
namespace
{
struct obj_t {
@@ -977,3 +980,258 @@ TEST_CASE("get_and_wait") {
}
}
}
TEST_CASE("jobber") {
{
jb::jobber j(1);
auto pv0 = j.async([](){
throw std::exception();
});
REQUIRE_THROWS_AS(pv0.get(), std::exception);
}
{
int v5 = 5;
jb::jobber j(1);
auto pv0 = j.async([](int v){
REQUIRE(v == 5);
throw std::exception();
}, v5);
REQUIRE_THROWS_AS(pv0.get(), std::exception);
auto pv1 = j.async([](int& v){
REQUIRE(v == 5);
return v != 5
? 0
: throw std::exception();
}, std::ref(v5));
REQUIRE_THROWS_AS(pv1.get(), std::exception);
auto pv3 = j.async([](int& v){
v = 4;
return v;
}, std::ref(v5));
REQUIRE(pv3.get() == v5);
REQUIRE(v5 == 4);
}
{
jb::jobber j(1);
auto p0 = j.async([](float angle){
return std::sin(angle);
}, M_PI);
auto p1 = j.async([](float angle){
return std::cos(angle);
}, M_PI * 2);
REQUIRE(p0.get() == Approx(0.f).margin(0.01f));
REQUIRE(p1.get() == Approx(1.f).margin(0.01f));
}
{
jb::jobber j(1);
j.pause();
jb::jobber_priority max_priority = jb::jobber_priority::highest;
j.async([](){
std::this_thread::sleep_for(std::chrono::milliseconds(2));
});
for ( std::size_t i = 0; i < 10; ++i ) {
jb::jobber_priority p = static_cast<jb::jobber_priority>(
i % static_cast<std::size_t>(jb::jobber_priority::highest));
j.async(p, [&max_priority](jb::jobber_priority priority) {
REQUIRE(priority <= max_priority);
max_priority = priority;
}, p);
}
j.resume();
j.wait_all();
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 10; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
j.resume();
REQUIRE(counter < 10);
j.wait_all();
REQUIRE(counter == 10);
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 10; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
REQUIRE(counter < 10);
j.active_wait_all();
REQUIRE(counter == 10);
}
{
jb::jobber j(1);
const auto time_now = [](){
return std::chrono::high_resolution_clock::now();
};
REQUIRE(jb::jobber_wait_status::no_timeout == j.wait_all_for(std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::no_timeout == j.wait_all_until(time_now() + std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::no_timeout == j.active_wait_all_for(std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::no_timeout == j.active_wait_all_until(time_now() + std::chrono::milliseconds(-1)));
j.pause();
j.async([]{});
REQUIRE(jb::jobber_wait_status::timeout == j.wait_all_for(std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::timeout == j.wait_all_until(time_now() + std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::timeout == j.active_wait_all_for(std::chrono::milliseconds(-1)));
REQUIRE(jb::jobber_wait_status::timeout == j.active_wait_all_until(time_now() + std::chrono::milliseconds(-1)));
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 10; ++i ) {
j.async([&counter](){
++counter;
});
}
const auto time_now = [](){
return std::chrono::high_resolution_clock::now();
};
j.wait_all_for(std::chrono::milliseconds(10));
j.wait_all_until(time_now() + std::chrono::milliseconds(10));
REQUIRE(counter == 0);
j.active_wait_all_for(std::chrono::milliseconds(10));
j.active_wait_all_until(time_now() + std::chrono::milliseconds(10));
REQUIRE(counter > 0);
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 50; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
const auto time_now = [](){
return std::chrono::high_resolution_clock::now();
};
const auto b = time_now();
j.resume();
j.wait_all_for(std::chrono::milliseconds(100));
REQUIRE(time_now() - b > std::chrono::milliseconds(50));
REQUIRE(counter > 2);
REQUIRE(counter < 50);
j.wait_all_until(time_now() + std::chrono::seconds(3));
REQUIRE(counter == 50);
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 50; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
const auto time_now = [](){
return std::chrono::high_resolution_clock::now();
};
const auto b = time_now();
j.wait_all_for(std::chrono::milliseconds(15));
REQUIRE(time_now() - b > std::chrono::milliseconds(10));
REQUIRE(counter == 0);
j.wait_all_until(time_now() + std::chrono::milliseconds(15));
REQUIRE(time_now() - b > std::chrono::milliseconds(20));
REQUIRE(counter == 0);
j.active_wait_all_for(std::chrono::milliseconds(100));
REQUIRE(time_now() - b > std::chrono::milliseconds(70));
REQUIRE(counter > 2);
REQUIRE(counter < 50);
j.active_wait_all_until(time_now() + std::chrono::seconds(3));
REQUIRE(counter == 50);
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 30; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
j.resume();
REQUIRE(jb::jobber_wait_status::timeout == j.wait_all_for(std::chrono::milliseconds(50)));
REQUIRE(counter > 0);
REQUIRE(jb::jobber_wait_status::no_timeout == j.wait_all_for(std::chrono::seconds(5)));
REQUIRE(counter == 30);
}
{
jb::jobber j(1);
std::atomic<int> counter = ATOMIC_VAR_INIT(0);
j.pause();
for ( std::size_t i = 0; i < 30; ++i ) {
j.async([&counter](){
++counter;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
});
}
REQUIRE(jb::jobber_wait_status::timeout == j.active_wait_all_for(std::chrono::milliseconds(50)));
REQUIRE(counter > 0);
REQUIRE(jb::jobber_wait_status::no_timeout == j.active_wait_all_for(std::chrono::seconds(5)));
REQUIRE(counter == 30);
}
{
jb::jobber j(2);
jb::jobber g(2);
std::vector<pr::promise<float>> jp(50);
for ( auto& jpi : jp ) {
jpi = j.async([&g](){
std::vector<pr::promise<float>> gp(50);
for ( std::size_t i = 0; i < gp.size(); ++i ) {
gp[i] = g.async([](float angle){
return std::sin(angle);
}, i);
}
return std::accumulate(gp.begin(), gp.end(), 0.f,
[](float r, pr::promise<float>& f){
return r + f.get();
});
});
}
float r0 = std::accumulate(jp.begin(), jp.end(), 0.f,
[](float r, pr::promise<float>& f){
return r + f.get();
});
float r1 = 0.f;
for ( std::size_t i = 0; i < 50; ++i ) {
r1 += std::sin(static_cast<float>(i));
}
REQUIRE(r0 == Approx(r1 * 50.f).margin(0.01f));
}
}