Added various tests to tests/thread_tests.cpp

This commit is contained in:
Galik 2017-04-03 05:55:29 +01:00
parent a423e75f49
commit 7c378aaa71

View File

@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
@ -15,98 +15,191 @@
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <mutex>
#include <random>
#include <string>
#include <atomic>
#include <chrono>
#include <thread>
#include <gsl/gsl_assert>
#include <gsl/gsl_thread>
namespace
using std::chrono::steady_clock;
using std::this_thread::sleep_for;
const auto t_100ms = std::chrono::milliseconds(100);
SUITE(raii_thread_tests)
{
inline
int random_number(int from, int to)
{
static std::mutex mtx;
static std::mt19937 mt{std::random_device{}()};
std::lock_guard<std::mutex> lock(mtx);
return std::uniform_int_distribution<int>{from, to}(mt);
}
SUITE(thread_tests)
{
class scope_test_fixture
TEST(raii_thread_same_scope_clock_test)
{
public:
scope_test_fixture(): v1(fill( 0, 49)), v2(fill(50, 99)), v3(fill( 0, 99)) {}
auto start_time = steady_clock::now();
std::mutex mtx;
const std::vector<int> v1;
const std::vector<int> v2;
const std::vector<int> v3;
gsl::raii_thread t{[&]{ sleep_for(t_100ms); }};
private:
std::vector<int> fill(int from, int to)
{
Ensures(from < to);
std::vector<int> v(std::size_t(to - from));
std::iota(std::begin(v), std::end(v), from);
return v;
}
};
auto end_time = steady_clock::now();
TEST(same_scope)
{
scope_test_fixture fixture;
std::vector<int> v;
gsl::raii_thread t1{[&]{
for(auto i: fixture.v1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
}};
for(auto i: fixture.v2)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
CHECK(end_time - start_time < t_100ms);
}
CHECK(v != fixture.v3);
}
TEST(different_scope)
TEST(raii_thread_different_scope_clock_test)
{
scope_test_fixture fixture;
std::vector<int> v;
auto start_time = steady_clock::now();
{
gsl::raii_thread t1{[&]{
for(auto i: fixture.v1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
}};
gsl::raii_thread t{[&]{ sleep_for(t_100ms); }};
}
for(auto i: fixture.v2)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
auto end_time = steady_clock::now();
CHECK(end_time - start_time >= t_100ms);
}
CHECK(v == fixture.v3);
TEST(raii_thread_ctor_test)
{
auto start_time = steady_clock::now();
gsl::raii_thread t1{[&]{ sleep_for(t_100ms); }};
{
gsl::raii_thread t2{std::move(t1)};
}
auto end_time = steady_clock::now();
CHECK(end_time - start_time >= t_100ms);
}
TEST(raii_thread_assign_test)
{
auto start_time = steady_clock::now();
gsl::raii_thread t1{[&]{ sleep_for(t_100ms); }};
{
gsl::raii_thread t2;
t2 = std::move(t1);
}
auto end_time = steady_clock::now();
CHECK(end_time - start_time >= t_100ms);
}
TEST(raii_thread_std_thread_ctor_test)
{
auto start_time = steady_clock::now();
std::thread t1{[&]{ sleep_for(t_100ms); }};
{
gsl::raii_thread t2{std::move(t1)};
}
auto end_time = steady_clock::now();
CHECK(end_time - start_time >= t_100ms);
}
TEST(raii_thread_std_thread_assign_test)
{
auto start_time = steady_clock::now();
std::thread t1{[&]{ sleep_for(t_100ms); }};
{
gsl::raii_thread t2;
t2 = std::move(t1);
}
auto end_time = steady_clock::now();
CHECK(end_time - start_time >= t_100ms);
}
TEST(raii_thread_swap_test)
{
gsl::raii_thread t1{[&]{ sleep_for(t_100ms); }};
gsl::raii_thread t2{[&]{ sleep_for(t_100ms); }};
auto id1 = t1.get_id();
auto id2 = t2.get_id();
std::swap(t1, t2);
CHECK(t1.get_id() == id2);
CHECK(t2.get_id() == id1);
t1.swap(t2);
CHECK(t1.get_id() == id1);
CHECK(t2.get_id() == id2);
}
}
int main(int, const char* []) { return UnitTest::RunAllTests(); }
SUITE(detached_thread_tests)
{
TEST(detached_thread_out_of_scope_test)
{
std::atomic_bool b{false};
{
gsl::detached_thread t{[&]{ sleep_for(t_100ms); b = true; }};
}
CHECK(b == false);
sleep_for(t_100ms * 2);
CHECK(b == true);
}
TEST(detached_thread_temporary_test)
{
std::atomic_bool b{false};
gsl::detached_thread{[&]{ sleep_for(t_100ms); b = true; }};
CHECK(b == false);
sleep_for(t_100ms * 2);
CHECK(b == true);
}
TEST(detached_thread_ctor_std_thread_test)
{
std::thread t1{[&]{ sleep_for(t_100ms); }};
gsl::detached_thread t2{std::move(t1)};
}
TEST(detached_thread_assign_std_thread_test)
{
std::thread t1{[&]{ sleep_for(t_100ms); }};
gsl::detached_thread t2;
t2 = std::move(t1);
}
TEST(detached_thread_temporary_assign_std_thread_test)
{
std::thread t1{[&]{ sleep_for(t_100ms); }};
gsl::detached_thread{std::move(t1)};
}
TEST(detached_thread_swap_test)
{
gsl::detached_thread t1{[&]{ sleep_for(t_100ms); }};
gsl::detached_thread t2{[&]{ sleep_for(t_100ms); }};
auto id1 = t1.get_id();
auto id2 = t2.get_id();
std::swap(t1, t2);
CHECK(t1.get_id() == id2);
CHECK(t2.get_id() == id1);
t1.swap(t2);
CHECK(t1.get_id() == id1);
CHECK(t2.get_id() == id2);
}
}
int main() { return UnitTest::RunAllTests(); }