queue: Fix up the length_str() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-05-11 19:52:08 -04:00
parent 0cc8072159
commit bcfa735dd7
3 changed files with 52 additions and 36 deletions

View File

@ -5,7 +5,8 @@
#define OCARINA_QUEUE_H
#include <file.h>
#include <library.h>
#include <tags.h>
#include <vector>
#include <list>
@ -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);

View File

@ -2,7 +2,6 @@
* Copyright 2013 (c) Anna Schumaker.
*/
#include <callback.h>
#include <library.h>
#include <queue.h>
#include <stdlib.h>
@ -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 {

View File

@ -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)