ocarina/tests/core/tempq.c

174 lines
4.3 KiB
C
Raw Normal View History

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/collection.h>
#include <core/filter.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/tags/tags.h>
#include <core/tempq.h>
#include <tests/test.h>
static void test_init()
{
idle_init_sync();
filter_init();
tags_init();
playlist_init(NULL);
collection_init();
test_equal((void *)tempq_next(), NULL);
tempq_init(NULL);
while (idle_run_task()) {};
test_equal((void *)tempq_next(), NULL);
tempq_move(NULL, 1);
test_equal((void *)tempq_get(0), NULL);
test_equal(tempq_count(), 0);
test_equal(tempq_index(NULL), (unsigned int)-1);
}
static void test_alloc()
{
struct queue *q0, *q1;
q0 = tempq_alloc(0);
test_not_equal((void *)q0, NULL);
test_equal(queue_has_flag(q0, Q_ENABLED), (bool)true);
test_equal(queue_has_flag(q0, Q_RANDOM), (bool)false);
test_equal(queue_has_flag(q0, Q_SAVE_SORT), (bool)true);
test_equal(queue_has_flag(q0, Q_SAVE_FLAGS), (bool)true);
test_equal((void *)tempq_get(0), (void *)q0);
test_equal(tempq_count(), 1);
test_equal(tempq_index(q0), 0);
q1 = tempq_alloc(Q_RANDOM);
test_not_equal((void *)q1, NULL);
test_equal(queue_has_flag(q1, Q_ENABLED), (bool)true);
test_equal(queue_has_flag(q1, Q_RANDOM), (bool)true);
test_equal(queue_has_flag(q1, Q_SAVE_SORT), (bool)true);
test_equal(queue_has_flag(q1, Q_SAVE_FLAGS), (bool)true);
test_equal((void *)tempq_get(1), (void *)q1);
test_equal(tempq_count(), 2);
test_equal(tempq_index(q1), 1);
tempq_deinit();
test_equal(tempq_count(), 0);
tempq_init(NULL);
test_equal(tempq_count(), 0);
while (idle_run_task()) {};
test_equal(tempq_count(), 2);
q0 = tempq_get(0);
q1 = tempq_get(1);
test_equal(queue_has_flag(q0, Q_RANDOM), (bool)false);
test_equal(queue_has_flag(q1, Q_RANDOM), (bool)true);
}
static void test_move()
{
struct queue *q0 = tempq_get(0);
struct queue *q1 = tempq_get(1);
tempq_move(NULL, 0);
tempq_move(q1, 0);
test_equal((void *)tempq_get(0), (void *)q1);
test_equal((void *)tempq_get(1), (void *)q0);
test_equal((void *)tempq_get(2), NULL);
tempq_move(q0, 1);
test_equal((void *)tempq_get(0), (void *)q1);
test_equal((void *)tempq_get(1), (void *)q0);
test_equal((void *)tempq_get(2), NULL);
tempq_deinit();
test_equal(tempq_count(), 0);
tempq_init(NULL);
while (idle_run_task()) {};
q0 = tempq_get(1);
q1 = tempq_get(0);
test_equal(queue_has_flag(q0, Q_RANDOM), (bool)false);
test_equal(queue_has_flag(q1, Q_RANDOM), (bool)true);
}
static void test_free()
{
struct queue *q0 = tempq_get(1);
struct queue *q1 = tempq_get(0);
tempq_free(q0);
test_equal((void *)tempq_get(0), (void *)q1);
test_equal(tempq_count(), 1);
tempq_free(q1);
test_equal((void *)tempq_get(0), NULL);
test_equal(tempq_count(), 0);
tempq_deinit();
tempq_init(NULL);
test_equal(tempq_count(), 0);
}
static void test_next()
{
const struct database *track_db = track_db_get();
struct queue *q0, *q1;
unsigned int i;
collection_add("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()) {};
test_equal(tempq_count(), 2);
q0 = tempq_get(0);
q1 = tempq_get(1);
for (i = 0; i < track_db->db_size; i++) {
test_loop_equal(queue_size(q0), track_db->db_size - i, i);
test_loop_equal((void *)tempq_next(), (void *)track_get(i), i);
} test_loop_passed();
test_equal((void *)tempq_get(0), (void *)q1);
test_equal(tempq_count(), 1);
test_equal(queue_size(q1), track_db->db_size);
tempq_deinit();
tempq_init(NULL);
while (idle_run_task()) {};
test_equal(tempq_count(), 1);
q1 = tempq_get(0);
for (i = 0; i < track_db->db_size; i++) {
test_loop_equal(queue_size(q1), track_db->db_size - i, i);
test_loop_equal((void *)tempq_next(), (void *)track_get(i), i);
} test_loop_passed();
test_equal((void *)tempq_get(0), NULL);
test_equal(tempq_count(), 0);
tempq_deinit();
playlist_deinit();
tags_deinit();
filter_deinit();
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Temporary Queue Initialization", test_init),
UNIT_TEST("Temporary Queue Alloc", test_alloc),
UNIT_TEST("Temporary Queue Move", test_move),
UNIT_TEST("Temporary Queue Free", test_free),
UNIT_TEST("Temporary Queue Next Track", test_next),
);