2014-04-13 12:26:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-09-29 16:25:16 -04:00
|
|
|
#include <core/filter.h>
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/queue.h>
|
2015-09-09 09:00:17 -04:00
|
|
|
extern "C" {
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/random.h>
|
2015-09-09 09:00:17 -04:00
|
|
|
}
|
2014-12-02 08:16:22 -05:00
|
|
|
#include <core/tags/tags.h>
|
2015-08-28 09:26:22 -04:00
|
|
|
#include "test.h"
|
2014-04-13 12:26:32 -04:00
|
|
|
|
2014-04-13 18:38:49 -04:00
|
|
|
|
2014-05-11 10:40:54 -04:00
|
|
|
unsigned int count_add = 0;
|
2014-05-11 11:23:15 -04:00
|
|
|
unsigned int count_del = 0;
|
2014-05-11 18:32:53 -04:00
|
|
|
unsigned int count_updated = 0;
|
|
|
|
unsigned int last_update = 0;
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int expected = 0;
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *TRACK_NULL = NULL;
|
2014-05-11 10:40:54 -04:00
|
|
|
|
|
|
|
|
2015-04-03 10:30:46 -04:00
|
|
|
static class TestNotifier : public QNotifier {
|
|
|
|
public:
|
|
|
|
TestNotifier() : QNotifier() {}
|
2015-04-03 12:24:26 -04:00
|
|
|
void on_track_added(unsigned int i) { count_add++; }
|
|
|
|
void on_track_removed(unsigned int i) { count_del++; }
|
2015-04-03 20:54:45 -04:00
|
|
|
void on_track_updated(unsigned int i) { count_updated++; last_update = i; }
|
2015-04-03 10:30:46 -04:00
|
|
|
} test_notifier;
|
|
|
|
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
class TestQueue : public Queue {
|
2014-04-13 12:26:32 -04:00
|
|
|
public:
|
2015-04-03 10:30:46 -04:00
|
|
|
TestQueue() : Queue() { set_notifier(&test_notifier); }
|
|
|
|
TestQueue(unsigned int f) : Queue(f) { set_notifier(&test_notifier); }
|
2015-04-04 10:17:28 -04:00
|
|
|
~TestQueue() {}
|
2014-04-13 12:26:32 -04:00
|
|
|
unsigned int get_cur() { return _cur; }
|
|
|
|
unsigned int get_flags() { return _flags; }
|
2015-04-03 10:30:46 -04:00
|
|
|
QNotifier *get_notify() { return _notify; }
|
2014-05-18 14:11:21 -04:00
|
|
|
std::vector <sort_info> get_sorder() { return _sort_order; };
|
2014-04-13 12:26:32 -04:00
|
|
|
};
|
|
|
|
|
2014-04-13 18:38:49 -04:00
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
void test_default()
|
|
|
|
{
|
|
|
|
TestQueue q;
|
|
|
|
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_cur(), (unsigned)-1);
|
|
|
|
test_equal(q.get_flags(), (unsigned)0);
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), (unsigned)0);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_sorder().size(), (size_t)0);
|
2015-10-24 19:34:45 -04:00
|
|
|
test_equal(q.next(), (struct track *)NULL);
|
2015-04-03 10:30:46 -04:00
|
|
|
test_equal(q.get_notify(), &test_notifier);
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
2015-01-11 09:36:37 -05:00
|
|
|
void test_constructor()
|
2014-04-13 18:38:49 -04:00
|
|
|
{
|
2015-01-11 09:36:37 -05:00
|
|
|
unsigned int flags = Q_ENABLED | Q_RANDOM;
|
2014-12-07 09:57:43 -05:00
|
|
|
TestQueue q(flags);
|
2014-04-13 18:38:49 -04:00
|
|
|
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_cur(), (unsigned)-1);
|
|
|
|
test_equal(q.get_flags(), flags);
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), (unsigned)0);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_sorder().size(), (size_t)0);
|
2015-10-24 19:34:45 -04:00
|
|
|
test_equal(q.next(), (struct track *)NULL);
|
2015-04-03 10:30:46 -04:00
|
|
|
test_equal(q.get_notify(), &test_notifier);
|
2014-04-26 11:00:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_flags()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
|
|
|
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_flags(), (unsigned)0);
|
2014-04-26 11:00:44 -04:00
|
|
|
|
|
|
|
q.set_flag(Q_ENABLED);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_flags(), (unsigned)Q_ENABLED);
|
2014-04-26 11:00:44 -04:00
|
|
|
|
|
|
|
q.unset_flag(Q_ENABLED);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.get_flags(), (unsigned)0);
|
2014-04-26 11:00:44 -04:00
|
|
|
|
|
|
|
q.set_flag(Q_REPEAT);
|
|
|
|
q.set_flag(Q_RANDOM);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.has_flag(Q_ENABLED), false);
|
|
|
|
test_equal(q.has_flag(Q_RANDOM), true);
|
|
|
|
test_equal(q.has_flag(Q_REPEAT), true);
|
|
|
|
test_equal(q.has_flag(Q_NO_SORT), false);
|
2015-04-04 09:37:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-11 10:40:54 -04:00
|
|
|
void test_add_remove()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *track;
|
2015-08-28 09:26:22 -04:00
|
|
|
|
|
|
|
test_cp_data_dir();
|
2015-09-29 16:25:16 -04:00
|
|
|
filter_init();
|
2015-08-28 09:26:22 -04:00
|
|
|
tags :: init();
|
2014-05-11 10:40:54 -04:00
|
|
|
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), expected);
|
2014-05-11 19:52:08 -04:00
|
|
|
test_equal(q.size(), (unsigned)0);
|
|
|
|
|
2014-05-11 11:23:15 -04:00
|
|
|
/* Add tracks */
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++) {
|
|
|
|
track = tags :: get_track(i);
|
|
|
|
expected += track->length();
|
|
|
|
test_loop_equal(q.add(track), i, i);
|
|
|
|
test_loop_equal(count_add, i + 1, i);
|
|
|
|
} test_loop_passed();
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), expected);
|
2014-05-11 19:52:08 -04:00
|
|
|
test_equal(q.size(), (unsigned)24);
|
2014-05-11 11:23:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Add everything again */
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 24; i < 48; i++) {
|
|
|
|
track = tags :: get_track(i - 24);
|
|
|
|
expected += track->length();
|
|
|
|
test_loop_equal(q.add(track), i, i);
|
|
|
|
test_loop_equal(count_add, i + 1, i);
|
|
|
|
} test_loop_passed();
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), expected);
|
2014-05-11 11:23:15 -04:00
|
|
|
test_equal(q.size(), (unsigned)48);
|
|
|
|
|
|
|
|
|
|
|
|
/* Test removing multiple tracks at once */
|
2015-01-04 17:56:54 -05:00
|
|
|
count_del = 0;
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 12; i++) {
|
|
|
|
track = tags :: get_track(i);
|
|
|
|
q.del(track);
|
|
|
|
expected -= track->length() * 2;
|
|
|
|
test_loop_equal(count_del, (i + 1) * 2, i);
|
|
|
|
}
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), expected);
|
2014-05-11 11:23:15 -04:00
|
|
|
test_equal(q.size(), (unsigned)24);
|
|
|
|
|
|
|
|
|
|
|
|
/* Test removing tracks one at a time */
|
|
|
|
count_del = 0;
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 12; i++) {
|
|
|
|
expected -= q[23 - i]->length();
|
|
|
|
q.del((unsigned int)(23 - i));
|
|
|
|
test_loop_equal(count_del, (i + 1), i);
|
|
|
|
} test_loop_passed();
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), expected);
|
2014-05-11 11:23:15 -04:00
|
|
|
test_equal(q.size(), (unsigned)12);
|
2014-05-11 10:40:54 -04:00
|
|
|
|
2014-05-11 11:23:15 -04:00
|
|
|
|
|
|
|
/* Remove remaining tracks */
|
2015-01-04 17:56:54 -05:00
|
|
|
count_del = 0;
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 12; i++) {
|
|
|
|
expected -= q[0]->length();
|
|
|
|
q.del((unsigned int)0);
|
|
|
|
test_loop_equal(count_del, i + 1, i);
|
|
|
|
} test_loop_passed();
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), (unsigned)0);
|
2014-05-11 11:23:15 -04:00
|
|
|
test_equal(q.size(), (unsigned)0);
|
2014-05-11 10:40:54 -04:00
|
|
|
}
|
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
static void test_fill_q(TestQueue *q)
|
2015-01-04 17:56:54 -05:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
2015-08-28 09:26:22 -04:00
|
|
|
q->add(tags :: get_track(i));
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
2014-05-11 18:32:53 -04:00
|
|
|
void test_updated()
|
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *track;
|
2014-05-11 18:32:53 -04:00
|
|
|
TestQueue q(0);
|
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++) {
|
|
|
|
q.updated(tags :: get_track(i));
|
|
|
|
test_loop_equal(last_update, i, i);
|
|
|
|
test_loop_equal(count_updated, i + 1, i);
|
|
|
|
} test_loop_passed();
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2014-12-02 08:16:22 -05:00
|
|
|
track = tags :: get_track(0);
|
2014-05-11 18:32:53 -04:00
|
|
|
q.add(track);
|
|
|
|
q.add(track);
|
|
|
|
q.updated(track);
|
2015-04-03 20:54:45 -04:00
|
|
|
test_equal(count_updated, (unsigned)24 + 3);
|
2014-05-11 18:32:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int expected_rand[] = { 1, 4, 8, 13, 19, 3, 14, 16, 20, 2, 11, 17,
|
|
|
|
23, 6, 12, 18, 0, 5, 9, 10, 15, 21, 22, 7 };
|
|
|
|
|
2014-05-18 11:08:58 -04:00
|
|
|
void test_next()
|
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *track;
|
2014-05-18 11:08:58 -04:00
|
|
|
TestQueue q(0);
|
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++) {
|
|
|
|
track = q.next();
|
|
|
|
test_loop_not_equal(track, NULL, i);
|
|
|
|
test_loop_equal(track->index(), i % 24, i);
|
|
|
|
} test_loop_passed();
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.size(), (unsigned)0);
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), 0);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.next(), TRACK_NULL);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
q.set_flag(Q_RANDOM);
|
|
|
|
random_seed(0);
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++) {
|
|
|
|
track = q.next();
|
|
|
|
test_loop_not_equal(track, NULL, i);
|
|
|
|
test_loop_equal(track->index(), expected_rand[i], i);
|
|
|
|
} test_loop_passed();
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.size(), (unsigned)0);
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(q.length(), 0);
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.next(), TRACK_NULL);
|
2014-05-18 11:08:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
q.set_flag(Q_REPEAT);
|
|
|
|
q.unset_flag(Q_RANDOM);
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 48; i++) {
|
|
|
|
track = q.next();
|
|
|
|
test_loop_not_equal(track, NULL, i);
|
|
|
|
test_loop_equal(track->index(), i % 24, i);
|
|
|
|
} test_loop_passed();
|
2014-05-18 14:24:46 -04:00
|
|
|
test_equal(q.size(), (unsigned)24);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_select()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2014-05-18 14:24:46 -04:00
|
|
|
|
|
|
|
test_equal(q.size(), (unsigned)24);
|
|
|
|
q.track_selected(10);
|
|
|
|
test_equal(q.size(), (unsigned)23);
|
2014-11-02 10:02:35 -05:00
|
|
|
test_equal(q.next()->index(), (unsigned)11);
|
2014-05-18 14:24:46 -04:00
|
|
|
|
|
|
|
q.set_flag(Q_REPEAT);
|
|
|
|
q.track_selected(0);
|
|
|
|
test_equal(q.size(), (unsigned)22);
|
2014-11-02 10:02:35 -05:00
|
|
|
test_equal(q.next()->index(), (unsigned)1);
|
2014-05-18 11:08:58 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
|
2014-05-18 14:11:21 -04:00
|
|
|
unsigned int exp_sort_title[] = { 1, 18, 19, 16, 20, 8, 2, 9, 23, 10, 17, 11,
|
|
|
|
3, 21, 4, 0, 5, 22, 6, 12, 7, 13, 14, 15 };
|
|
|
|
unsigned int exp_sort_ye_ti[] = { 0, 3, 2, 1, 7, 6, 5, 4, 11, 10, 9, 8,
|
2015-01-06 08:24:33 -05:00
|
|
|
17, 16, 19, 18, 22, 21, 23, 20, 15, 14, 13, 12 };
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
void test_sorting()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
|
|
|
|
|
|
|
q.sort(SORT_TITLE, true);
|
|
|
|
test_equal(q.get_sorder().size(), (size_t)1);
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
test_loop_equal(q[i]->index(), exp_sort_title[i], i);
|
|
|
|
test_loop_passed();
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
q.sort(SORT_TITLE, false);
|
|
|
|
test_equal(q.get_sorder().size(), (size_t)1);
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
test_loop_equal(q[i]->index(), exp_sort_title[23 - i], i);
|
|
|
|
test_loop_passed();
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
q.sort(SORT_LENGTH, true);
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
test_loop_equal(q[i]->index(), i, i);
|
|
|
|
test_loop_passed();
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
q.sort(SORT_YEAR, true);
|
|
|
|
q.sort(SORT_TITLE, false);
|
|
|
|
q.sort(SORT_TITLE, false);
|
|
|
|
test_equal(q.get_sorder().size(), (size_t)2);
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
test_loop_equal(q[i]->index(), exp_sort_ye_ti[i], i);
|
|
|
|
test_loop_passed();
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-18 14:39:20 -04:00
|
|
|
void test_saving()
|
|
|
|
{
|
|
|
|
TestQueue q(Q_RANDOM);
|
|
|
|
TestQueue r(0);
|
2015-09-10 10:33:24 -04:00
|
|
|
file f;
|
2014-05-18 14:39:20 -04:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
test_fill_q(&q);
|
2014-05-18 14:39:20 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&f, "test.q", 0);
|
2015-09-10 09:46:33 -04:00
|
|
|
file_open(&f, OPEN_WRITE);
|
2014-05-18 14:39:20 -04:00
|
|
|
q.write(f);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-05-18 14:39:20 -04:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
file_open(&f, OPEN_READ);
|
2014-05-18 14:39:20 -04:00
|
|
|
r.read(f);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-05-18 14:39:20 -04:00
|
|
|
|
|
|
|
test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM));
|
|
|
|
test_equal(r.size(), q.size());
|
2015-01-29 08:27:25 -05:00
|
|
|
test_equal(r.length(), q.length());
|
2014-05-11 10:18:48 -04:00
|
|
|
|
2015-08-28 09:26:22 -04:00
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
test_loop_equal(q[i]->index(), r[i]->index(), i);
|
|
|
|
test_loop_passed();
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
2015-08-28 09:26:22 -04:00
|
|
|
|
|
|
|
DECLARE_UNIT_TESTS(
|
|
|
|
UNIT_TEST("Queue Default Constructor", test_default),
|
|
|
|
UNIT_TEST("Queue Constructor", test_constructor),
|
|
|
|
UNIT_TEST("Queue Flags", test_flags),
|
|
|
|
UNIT_TEST("Queue Add and Remove", test_add_remove),
|
|
|
|
UNIT_TEST("Queue Track Updated", test_updated),
|
|
|
|
UNIT_TEST("Queue Pick Next Track", test_next),
|
|
|
|
UNIT_TEST("Queue Select Track", test_select),
|
|
|
|
UNIT_TEST("Queue Sorting", test_sorting),
|
|
|
|
UNIT_TEST("Queue Save and Load", test_saving),
|
|
|
|
);
|