From bcfa735dd7d1a30c00aa7ab18dd3c2c0834922fc Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 11 May 2014 19:52:08 -0400 Subject: [PATCH] queue: Fix up the length_str() function Signed-off-by: Anna Schumaker --- include/queue.h | 8 ++++--- lib/queue.cpp | 64 ++++++++++++++++++++++++------------------------- tests/queue.cpp | 16 +++++++++++++ 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/include/queue.h b/include/queue.h index c2ed2482..465f3d06 100644 --- a/include/queue.h +++ b/include/queue.h @@ -5,7 +5,8 @@ #define OCARINA_QUEUE_H #include -#include +#include + #include #include @@ -48,13 +49,14 @@ public: void unset_flag(queue_flags); bool has_flag(queue_flags); - std::string get_length_str(); - virtual unsigned int add(Track *); void del(Track *); void del(unsigned int); void updated(Track *); + unsigned int size(); + const std::string size_str(); + const std::string length_str(); void add_sort(sort_t, bool ascending = true); void reset_sort(sort_t, bool ascending = true); diff --git a/lib/queue.cpp b/lib/queue.cpp index 1cab2051..4ac3f76f 100644 --- a/lib/queue.cpp +++ b/lib/queue.cpp @@ -2,7 +2,6 @@ * Copyright 2013 (c) Anna Schumaker. */ #include -#include #include #include @@ -60,38 +59,6 @@ bool Queue :: has_flag(queue_flags f) return (_flags & f) == (unsigned int)f; } -static inline void add_duration(std::stringstream &ss, unsigned int dur, - unsigned int remaining, const std::string &field) -{ - if (dur > 0) { - ss << dur << " " << field; - if (dur > 1) - ss << "s"; - if (remaining > 0) - ss << ", "; - } -} - -std::string Queue :: get_length_str() -{ - std::stringstream ss; - unsigned int len = _length; - - unsigned int days = len / O_DAYS; - len -= days * O_DAYS; - add_duration(ss, days, len, "day"); - - unsigned int hours = len / O_HOURS; - len -= hours *O_HOURS; - add_duration(ss, hours, len, "hour"); - unsigned int mins = len / O_MINUTES; - add_duration(ss, mins, len, "minute"); - unsigned int secs = len - (mins * O_MINUTES); - add_duration(ss, secs, 0, "second"); - - return ss.str(); -} - /* * std::string.compare() returns * 0: Strings are equal @@ -202,6 +169,37 @@ unsigned int Queue :: size() return _tracks.size(); } +const std::string Queue :: size_str() +{ + std::stringstream ss; + ss << size(); + return ss.str(); +} + +const std::string Queue :: length_str() +{ + std::stringstream ss; + unsigned int factor[4] = { O_DAYS, O_HOURS, O_MINUTES, 1 }; + std::string fields[4] = { "day", "hour", "minute", "second" }; + unsigned int len = _length; + + for (unsigned int i = 0; i < 4; i++) { + unsigned int dur = len / factor[i]; + len -= dur * factor[i]; + + if (dur > 0) { + ss << dur << " " << fields[i]; + if (dur > 1) + ss << "s"; + if (len > 0) + ss << ", "; + } + } + + return ss.str(); +} + + /* Sorting function */ class SortTracks { diff --git a/tests/queue.cpp b/tests/queue.cpp index cdf9989b..ea94b797 100644 --- a/tests/queue.cpp +++ b/tests/queue.cpp @@ -104,6 +104,11 @@ void test_add_remove() get_callbacks()->on_queue_track_add = test_add_cb; get_callbacks()->on_queue_track_del = test_del_cb; + test_equal(q.get_length(), expected); + test_equal(q.length_str(), (std::string)""); + test_equal(q.size(), (unsigned)0); + test_equal(q.size_str(), (std::string)"0"); + /* Add tracks */ test :: begin(); @@ -113,6 +118,9 @@ void test_add_remove() } test :: success(); test_equal(q.get_length(), expected); + test_equal(q.length_str(), (std::string)"1 hour, 26 minutes"); + test_equal(q.size(), (unsigned)24); + test_equal(q.size_str(), (std::string)"24"); /* Add everything again */ @@ -123,7 +131,9 @@ void test_add_remove() } test :: success(); test_equal(q.get_length(), expected); + test_equal(q.length_str(), (std::string)"2 hours, 52 minutes"); test_equal(q.size(), (unsigned)48); + test_equal(q.size_str(), (std::string)"48"); /* Test removing multiple tracks at once */ @@ -135,7 +145,9 @@ void test_add_remove() } test :: success(); test_equal(q.get_length(), expected); + test_equal(q.length_str(), (std::string)"1 hour, 50 minutes"); test_equal(q.size(), (unsigned)24); + test_equal(q.size_str(), (std::string)"24"); /* Test removing tracks one at a time */ @@ -149,7 +161,9 @@ void test_add_remove() } test :: success(); test_equal(q.get_length(), expected); + test_equal(q.length_str(), (std::string)"55 minutes"); test_equal(q.size(), (unsigned)12); + test_equal(q.size_str(), (std::string)"12"); /* Remove remaining tracks */ @@ -160,7 +174,9 @@ void test_add_remove() q.del((unsigned)0); test :: success(); test_equal(q.get_length(), (unsigned)0); + test_equal(q.length_str(), (std::string)""); test_equal(q.size(), (unsigned)0); + test_equal(q.size_str(), (std::string)"0"); } void test_updated_cb(Queue *q, unsigned int row)