223 lines
4.5 KiB
C++
223 lines
4.5 KiB
C++
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <idle.h>
|
|
#include <playqueue.h>
|
|
#include <print.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <string>
|
|
|
|
void test_flags(const std :: string &test, Playqueue &pqueue, unsigned int expected)
|
|
{
|
|
print("Test %s: ", test.c_str());
|
|
if (pqueue.get_flags() == expected)
|
|
print("SUCCESS\n");
|
|
else
|
|
print("FAILED\n");
|
|
}
|
|
|
|
void test_add_tracks(const std :: string & test, Playqueue &pqueue,
|
|
unsigned int start, unsigned int end)
|
|
{
|
|
bool passed = true;
|
|
print("Test %s: ", test.c_str());
|
|
for (unsigned int i = start; i <= end; i++) {
|
|
if (pqueue.add(i) != (i - start))
|
|
passed = false;
|
|
}
|
|
|
|
if (passed == true)
|
|
print("SUCCESS\n");
|
|
else
|
|
print("FAILED\n");
|
|
}
|
|
|
|
void test_add_tracks_front(const std :: string &test, Playqueue &pqueue,
|
|
unsigned int n)
|
|
{
|
|
bool passed = true;
|
|
|
|
print("Test %s: ", test.c_str());
|
|
for (unsigned int i = 0; i < n; i++) {
|
|
if (pqueue.add_front(i) != 0)
|
|
passed = false;
|
|
}
|
|
|
|
if (passed == true)
|
|
print("SUCCESS\n");
|
|
else
|
|
print("FAILED\n");
|
|
}
|
|
|
|
void test_rm_tracks(const std :: string & test, Playqueue & pqueue, unsigned int n)
|
|
{
|
|
unsigned int size = pqueue.size();
|
|
unsigned int expected = size - n;
|
|
|
|
print("Test %s: ", test.c_str());
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
|
pqueue.del(i);
|
|
|
|
if (pqueue.size() == expected)
|
|
print("SUCCESS\n");
|
|
else
|
|
print("FAILED\n");
|
|
}
|
|
|
|
void test_pqueue_status(const std :: string &test, Playqueue &pqueue)
|
|
{
|
|
print("Test %s: size: %u, length: %u\n", test.c_str(),
|
|
pqueue.size(), pqueue.get_length());
|
|
}
|
|
|
|
/* Test flag setting / unsetting / getting */
|
|
void test_0()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
|
|
test_flags("0a", pqueue, PQ_ENABLED);
|
|
pqueue.set_flag(PQ_RANDOM);
|
|
test_flags("0b", pqueue, PQ_ENABLED | PQ_RANDOM);
|
|
pqueue.set_flag(PQ_ENABLED);
|
|
test_flags("0c", pqueue, PQ_ENABLED | PQ_RANDOM);
|
|
pqueue.unset_flag(PQ_ENABLED);
|
|
test_flags("0d", pqueue, PQ_RANDOM);
|
|
pqueue.unset_flag(PQ_RANDOM);
|
|
test_flags("0f", pqueue, 0);
|
|
print("\n");
|
|
}
|
|
|
|
/* Test adding / deleting / size queries */
|
|
void test_1()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
test_add_tracks("1a", pqueue, 10, 49);
|
|
test_pqueue_status("1b", pqueue);
|
|
test_rm_tracks("1c", pqueue, 10);
|
|
test_pqueue_status("1d", pqueue);
|
|
test_add_tracks_front("1e", pqueue, 5);
|
|
test_pqueue_status("1f", pqueue);
|
|
print("\n");
|
|
}
|
|
|
|
/* Test read / write */
|
|
void test_2()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
Playqueue pqueue2(PQ_ENABLED);
|
|
|
|
File f("pqueue.lst", FILE_TYPE_DATA);
|
|
|
|
pqueue.set_flag(PQ_RANDOM);
|
|
test_add_tracks("2a", pqueue, 0, 99);
|
|
|
|
f.open(OPEN_WRITE);
|
|
pqueue.write(f);
|
|
f.close();
|
|
|
|
f.open(OPEN_READ);
|
|
pqueue2.read(f);
|
|
f.close();
|
|
|
|
print("Test 2b: ");
|
|
if (pqueue.get_flags() != pqueue2.get_flags()) {
|
|
print("FAILED: flag mismatch\n");
|
|
return;
|
|
} else if (pqueue.size() != pqueue2.size()) {
|
|
print("FAILED: size mismatch\n");
|
|
return;
|
|
}
|
|
|
|
print("SUCCESS\n\n");
|
|
}
|
|
|
|
/* Sequential next() without removing tracks */
|
|
void test_3()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
pqueue.set_flag(PQ_REPEAT);
|
|
test_add_tracks("3a", pqueue, 0, 15);
|
|
for (unsigned int i = 0; i < 25; i++) {
|
|
print("Selecting id: %u\n", pqueue.next());
|
|
if (i == 4)
|
|
pqueue.reset_cur();
|
|
}
|
|
test_pqueue_status("3b", pqueue);
|
|
print("\n");
|
|
}
|
|
|
|
/* Sequential next() with removal */
|
|
void test_4()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
test_add_tracks("4a", pqueue, 0, 15);
|
|
while (pqueue.size() > 0)
|
|
print("Selecting id: %u\n", pqueue.next());
|
|
test_pqueue_status("4b", pqueue);
|
|
print("\n");
|
|
}
|
|
|
|
/* Random next() without removing tracks */
|
|
void test_5()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
pqueue.set_flag(PQ_RANDOM);
|
|
pqueue.set_flag(PQ_REPEAT);
|
|
test_add_tracks("5a", pqueue, 0, 15);
|
|
for (unsigned int i = 0; i < 30; i++)
|
|
print("Selecting id: %u\n", pqueue.next());
|
|
test_pqueue_status("5b", pqueue);
|
|
print("\n");
|
|
}
|
|
|
|
/* Random next() with removal */
|
|
void test_6()
|
|
{
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
pqueue.set_flag(PQ_RANDOM);
|
|
test_add_tracks("6a", pqueue, 0, 15);
|
|
while (pqueue.size() > 0)
|
|
print("Selecting id: %u\n", pqueue.next());
|
|
test_pqueue_status("6b", pqueue);
|
|
print("\n");
|
|
}
|
|
|
|
void test_7()
|
|
{
|
|
char test[] = "7a";
|
|
Playqueue pqueue(PQ_ENABLED);
|
|
|
|
for (unsigned int i = 0; i < 60; i++)
|
|
pqueue.add(i % 15);
|
|
test_pqueue_status(test, pqueue);
|
|
|
|
for (unsigned int i = 0; i < 15; i++) {
|
|
pqueue.del_track(i);
|
|
if ((i + 1) % 3 == 0) {
|
|
test[1]++;
|
|
test_pqueue_status(test, pqueue);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
srand(42);
|
|
library :: init();
|
|
library :: reset();
|
|
library :: add_path("/tmp/library/0");
|
|
while (idle :: run_task());
|
|
|
|
test_0();
|
|
test_1();
|
|
test_2();
|
|
test_3();
|
|
test_4();
|
|
test_5();
|
|
test_6();
|
|
test_7();
|
|
return 0;
|
|
}
|