From 51379c7e8c2921a13c8c5c1d865279b150d910c5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 12 Dec 2015 11:26:59 -0500 Subject: [PATCH] core/history: Add history_prev() function Signed-off-by: Anna Schumaker --- core/audio.cpp | 4 +++- core/deck.cpp | 7 ------- core/history.c | 5 +++++ include/core/deck.h | 5 ----- include/core/history.h | 3 +++ tests/core/deck.cpp | 10 ---------- tests/core/history.c | 13 +++++++++++++ 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index 29a47238..c2108f82 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -132,11 +132,13 @@ int64_t audio :: duration() void audio :: next() { _load_track(deck :: next(), cur_driver->is_playing()); + if (cur_track) + history_add(cur_track); } void audio :: prev() { - _load_track(deck :: prev(), cur_driver->is_playing()); + _load_track(history_prev(), cur_driver->is_playing()); } void audio :: load_track(struct track *track) diff --git a/core/deck.cpp b/core/deck.cpp index 018eeb1d..0aace50b 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -217,16 +217,9 @@ struct track *deck :: next() if (!track) track = queue_next(collection_get_queue()); - if (track) - history_add(track); return track; } -struct track *deck :: prev() -{ - return queue_next(history_get_queue()); -} - std::list &deck :: get_queues() { return queue_deck; diff --git a/core/history.c b/core/history.c index 4e92a093..e336db81 100644 --- a/core/history.c +++ b/core/history.c @@ -23,6 +23,11 @@ void history_add(struct track *track) _q_iter_set(&history_queue.q_tracks, &history_queue.q_cur, 0); } +struct track *history_prev() +{ + return queue_next(&history_queue); +} + struct queue *history_get_queue() { return &history_queue; diff --git a/include/core/deck.h b/include/core/deck.h index a9c62b2c..709b1d17 100644 --- a/include/core/deck.h +++ b/include/core/deck.h @@ -100,11 +100,6 @@ namespace deck */ struct track *next(); - /** - * @return A track from the recent tracks queue. - */ - struct track *prev(); - /** * @return The deck of queues. */ diff --git a/include/core/history.h b/include/core/history.h index 5a837389..23f3947c 100644 --- a/include/core/history.h +++ b/include/core/history.h @@ -16,6 +16,9 @@ void history_deinit(); /* Called to add a track to the history queue. */ void history_add(struct track *); +/* Called to pick a track from the history. */ +struct track *history_prev(); + /* Called to access the queue of recent tracks. */ struct queue *history_get_queue(); diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index f2d65114..44e5f19b 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -116,22 +116,12 @@ static void test_next_prev() test_not_equal(q, Q_NULL); test_equal(queue_size(q), (unsigned)0); - test_equal(deck :: prev(), TRACK_NULL); for (unsigned int i = 0; i < 2; i++) { test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)0); test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)1); test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)2); test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)3); - test_equal(queue_size(q), 4 * (i + 1)); - } - - for (unsigned int i = 0; i < 2; i++) { - if (i == 1) - test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)3); - test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)2); - test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)1); - test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)0); } test_equal(deck :: get_queues().size(), (size_t)1); diff --git a/tests/core/history.c b/tests/core/history.c index 0d9a7016..bddc7f6b 100644 --- a/tests/core/history.c +++ b/tests/core/history.c @@ -38,18 +38,31 @@ static void test_history() { const struct database *track_db = track_db_get(); struct queue *q = history_get_queue(); + struct track *prev = track_get(0); struct db_entry *track, *next; unsigned int i = 0; + test_equal((void *)history_prev(), NULL); + /* Add tracks once */ db_for_each(track, next, track_db) { history_add(TRACK(track)); test_loop_equal(queue_size(q), i + 1, i); + test_loop_equal(q->q_cur.it_pos, 0, i); test_loop_equal((void *)queue_at(q, 0), (void *)TRACK(track), i); + test_loop_equal((void *)history_prev(), (void *)prev, i); + prev = TRACK(track); i++; } test_loop_passed(); test_equal(queue_size(q), track_db->db_size); + /* Cycle through the history queue. */ + _q_iter_set(&q->q_tracks, &q->q_cur, 0); + for (i = 2; i <= track_db->db_size; i++) { + test_loop_equal((void *)history_prev(), + (void *)track_get(track_db->db_size - i), i); + } test_loop_passed(); + i = 0; /* Add tracks again, old tracks should remain */ db_for_each(track, next, track_db) {