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:
parent
e47eb69859
commit
6c6437c2bd
|
@ -201,13 +201,6 @@ unsigned int Queue :: size()
|
||||||
return _tracks.size();
|
return _tracks.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string Queue :: size_str()
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << size();
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string Queue :: length_str()
|
const std::string Queue :: length_str()
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
#include <core/deck.h>
|
#include <core/deck.h>
|
||||||
#include <core/filter.h>
|
#include <core/filter.h>
|
||||||
#include <core/playlist.h>
|
#include <core/playlist.h>
|
||||||
|
#include <core/string.h>
|
||||||
#include <gui/tabs.h>
|
#include <gui/tabs.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
@ -128,11 +129,8 @@ void Tab :: tab_dec_sort_count()
|
||||||
|
|
||||||
void Tab :: tab_set_size()
|
void Tab :: tab_set_size()
|
||||||
{
|
{
|
||||||
if (tab_size) {
|
if (tab_size)
|
||||||
std::stringstream ss;
|
tab_size->set_text(string :: utos(tab_pq->size()));
|
||||||
ss << tab_pq->size();
|
|
||||||
tab_size->set_text(ss.str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tab :: tab_unmap()
|
void Tab :: tab_unmap()
|
||||||
|
|
|
@ -165,13 +165,6 @@ public:
|
||||||
*/
|
*/
|
||||||
unsigned int size();
|
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.
|
* Find the runtime of the queue.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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 */
|
|
@ -1,4 +1,5 @@
|
||||||
version
|
version
|
||||||
|
string
|
||||||
file
|
file
|
||||||
database
|
database
|
||||||
index
|
index
|
||||||
|
|
|
@ -8,6 +8,7 @@ def test(name):
|
||||||
|
|
||||||
|
|
||||||
test( "version" )
|
test( "version" )
|
||||||
|
test( "string" )
|
||||||
test( "file" )
|
test( "file" )
|
||||||
test( "database" )
|
test( "database" )
|
||||||
test( "index" )
|
test( "index" )
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
* Copyright 2014 (c) Anna Schumaker.
|
* Copyright 2014 (c) Anna Schumaker.
|
||||||
* Test a Database
|
* Test a Database
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <core/database.h>
|
#include <core/database.h>
|
||||||
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,9 +21,7 @@ public:
|
||||||
|
|
||||||
const std::string primary_key() const
|
const std::string primary_key() const
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
return string :: utos(val);
|
||||||
ss << val;
|
|
||||||
return ss.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(File &f) { f << 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)
|
static void db_test(unsigned int n, bool autosave)
|
||||||
{
|
{
|
||||||
|
std::string n_str = " (n = " + string :: utos(n) + ")";
|
||||||
Database<IntEntry> db("database.db", autosave);
|
Database<IntEntry> db("database.db", autosave);
|
||||||
std::vector<IntEntry *> pointers;
|
std::vector<IntEntry *> pointers;
|
||||||
std::stringstream ss;
|
|
||||||
std::string n_str;
|
|
||||||
|
|
||||||
ss << " (n = " << n << ")";
|
|
||||||
n_str = ss.str();
|
|
||||||
|
|
||||||
N = n;
|
N = n;
|
||||||
DB = &db;
|
DB = &db;
|
||||||
|
|
|
@ -2,12 +2,10 @@
|
||||||
* Copyright 2014 (c) Anna Schumaker.
|
* Copyright 2014 (c) Anna Schumaker.
|
||||||
* Test the idle queue
|
* Test the idle queue
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <core/idle.h>
|
#include <core/idle.h>
|
||||||
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
static float N = 0;
|
static float N = 0;
|
||||||
static float cur = -1;
|
static float cur = -1;
|
||||||
static bool func_passed = false;
|
static bool func_passed = false;
|
||||||
|
@ -45,9 +43,7 @@ static void test_idle_queue()
|
||||||
|
|
||||||
static void do_test(unsigned int n)
|
static void do_test(unsigned int n)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::string n_str = " (n = " + string :: utos(n) + ")";
|
||||||
ss << " (n = " << n << ")";
|
|
||||||
const std::string n_str = ss.str();
|
|
||||||
|
|
||||||
N = n;
|
N = n;
|
||||||
cur = -1;
|
cur = -1;
|
||||||
|
|
|
@ -4,10 +4,9 @@
|
||||||
*/
|
*/
|
||||||
#include <core/index.h>
|
#include <core/index.h>
|
||||||
#include <core/print.h>
|
#include <core/print.h>
|
||||||
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
|
|
||||||
static unsigned int N = 0;
|
static unsigned int N = 0;
|
||||||
static Index *INDEX = NULL;
|
static Index *INDEX = NULL;
|
||||||
|
@ -115,12 +114,8 @@ static void test_saving()
|
||||||
|
|
||||||
static void index_test(unsigned int n)
|
static void index_test(unsigned int n)
|
||||||
{
|
{
|
||||||
|
std::string n_str = " (n = " + string :: utos(n) + ")";
|
||||||
Index index("index.idx", false);
|
Index index("index.idx", false);
|
||||||
std::stringstream ss;
|
|
||||||
std::string n_str;
|
|
||||||
|
|
||||||
ss << " (n = " << n << ")";
|
|
||||||
n_str = ss.str();
|
|
||||||
|
|
||||||
N = n;
|
N = n;
|
||||||
INDEX = &index;
|
INDEX = &index;
|
||||||
|
|
|
@ -135,7 +135,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), expected);
|
test_equal(q.get_length(), expected);
|
||||||
test_equal(q.length_str(), (std::string)"");
|
test_equal(q.length_str(), (std::string)"");
|
||||||
test_equal(q.size(), (unsigned)0);
|
test_equal(q.size(), (unsigned)0);
|
||||||
test_equal(q.size_str(), (std::string)"0");
|
|
||||||
|
|
||||||
|
|
||||||
/* Add tracks */
|
/* Add tracks */
|
||||||
|
@ -143,7 +142,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), expected);
|
test_equal(q.get_length(), expected);
|
||||||
test_equal(q.length_str(), (std::string)"1 hour, 26 minutes");
|
test_equal(q.length_str(), (std::string)"1 hour, 26 minutes");
|
||||||
test_equal(q.size(), (unsigned)24);
|
test_equal(q.size(), (unsigned)24);
|
||||||
test_equal(q.size_str(), (std::string)"24");
|
|
||||||
|
|
||||||
|
|
||||||
/* Add everything again */
|
/* Add everything again */
|
||||||
|
@ -151,7 +149,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), expected);
|
test_equal(q.get_length(), expected);
|
||||||
test_equal(q.length_str(), (std::string)"2 hours, 52 minutes");
|
test_equal(q.length_str(), (std::string)"2 hours, 52 minutes");
|
||||||
test_equal(q.size(), (unsigned)48);
|
test_equal(q.size(), (unsigned)48);
|
||||||
test_equal(q.size_str(), (std::string)"48");
|
|
||||||
|
|
||||||
|
|
||||||
/* Test removing multiple tracks at once */
|
/* Test removing multiple tracks at once */
|
||||||
|
@ -160,7 +157,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), expected);
|
test_equal(q.get_length(), expected);
|
||||||
test_equal(q.length_str(), (std::string)"1 hour, 50 minutes");
|
test_equal(q.length_str(), (std::string)"1 hour, 50 minutes");
|
||||||
test_equal(q.size(), (unsigned)24);
|
test_equal(q.size(), (unsigned)24);
|
||||||
test_equal(q.size_str(), (std::string)"24");
|
|
||||||
|
|
||||||
|
|
||||||
/* Test removing tracks one at a time */
|
/* Test removing tracks one at a time */
|
||||||
|
@ -169,7 +165,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), expected);
|
test_equal(q.get_length(), expected);
|
||||||
test_equal(q.length_str(), (std::string)"55 minutes");
|
test_equal(q.length_str(), (std::string)"55 minutes");
|
||||||
test_equal(q.size(), (unsigned)12);
|
test_equal(q.size(), (unsigned)12);
|
||||||
test_equal(q.size_str(), (std::string)"12");
|
|
||||||
|
|
||||||
|
|
||||||
/* Remove remaining tracks */
|
/* Remove remaining tracks */
|
||||||
|
@ -178,7 +173,6 @@ void test_add_remove()
|
||||||
test_equal(q.get_length(), (unsigned)0);
|
test_equal(q.get_length(), (unsigned)0);
|
||||||
test_equal(q.length_str(), (std::string)"");
|
test_equal(q.length_str(), (std::string)"");
|
||||||
test_equal(q.size(), (unsigned)0);
|
test_equal(q.size(), (unsigned)0);
|
||||||
test_equal(q.size_str(), (std::string)"0");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_updated_cb(Queue *q, unsigned int row)
|
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.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM));
|
||||||
test_equal(r.size(), q.size());
|
test_equal(r.size(), q.size());
|
||||||
test_equal(r.size_str(), q.size_str());
|
|
||||||
test_equal(r.length_str(), q.length_str());
|
test_equal(r.length_str(), q.length_str());
|
||||||
test_for_each(0, 24, 1, _test_saving_loop);
|
test_for_each(0, 24, 1, _test_saving_loop);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2014 (c) Anna Schumaker.
|
* Copyright 2014 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <core/random.h>
|
#include <core/random.h>
|
||||||
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
unsigned int SEED = 0;
|
unsigned int SEED = 0;
|
||||||
|
|
||||||
static void do_test_rng()
|
static void do_test_rng()
|
||||||
|
@ -23,9 +21,7 @@ static void do_test_rng()
|
||||||
|
|
||||||
static void test_rng(unsigned int seed)
|
static void test_rng(unsigned int seed)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::string seed_str = " (seed = " + string :: utos(seed) + ")";
|
||||||
ss << " (seed = " << seed << ")";
|
|
||||||
std::string seed_str = ss.str();
|
|
||||||
SEED = seed;
|
SEED = seed;
|
||||||
|
|
||||||
test :: run("Random Number Generator Test" + seed_str, do_test_rng);
|
test :: run("Random Number Generator Test" + seed_str, do_test_rng);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue