test_queue_priority.cxx 4.6 KB
Newer Older
1
#include "config.h"
2
#include "Queue.hxx"
3
#include "song.h"
4
#include "Directory.hxx"
5

6
Directory detached_root;
7

8
Directory::Directory() {}
9 10
Directory::~Directory() {}

11 12 13
struct song *
song_dup_detached(const struct song *src)
{
14
	return const_cast<song *>(src);
15
}
16 17 18 19 20 21 22 23 24 25

void
song_free(G_GNUC_UNUSED struct song *song)
{
}

G_GNUC_UNUSED
static void
dump_order(const struct queue *queue)
{
26 27
	g_printerr("queue length=%u, order:\n", queue->GetLength());
	for (unsigned i = 0; i < queue->GetLength(); ++i)
28 29 30 31 32
		g_printerr("  [%u] -> %u (prio=%u)\n", i, queue->order[i],
			   queue->items[queue->order[i]].priority);
}

static void
33
check_descending_priority(const struct queue *queue,
34 35
			 unsigned start_order)
{
36
	assert(start_order < queue->GetLength());
37 38

	uint8_t last_priority = 0xff;
39 40
	for (unsigned order = start_order; order < queue->GetLength(); ++order) {
		unsigned position = queue->OrderToPosition(order);
41 42
		uint8_t priority = queue->items[position].priority;
		assert(priority <= last_priority);
43
		(void)last_priority;
44 45 46 47 48 49 50
		last_priority = priority;
	}
}

int
main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
{
51
	static struct song songs[16];
52

53
	struct queue queue(32);
54 55

	for (unsigned i = 0; i < G_N_ELEMENTS(songs); ++i)
56
		queue.Append(&songs[i], 0);
57

58
	assert(queue.GetLength() == G_N_ELEMENTS(songs));
59 60 61

	/* priority=10 for 4 items */

62
	queue.SetPriorityRange(4, 8, 10, -1);
63 64

	queue.random = true;
65
	queue.ShuffleOrder();
66 67 68
	check_descending_priority(&queue, 0);

	for (unsigned i = 0; i < 4; ++i) {
69
		assert(queue.PositionToOrder(i) >= 4);
70 71 72
	}

	for (unsigned i = 4; i < 8; ++i) {
73
		assert(queue.PositionToOrder(i) < 4);
74 75 76
	}

	for (unsigned i = 8; i < G_N_ELEMENTS(songs); ++i) {
77
		assert(queue.PositionToOrder(i) >= 4);
78 79 80 81
	}

	/* priority=50 one more item */

82
	queue.SetPriorityRange(15, 16, 50, -1);
83 84
	check_descending_priority(&queue, 0);

85
	assert(queue.PositionToOrder(15) == 0);
86 87

	for (unsigned i = 0; i < 4; ++i) {
88
		assert(queue.PositionToOrder(i) >= 4);
89 90 91
	}

	for (unsigned i = 4; i < 8; ++i) {
92 93
		assert(queue.PositionToOrder(i) >= 1 &&
		       queue.PositionToOrder(i) < 5);
94 95 96
	}

	for (unsigned i = 8; i < 15; ++i) {
97
		assert(queue.PositionToOrder(i) >= 5);
98 99 100 101
	}

	/* priority=20 for one of the 4 priority=10 items */

102
	queue.SetPriorityRange(3, 4, 20, -1);
103 104
	check_descending_priority(&queue, 0);

105 106
	assert(queue.PositionToOrder(3) == 1);
	assert(queue.PositionToOrder(15) == 0);
107 108

	for (unsigned i = 0; i < 3; ++i) {
109
		assert(queue.PositionToOrder(i) >= 5);
110 111 112
	}

	for (unsigned i = 4; i < 8; ++i) {
113 114
		assert(queue.PositionToOrder(i) >= 2 &&
		       queue.PositionToOrder(i) < 6);
115 116 117
	}

	for (unsigned i = 8; i < 15; ++i) {
118
		assert(queue.PositionToOrder(i) >= 6);
119 120 121 122 123 124 125 126
	}

	/* 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 =
127
		queue.OrderToPosition(current_order);
128 129

	unsigned a_order = 3;
130
	unsigned a_position = queue.OrderToPosition(a_order);
131
	assert(queue.items[a_position].priority == 10);
132
	queue.SetPriority(a_position, 20, current_order);
133

134
	current_order = queue.PositionToOrder(current_position);
135 136
	assert(current_order == 3);

137
	a_order = queue.PositionToOrder(a_position);
138 139 140 141 142 143 144 145 146
	assert(a_order == 4);

	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;
147
	unsigned b_position = queue.OrderToPosition(b_order);
148
	assert(queue.items[b_position].priority == 0);
149
	queue.SetPriority(b_position, 70, current_order);
150

151
	current_order = queue.PositionToOrder(current_position);
152 153
	assert(current_order == 3);

154
	b_order = queue.PositionToOrder(b_position);
155 156 157 158 159 160 161 162 163
	assert(b_order == 4);

	check_descending_priority(&queue, current_order + 1);

	/* priority=60 for the old prio50 item; must not be moved,
	   because it's before the current song, and it's status
	   hasn't changed (it was already higher before) */

	unsigned c_order = 0;
164
	unsigned c_position = queue.OrderToPosition(c_order);
165
	assert(queue.items[c_position].priority == 50);
166
	queue.SetPriority(c_position, 60, current_order);
167

168
	current_order = queue.PositionToOrder(current_position);
169 170
	assert(current_order == 3);

171
	c_order = queue.PositionToOrder(c_position);
172 173 174 175
	assert(c_order == 0);

	/* move the prio=20 item back */

176
	a_order = queue.PositionToOrder(a_position);
177 178
	assert(a_order == 5);
	assert(queue.items[a_position].priority == 20);
179
	queue.SetPriority(a_position, 5, current_order);
180

181
	current_order = queue.PositionToOrder(current_position);
182 183
	assert(current_order == 3);

184
	a_order = queue.PositionToOrder(a_position);
185 186
	assert(a_order == 6);
}