string: Create a utos() function

This function converts unsigned ints into strings.  This allows me to
replace several almost identical functions with one function call.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-26 09:40:48 -05:00
parent e47eb69859
commit 6c6437c2bd
13 changed files with 71 additions and 55 deletions

View File

@ -201,13 +201,6 @@ 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;

13
core/string.cpp Normal file
View File

@ -0,0 +1,13 @@
/**
* @file
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/string.h>
#include <sstream>
const std::string string :: utos(unsigned int u)
{
std::stringstream ss;
ss << u;
return ss.str();
}

View File

@ -5,6 +5,7 @@
#include <core/deck.h>
#include <core/filter.h>
#include <core/playlist.h>
#include <core/string.h>
#include <gui/tabs.h>
#include <map>
@ -128,11 +129,8 @@ void Tab :: tab_dec_sort_count()
void Tab :: tab_set_size()
{
if (tab_size) {
std::stringstream ss;
ss << tab_pq->size();
tab_size->set_text(ss.str());
}
if (tab_size)
tab_size->set_text(string :: utos(tab_pq->size()));
}
void Tab :: tab_unmap()

View File

@ -165,13 +165,6 @@ public:
*/
unsigned int size();
/**
* Find the size of the queue, as a string.
*
* @return The number of tracks on the queue, in string form.
*/
const std::string size_str();
/**
* Find the runtime of the queue.
*

25
include/core/string.h Normal file
View File

@ -0,0 +1,25 @@
/**
* @file
* Copyright 2015 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_STRING_H
#define OCARINA_CORE_STRING_H
#include <string>
/**
* Namespace for string formatting functions.
*/
namespace string
{
/**
* Convert an unsigned integer into a string.
* @param u Integer to be converted.
* @return u, in string form.
*/
const std::string utos(unsigned int);
}
#endif /* OCARINA_CORE_STRING_H */

View File

@ -1,4 +1,5 @@
version
string
file
database
index

View File

@ -8,6 +8,7 @@ def test(name):
test( "version" )
test( "string" )
test( "file" )
test( "database" )
test( "index" )

View File

@ -2,11 +2,10 @@
* Copyright 2014 (c) Anna Schumaker.
* Test a Database
*/
#include <core/database.h>
#include <core/string.h>
#include <tests/test.h>
#include <sstream>
#include <vector>
@ -22,9 +21,7 @@ public:
const std::string primary_key() const
{
std::stringstream ss;
ss << val;
return ss.str();
return string :: utos(val);
}
void write(File &f) { f << val; }
@ -167,13 +164,9 @@ static void test_save_load()
static void db_test(unsigned int n, bool autosave)
{
std::string n_str = " (n = " + string :: utos(n) + ")";
Database<IntEntry> db("database.db", autosave);
std::vector<IntEntry *> pointers;
std::stringstream ss;
std::string n_str;
ss << " (n = " << n << ")";
n_str = ss.str();
N = n;
DB = &db;

View File

@ -2,12 +2,10 @@
* Copyright 2014 (c) Anna Schumaker.
* Test the idle queue
*/
#include <core/idle.h>
#include <core/string.h>
#include <tests/test.h>
#include <sstream>
static float N = 0;
static float cur = -1;
static bool func_passed = false;
@ -45,9 +43,7 @@ static void test_idle_queue()
static void do_test(unsigned int n)
{
std::stringstream ss;
ss << " (n = " << n << ")";
const std::string n_str = ss.str();
std::string n_str = " (n = " + string :: utos(n) + ")";
N = n;
cur = -1;

View File

@ -4,10 +4,9 @@
*/
#include <core/index.h>
#include <core/print.h>
#include <core/string.h>
#include <tests/test.h>
#include <sstream>
static unsigned int N = 0;
static Index *INDEX = NULL;
@ -115,12 +114,8 @@ static void test_saving()
static void index_test(unsigned int n)
{
std::string n_str = " (n = " + string :: utos(n) + ")";
Index index("index.idx", false);
std::stringstream ss;
std::string n_str;
ss << " (n = " << n << ")";
n_str = ss.str();
N = n;
INDEX = &index;

View File

@ -135,7 +135,6 @@ void test_add_remove()
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 */
@ -143,7 +142,6 @@ void test_add_remove()
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 */
@ -151,7 +149,6 @@ void test_add_remove()
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 */
@ -160,7 +157,6 @@ void test_add_remove()
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 */
@ -169,7 +165,6 @@ void test_add_remove()
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 */
@ -178,7 +173,6 @@ void test_add_remove()
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)
@ -386,7 +380,6 @@ void test_saving()
test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM));
test_equal(r.size(), q.size());
test_equal(r.size_str(), q.size_str());
test_equal(r.length_str(), q.length_str());
test_for_each(0, 24, 1, _test_saving_loop);
}

View File

@ -1,12 +1,10 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/random.h>
#include <core/string.h>
#include <tests/test.h>
#include <sstream>
unsigned int SEED = 0;
static void do_test_rng()
@ -23,9 +21,7 @@ static void do_test_rng()
static void test_rng(unsigned int seed)
{
std::stringstream ss;
ss << " (seed = " << seed << ")";
std::string seed_str = ss.str();
std::string seed_str = " (seed = " + string :: utos(seed) + ")";
SEED = seed;
test :: run("Random Number Generator Test" + seed_str, do_test_rng);

19
tests/core/string.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
* Copyright 2015 (c) Anna Schumaker.
* Test the string formatting functions.
*/
#include <core/string.h>
#include <tests/test.h>
void test_utos()
{
test_equal(string :: utos(0), "0");
test_equal(string :: utos(1), "1");
test_equal(string :: utos(42), "42");
test_equal(string :: utos(999), "999");
}
int main(int argc, char **argv)
{
test :: run("utos Test", test_utos);
}