/* * Copyright 2014 (c) Anna Schumaker. * Test the idle queue */ #include #include #include static unsigned int cur = -1; static bool inc_cur(void *data) { g_assert_cmpuint(++cur, ==, GPOINTER_TO_UINT(data)); return true; } static void test_idle(gconstpointer arg) { unsigned int n = GPOINTER_TO_UINT(arg); cur = -1; idle_init(); g_assert_cmpfloat(idle_progress(), ==, 1.0); g_assert_false(idle_run_task()); for (unsigned int i = 0; i < n; i++) idle_schedule(IDLE_SYNC, inc_cur, GINT_TO_POINTER(i)); g_assert_cmpfloat(idle_progress(), ==, 0.0); for (unsigned int i = 0; i < (n - 1); i++) { g_assert_true(idle_run_task()); g_assert_cmpfloat(idle_progress(), ==, ((i + 1) / (float)n)); } g_assert_false(idle_run_task()); g_assert_cmpfloat(idle_progress(), ==, 1.0); for (unsigned int i = n; i < (2 * n); i++) idle_schedule(IDLE_ASYNC, inc_cur, GINT_TO_POINTER(i)); while (idle_run_task()) {} g_assert_cmpfloat(idle_progress(), ==, 1.0); g_assert_cmpuint(cur, ==, (2 * n) - 1); for (unsigned int i = 0; i < n; i++) idle_schedule(IDLE_SYNC, inc_cur, GINT_TO_POINTER(i)); g_assert_cmpfloat(idle_progress(), ==, 0.0); idle_deinit(); g_assert_cmpfloat(idle_progress(), ==, 1.0); } 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(); }