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). // This code is licensed under the MIT License (MIT).
// //
@ -15,98 +15,191 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <UnitTest++/UnitTest++.h>
#include <mutex>
#include <random> #include <atomic>
#include <string> #include <chrono>
#include <thread>
#include <gsl/gsl_assert> #include <gsl/gsl_assert>
#include <gsl/gsl_thread> #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)
{ {
TEST(raii_thread_same_scope_clock_test)
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
{ {
public: auto start_time = steady_clock::now();
scope_test_fixture(): v1(fill( 0, 49)), v2(fill(50, 99)), v3(fill( 0, 99)) {}
std::mutex mtx; gsl::raii_thread t{[&]{ sleep_for(t_100ms); }};
const std::vector<int> v1;
const std::vector<int> v2;
const std::vector<int> v3;
private: auto end_time = steady_clock::now();
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;
}
};
TEST(same_scope) CHECK(end_time - start_time < t_100ms);
{
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(v != fixture.v3); TEST(raii_thread_different_scope_clock_test)
}
TEST(different_scope)
{ {
scope_test_fixture fixture; auto start_time = steady_clock::now();
std::vector<int> v;
{ {
gsl::raii_thread t1{[&]{ gsl::raii_thread t{[&]{ sleep_for(t_100ms); }};
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) auto end_time = steady_clock::now();
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1))); CHECK(end_time - start_time >= t_100ms);
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
} }
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(); }