2014-04-13 12:26:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2016-03-11 09:12:33 -05:00
|
|
|
#include <core/idle.h>
|
2015-12-02 10:22:11 -05:00
|
|
|
#include <core/queue.h>
|
2014-12-02 08:16:22 -05:00
|
|
|
#include <core/tags/tags.h>
|
2015-12-02 10:22:11 -05:00
|
|
|
#include <tests/test.h>
|
2014-04-13 12:26:32 -04:00
|
|
|
|
2014-04-13 18:38:49 -04:00
|
|
|
|
2016-01-12 08:02:28 -05:00
|
|
|
unsigned int count_init = 0;
|
2016-01-13 08:18:03 -05:00
|
|
|
unsigned int count_deinit = 0;
|
2015-11-23 08:36:29 -05:00
|
|
|
unsigned int count_added = 0;
|
2016-01-22 08:11:09 -05:00
|
|
|
unsigned int count_erase = 0;
|
2015-11-13 11:14:40 -05:00
|
|
|
unsigned int count_deleted = 0;
|
2016-01-29 10:12:09 -05:00
|
|
|
unsigned int count_cleared = 0;
|
2015-11-23 08:36:29 -05:00
|
|
|
unsigned int count_flags = 0;
|
|
|
|
unsigned int count_sort = 0;
|
2014-05-11 18:32:53 -04:00
|
|
|
unsigned int count_updated = 0;
|
2014-05-11 10:40:54 -04:00
|
|
|
|
2016-01-22 08:11:09 -05:00
|
|
|
bool can_erase = true;
|
|
|
|
|
2014-05-11 10:40:54 -04:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
static void *queue_op_init(struct queue *queue, void *data)
|
2016-01-12 08:02:28 -05:00
|
|
|
{
|
|
|
|
count_init++;
|
|
|
|
return GUINT_TO_POINTER(count_init);
|
|
|
|
}
|
|
|
|
|
2016-01-13 08:18:03 -05:00
|
|
|
static void queue_op_deinit(struct queue *queue)
|
|
|
|
{
|
|
|
|
count_deinit++;
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:45:39 -05:00
|
|
|
static void queue_op_added(struct queue *queue, unsigned int pos)
|
|
|
|
{
|
|
|
|
count_added++;
|
|
|
|
}
|
|
|
|
|
2016-01-22 08:11:09 -05:00
|
|
|
static bool queue_op_erase(struct queue *queue, struct track *track)
|
|
|
|
{
|
|
|
|
count_erase++;
|
|
|
|
return can_erase;
|
|
|
|
}
|
|
|
|
|
2015-11-29 20:01:12 -05:00
|
|
|
static void queue_op_removed(struct queue *queue, unsigned int pos)
|
|
|
|
{
|
|
|
|
count_deleted++;
|
|
|
|
}
|
|
|
|
|
2016-01-29 10:12:09 -05:00
|
|
|
static void queue_op_cleared(struct queue *queue, unsigned int n)
|
|
|
|
{
|
|
|
|
count_cleared++;
|
|
|
|
}
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
static void queue_op_save(struct queue *queue, enum queue_flags flag)
|
|
|
|
{
|
|
|
|
if (flag == Q_SAVE_FLAGS)
|
|
|
|
count_flags++;
|
|
|
|
else if (flag == Q_SAVE_SORT)
|
|
|
|
count_sort++;
|
|
|
|
}
|
|
|
|
|
2015-11-29 20:10:15 -05:00
|
|
|
static void queue_op_updated(struct queue *queue, unsigned int pos)
|
|
|
|
{
|
|
|
|
count_updated++;
|
|
|
|
}
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
|
|
|
|
static const struct queue_ops test_ops = {
|
2016-01-12 08:02:28 -05:00
|
|
|
.qop_init = queue_op_init,
|
2016-01-13 08:18:03 -05:00
|
|
|
.qop_deinit = queue_op_deinit,
|
2016-01-29 10:12:09 -05:00
|
|
|
.qop_added = queue_op_added,
|
2016-01-22 08:11:09 -05:00
|
|
|
.qop_erase = queue_op_erase,
|
2016-01-29 10:12:09 -05:00
|
|
|
.qop_removed = queue_op_removed,
|
|
|
|
.qop_cleared = queue_op_cleared,
|
|
|
|
.qop_save = queue_op_save,
|
|
|
|
.qop_updated = queue_op_updated,
|
2015-11-23 08:36:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
static void test_init()
|
|
|
|
{
|
2015-11-16 09:12:15 -05:00
|
|
|
struct queue q;
|
2016-04-03 09:29:32 -04:00
|
|
|
struct queue_iter it;
|
2014-04-13 18:38:49 -04:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, 0, NULL, NULL);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_init, ==, 0);
|
|
|
|
g_assert_null(q.q_private);
|
2016-01-12 08:02:28 -05:00
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
|
|
|
|
g_assert_cmpuint(q.q_flags, ==, 0);
|
|
|
|
g_assert_cmpuint(q.q_length, ==, 0);
|
|
|
|
g_assert_null(q.q_sort);
|
|
|
|
g_assert_null(q.q_ops);
|
|
|
|
g_assert_null(queue_next(&q));
|
2015-11-16 09:12:15 -05:00
|
|
|
|
2016-04-03 09:29:32 -04:00
|
|
|
queue_iter_init(&q, &it);
|
|
|
|
g_assert_null(it.it_iter);
|
|
|
|
g_assert_cmpuint(it.it_pos, ==, (unsigned int)-1);
|
|
|
|
g_assert_null(queue_iter_val(&it));
|
|
|
|
|
2016-01-13 08:18:03 -05:00
|
|
|
queue_deinit(&q);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_deinit, ==, 0);
|
2016-01-13 08:18:03 -05:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, Q_ENABLED | Q_RANDOM, &test_ops, NULL);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_init, ==, 1);
|
|
|
|
g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1);
|
2015-11-13 11:14:40 -05:00
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1);
|
|
|
|
g_assert_cmpuint(q.q_flags, ==, Q_ENABLED | Q_RANDOM);
|
|
|
|
g_assert_cmpuint(q.q_length, ==, 0);
|
|
|
|
g_assert_null(q.q_sort);
|
|
|
|
g_assert(q.q_ops == &test_ops);
|
|
|
|
g_assert_null(queue_next(&q));
|
2016-01-13 08:18:03 -05:00
|
|
|
|
|
|
|
queue_deinit(&q);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_deinit, ==, 1);
|
2014-04-26 11:00:44 -04:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
static void test_flags()
|
2014-04-26 11:00:44 -04:00
|
|
|
{
|
2015-11-23 08:36:29 -05:00
|
|
|
struct queue q;
|
2014-04-26 11:00:44 -04:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, 0, &test_ops, NULL);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_flags, ==, 0);
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_ENABLED));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_RANDOM));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_REPEAT));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_NO_SORT));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_SAVE_FLAGS));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_SAVE_SORT));
|
|
|
|
g_assert_false(queue_has_flag(&q, Q_ADD_FRONT));
|
2014-04-26 11:00:44 -04:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&q, Q_ENABLED);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_flags, ==, Q_ENABLED);
|
|
|
|
g_assert_cmpuint(count_flags, ==, 0);
|
2014-04-26 11:00:44 -04:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_unset_flag(&q, Q_ENABLED);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_flags, ==, 0);
|
|
|
|
g_assert_cmpuint(count_flags, ==, 0);
|
2014-04-26 11:00:44 -04:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&q, Q_SAVE_FLAGS);
|
|
|
|
queue_set_flag(&q, Q_ENABLED);
|
|
|
|
queue_set_flag(&q, Q_RANDOM);
|
|
|
|
queue_set_flag(&q, Q_REPEAT);
|
|
|
|
queue_set_flag(&q, Q_NO_SORT);
|
2015-11-25 09:04:10 -05:00
|
|
|
queue_set_flag(&q, Q_ADD_FRONT);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_true(queue_has_flag(&q, Q_ENABLED));
|
|
|
|
g_assert_true(queue_has_flag(&q, Q_RANDOM));
|
|
|
|
g_assert_true(queue_has_flag(&q, Q_REPEAT));
|
|
|
|
g_assert_true(queue_has_flag(&q, Q_NO_SORT));
|
|
|
|
g_assert_true(queue_has_flag(&q, Q_SAVE_FLAGS));
|
|
|
|
g_assert_true(queue_has_flag(&q, Q_ADD_FRONT));
|
|
|
|
g_assert_cmpuint(count_flags, ==, 6);
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_unset_flag(&q, Q_ENABLED);
|
|
|
|
queue_unset_flag(&q, Q_RANDOM);
|
|
|
|
queue_unset_flag(&q, Q_REPEAT);
|
|
|
|
queue_unset_flag(&q, Q_NO_SORT);
|
2015-11-25 09:04:10 -05:00
|
|
|
queue_unset_flag(&q, Q_ADD_FRONT);
|
|
|
|
queue_unset_flag(&q, Q_SAVE_FLAGS);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_flags, ==, 0);
|
|
|
|
g_assert_cmpuint(count_flags, ==, 11);
|
2015-04-04 09:37:03 -04:00
|
|
|
}
|
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
static void test_queue(gconstpointer arg)
|
2014-05-11 10:40:54 -04:00
|
|
|
{
|
2016-03-11 09:12:33 -05:00
|
|
|
unsigned int N = GPOINTER_TO_UINT(arg);
|
2015-11-13 11:14:40 -05:00
|
|
|
unsigned int ex_length = 0;
|
|
|
|
unsigned int ex_size = N;
|
2016-04-03 09:29:32 -04:00
|
|
|
struct queue_iter it;
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *track;
|
2015-11-13 11:14:40 -05:00
|
|
|
unsigned int i;
|
2015-11-23 08:36:29 -05:00
|
|
|
struct queue q;
|
2015-08-28 09:26:22 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
count_added = 0;
|
|
|
|
count_deleted = 0;
|
2016-01-29 10:12:09 -05:00
|
|
|
count_cleared = 0;
|
2015-11-13 11:14:40 -05:00
|
|
|
count_updated = 0;
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, 0, &test_ops, NULL);
|
2014-05-11 19:52:08 -04:00
|
|
|
|
2015-11-25 08:24:04 -05:00
|
|
|
/* queue_add() */
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < N; i++) {
|
|
|
|
track = track_get(i % 13);
|
|
|
|
ex_length += track->tr_length;
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_add(&q, track), ==, i);
|
|
|
|
g_assert_cmpuint(count_added, ==, i + 1);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(q.q_length, ==, ex_length);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
2014-05-11 11:23:15 -04:00
|
|
|
|
2016-04-03 09:29:32 -04:00
|
|
|
/* queue_iter_init() */
|
|
|
|
if (N > 0) {
|
|
|
|
queue_iter_init(&q, &it);
|
|
|
|
g_assert_nonnull(it.it_iter);
|
|
|
|
g_assert_cmpuint(it.it_pos, ==, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* queue_for_each() */
|
|
|
|
i = 0;
|
|
|
|
queue_for_each(&q, &it) {
|
|
|
|
g_assert_cmpuint(it.it_pos, ==, i);
|
|
|
|
g_assert(queue_iter_val(&it) == track_get(i % 13));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(i, ==, N);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
|
|
|
|
2016-04-16 16:42:54 -04:00
|
|
|
/* queue_remove_all() and queue_has() */
|
2015-11-13 11:14:40 -05:00
|
|
|
track = track_get(0);
|
|
|
|
ex_length -= track->tr_length * (N / 13);
|
|
|
|
ex_size -= (N / 13);
|
2016-04-16 16:42:54 -04:00
|
|
|
if (N > 0)
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_true(queue_has(&q, track));
|
2016-04-16 16:42:54 -04:00
|
|
|
else
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_false(queue_has(&q, track));
|
|
|
|
g_assert_cmpuint(queue_remove_all(&q, track), ==, N / 13);
|
|
|
|
g_assert_cmpuint(q.q_length, ==, ex_length);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
|
|
|
g_assert_false(queue_has(&q, track));
|
2015-11-13 11:14:40 -05:00
|
|
|
|
2016-01-22 08:11:09 -05:00
|
|
|
/* queue_erase() = false */
|
|
|
|
can_erase = false;
|
|
|
|
for (i = 0; i < ex_size; i += 11) {
|
|
|
|
queue_erase(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_length, ==, ex_length);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
|
|
|
}
|
2016-01-22 08:11:09 -05:00
|
|
|
|
|
|
|
/* queue_remove() and queue_erase() == true */
|
|
|
|
can_erase = true;
|
2015-11-13 11:14:40 -05:00
|
|
|
track = track_get(1);
|
|
|
|
ex_length -= track->tr_length * (N / 13);
|
|
|
|
ex_size -= (N / 13);
|
|
|
|
for (i = 0; i < ex_size; i += 11) {
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert(queue_at(&q, i) == track);
|
2016-01-22 08:11:09 -05:00
|
|
|
if (i % 2 == 0)
|
|
|
|
queue_remove(&q, i);
|
|
|
|
else
|
|
|
|
queue_erase(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
}
|
|
|
|
g_assert_cmpuint(q.q_length, ==, ex_length);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
2014-05-11 11:23:15 -04:00
|
|
|
|
2015-11-20 08:02:54 -05:00
|
|
|
/* queue_updated() */
|
2015-11-13 11:14:40 -05:00
|
|
|
track = track_get(2);
|
2015-11-20 08:02:54 -05:00
|
|
|
queue_updated(&q, track);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_updated, ==, N / 13);
|
2014-05-11 11:23:15 -04:00
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_null(queue_next(&q));
|
|
|
|
g_assert_cmpint(queue_size(&q), ==, ex_size);
|
2014-05-11 11:23:15 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
/* Tracks should not be removed. */
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&q, Q_ENABLED);
|
|
|
|
queue_set_flag(&q, Q_REPEAT);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < ex_size; i++) {
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert(queue_next(&q) == track_get((i % 11) + 2));
|
|
|
|
g_assert_cmpuint(count_updated, ==, (N / 13) + (2 * i) + 1);
|
2015-11-19 08:08:14 -05:00
|
|
|
queue_selected(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_updated, ==, (N / 13) + (2 * i) + 2);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
|
|
|
}
|
2014-05-11 11:23:15 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
/* Tracks should be removed. */
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_unset_flag(&q, Q_REPEAT);
|
2016-01-29 10:12:09 -05:00
|
|
|
for (i = 0; i < ex_size / 2; i++) {
|
|
|
|
track = queue_next(&q);
|
|
|
|
ex_length -= track->tr_length;
|
|
|
|
ex_size--;
|
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert(track == track_get((i % 11) + 2));
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, ex_size);
|
|
|
|
g_assert_cmpuint(q.q_length, ==, ex_length);
|
|
|
|
}
|
2014-05-11 10:40:54 -04:00
|
|
|
|
2016-01-29 10:12:09 -05:00
|
|
|
queue_clear(&q);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_cleared, ==, 1);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 0);
|
|
|
|
g_assert_cmpuint(q.q_length, ==, 0);
|
2016-01-29 10:12:09 -05:00
|
|
|
|
2015-12-07 10:00:19 -05:00
|
|
|
queue_deinit(&q);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(count_cleared, ==, 2);
|
|
|
|
g_assert_null(q.q_sort);
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
static void test_rand_select()
|
2014-05-11 18:32:53 -04:00
|
|
|
{
|
2015-11-13 11:14:40 -05:00
|
|
|
unsigned int i;
|
2015-11-23 08:36:29 -05:00
|
|
|
struct queue q;
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
g_random_set_seed(0);
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, Q_ENABLED | Q_RANDOM, &test_ops, NULL);
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
/* Call next() on an empty queue. */
|
|
|
|
for (i = 0; i < 13; i++) {
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_null(queue_next(&q));
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 0);
|
|
|
|
}
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&q, track_get(i));
|
2016-03-27 12:19:28 -04:00
|
|
|
queue_sort(&q, COMPARE_TRACK, true);
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
/*
|
|
|
|
* The comments below use the following notation:
|
|
|
|
* <val>: The value pointed to by q._cur.
|
|
|
|
* (val): The value selected by q.track_selected().
|
|
|
|
* [val]: The value picked by q.next().
|
|
|
|
*/
|
2015-08-28 09:26:22 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 9, q = { <>, 1, 2, 3, 4, 5, 6, 7, 8, [9], 10, 11, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 9);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 12);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 6, q = { 1, 2, 3, 4, 5, 6, (7), <8>, 10, 11, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 6)->tr_track, ==, 7);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 11);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 10, q = { 1, 2, 3, 4, [5], <6>, 8, 10, 11, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 5);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 10);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 7, q = { 1, 2, 3, <4>, 6, 8, 10, (11), 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 7)->tr_track, ==, 11);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 9);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 6, q = { 1, 2, 3, [4], 6, 8, <10>, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 4);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 8);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 2, q = { 1, 2, (<3>), 6, 8, 10, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 2)->tr_track, ==, 3);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 7);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 1, q = { 1, <2>, [6], 8, 10, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 6);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 6);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 1, q = { 1, (<2>), 8, 10, 12, 13 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 1)->tr_track, ==, 2);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 5);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 4, q = { <1>, 8, 10, 12, [13] } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 13);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 4);
|
2014-05-18 14:24:46 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 1, q = { [1], 8, 10, <12> } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 1);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 3);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 1, q = { <>, 8, (10), 12 } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 1)->tr_track, ==, 10);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 2);
|
2014-05-18 14:24:46 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* rand() = 1, q = { <8>, [12] } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_next(&q)->tr_track, ==, 12);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 1);
|
2014-05-18 14:24:46 -04:00
|
|
|
|
2016-03-11 14:56:44 -05:00
|
|
|
/* select = 0, q = { (<8>) } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_selected(&q, 0)->tr_track, ==, 8);
|
|
|
|
g_assert_cmpuint(queue_size(&q), ==, 0);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
/* q = { } */
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_null(queue_next(&q));
|
|
|
|
g_assert_null(queue_selected(&q, 3));
|
2015-12-07 10:00:19 -05:00
|
|
|
|
|
|
|
queue_deinit(&q);
|
2015-11-13 11:14:40 -05:00
|
|
|
}
|
2014-05-18 14:11:21 -04:00
|
|
|
|
2015-11-13 11:14:40 -05:00
|
|
|
static void test_sorting()
|
2014-05-18 14:11:21 -04:00
|
|
|
{
|
2016-07-27 08:15:15 -04:00
|
|
|
unsigned int ex_count[] = { 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 };
|
|
|
|
unsigned int ex_title[] = { 7, 10, 9, 4, 3, 6, 2, 5, 12, 11, 13, 1, 8 };
|
|
|
|
unsigned int ex_co_ti[] = { 4, 3, 6, 2, 5, 1, 7, 10, 9, 12, 11, 13, 8 };
|
2015-11-13 11:14:40 -05:00
|
|
|
struct track *track;
|
|
|
|
unsigned int i;
|
2015-11-23 08:36:29 -05:00
|
|
|
struct queue q;
|
2014-05-18 14:11:21 -04:00
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, Q_SAVE_SORT | Q_ADD_FRONT, &test_ops, NULL);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
|
|
|
track = track_get(i);
|
2016-07-27 08:15:15 -04:00
|
|
|
track->tr_count = (track->tr_track <= 6) ? 4 : 2;
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&q, track);
|
2015-11-13 11:14:40 -05:00
|
|
|
}
|
2014-05-18 14:11:21 -04:00
|
|
|
|
2015-11-25 09:04:10 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_dbe.dbe_index, ==, 12 - i);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 0);
|
2015-11-25 09:04:10 -05:00
|
|
|
|
|
|
|
queue_sort(&q, COMPARE_TRACK, true);
|
|
|
|
for (i = 0; i < 13; i++) {
|
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, i + 1);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 1);
|
2015-11-25 09:04:10 -05:00
|
|
|
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&q, COMPARE_COUNT, true);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
2015-11-19 13:50:08 -05:00
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, ex_count[i]);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 2);
|
2014-05-18 14:11:21 -04:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&q, Q_NO_SORT);
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&q, COMPARE_TITLE, true);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
2015-11-19 13:50:08 -05:00
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, ex_count[i]);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 2);
|
2014-05-18 14:11:21 -04:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_unset_flag(&q, Q_NO_SORT);
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&q, COMPARE_TITLE, true);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
2015-11-19 13:50:08 -05:00
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, ex_title[i]);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 3);
|
2015-11-13 11:14:40 -05:00
|
|
|
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&q, COMPARE_COUNT, true);
|
|
|
|
queue_sort(&q, COMPARE_TITLE, false);
|
|
|
|
queue_sort(&q, COMPARE_COUNT, false);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
2015-11-19 13:50:08 -05:00
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, ex_co_ti[i]);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 6);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_unset_flag(&q, Q_SAVE_SORT);
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&q, COMPARE_ARTIST, true);
|
|
|
|
queue_sort(&q, COMPARE_ALBUM, false);
|
|
|
|
queue_sort(&q, COMPARE_TRACK, false);
|
|
|
|
queue_sort(&q, COMPARE_TRACK, false);
|
2015-11-13 11:14:40 -05:00
|
|
|
for (i = 0; i < 13; i++) {
|
2015-11-19 13:50:08 -05:00
|
|
|
track = queue_at(&q, i);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_nonnull(track);
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, 13 - i);
|
|
|
|
}
|
|
|
|
g_assert_cmpuint(count_sort, ==, 6);
|
2014-05-11 10:18:48 -04:00
|
|
|
|
2015-12-07 10:00:19 -05:00
|
|
|
queue_deinit(&q);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(q.q_length, ==, 0);
|
|
|
|
g_assert_cmpuint(queue_size(&q),==, 0);
|
|
|
|
g_assert_null(q.q_sort);
|
2016-04-25 07:58:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_save_load()
|
|
|
|
{
|
2016-08-17 14:17:52 -04:00
|
|
|
struct file f = FILE_INIT("queue.q", 0);
|
2016-04-25 07:58:19 -04:00
|
|
|
struct queue q, r;
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
queue_init(&q, 0, &test_ops, NULL);
|
|
|
|
queue_init(&r, 0, &test_ops, NULL);
|
2016-04-25 07:58:19 -04:00
|
|
|
|
|
|
|
for (i = 0; i < 13; i++)
|
|
|
|
queue_add(&q, track_get(i));
|
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_false(file_exists(&f));
|
|
|
|
g_assert_true(file_open(&f, OPEN_WRITE));
|
2016-04-25 07:58:19 -04:00
|
|
|
queue_save_tracks(&q, &f);
|
|
|
|
file_close(&f);
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_true(file_exists(&f));
|
2016-04-25 07:58:19 -04:00
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_true(file_open(&f, OPEN_READ));
|
2016-04-25 07:58:19 -04:00
|
|
|
queue_load_tracks(&r, &f);
|
|
|
|
file_close(&f);
|
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
g_assert_cmpuint(queue_size(&r), ==, 13);
|
|
|
|
for (i = 0; i < 13; i++)
|
|
|
|
g_assert_true(queue_has(&r, track_get(i)));
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
2015-08-28 09:26:22 -04:00
|
|
|
|
2016-03-11 09:12:33 -05:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct library *library;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
idle_init_sync();
|
|
|
|
tags_init();
|
|
|
|
while (idle_run_task()) {}
|
|
|
|
|
|
|
|
library = library_find("tests/Music");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/03 - Hyrule Field.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/04 - Hyrule Castle.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/05 - Lon Lon Ranch.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/06 - Kakariko Village.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/07 - Death Mountain.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/08 - Zora's Domain.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/09 - Gerudo Valley.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/10 - Ganondorf.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/11 - Princess Zelda.ogg");
|
|
|
|
track_add(library, "tests/Music/Hyrule Symphony/12 - Ocarina Medley.ogg");
|
|
|
|
track_add(library,
|
|
|
|
"tests/Music/Hyrule Symphony/13 - The Legend of Zelda Medley.ogg");
|
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_add_func("/Core/Queue/Initialization", test_init);
|
|
|
|
g_test_add_func("/Core/Queue/Flags", test_flags);
|
|
|
|
g_test_add_data_func("/Core/Queue/n = 0", GUINT_TO_POINTER( 0), test_queue);
|
|
|
|
g_test_add_data_func("/Core/Queue/n = 13", GUINT_TO_POINTER( 13), test_queue);
|
|
|
|
g_test_add_data_func("/Core/Queue/n = 100,009)", GUINT_TO_POINTER(100009), test_queue);
|
|
|
|
g_test_add_func("/Core/Queue/Random Next and Selection", test_rand_select);
|
|
|
|
g_test_add_func("/Core/Queue/Sorting", test_sorting);
|
|
|
|
g_test_add_func("/Core/Queue/Save and Load", test_save_load);
|
|
|
|
ret = g_test_run();
|
|
|
|
|
|
|
|
tags_deinit();
|
|
|
|
idle_deinit();
|
|
|
|
return ret;
|
|
|
|
}
|