From 4998b0867b65d39e9c1ceef12df7ee47386fe777 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 3 Apr 2016 11:50:06 -0400 Subject: [PATCH] core/tags/track: Find the average play count of tracks This function returns the average count of tracks that have been played, and not the average count of all tracks. Implements #29: Efficiently calculate average play count of tracks Signed-off-by: Anna Schumaker --- core/tags/track.c | 7 +++++++ include/core/tags/track.h | 3 +++ tests/core/tags/track.c | 6 ++++++ tests/gui/view.c | 1 + 4 files changed, 17 insertions(+) diff --git a/core/tags/track.c b/core/tags/track.c index 99d656e5..f174c603 100644 --- a/core/tags/track.c +++ b/core/tags/track.c @@ -205,6 +205,13 @@ unsigned int track_db_count_plays() return play_count; } +unsigned int track_db_average_plays() +{ + if (unplayed_count == track_db.db_size) + return 0; + return play_count / (track_db.db_size - unplayed_count); +} + struct track *track_add(struct library *library, const gchar *filepath) { unsigned int offset = strlen(library->li_path) + 1; diff --git a/include/core/tags/track.h b/include/core/tags/track.h index e5b9cd4d..e62e55fb 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -80,6 +80,9 @@ unsigned int track_db_count_unplayed(); /* Called to find the total play count of all tracks in the database. */ unsigned int track_db_count_plays(); +/* Called to find the average play count of tracks in the database. */ +unsigned int track_db_average_plays(); + /* Called to add a track tag to the database. */ struct track *track_add(struct library *, const gchar *); diff --git a/tests/core/tags/track.c b/tests/core/tags/track.c index 81c1fdb4..f179b1eb 100644 --- a/tests/core/tags/track.c +++ b/tests/core/tags/track.c @@ -232,6 +232,7 @@ static void test_track_db() test_equal(track_db_count_unplayed(), 0); test_equal(track_db_count_plays(), 0); + test_equal(track_db_average_plays(), 0); track = track_add(library, path); test_verify_track(track); test_equal(track_db_count_unplayed(), 1); @@ -267,12 +268,15 @@ static void test_track_db() test_equal(track_db_count_unplayed(), 2); test_equal(track_db_count_plays(), 0); + test_equal(track_db_average_plays(), 0); track_played(track); test_equal(track_db_count_unplayed(), 1); test_equal(track_db_count_plays(), 1); + test_equal(track_db_average_plays(), 1); track_played(track); test_equal(track_db_count_unplayed(), 1); test_equal(track_db_count_plays(), 2); + test_equal(track_db_average_plays(), 2); track = track_add(library, "tests/Music/invalid_track"); test_equal((void *)track, NULL); @@ -289,9 +293,11 @@ static void test_track_db() test_equal(track_db_get()->db_size, 2); test_equal(track_db_count_unplayed(), 1); test_equal(track_db_count_plays(), 2); + test_equal(track_db_average_plays(), 2); track_remove(track_get(2)); test_equal(track_db_count_plays(), 0); + test_equal(track_db_average_plays(), 0); track_remove_all(library); test_equal(track_db_get()->db_size, 0); diff --git a/tests/gui/view.c b/tests/gui/view.c index 4729de7a..b82593a7 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -70,6 +70,7 @@ static void test_treeview() gui_builder_init("share/ocarina/ocarina6.glade"); gui_settings_init(); gui_view_init(); + while (idle_run_task()) {} collection_add("tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {}