From 83a8b5ae1dff86625b192a89d6e64625b7279905 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 23 Dec 2013 10:22:37 -0500 Subject: [PATCH] deck: Implement next() I make sure to remove empty playlists from the deck and throw an error if there are no playable tracks on the deck. Signed-off-by: Anna Schumaker --- design/deck.txt | 2 ++ include/deck.h | 1 + lib/deck.cpp | 17 +++++++++++++ tests/deck/deck.cpp | 24 ++++++++++++++++++ tests/deck/deck.good | 58 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+) diff --git a/design/deck.txt b/design/deck.txt index 1c15fa12..69224b6e 100644 --- a/design/deck.txt +++ b/design/deck.txt @@ -47,6 +47,8 @@ Deck: (lib/deck.cpp) If the playlist is empty after calling next(), remove it from the deck. + If there are no playable IDs, throw -1. + void deck :: reset(); This function only exists if CONFIG_DEBUG is enabled. Erase all the playlist information and reset the deck list. diff --git a/include/deck.h b/include/deck.h index a2d53639..658a159f 100644 --- a/include/deck.h +++ b/include/deck.h @@ -13,6 +13,7 @@ namespace deck void remove(unsigned int); Playlist *get(unsigned int); void move(unsigned int, unsigned int); + unsigned int next(); #ifdef CONFIG_DEBUG void print_info(); diff --git a/lib/deck.cpp b/lib/deck.cpp index 409d8b5a..26687634 100644 --- a/lib/deck.cpp +++ b/lib/deck.cpp @@ -48,6 +48,23 @@ void deck :: move(unsigned int old_pos, unsigned int new_pos) playlist_deck.splice(it_new, playlist_deck, it_old); } +unsigned int deck :: next() +{ + unsigned int id = 0; + std::list::iterator it; + + for (it = playlist_deck.begin(); it != playlist_deck.end(); it++) { + if (it->get_flags() & PL_ENABLED) { + id = it->next(); + if (it->size() == 0) + playlist_deck.erase(it); + return id; + } + } + + throw -1; +} + #ifdef CONFIG_DEBUG void deck :: print_info() { diff --git a/tests/deck/deck.cpp b/tests/deck/deck.cpp index 4d7db96f..d649ae18 100644 --- a/tests/deck/deck.cpp +++ b/tests/deck/deck.cpp @@ -68,11 +68,35 @@ void test_3() print("\n"); } +/* Test the next() function for playlists */ +void test_4() +{ + print("Test 4:\n"); + + deck :: get(0)->unset_flag(PL_ENABLED); + deck :: get(1)->unset_flag(PL_ENABLED); + deck :: get(4)->unset_flag(PL_ENABLED); + + for (unsigned int i = 0; i < 37; i++) { + try { + print("Playing id: %u\n", deck :: next()); + if (i == 11 || i == 25) + deck :: print_info(); + } catch (int error) { + print("No playable tracks!\n"); + break; + } + } + + deck :: print_info(); +} + int main(int argc, char **argv) { test_0(); test_1(); test_2(); test_3(); + test_4(); return 0; } diff --git a/tests/deck/deck.good b/tests/deck/deck.good index f7dfe37e..24974519 100644 --- a/tests/deck/deck.good +++ b/tests/deck/deck.good @@ -55,3 +55,61 @@ deck[2] = Playlist { size = 12, flags = 1 } deck[3] = Playlist { size = 14, flags = 1 } deck[4] = Playlist { size = 15, flags = 1 } deck[5] = Playlist { size = 10, flags = 1 } + +deck[0] = Playlist { size = 16, flags = 1 } +deck[1] = Playlist { size = 19, flags = 1 } +deck[2] = Playlist { size = 12, flags = 1 } +deck[3] = Playlist { size = 14, flags = 1 } +deck[4] = Playlist { size = 15, flags = 1 } +deck[5] = Playlist { size = 10, flags = 1 } + +Test 4: +Playing id: 0 +Playing id: 1 +Playing id: 2 +Playing id: 3 +Playing id: 4 +Playing id: 5 +Playing id: 6 +Playing id: 7 +Playing id: 8 +Playing id: 9 +Playing id: 10 +Playing id: 11 +deck[0] = Playlist { size = 16, flags = 0 } +deck[1] = Playlist { size = 19, flags = 0 } +deck[2] = Playlist { size = 14, flags = 1 } +deck[3] = Playlist { size = 15, flags = 0 } +deck[4] = Playlist { size = 10, flags = 1 } +Playing id: 0 +Playing id: 1 +Playing id: 2 +Playing id: 3 +Playing id: 4 +Playing id: 5 +Playing id: 6 +Playing id: 7 +Playing id: 8 +Playing id: 9 +Playing id: 10 +Playing id: 11 +Playing id: 12 +Playing id: 13 +deck[0] = Playlist { size = 16, flags = 0 } +deck[1] = Playlist { size = 19, flags = 0 } +deck[2] = Playlist { size = 15, flags = 0 } +deck[3] = Playlist { size = 10, flags = 1 } +Playing id: 0 +Playing id: 1 +Playing id: 2 +Playing id: 3 +Playing id: 4 +Playing id: 5 +Playing id: 6 +Playing id: 7 +Playing id: 8 +Playing id: 9 +No playable tracks! +deck[0] = Playlist { size = 16, flags = 0 } +deck[1] = Playlist { size = 19, flags = 0 } +deck[2] = Playlist { size = 15, flags = 0 }