2014-04-13 12:26:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/callback.h>
|
|
|
|
#include <core/queue.h>
|
|
|
|
#include <core/random.h>
|
2014-12-02 08:16:22 -05:00
|
|
|
#include <core/tags/tags.h>
|
2014-06-14 10:50:18 -04:00
|
|
|
#include <tests/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;
|
2014-05-18 11:08:58 -04:00
|
|
|
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); }
|
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
|
|
|
};
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
TestQueue *Q = NULL, *R = NULL;
|
2014-05-11 18:32:53 -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);
|
|
|
|
test_equal(q.next(), (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);
|
|
|
|
test_equal(q.next(), (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);
|
2014-04-13 18:38:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
|
|
|
|
unsigned int _test_add_loop(unsigned int i)
|
2014-05-11 11:23:15 -04:00
|
|
|
{
|
2015-01-04 17:56:54 -05:00
|
|
|
Track *track = tags :: get_track(i % 24);
|
|
|
|
unsigned int index = Q->add(track);
|
|
|
|
expected += track->length();
|
|
|
|
if (index != i)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
if ((index + 1) != count_add)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return LOOP_PASSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_del_loop(unsigned int i)
|
|
|
|
{
|
|
|
|
Track *track = tags :: get_track(i);
|
|
|
|
unsigned int j = (i + 1) * 2;
|
|
|
|
|
|
|
|
Q->del(track);
|
|
|
|
expected -= track->length() * 2;
|
|
|
|
if (count_del != j)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return LOOP_PASSED;
|
2014-05-11 11:23:15 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int _test_del_middle_loop(unsigned int i)
|
2014-05-11 11:23:15 -04:00
|
|
|
{
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int j = 23 - i;
|
|
|
|
expected -= (*Q)[j]->length();
|
|
|
|
Q->del(j);
|
|
|
|
return (count_del == (i + 1)) ? LOOP_PASSED : LOOP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_del_front_loop(unsigned int i)
|
|
|
|
{
|
|
|
|
expected -= (*Q)[0]->length();
|
|
|
|
Q->del((unsigned int)0);
|
|
|
|
return (count_del == (i + 1)) ? LOOP_PASSED : LOOP_FAILED;
|
2014-05-11 11:23:15 -04:00
|
|
|
}
|
|
|
|
|
2014-05-11 10:40:54 -04:00
|
|
|
void test_add_remove()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
2015-01-04 17:56:54 -05:00
|
|
|
Q = &q;
|
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-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_add_loop);
|
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-01-04 17:56:54 -05:00
|
|
|
test_for_each(24, 48, 1, _test_add_loop);
|
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;
|
|
|
|
test_for_each(0, 12, 1, _test_del_loop);
|
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-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 12, 1, _test_del_middle_loop);
|
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;
|
|
|
|
test_for_each(0, 12, 1, _test_del_front_loop);
|
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-01-04 17:56:54 -05:00
|
|
|
static void test_fill_q()
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 24; i++)
|
|
|
|
Q->add(tags :: get_track(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_update_loop(unsigned int i)
|
|
|
|
{
|
|
|
|
Q->updated(tags :: get_track(i));
|
2015-04-03 20:54:45 -04:00
|
|
|
if (last_update != i)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return ((i+1) == count_updated) ? LOOP_PASSED : LOOP_FAILED;
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
2014-05-11 18:32:53 -04:00
|
|
|
void test_updated()
|
|
|
|
{
|
|
|
|
Track *track;
|
|
|
|
TestQueue q(0);
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
Q = &q;
|
2014-05-11 18:32:53 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
test_fill_q();
|
|
|
|
test_for_each(0, 24, 1, _test_update_loop);
|
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-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 };
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int _test_next_loop(unsigned int i)
|
2014-05-18 11:08:58 -04:00
|
|
|
{
|
2015-01-04 17:56:54 -05:00
|
|
|
Track *track = Q->next();
|
|
|
|
if (track == TRACK_NULL)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return (track->index() == (i % 24)) ? LOOP_PASSED : LOOP_FAILED;
|
2014-05-18 11:08:58 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int _test_next_rand_loop(unsigned int i)
|
|
|
|
{
|
|
|
|
Track *track = Q->next();
|
|
|
|
if (track == TRACK_NULL)
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return (track->index() == expected_rand[i]) ? LOOP_PASSED : LOOP_FAILED;
|
|
|
|
}
|
2014-05-18 11:08:58 -04:00
|
|
|
|
|
|
|
void test_next()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
Q = &q;
|
|
|
|
test_fill_q();
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_next_loop);
|
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-01-04 17:56:54 -05:00
|
|
|
test_fill_q();
|
2014-05-18 11:08:58 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_next_rand_loop);
|
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-01-04 17:56:54 -05:00
|
|
|
test_fill_q();
|
|
|
|
|
|
|
|
test_for_each(0, 48, 1, _test_next_loop);
|
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
|
|
|
|
|
|
|
Q = &q;
|
|
|
|
test_fill_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
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
unsigned int _test_sort_title(unsigned int i)
|
|
|
|
{
|
|
|
|
if ((*Q)[i]->index() == exp_sort_title[i])
|
|
|
|
return LOOP_PASSED;
|
|
|
|
return LOOP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_sort_title_reverse(unsigned int i)
|
|
|
|
{
|
|
|
|
if ((*Q)[i]->index() == exp_sort_title[23 - i])
|
|
|
|
return LOOP_PASSED;
|
|
|
|
return LOOP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_sort_index(unsigned int i)
|
|
|
|
{
|
|
|
|
if ((*Q)[i]->index() == i)
|
|
|
|
return LOOP_PASSED;
|
|
|
|
return LOOP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int _test_sort_ye_ti(unsigned int i)
|
|
|
|
{
|
2015-01-06 08:24:33 -05:00
|
|
|
if ((*Q)[i]->index() != exp_sort_ye_ti[i])
|
|
|
|
return LOOP_FAILED;
|
|
|
|
return LOOP_PASSED;
|
2015-01-04 17:56:54 -05:00
|
|
|
}
|
|
|
|
|
2014-05-18 14:11:21 -04:00
|
|
|
void test_sorting()
|
|
|
|
{
|
|
|
|
TestQueue q(0);
|
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
Q = &q;
|
2014-05-18 14:11:21 -04:00
|
|
|
q.sort(SORT_TITLE, true);
|
|
|
|
test_equal(q.get_sorder().size(), (size_t)1);
|
2015-01-04 17:56:54 -05:00
|
|
|
test_fill_q();
|
|
|
|
test_for_each(0, 24, 1, _test_sort_title);
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
q.sort(SORT_TITLE, false);
|
|
|
|
test_equal(q.get_sorder().size(), (size_t)1);
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_sort_title_reverse);
|
2014-05-18 14:11:21 -04:00
|
|
|
|
|
|
|
q.sort(SORT_LENGTH, true);
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 1, 24, _test_sort_index);
|
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-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_sort_ye_ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int _test_saving_loop(unsigned int i)
|
|
|
|
{
|
|
|
|
if ((*Q)[i]->index() == (*R)[i]->index())
|
|
|
|
return LOOP_PASSED;
|
|
|
|
return LOOP_FAILED;
|
2014-05-18 14:11:21 -04:00
|
|
|
}
|
|
|
|
|
2014-05-18 14:39:20 -04:00
|
|
|
void test_saving()
|
|
|
|
{
|
|
|
|
TestQueue q(Q_RANDOM);
|
|
|
|
TestQueue r(0);
|
2014-05-26 19:10:24 -04:00
|
|
|
File f("test.q", 0);
|
2014-05-18 14:39:20 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
Q = &q;
|
|
|
|
R = &r;
|
|
|
|
test_fill_q();
|
2014-05-18 14:39:20 -04:00
|
|
|
|
|
|
|
f.open(OPEN_WRITE);
|
|
|
|
q.write(f);
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
f.open(OPEN_READ);
|
|
|
|
r.read(f);
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
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());
|
2015-01-04 17:56:54 -05:00
|
|
|
test_for_each(0, 24, 1, _test_saving_loop);
|
2014-05-18 14:39:20 -04:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-05-26 19:10:24 -04:00
|
|
|
test :: cp_data_dir();
|
2014-12-02 08:16:22 -05:00
|
|
|
tags :: init();
|
2014-05-11 10:18:48 -04:00
|
|
|
|
2015-01-04 17:56:54 -05:00
|
|
|
test :: run("Queue Default Constructor Test", test_default);
|
|
|
|
test :: run("Queue Constructor Test", test_constructor);
|
|
|
|
test :: run("Queue Flag Test", test_flags);
|
|
|
|
test :: run("Queue Add and Remove Test", test_add_remove);
|
|
|
|
test :: run("Queue Track Updated Test", test_updated);
|
|
|
|
test :: run("Queue Pick Next Test", test_next);
|
|
|
|
test :: run("Queue Select Track Test", test_select);
|
|
|
|
test :: run("Queue Sorting Test", test_sorting);
|
|
|
|
test :: run("Queue Save and Load Test", test_saving);
|
2014-04-13 12:26:32 -04:00
|
|
|
return 0;
|
|
|
|
}
|