Commit e1809547 authored by Davis King's avatar Davis King

Added some more tests for the thread_pool object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402681
parent bbf90ce0
...@@ -25,6 +25,16 @@ namespace ...@@ -25,6 +25,16 @@ namespace
float val; float val;
}; };
struct add_functor
{
template <typename T, typename U, typename V>
void operator()(T a, U b, V& res)
{
dlib::sleep(20);
res = a + b;
}
};
void gset_struct_to_zero (some_struct& a) { a.val = 0; } void gset_struct_to_zero (some_struct& a) { a.val = 0; }
void gset_to_zero (int& a) { a = 0; } void gset_to_zero (int& a) { a = 0; }
void gincrement (int& a) { ++a; } void gincrement (int& a) { ++a; }
...@@ -44,138 +54,150 @@ namespace ...@@ -44,138 +54,150 @@ namespace
void perform_test ( void perform_test (
) )
{ {
thread_pool tp(3); for (int num_threads= 0; num_threads < 4; ++num_threads)
print_spinner();
future<int> a, b, c, res;
future<some_struct> obj;
for (int i = 0; i < 4; ++i)
{ {
a = 1; thread_pool tp(num_threads);
b = 2; print_spinner();
c = 3;
res = 4; future<int> a, b, c, res;
future<some_struct> obj;
DLIB_CASSERT(a==a,"");
DLIB_CASSERT(a!=b,""); for (int i = 0; i < 4; ++i)
DLIB_CASSERT(a==1,""); {
a = 1;
tp.add_task(gset_to_zero, a); b = 2;
tp.add_task(gset_to_zero, b); c = 3;
tp.add_task(*this, &thread_pool_tester::set_to_zero, c); res = 4;
tp.add_task(gset_to_zero, res);
DLIB_CASSERT(a == 0,"");
DLIB_CASSERT(b == 0,""); DLIB_CASSERT(a==a,"");
DLIB_CASSERT(c == 0,""); DLIB_CASSERT(a!=b,"");
DLIB_CASSERT(res == 0,""); DLIB_CASSERT(a==1,"");
tp.add_task(gset_to_zero, a);
tp.add_task(gincrement, a); tp.add_task(gset_to_zero, b);
tp.add_task(*this, &thread_pool_tester::increment, b); tp.add_task(*this, &thread_pool_tester::set_to_zero, c);
tp.add_task(*this, &thread_pool_tester::increment, c); tp.add_task(gset_to_zero, res);
tp.add_task(gincrement, res); DLIB_CASSERT(a == 0,"");
DLIB_CASSERT(b == 0,"");
DLIB_CASSERT(a == 1,""); DLIB_CASSERT(c == 0,"");
DLIB_CASSERT(b == 1,""); DLIB_CASSERT(res == 0,"");
DLIB_CASSERT(c == 1,"");
DLIB_CASSERT(res == 1,"");
tp.add_task(gincrement, a);
tp.add_task(&gincrement, a); tp.add_task(*this, &thread_pool_tester::increment, b);
tp.add_task(*this, &thread_pool_tester::increment, b); tp.add_task(*this, &thread_pool_tester::increment, c);
tp.add_task(*this, &thread_pool_tester::increment, c); tp.add_task(gincrement, res);
tp.add_task(&gincrement, res);
tp.add_task(gincrement, a); DLIB_CASSERT(a == 1,"");
tp.add_task(*this, &thread_pool_tester::increment, b); DLIB_CASSERT(b == 1,"");
tp.add_task(*this, &thread_pool_tester::increment, c); DLIB_CASSERT(c == 1,"");
tp.add_task(gincrement, res); DLIB_CASSERT(res == 1,"");
DLIB_CASSERT(a == 3,""); tp.add_task(&gincrement, a);
DLIB_CASSERT(b == 3,""); tp.add_task(*this, &thread_pool_tester::increment, b);
DLIB_CASSERT(c == 3,""); tp.add_task(*this, &thread_pool_tester::increment, c);
DLIB_CASSERT(res == 3,""); tp.add_task(&gincrement, res);
tp.add_task(gincrement, a);
tp.add_task(*this, &thread_pool_tester::increment, c); tp.add_task(*this, &thread_pool_tester::increment, b);
tp.add_task(gincrement, res); tp.add_task(*this, &thread_pool_tester::increment, c);
DLIB_CASSERT(c == 4,""); tp.add_task(gincrement, res);
DLIB_CASSERT(res == 4,"");
DLIB_CASSERT(a == 3,"");
DLIB_CASSERT(b == 3,"");
tp.add_task(gadd, a, b, res); DLIB_CASSERT(c == 3,"");
DLIB_CASSERT(res == a+b,""); DLIB_CASSERT(res == 3,"");
DLIB_CASSERT(res == 6,"");
a = 3; tp.add_task(*this, &thread_pool_tester::increment, c);
b = 4; tp.add_task(gincrement, res);
res = 99; DLIB_CASSERT(c == 4,"");
DLIB_CASSERT(res == 99,""); DLIB_CASSERT(res == 4,"");
tp.add_task(*this, &thread_pool_tester::add, a, b, res);
DLIB_CASSERT(res == a+b,"");
DLIB_CASSERT(res == 7,""); tp.add_task(gadd, a, b, res);
DLIB_CASSERT(res == a+b,"");
a = 1; DLIB_CASSERT(res == 6,"");
b = 2; a = 3;
c = 3; b = 4;
res = 88; res = 99;
DLIB_CASSERT(res == 88,""); DLIB_CASSERT(res == 99,"");
DLIB_CASSERT(a == 1,""); tp.add_task(*this, &thread_pool_tester::add, a, b, res);
DLIB_CASSERT(b == 2,""); DLIB_CASSERT(res == a+b,"");
DLIB_CASSERT(c == 3,""); DLIB_CASSERT(res == 7,"");
tp.add_task(gadd2, a, b, c, res); a = 1;
DLIB_CASSERT(res == 6,""); b = 2;
DLIB_CASSERT(a == 1,""); c = 3;
DLIB_CASSERT(b == 2,""); res = 88;
DLIB_CASSERT(c == 3,""); DLIB_CASSERT(res == 88,"");
DLIB_CASSERT(a == 1,"");
a = 1; DLIB_CASSERT(b == 2,"");
b = 2; DLIB_CASSERT(c == 3,"");
c = 3;
res = 88; tp.add_task(gadd2, a, b, c, res);
DLIB_CASSERT(res == 88,""); DLIB_CASSERT(res == 6,"");
DLIB_CASSERT(a == 1,""); DLIB_CASSERT(a == 1,"");
DLIB_CASSERT(b == 2,""); DLIB_CASSERT(b == 2,"");
DLIB_CASSERT(c == 3,""); DLIB_CASSERT(c == 3,"");
tp.add_task(*this, &thread_pool_tester::add2, a, b, c, res);
DLIB_CASSERT(res == 6,""); a = 1;
DLIB_CASSERT(a == 1,""); b = 2;
DLIB_CASSERT(b == 2,""); c = 3;
DLIB_CASSERT(c == 3,""); res = 88;
DLIB_CASSERT(res == 88,"");
a = 1; DLIB_CASSERT(a == 1,"");
b = 2; DLIB_CASSERT(b == 2,"");
c = 3; DLIB_CASSERT(c == 3,"");
res = 88; tp.add_task(*this, &thread_pool_tester::add2, a, b, c, res);
tp.add_task(gadd1, a, b); DLIB_CASSERT(res == 6,"");
DLIB_CASSERT(a == 1,""); DLIB_CASSERT(a == 1,"");
DLIB_CASSERT(b == 3,""); DLIB_CASSERT(b == 2,"");
a = 2; DLIB_CASSERT(c == 3,"");
tp.add_task(*this, &thread_pool_tester::add1, a, b);
DLIB_CASSERT(a == 2,""); a = 1;
DLIB_CASSERT(b == 5,""); b = 2;
c = 3;
res = 88;
val = 4; tp.add_task(gadd1, a, b);
uint64 id = tp.add_task(*this, &thread_pool_tester::zero_val); DLIB_CASSERT(a == 1,"");
tp.wait_for_task(id); DLIB_CASSERT(b == 3,"");
DLIB_CASSERT(val == 0,""); a = 2;
id = tp.add_task(*this, &thread_pool_tester::accum2, 1,2); tp.add_task(*this, &thread_pool_tester::add1, a, b);
tp.wait_for_all_tasks(); DLIB_CASSERT(a == 2,"");
DLIB_CASSERT(val == 3,""); DLIB_CASSERT(b == 5,"");
id = tp.add_task(*this, &thread_pool_tester::accum1, 3);
tp.wait_for_task(id);
DLIB_CASSERT(val == 6,""); val = 4;
uint64 id = tp.add_task(*this, &thread_pool_tester::zero_val);
tp.wait_for_task(id);
obj.get().val = 8; DLIB_CASSERT(val == 0,"");
DLIB_CASSERT(obj.get().val == 8,""); id = tp.add_task(*this, &thread_pool_tester::accum2, 1,2);
tp.add_task(gset_struct_to_zero, obj); tp.wait_for_all_tasks();
DLIB_CASSERT(obj.get().val == 0,""); DLIB_CASSERT(val == 3,"");
obj.get().val = 8; id = tp.add_task(*this, &thread_pool_tester::accum1, 3);
DLIB_CASSERT(obj.get().val == 8,""); tp.wait_for_task(id);
tp.add_task(*this,&thread_pool_tester::set_struct_to_zero, obj); DLIB_CASSERT(val == 6,"");
DLIB_CASSERT(obj.get().val == 0,"");
obj.get().val = 8;
DLIB_CASSERT(obj.get().val == 8,"");
tp.add_task(gset_struct_to_zero, obj);
DLIB_CASSERT(obj.get().val == 0,"");
obj.get().val = 8;
DLIB_CASSERT(obj.get().val == 8,"");
tp.add_task(*this,&thread_pool_tester::set_struct_to_zero, obj);
DLIB_CASSERT(obj.get().val == 0,"");
a = 1;
b = 2;
res = 0;
add_functor f;
tp.add_task(f, a, b, res);
DLIB_CASSERT(a == 1,"");
DLIB_CASSERT(b == 2,"");
DLIB_CASSERT(res == 3,"");
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment