queue: Update the next() function
I also added in code for testing. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
44f93d85e4
commit
124f275ffd
|
@ -53,6 +53,7 @@ public:
|
|||
void del(Track *);
|
||||
void del(unsigned int);
|
||||
void updated(Track *);
|
||||
Track *next();
|
||||
|
||||
unsigned int size();
|
||||
const std::string size_str();
|
||||
|
@ -64,7 +65,6 @@ public:
|
|||
std::list <sort_info> &get_sort_order();
|
||||
|
||||
Track *operator[](unsigned int);
|
||||
Track *next();
|
||||
void set_cur(unsigned int);
|
||||
void path_selected(unsigned int);
|
||||
#ifdef CONFIG_TEST
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
*/
|
||||
#include <callback.h>
|
||||
#include <queue.h>
|
||||
#include <random.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
|
@ -164,6 +164,28 @@ void Queue :: updated(Track *track)
|
|||
}
|
||||
}
|
||||
|
||||
Track *Queue :: next()
|
||||
{
|
||||
Track *res;
|
||||
|
||||
if (_tracks.size() == 0)
|
||||
return NULL;
|
||||
else if (_tracks.size() == 1)
|
||||
_cur = 0;
|
||||
else if (_flags & Q_RANDOM)
|
||||
_cur += random(1, _tracks.size() / 2);
|
||||
else
|
||||
_cur++;
|
||||
|
||||
_cur %= _tracks.size();
|
||||
res = _tracks[_cur];
|
||||
if (!(_flags & Q_REPEAT)) {
|
||||
del(_cur);
|
||||
_cur--;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned int Queue :: size()
|
||||
{
|
||||
return _tracks.size();
|
||||
|
@ -272,30 +294,6 @@ Track *Queue :: operator[](unsigned int i)
|
|||
return _tracks[i];
|
||||
}
|
||||
|
||||
Track *Queue :: next()
|
||||
{
|
||||
Track *res;
|
||||
|
||||
if (_tracks.size() == 0)
|
||||
return NULL;
|
||||
else if (_tracks.size() == 1)
|
||||
_cur = 0;
|
||||
else if (_flags & Q_RANDOM)
|
||||
_cur += rand() % (_tracks.size() / 2) + 1;
|
||||
else
|
||||
_cur++;
|
||||
|
||||
if (_cur >= _tracks.size())
|
||||
_cur -= _tracks.size();
|
||||
|
||||
res = _tracks[_cur];
|
||||
if (!(_flags & Q_REPEAT)) {
|
||||
del(_cur);
|
||||
_cur--;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void Queue :: set_cur(unsigned int c)
|
||||
{
|
||||
_cur = c;
|
||||
|
|
|
@ -17,7 +17,7 @@ tests = [
|
|||
("idle.cpp", False, [ "idle.cpp" ], []),
|
||||
("tags.cpp", True, [], [ "taglib" ]),
|
||||
("random.cpp", False, [ "random.cpp" ], []),
|
||||
("queue.cpp", True, [ "callback.cpp" ], []),
|
||||
("queue.cpp", True, [ "callback.cpp", "random.cpp" ], []),
|
||||
|
||||
]
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
#include <callback.h>
|
||||
#include <queue.h>
|
||||
#include <random.h>
|
||||
#include <tags.h>
|
||||
#include "test.h"
|
||||
|
||||
|
@ -11,6 +12,7 @@ unsigned int count_add = 0;
|
|||
unsigned int count_del = 0;
|
||||
unsigned int count_updated = 0;
|
||||
unsigned int last_update = 0;
|
||||
Track *TRACK_NULL = NULL;
|
||||
|
||||
|
||||
class TestQueue : public Queue
|
||||
|
@ -25,6 +27,7 @@ public:
|
|||
};
|
||||
|
||||
void test_add_cb_noop(Queue *q, unsigned int id) { }
|
||||
void test_del_cb_noop(Queue *q, unsigned int id) { }
|
||||
|
||||
|
||||
void test_default()
|
||||
|
@ -223,6 +226,64 @@ void test_updated()
|
|||
test_equal(count_updated, (unsigned)3);
|
||||
}
|
||||
|
||||
static void test_fill_q(TestQueue *q)
|
||||
{
|
||||
for (unsigned int i = 0; i < 24; i++)
|
||||
q->add(tagdb :: lookup(i));
|
||||
}
|
||||
|
||||
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 };
|
||||
|
||||
void test_next()
|
||||
{
|
||||
Track *track;
|
||||
TestQueue q(0);
|
||||
|
||||
get_callbacks()->on_queue_track_del = test_del_cb_noop;
|
||||
|
||||
test_fill_q(&q);
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < 24; i++) {
|
||||
track = q.next();
|
||||
check_not_equal(track, TRACK_NULL);
|
||||
check_equal(track->id, i);
|
||||
}
|
||||
test :: success();
|
||||
test :: equal(q.size(), (unsigned)0);
|
||||
test :: equal(q.length_str(), (std::string)"");
|
||||
test :: equal(q.next(), TRACK_NULL);
|
||||
|
||||
|
||||
q.set_flag(Q_RANDOM);
|
||||
random_seed(0);
|
||||
|
||||
test_fill_q(&q);
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < 24; i++) {
|
||||
track = q.next();
|
||||
check_not_equal(track, TRACK_NULL);
|
||||
check_equal(track->id, expected_rand[i]);
|
||||
}
|
||||
test :: success();
|
||||
test :: equal(q.size(), (unsigned)0);
|
||||
test :: equal(q.length_str(), (std::string)"");
|
||||
test :: equal(q.next(), TRACK_NULL);
|
||||
|
||||
|
||||
q.set_flag(Q_REPEAT);
|
||||
q.unset_flag(Q_RANDOM);
|
||||
test_fill_q(&q);
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < 48; i++) {
|
||||
track = q.next();
|
||||
check_not_equal(track, TRACK_NULL);
|
||||
check_equal(track->id, i % 24);
|
||||
}
|
||||
test :: success();
|
||||
test :: equal(q.size(), (unsigned)24);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test :: cp_library();
|
||||
|
@ -233,5 +294,6 @@ int main(int argc, char **argv)
|
|||
run_test("Queue Flag Test", test_flags);
|
||||
run_test("Queue Add and Remove Test", test_add_remove);
|
||||
run_test("Queue Track Updated Test", test_updated);
|
||||
run_test("Queue Pick Next Test", test_next);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue