/* * Copyright 2013 (c) Anna Schumaker. */ #include #include #include #include #include static void test_init() { g_assert_null(tempq_next()); tempq_init(NULL); while (idle_run_task()) {}; g_assert_null(tempq_next()); tempq_move(NULL, 1); g_assert_null(tempq_get(0)); g_assert_cmpuint(tempq_count(), ==, 0); g_assert_cmpuint(tempq_index(NULL), ==, (unsigned int)-1); } static void test_alloc() { struct queue *q0, *q1; q0 = tempq_alloc(0); g_assert_nonnull(q0); g_assert_true(queue_has_flag(q0, Q_ENABLED)); g_assert_false(queue_has_flag(q0, Q_RANDOM)); g_assert_true(queue_has_flag(q0, Q_SAVE_SORT)); g_assert_true(queue_has_flag(q0, Q_SAVE_FLAGS)); g_assert(tempq_get(0) == q0); g_assert_cmpuint(tempq_count(), ==, 1); g_assert_cmpuint(tempq_index(q0), ==, 0); q1 = tempq_alloc(Q_RANDOM); g_assert_nonnull(q1); g_assert_true(queue_has_flag(q1, Q_ENABLED)); g_assert_true(queue_has_flag(q1, Q_RANDOM)); g_assert_true(queue_has_flag(q1, Q_SAVE_SORT)); g_assert_true(queue_has_flag(q1, Q_SAVE_FLAGS)); g_assert(tempq_get(1) == q1); g_assert_cmpuint(tempq_count(), ==, 2); g_assert_cmpuint(tempq_index(q1), ==, 1); tempq_deinit(); g_assert_cmpuint(tempq_count(), ==, 0); tempq_init(NULL); g_assert_cmpuint(tempq_count(), ==, 0); while (idle_run_task()) {}; g_assert_cmpuint(tempq_count(), ==, 2); q0 = tempq_get(0); q1 = tempq_get(1); g_assert_false(queue_has_flag(q0, Q_RANDOM)); g_assert_true(queue_has_flag(q1, Q_RANDOM)); } static void test_move() { struct queue *q0 = tempq_get(0); struct queue *q1 = tempq_get(1); tempq_move(NULL, 0); tempq_move(q1, 0); g_assert(tempq_get(0) == q1); g_assert(tempq_get(1) == q0); g_assert_null(tempq_get(2)); tempq_move(q0, 1); g_assert(tempq_get(0) == q1); g_assert(tempq_get(1) == q0); g_assert_null(tempq_get(2)); tempq_deinit(); g_assert_cmpuint(tempq_count(), ==, 0); tempq_init(NULL); while (idle_run_task()) {}; q0 = tempq_get(1); q1 = tempq_get(0); g_assert_false(queue_has_flag(q0, Q_RANDOM)); g_assert_true(queue_has_flag(q1, Q_RANDOM)); } static void test_free() { struct queue *q0 = tempq_get(1); struct queue *q1 = tempq_get(0); tempq_free(q0); g_assert(tempq_get(0) == q1); g_assert_cmpuint(tempq_count(), ==, 1); tempq_free(q1); g_assert_null(tempq_get(0)); g_assert_cmpuint(tempq_count(), ==, 0); tempq_deinit(); tempq_init(NULL); g_assert_cmpuint(tempq_count(), ==, 0); } static void test_next() { const struct database *track_db = track_db_get(); struct queue *q0, *q1; unsigned int i; playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task()) {}; q0 = tempq_alloc(0); q1 = tempq_alloc(0); for (i = 0; i < track_db->db_size; i++) { queue_add(q0, track_get(i)); queue_add(q1, track_get(i)); } tempq_save(NULL, Q_ENABLED); tempq_deinit(); tempq_init(NULL); while (idle_run_task()) {}; g_assert_cmpuint(tempq_count(), ==, 2); q0 = tempq_get(0); q1 = tempq_get(1); for (i = 0; i < track_db->db_size; i++) { g_assert_cmpuint(queue_size(q0), ==, track_db->db_size - i); g_assert(tempq_next() == track_get(i)); } g_assert(tempq_get(0) == q1); g_assert_cmpuint(tempq_count(), ==, 1); g_assert_cmpuint(queue_size(q1), ==, track_db->db_size); tempq_deinit(); tempq_init(NULL); while (idle_run_task()) {}; g_assert_cmpuint(tempq_count(), ==, 1); q1 = tempq_get(0); for (i = 0; i < track_db->db_size; i++) { g_assert_cmpuint(queue_size(q1), ==, track_db->db_size - i); g_assert(tempq_next() == track_get(i)); } g_assert_null(tempq_get(0)); g_assert_cmpuint(tempq_count(), ==, 0); } int main(int argc, char **argv) { int ret; idle_init_sync(); tags_init(); playlist_init(NULL); g_test_init(&argc, &argv, NULL); g_test_add_func("/Core/Temporary Queue/Initialization", test_init); g_test_add_func("/Core/Temporary Queue/Alloc", test_alloc); g_test_add_func("/Core/Temporary Queue/Move", test_move); g_test_add_func("/Core/Temporary Queue/Free", test_free); g_test_add_func("/Core/Temporary Queue/Next Track", test_next); ret = g_test_run(); tempq_deinit(); playlist_deinit(); tags_deinit(); idle_deinit(); return ret; }