2014-03-16 13:09:33 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
* Test the idle queue
|
|
|
|
*/
|
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/idle.h>
|
2014-06-14 10:50:18 -04:00
|
|
|
#include <tests/test.h>
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2014-05-04 10:08:00 -04:00
|
|
|
#include <sstream>
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-01-06 08:31:41 -05:00
|
|
|
static float N = 0;
|
2014-05-04 10:08:00 -04:00
|
|
|
static float cur = -1;
|
2015-01-04 17:56:54 -05:00
|
|
|
static bool func_passed = false;
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2014-05-04 10:08:00 -04:00
|
|
|
static void inc_cur(float &expected)
|
2014-03-16 13:09:33 -04:00
|
|
|
{
|
|
|
|
cur++;
|
2015-01-04 17:56:54 -05:00
|
|
|
func_passed = (cur == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int _test_idle_queue_step(unsigned int i)
|
|
|
|
{
|
|
|
|
if (idle :: run_task() == false)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
if (idle :: get_progress() != ((i + 1) / N))
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return func_passed ? LOOP_PASSED : LOOP_FAILED;
|
2014-03-16 13:09:33 -04:00
|
|
|
}
|
|
|
|
|
2015-01-06 08:31:41 -05:00
|
|
|
static void test_idle_queue()
|
2014-03-16 13:09:33 -04:00
|
|
|
{
|
2014-05-04 10:08:00 -04:00
|
|
|
test_equal(idle :: get_progress(), (float)1.0);
|
|
|
|
test_equal(idle :: run_task(), false);
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-01-06 08:31:41 -05:00
|
|
|
for (float i = 0; i < N; i++)
|
2014-03-16 13:09:33 -04:00
|
|
|
idle :: schedule(inc_cur, i);
|
2014-05-04 10:08:00 -04:00
|
|
|
test_equal(idle :: get_progress(), (float)0.0);
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, N - 1, 1, _test_idle_queue_step);
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2014-05-04 10:08:00 -04:00
|
|
|
test_equal(idle :: run_task(), false);
|
|
|
|
test_equal(idle :: get_progress(), (float)1.0);
|
|
|
|
}
|
|
|
|
|
2015-01-06 08:31:41 -05:00
|
|
|
static void do_test(unsigned int n)
|
2014-05-04 10:08:00 -04:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " (n = " << n << ")";
|
|
|
|
const std::string n_str = ss.str();
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-01-06 08:31:41 -05:00
|
|
|
N = n;
|
2014-05-04 10:08:00 -04:00
|
|
|
cur = -1;
|
2015-01-04 17:56:54 -05:00
|
|
|
test :: run("Idle Queue Test" + n_str, test_idle_queue);
|
2014-05-04 10:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
do_test(10);
|
|
|
|
do_test(100);
|
|
|
|
do_test(1000);
|
|
|
|
do_test(10000);
|
|
|
|
do_test(100000);
|
2014-03-16 13:09:33 -04:00
|
|
|
return 0;
|
|
|
|
}
|