2012-08-25 10:24:34 +02:00
|
|
|
#include "config.h"
|
2014-01-24 00:17:50 +01:00
|
|
|
#include "queue/Queue.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2012-08-25 10:24:34 +02:00
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
2013-01-30 22:53:12 +01:00
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
Tag::Tag(const Tag &) {}
|
2017-06-04 13:09:11 +02:00
|
|
|
void Tag::Clear() noexcept {}
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
static void
|
2014-01-20 08:57:22 +01:00
|
|
|
check_descending_priority(const Queue *queue,
|
2013-10-16 21:55:00 +02:00
|
|
|
unsigned start_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(start_order < queue->GetLength());
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
uint8_t last_priority = 0xff;
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned order = start_order; order < queue->GetLength(); ++order) {
|
|
|
|
unsigned position = queue->OrderToPosition(order);
|
2011-07-19 00:34:33 +02:00
|
|
|
uint8_t priority = queue->items[position].priority;
|
|
|
|
assert(priority <= last_priority);
|
2011-09-16 08:07:41 +02:00
|
|
|
(void)last_priority;
|
2011-07-19 00:34:33 +02:00
|
|
|
last_priority = priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
class QueuePriorityTest : public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(QueuePriorityTest);
|
|
|
|
CPPUNIT_TEST(TestPriority);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void TestPriority();
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
QueuePriorityTest::TestPriority()
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2014-01-07 21:39:47 +01:00
|
|
|
DetachedSong songs[16] = {
|
|
|
|
DetachedSong("0.ogg"),
|
|
|
|
DetachedSong("1.ogg"),
|
|
|
|
DetachedSong("2.ogg"),
|
|
|
|
DetachedSong("3.ogg"),
|
|
|
|
DetachedSong("4.ogg"),
|
|
|
|
DetachedSong("5.ogg"),
|
|
|
|
DetachedSong("6.ogg"),
|
|
|
|
DetachedSong("7.ogg"),
|
|
|
|
DetachedSong("8.ogg"),
|
|
|
|
DetachedSong("9.ogg"),
|
|
|
|
DetachedSong("a.ogg"),
|
|
|
|
DetachedSong("b.ogg"),
|
|
|
|
DetachedSong("c.ogg"),
|
|
|
|
DetachedSong("d.ogg"),
|
|
|
|
DetachedSong("e.ogg"),
|
|
|
|
DetachedSong("f.ogg"),
|
|
|
|
};
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2014-01-20 08:57:22 +01:00
|
|
|
Queue queue(32);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(songs); ++i)
|
2014-01-07 21:39:47 +01:00
|
|
|
queue.Append(DetachedSong(songs[i]), 0);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(unsigned(ARRAY_SIZE(songs)), queue.GetLength());
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/* priority=10 for 4 items */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriorityRange(4, 8, 10, -1);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
queue.random = true;
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.ShuffleOrder();
|
2011-07-19 00:34:33 +02:00
|
|
|
check_descending_priority(&queue, 0);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 4);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 4; i < 8; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) < 4);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 8; i < ARRAY_SIZE(songs); ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 4);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* priority=50 one more item */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriorityRange(15, 16, 50, -1);
|
2011-07-19 00:34:33 +02:00
|
|
|
check_descending_priority(&queue, 0);
|
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0u, queue.PositionToOrder(15));
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 4);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 4; i < 8; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 1 &&
|
|
|
|
queue.PositionToOrder(i) < 5);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 8; i < 15; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 5);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* priority=20 for one of the 4 priority=10 items */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriorityRange(3, 4, 20, -1);
|
2011-07-19 00:34:33 +02:00
|
|
|
check_descending_priority(&queue, 0);
|
|
|
|
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1u, queue.PositionToOrder(3));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0u, queue.PositionToOrder(15));
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 5);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 4; i < 8; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 2 &&
|
|
|
|
queue.PositionToOrder(i) < 6);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 8; i < 15; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(queue.PositionToOrder(i) >= 6);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* priority=20 for another one of the 4 priority=10 items;
|
|
|
|
pass "after_order" (with priority=10) and see if it's moved
|
|
|
|
after that one */
|
|
|
|
|
|
|
|
unsigned current_order = 4;
|
|
|
|
unsigned current_position =
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.OrderToPosition(current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
unsigned a_order = 3;
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned a_position = queue.OrderToPosition(a_order);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(10u, unsigned(queue.items[a_position].priority));
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriority(a_position, 20, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
current_order = queue.PositionToOrder(current_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(3u, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
a_order = queue.PositionToOrder(a_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(4u, a_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
check_descending_priority(&queue, current_order + 1);
|
|
|
|
|
|
|
|
/* priority=70 for one of the last items; must be inserted
|
|
|
|
right after the current song, before the priority=20 one we
|
|
|
|
just created */
|
|
|
|
|
|
|
|
unsigned b_order = 10;
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned b_position = queue.OrderToPosition(b_order);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0u, unsigned(queue.items[b_position].priority));
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriority(b_position, 70, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
current_order = queue.PositionToOrder(current_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(3u, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
b_order = queue.PositionToOrder(b_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(4u, b_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
check_descending_priority(&queue, current_order + 1);
|
|
|
|
|
|
|
|
/* move the prio=20 item back */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
a_order = queue.PositionToOrder(a_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(5u, a_order);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(20u, unsigned(queue.items[a_position].priority));
|
2013-01-06 21:33:58 +01:00
|
|
|
queue.SetPriority(a_position, 5, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
current_order = queue.PositionToOrder(current_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(3u, current_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
a_order = queue.PositionToOrder(a_position);
|
2013-10-16 21:55:00 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(6u, a_order);
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(QueuePriorityTest);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(gcc_unused int argc, gcc_unused char **argv)
|
|
|
|
{
|
|
|
|
CppUnit::TextUi::TestRunner runner;
|
|
|
|
auto ®istry = CppUnit::TestFactoryRegistry::getRegistry();
|
|
|
|
runner.addTest(registry.makeTest());
|
|
|
|
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|