diff --git a/core/queue.cpp b/core/queue.cpp index 0725593e..7f08870e 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -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; diff --git a/core/string.cpp b/core/string.cpp new file mode 100644 index 00000000..9951073e --- /dev/null +++ b/core/string.cpp @@ -0,0 +1,13 @@ +/** + * @file + * Copyright 2015 (c) Anna Schumaker. + */ +#include +#include + +const std::string string :: utos(unsigned int u) +{ + std::stringstream ss; + ss << u; + return ss.str(); +} diff --git a/gui/tabs.cpp b/gui/tabs.cpp index 28edde7e..367a2efe 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -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() diff --git a/include/core/queue.h b/include/core/queue.h index ea063d74..04f203aa 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -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. * diff --git a/include/core/string.h b/include/core/string.h new file mode 100644 index 00000000..55fefe5f --- /dev/null +++ b/include/core/string.h @@ -0,0 +1,25 @@ +/** + * @file + * Copyright 2015 (c) Anna Schumaker. + */ +#ifndef OCARINA_CORE_STRING_H +#define OCARINA_CORE_STRING_H + +#include + +/** + * 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 */ diff --git a/tests/core/.gitignore b/tests/core/.gitignore index 7c4efbbe..0b6e3836 100644 --- a/tests/core/.gitignore +++ b/tests/core/.gitignore @@ -1,4 +1,5 @@ version +string file database index diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 661356ad..b86a4009 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -8,6 +8,7 @@ def test(name): test( "version" ) +test( "string" ) test( "file" ) test( "database" ) test( "index" ) diff --git a/tests/core/database.cpp b/tests/core/database.cpp index 116d443d..7312f19b 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -2,11 +2,10 @@ * Copyright 2014 (c) Anna Schumaker. * Test a Database */ - #include +#include #include -#include #include @@ -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 db("database.db", autosave); std::vector pointers; - std::stringstream ss; - std::string n_str; - - ss << " (n = " << n << ")"; - n_str = ss.str(); N = n; DB = &db; diff --git a/tests/core/idle.cpp b/tests/core/idle.cpp index 7b61db68..72ee8f82 100644 --- a/tests/core/idle.cpp +++ b/tests/core/idle.cpp @@ -2,12 +2,10 @@ * Copyright 2014 (c) Anna Schumaker. * Test the idle queue */ - #include +#include #include -#include - 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; diff --git a/tests/core/index.cpp b/tests/core/index.cpp index bbdf6890..02be6d10 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -4,10 +4,9 @@ */ #include #include +#include #include -#include - 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; diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 848075d5..209cf604 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -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); } diff --git a/tests/core/random.cpp b/tests/core/random.cpp index 923f31d7..216b7d37 100644 --- a/tests/core/random.cpp +++ b/tests/core/random.cpp @@ -1,12 +1,10 @@ /* * Copyright 2014 (c) Anna Schumaker. */ - #include +#include #include -#include - 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); diff --git a/tests/core/string.cpp b/tests/core/string.cpp new file mode 100644 index 00000000..64586bb3 --- /dev/null +++ b/tests/core/string.cpp @@ -0,0 +1,19 @@ +/* + * Copyright 2015 (c) Anna Schumaker. + * Test the string formatting functions. + */ +#include +#include + +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); +}