playqueue: Updates for my recent renaiming
Rename everything from playlist -> playqueue. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
98e8324a61
commit
82a57ec2b5
|
@ -1,33 +1,33 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2013 (c) Anna Schumaker.
|
* Copyright 2013 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
#ifndef OCARINA_PLAYLIST_H
|
#ifndef OCARINA_PLAYQUEUE_H
|
||||||
#define OCARINA_PLAYLIST_H
|
#define OCARINA_PLAYQUEUE_H
|
||||||
|
|
||||||
#include <file.h>
|
#include <file.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
enum playlist_flags {
|
enum playqueue_flags {
|
||||||
PL_ENABLED = (1 << 0),
|
PQ_ENABLED = (1 << 0),
|
||||||
PL_RANDOM = (1 << 1),
|
PQ_RANDOM = (1 << 1),
|
||||||
PL_LOCKED = (1 << 2),
|
PQ_REPEAT = (1 << 2),
|
||||||
};
|
};
|
||||||
|
|
||||||
class Playlist {
|
class Playqueue {
|
||||||
private:
|
private:
|
||||||
std :: vector <unsigned int> tracks;
|
std :: vector <unsigned int> tracks;
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
unsigned int cur;
|
unsigned int cur;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Playlist();
|
Playqueue();
|
||||||
Playlist(playlist_flags);
|
Playqueue(playqueue_flags);
|
||||||
~Playlist();
|
~Playqueue();
|
||||||
void write(File &);
|
void write(File &);
|
||||||
void read(File &);
|
void read(File &);
|
||||||
|
|
||||||
void set_flag(playlist_flags);
|
void set_flag(playqueue_flags);
|
||||||
void unset_flag(playlist_flags);
|
void unset_flag(playqueue_flags);
|
||||||
const unsigned int get_flags();
|
const unsigned int get_flags();
|
||||||
|
|
||||||
unsigned int add(unsigned int);
|
unsigned int add(unsigned int);
|
||||||
|
@ -37,4 +37,4 @@ public:
|
||||||
unsigned int next();
|
unsigned int next();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* OCARINA_PLAYLIST_H */
|
#endif /* OCARINA_PLAYQUEUE_H */
|
||||||
|
|
|
@ -23,7 +23,7 @@ modules = {
|
||||||
"IDLE" : Module("idle.cpp"),
|
"IDLE" : Module("idle.cpp"),
|
||||||
"LIBRARY" : Module("library.cpp", package = "taglib", depends = [ "DATABASE", "FILTER", "IDLE" ]),
|
"LIBRARY" : Module("library.cpp", package = "taglib", depends = [ "DATABASE", "FILTER", "IDLE" ]),
|
||||||
"PLAYLIST" : Module("playlist.cpp", depends = [ "DATABASE" ]),
|
"PLAYLIST" : Module("playlist.cpp", depends = [ "DATABASE" ]),
|
||||||
"PLAYQUEUE" : Module("playlist.cpp", depends = [ "FILE" ]),
|
"PLAYQUEUE" : Module("playqueue.cpp", depends = [ "FILE" ]),
|
||||||
|
|
||||||
###########################
|
###########################
|
||||||
###########################
|
###########################
|
||||||
|
|
|
@ -1,31 +1,31 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2013 (c) Anna Schumaker.
|
* Copyright 2013 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
#include <playlist.h>
|
#include <playqueue.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Playlist :: Playlist()
|
Playqueue :: Playqueue()
|
||||||
: flags(0), cur(-1)
|
: flags(0), cur(-1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Playlist :: Playlist(playlist_flags f)
|
Playqueue :: Playqueue(playqueue_flags f)
|
||||||
: flags(f), cur(-1)
|
: flags(f), cur(-1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Playlist :: ~Playlist()
|
Playqueue :: ~Playqueue()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist :: write(File &f)
|
void Playqueue :: write(File &f)
|
||||||
{
|
{
|
||||||
f << flags << " " << tracks.size();
|
f << flags << " " << tracks.size();
|
||||||
for (unsigned int i = 0; i < tracks.size(); i++)
|
for (unsigned int i = 0; i < tracks.size(); i++)
|
||||||
f << " " << tracks[i];
|
f << " " << tracks[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist :: read(File &f)
|
void Playqueue :: read(File &f)
|
||||||
{
|
{
|
||||||
unsigned int n;
|
unsigned int n;
|
||||||
f >> flags >> n;
|
f >> flags >> n;
|
||||||
|
@ -34,44 +34,44 @@ void Playlist :: read(File &f)
|
||||||
f >> tracks[i];
|
f >> tracks[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist :: set_flag(playlist_flags f)
|
void Playqueue :: set_flag(playqueue_flags f)
|
||||||
{
|
{
|
||||||
flags |= f;
|
flags |= f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist :: unset_flag(playlist_flags f)
|
void Playqueue :: unset_flag(playqueue_flags f)
|
||||||
{
|
{
|
||||||
flags &= ~f;
|
flags &= ~f;
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int Playlist :: get_flags()
|
const unsigned int Playqueue :: get_flags()
|
||||||
{
|
{
|
||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Playlist :: add(unsigned int track_id)
|
unsigned int Playqueue :: add(unsigned int track_id)
|
||||||
{
|
{
|
||||||
tracks.push_back(track_id);
|
tracks.push_back(track_id);
|
||||||
return tracks.size() - 1;
|
return tracks.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist :: del(unsigned int plist_id)
|
void Playqueue :: del(unsigned int plist_id)
|
||||||
{
|
{
|
||||||
tracks.erase(tracks.begin() + plist_id);
|
tracks.erase(tracks.begin() + plist_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Playlist :: size()
|
unsigned int Playqueue :: size()
|
||||||
{
|
{
|
||||||
return tracks.size();
|
return tracks.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Playlist :: next()
|
unsigned int Playqueue :: next()
|
||||||
{
|
{
|
||||||
unsigned int res;
|
unsigned int res;
|
||||||
|
|
||||||
if (tracks.size() == 1)
|
if (tracks.size() == 1)
|
||||||
cur = 0;
|
cur = 0;
|
||||||
else if (flags & PL_RANDOM)
|
else if (flags & PQ_RANDOM)
|
||||||
cur += rand() % (tracks.size() / 2) + 1;
|
cur += rand() % (tracks.size() / 2) + 1;
|
||||||
else
|
else
|
||||||
cur++;
|
cur++;
|
||||||
|
@ -80,7 +80,7 @@ unsigned int Playlist :: next()
|
||||||
cur -= tracks.size();
|
cur -= tracks.size();
|
||||||
|
|
||||||
res = tracks[cur];
|
res = tracks[cur];
|
||||||
if (!(flags & PL_LOCKED)) {
|
if (!(flags & PQ_REPEAT)) {
|
||||||
tracks.erase(tracks.begin() + cur);
|
tracks.erase(tracks.begin() + cur);
|
||||||
cur--;
|
cur--;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
Import("Test", "CONFIG")
|
Import("Test", "CONFIG")
|
||||||
|
|
||||||
CONFIG.PLAYLIST = True
|
CONFIG.PLAYQUEUE = True
|
||||||
|
|
||||||
Test("playlist", "playlist.cpp")
|
Test("playqueue", "playqueue.cpp")
|
||||||
|
|
|
@ -1,164 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2013 (c) Anna Schumaker.
|
|
||||||
*/
|
|
||||||
#include <playlist.h>
|
|
||||||
#include <print.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
void test_flags(const std :: string &test, Playlist &plist, unsigned int expected)
|
|
||||||
{
|
|
||||||
print("Test %s: ", test.c_str());
|
|
||||||
if (plist.get_flags() == expected)
|
|
||||||
print("SUCCESS\n");
|
|
||||||
else
|
|
||||||
print("FAILED\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_add_tracks(const std :: string & test, Playlist &plist,
|
|
||||||
unsigned int start, unsigned int end)
|
|
||||||
{
|
|
||||||
bool passed = true;
|
|
||||||
print("Test %s: ", test.c_str());
|
|
||||||
for (unsigned int i = start; i <= end; i++) {
|
|
||||||
if (plist.add(i) != (i - start))
|
|
||||||
passed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (passed == true)
|
|
||||||
print("SUCCESS\n");
|
|
||||||
else
|
|
||||||
print("FAILED\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_rm_tracks(const std :: string & test, Playlist & plist, unsigned int n)
|
|
||||||
{
|
|
||||||
unsigned int size = plist.size();
|
|
||||||
unsigned int expected = size - n;
|
|
||||||
|
|
||||||
print("Test %s: ", test.c_str());
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < n; i++)
|
|
||||||
plist.del(i);
|
|
||||||
|
|
||||||
if (plist.size() == expected)
|
|
||||||
print("SUCCESS\n");
|
|
||||||
else
|
|
||||||
print("FAILED\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Test flag setting / unsetting / getting */
|
|
||||||
void test_0()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
|
|
||||||
test_flags("0a", plist, PL_ENABLED);
|
|
||||||
plist.set_flag(PL_RANDOM);
|
|
||||||
test_flags("0b", plist, PL_ENABLED | PL_RANDOM);
|
|
||||||
plist.set_flag(PL_ENABLED);
|
|
||||||
test_flags("0c", plist, PL_ENABLED | PL_RANDOM);
|
|
||||||
plist.unset_flag(PL_ENABLED);
|
|
||||||
test_flags("0d", plist, PL_RANDOM);
|
|
||||||
plist.unset_flag(PL_RANDOM);
|
|
||||||
test_flags("0f", plist, 0);
|
|
||||||
print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Test adding / deleting / size queries */
|
|
||||||
void test_1()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
test_add_tracks("1a", plist, 10, 49);
|
|
||||||
print("Test 1b: Plist size: %u\n", plist.size());
|
|
||||||
test_rm_tracks("1c", plist, 10);
|
|
||||||
print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Test read / write */
|
|
||||||
void test_2()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
Playlist plist2(PL_ENABLED);
|
|
||||||
|
|
||||||
File f("plist.lst", FILE_TYPE_DATA);
|
|
||||||
|
|
||||||
plist.set_flag(PL_RANDOM);
|
|
||||||
test_add_tracks("2a", plist, 0, 99);
|
|
||||||
|
|
||||||
f.open(OPEN_WRITE);
|
|
||||||
plist.write(f);
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
f.open(OPEN_READ);
|
|
||||||
plist2.read(f);
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
print("Test 2b: ");
|
|
||||||
if (plist.get_flags() != plist2.get_flags()) {
|
|
||||||
print("FAILED: flag mismatch\n");
|
|
||||||
return;
|
|
||||||
} else if (plist.size() != plist2.size()) {
|
|
||||||
print("FAILED: size mismatch\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
print("SUCCESS\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Sequential next() without removing tracks */
|
|
||||||
void test_3()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
plist.set_flag(PL_LOCKED);
|
|
||||||
test_add_tracks("3", plist, 0, 15);
|
|
||||||
for (unsigned int i = 0; i < 20; i++)
|
|
||||||
print("Selecting id: %u\n", plist.next());
|
|
||||||
print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Sequential next() with removal */
|
|
||||||
void test_4()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
test_add_tracks("4", plist, 0, 15);
|
|
||||||
while (plist.size() > 0)
|
|
||||||
print("Selecting id: %u\n", plist.next());
|
|
||||||
print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Random next() without removing tracks */
|
|
||||||
void test_5()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
plist.set_flag(PL_RANDOM);
|
|
||||||
plist.set_flag(PL_LOCKED);
|
|
||||||
test_add_tracks("5", plist, 0, 15);
|
|
||||||
for (unsigned int i = 0; i < 30; i++)
|
|
||||||
print("Selecting id: %u\n", plist.next());
|
|
||||||
print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Random next() with removal */
|
|
||||||
void test_6()
|
|
||||||
{
|
|
||||||
Playlist plist(PL_ENABLED);
|
|
||||||
plist.set_flag(PL_RANDOM);
|
|
||||||
test_add_tracks("6", plist, 0, 15);
|
|
||||||
while (plist.size() > 0)
|
|
||||||
print("Selecting id: %u\n", plist.next());
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
srand(42);
|
|
||||||
|
|
||||||
test_0();
|
|
||||||
test_1();
|
|
||||||
test_2();
|
|
||||||
test_3();
|
|
||||||
test_4();
|
|
||||||
test_5();
|
|
||||||
test_6();
|
|
||||||
return 0;
|
|
||||||
}
|
|
164
tests/playqueue/playqueue.cpp
Normal file
164
tests/playqueue/playqueue.cpp
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013 (c) Anna Schumaker.
|
||||||
|
*/
|
||||||
|
#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_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");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
print("Test 1b: Plist size: %u\n", pqueue.size());
|
||||||
|
test_rm_tracks("1c", pqueue, 10);
|
||||||
|
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("3", pqueue, 0, 15);
|
||||||
|
for (unsigned int i = 0; i < 20; i++)
|
||||||
|
print("Selecting id: %u\n", pqueue.next());
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sequential next() with removal */
|
||||||
|
void test_4()
|
||||||
|
{
|
||||||
|
Playqueue pqueue(PQ_ENABLED);
|
||||||
|
test_add_tracks("4", pqueue, 0, 15);
|
||||||
|
while (pqueue.size() > 0)
|
||||||
|
print("Selecting id: %u\n", pqueue.next());
|
||||||
|
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("5", pqueue, 0, 15);
|
||||||
|
for (unsigned int i = 0; i < 30; i++)
|
||||||
|
print("Selecting id: %u\n", pqueue.next());
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Random next() with removal */
|
||||||
|
void test_6()
|
||||||
|
{
|
||||||
|
Playqueue pqueue(PQ_ENABLED);
|
||||||
|
pqueue.set_flag(PQ_RANDOM);
|
||||||
|
test_add_tracks("6", pqueue, 0, 15);
|
||||||
|
while (pqueue.size() > 0)
|
||||||
|
print("Selecting id: %u\n", pqueue.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
srand(42);
|
||||||
|
|
||||||
|
test_0();
|
||||||
|
test_1();
|
||||||
|
test_2();
|
||||||
|
test_3();
|
||||||
|
test_4();
|
||||||
|
test_5();
|
||||||
|
test_6();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user