From 9a16a18b9ff5303bee93269fa3a7c267256e9d38 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 16 Nov 2015 09:38:31 -0500 Subject: [PATCH] core/tags/album: Add an album_compare_year() function With special handling for albums with the same year but different names. Signed-off-by: Anna Schumaker --- core/queue.cpp | 3 +-- core/tags/album.c | 7 +++++++ include/core/tags/album.h | 5 ++++- tests/core/tags/album.c | 9 +++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/queue.cpp b/core/queue.cpp index 571a015c..db9555bf 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -94,8 +94,7 @@ static inline int track_compare(struct track *lhs, struct track *rhs, sort_t fie case SORT_TRACK: return lhs->tr_track - rhs->tr_track; case SORT_YEAR: - if (lhs->tr_album->al_year - rhs->tr_album->al_year != 0) - return lhs->tr_album->al_year - rhs->tr_album->al_year; + return album_compare_year(lhs->tr_album, rhs->tr_album); case SORT_ALBUM: return album_compare(lhs->tr_album, rhs->tr_album); } diff --git a/core/tags/album.c b/core/tags/album.c index 79f9732c..defa9c4c 100644 --- a/core/tags/album.c +++ b/core/tags/album.c @@ -105,6 +105,13 @@ int album_compare(struct album *lhs, struct album *rhs) return string_compare(lhs->al_lower, rhs->al_lower); } +int album_compare_year(struct album *lhs, struct album *rhs) +{ + if (lhs->al_year - rhs->al_year == 0) + return album_compare(lhs, rhs); + return lhs->al_year - rhs->al_year; +} + #ifdef CONFIG_TESTING const struct db_ops *test_album_ops() { return &album_ops; } #endif /* CONFIG_TESTING */ diff --git a/include/core/tags/album.h b/include/core/tags/album.h index 69237b13..173ba40a 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -38,9 +38,12 @@ struct album *album_find(const gchar *, unsigned int); /* Called to get an album tag with a specific index. */ struct album *album_get(const unsigned int); -/* Called to compare two album tags. */ +/* Called to compare two album tags by name. */ int album_compare(struct album *, struct album *); +/* Called to compare two album tags by year. */ +int album_compare_year(struct album *, struct album *); + #ifdef CONFIG_TESTING const struct db_ops *test_album_ops(); #endif /* CONFIG_TESTING */ diff --git a/tests/core/tags/album.c b/tests/core/tags/album.c index 66f1cd18..c864a66d 100644 --- a/tests/core/tags/album.c +++ b/tests/core/tags/album.c @@ -61,6 +61,15 @@ static void test_album_compare() test_equal(album_compare(twilight, skyward), 1); test_equal(album_compare(skyward, twilight), -1); + test_equal(album_compare_year(twilight, twilight), 0); + test_equal(album_compare_year(twilight, skyward), -5); + test_equal(album_compare_year(skyward, twilight), 5); + + skyward->al_year = 2006; + test_equal(album_compare_year(twilight, twilight), 0); + test_equal(album_compare_year(twilight, skyward), 1); + test_equal(album_compare_year(skyward, twilight), -1); + album_ops->dbe_free(&twilight->al_dbe); album_ops->dbe_free(&skyward->al_dbe); }