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>
|
2015-11-10 14:18:11 -05:00
|
|
|
#include <tests/test.h>
|
2016-03-29 08:39:00 -04:00
|
|
|
#include <glib.h>
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
static unsigned int cur = -1;
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2016-05-02 18:42:38 -04:00
|
|
|
static bool inc_cur(void *data)
|
2014-03-16 13:09:33 -04:00
|
|
|
{
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpuint(++cur, ==, GPOINTER_TO_UINT(data));
|
2016-05-02 18:42:38 -04:00
|
|
|
return true;
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 08:28:11 -05:00
|
|
|
static void test_idle(gconstpointer arg)
|
2015-01-04 17:56:54 -05:00
|
|
|
{
|
2016-03-07 08:28:11 -05:00
|
|
|
unsigned int n = GPOINTER_TO_UINT(arg);
|
2015-08-27 08:29:39 -04:00
|
|
|
cur = -1;
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2016-04-30 10:11:48 -04:00
|
|
|
idle_init();
|
|
|
|
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 1.0);
|
|
|
|
g_assert_false(idle_run_task());
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
for (unsigned int i = 0; i < n; i++)
|
2016-04-30 09:16:02 -04:00
|
|
|
idle_schedule(IDLE_SYNC, inc_cur, GINT_TO_POINTER(i));
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 0.0);
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2015-08-27 08:29:39 -04:00
|
|
|
for (unsigned int i = 0; i < (n - 1); i++) {
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_true(idle_run_task());
|
|
|
|
g_assert_cmpfloat(idle_progress(), ==, ((i + 1) / (float)n));
|
|
|
|
}
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_false(idle_run_task());
|
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 1.0);
|
2016-03-01 08:27:13 -05:00
|
|
|
|
2016-04-30 10:11:48 -04:00
|
|
|
for (unsigned int i = n; i < (2 * n); i++)
|
|
|
|
idle_schedule(IDLE_ASYNC, inc_cur, GINT_TO_POINTER(i));
|
|
|
|
while (idle_run_task()) {}
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 1.0);
|
|
|
|
g_assert_cmpuint(cur, ==, (2 * n) - 1);
|
2016-04-30 10:11:48 -04:00
|
|
|
|
2016-03-01 08:27:13 -05:00
|
|
|
for (unsigned int i = 0; i < n; i++)
|
2016-04-30 09:16:02 -04:00
|
|
|
idle_schedule(IDLE_SYNC, inc_cur, GINT_TO_POINTER(i));
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 0.0);
|
2016-03-01 08:27:13 -05:00
|
|
|
|
2016-04-30 10:11:48 -04:00
|
|
|
idle_deinit();
|
2016-03-07 08:28:11 -05:00
|
|
|
g_assert_cmpfloat(idle_progress(), ==, 1.0);
|
2014-05-04 10:08:00 -04:00
|
|
|
}
|
|
|
|
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2016-03-07 08:28:11 -05:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_add_data_func("/Core/Idle/n = 10", GUINT_TO_POINTER( 10), test_idle);
|
|
|
|
g_test_add_data_func("/Core/Idle/n = 100", GUINT_TO_POINTER( 100), test_idle);
|
|
|
|
g_test_add_data_func("/Core/Idle/n = 1,000", GUINT_TO_POINTER( 1000), test_idle);
|
|
|
|
g_test_add_data_func("/Core/Idle/n = 10,000", GUINT_TO_POINTER( 10000), test_idle);
|
|
|
|
g_test_add_data_func("/Core/Idle/n = 100,000", GUINT_TO_POINTER(100000), test_idle);
|
|
|
|
return g_test_run();
|
|
|
|
}
|