From 074339040b45e1a1c04d15c7feeb773af54bd610 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 10 Sep 2015 10:33:24 -0400 Subject: [PATCH] core/file: Remove the file struct constructor And replace it with the function file_init(). Let's take this chance to rework parts of the unit test as well. Signed-off-by: Anna Schumaker --- core/audio.cpp | 3 +- core/deck.cpp | 3 +- core/file.cpp | 7 ++- core/library.cpp | 3 +- include/core/database.hpp | 3 +- include/core/file.h | 11 ++--- tests/core/deck.cpp | 3 +- tests/core/file.cpp | 95 ++++++++++++++++++++++--------------- tests/core/queue.cpp | 3 +- tests/core/tags/album.cpp | 3 +- tests/core/tags/generic.cpp | 3 +- tests/core/tags/library.cpp | 3 +- tests/core/tags/track.cpp | 3 +- 13 files changed, 84 insertions(+), 59 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index 60183180..116b51de 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -11,7 +11,7 @@ static bool _pause_enabled = false; static unsigned int _pause_count = 0; static Track *cur_track = NULL; -static file f_cur_track("cur_track", 0); +static struct file f_cur_track; static AudioDriver *cur_driver = NULL; @@ -79,6 +79,7 @@ void audio :: init() { unsigned int id; + file_init(&f_cur_track, "cur_track", 0); if (file_exists(&f_cur_track)) { file_open(&f_cur_track, OPEN_READ); f_cur_track >> id; diff --git a/core/deck.cpp b/core/deck.cpp index 0058cee6..e359b83f 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -22,7 +22,7 @@ public: static std::list queue_deck; static RecentQueue recent_queue; -static file deck_file("deck", 1); +static struct file deck_file; TempQueue :: TempQueue() {} @@ -91,6 +91,7 @@ void deck :: init() bool upgraded = false; std::list::iterator it; + file_init(&deck_file, "deck", 1); if (!file_open(&deck_file, OPEN_READ)) return; diff --git a/core/file.cpp b/core/file.cpp index c3d05997..2441a82e 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -25,9 +25,12 @@ static bool __file_mkdir() } -file :: file(const std::string &name, unsigned int version) - : f_mode(NOT_OPEN), f_version(version), f_prev(0), f_name(name) +void file_init(struct file *file, const std::string &name, unsigned int version) { + file->f_mode = NOT_OPEN; + file->f_version = version; + file->f_prev = 0; + file->f_name = name; } const std::string file_path(struct file *file) diff --git a/core/library.cpp b/core/library.cpp index a1c21259..cc4f7233 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -16,8 +16,9 @@ private: public: - LibraryQueue() : Queue(Q_ENABLED | Q_REPEAT), f("library.q", 0) + LibraryQueue() : Queue(Q_ENABLED | Q_REPEAT) { + file_init(&f, "library.q", 0); Queue :: sort(SORT_ARTIST, true); Queue :: sort(SORT_YEAR, false); Queue :: sort(SORT_TRACK, false); diff --git a/include/core/database.hpp b/include/core/database.hpp index 3cfcaa21..f4ad3116 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -9,8 +9,9 @@ template Database :: Database(std::string filepath, bool autosave) - : _size(0), _autosave(autosave), _file(filepath, 0) + : _size(0), _autosave(autosave) { + file_init(&_file, filepath, 0); } template diff --git a/include/core/file.h b/include/core/file.h index 8b4243b7..4a41d441 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -48,14 +48,6 @@ struct file : public std::fstream { unsigned int f_prev; /* The file's on-disk data version. */ std::string f_name; /* The file's basename. */ - /** - * Set up a new file object. - * - * @param name The name of the file. - * @param version The file version of the new file. - */ - file(const std::string &, unsigned int); - /** * Read an entire line from the file and return it to the caller. * In theory a return value optimization will occur so returning @@ -67,6 +59,9 @@ struct file : public std::fstream { }; +/* Initialize a new file object. */ +void file_init(struct file *, const std::string &, unsigned int); + /* Returns the full path of the file or an empty string if filename is not set. */ const std::string file_path(struct file *); diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index c893b218..65a1edfa 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -12,7 +12,7 @@ static Track *TRACK_NULL = NULL; static void test_init() { unsigned int val; - file f("deck", 0); + file f; std::list::iterator it; test_equal(deck :: next(), TRACK_NULL); @@ -38,6 +38,7 @@ static void test_init() /* * Test that we saved the deck in the new format */ + file_init(&f, "deck", 0); file_open(&f, OPEN_READ); test_equal(file_version(&f), (unsigned)1); f >> val; /* number of queues */ diff --git a/tests/core/file.cpp b/tests/core/file.cpp index ec2d176b..3afbf1ad 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -5,62 +5,79 @@ #include "test.h" -static void test_filepath() +static void test_verify_constructor(struct file *file, const std::string &fpath) { + test_equal(file_version(file), 0); + test_equal(file->f_mode, NOT_OPEN); + test_equal(file_path(file), fpath); +} + +static void test_empty() +{ + struct file fempty; + + file_init(&fempty, "", 0); + test_verify_constructor(&fempty, ""); + + test_equal(file_open(&fempty, NOT_OPEN), (bool)false); + test_equal(file_open(&fempty, OPEN_READ), (bool)false); + test_equal(fempty.f_mode, NOT_OPEN); + test_equal(file_open(&fempty, OPEN_WRITE), (bool)false); + test_equal(fempty.f_mode, NOT_OPEN); + + test_equal(file_exists(&fempty), (bool)false); + test_equal(test_data_file_exists(NULL), (bool)false); +} + +static void test_file() +{ + struct file file; gchar *filepath = test_data_file("file.txt"); - file a("", 0); - file b("file.txt", 0); + file_init(&file, "file.txt", 0); + test_verify_constructor(&file, filepath); + test_equal(file_exists(&file), (bool)false); + test_equal(test_data_file_exists(NULL), (bool)false); - test_equal(file_version(&a), (unsigned)0); - test_equal((std::string)file_path(&a), (std::string)""); + test_equal(file_open(&file, NOT_OPEN), (bool)false); + test_equal(file_open(&file, OPEN_READ), (bool)false); + test_equal(file_open(&file, OPEN_WRITE), (bool)true); + test_equal(file.f_mode, OPEN_WRITE); + test_equal(file_open(&file, OPEN_WRITE), (bool)false); - test_equal(file_version(&b), (unsigned)0); - test_equal((std::string)file_path(&b), filepath); + file_close(&file); + test_equal(file.f_mode, NOT_OPEN); - test_equal(a.f_mode, NOT_OPEN); - test_equal(b.f_mode, NOT_OPEN); + test_equal(file_exists(&file), (bool)true); + test_equal(test_data_file_exists("file.txt"), (bool)true); - test_equal(file_exists(&a), false); - test_equal(file_exists(&b), false); - test_equal(test_data_file_exists(NULL), false); + g_chmod(file_path(&file).c_str(), 0444); + test_equal(file_open(&file, OPEN_WRITE), (bool)false); + g_chmod(file_path(&file).c_str(), 0200); + test_equal(file_open(&file, OPEN_READ), (bool)false); + g_chmod(file_path(&file).c_str(), 0644); + + test_equal(file_open(&file, OPEN_READ), (bool)true); + test_equal(file.f_mode, OPEN_READ); + test_equal(file_open(&file, OPEN_READ), (bool)false); + file_close(&file); g_free(filepath); } -static void test_open() -{ - file a("", 0); - - test_equal(file_open(&a, OPEN_READ), false); - test_equal(file_open(&a, OPEN_WRITE), false); - file_close(&a); - test_equal(a.f_mode, NOT_OPEN); - - file b("file.txt", 0); - test_equal(file_open(&b, NOT_OPEN), false); - test_equal(file_open(&b, OPEN_READ), false); - test_equal(file_open(&b, OPEN_WRITE), true); - test_equal(b.f_mode, OPEN_WRITE); - test_equal(file_open(&b, OPEN_WRITE), false); - file_close(&b); - test_equal(b.f_mode, NOT_OPEN); - - test_equal(test_data_file_exists("file.txt"), true); -} - static void test_io() { - file a("file.txt", 1); + struct file a, b; + std::string res; + file_init(&a, "file.txt", 1); test_equal(file_open(&a, OPEN_WRITE), true); a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl; file_close(&a); test_equal(file_exists(&a), true); - file b("file.txt", 0); - std::string res; + file_init(&b, "file.txt", 0); test_equal(file_open(&b, OPEN_READ), true); test_equal(file_version(&b), (unsigned)1); b >> res; @@ -73,7 +90,7 @@ static void test_io() } DECLARE_UNIT_TESTS( - UNIT_TEST("file Constructor", test_filepath), - UNIT_TEST("file Opening", test_open), - UNIT_TEST("file I/O", test_io), + UNIT_TEST("File (Path = \"\")", test_empty), + UNIT_TEST("File (Path = \"file.txt\")", test_file), + UNIT_TEST("File I/O", test_io), ); diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 10b8301e..0758cbf8 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -283,10 +283,11 @@ void test_saving() { TestQueue q(Q_RANDOM); TestQueue r(0); - file f("test.q", 0); + file f; test_fill_q(&q); + file_init(&f, "test.q", 0); file_open(&f, OPEN_WRITE); q.write(f); file_close(&f); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index ec097b98..0f5c4708 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -7,13 +7,14 @@ static void test_album_tag() { Album album("Hyrule Symphony", 1998); - file f("album_tag", 0); + file f; test_equal(album.name(), (std::string)"Hyrule Symphony"); test_equal(album.lowercase(), (std::string)"hyrule symphony"); test_equal(album.year(), (unsigned int)1998); test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony"); + file_init(&f, "album_tag", 0); file_open(&f, OPEN_WRITE); album.write(f); file_close(&f); diff --git a/tests/core/tags/generic.cpp b/tests/core/tags/generic.cpp index 2247ed1d..0009a654 100644 --- a/tests/core/tags/generic.cpp +++ b/tests/core/tags/generic.cpp @@ -7,12 +7,13 @@ static void test_generic_tag() { GenericTag tag("Generic Tag"); - file f("generic_tag", 0); + file f; test_equal(tag.name(), (std::string)"Generic Tag"); test_equal(tag.lowercase(), (std::string)"generic tag"); test_equal(tag.primary_key(), (std::string)"Generic Tag"); + file_init(&f, "generic_tag", 0); file_open(&f, OPEN_WRITE); tag.write(f); file_close(&f); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index 2bc4049f..a9cf4a43 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -7,7 +7,7 @@ static void test_library_tag() { Library library("/home/Zelda/Music"); - file f("library_tag", 0); + file f; test_equal(library.primary_key(), (std::string)"/home/Zelda/Music"); test_equal(library.size(), (unsigned)0); @@ -23,6 +23,7 @@ static void test_library_tag() test_equal(library.size(), (unsigned)21); + file_init(&f, "library_tag", 0); file_open(&f, OPEN_WRITE); library.write(f); file_close(&f); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 80954799..6372e48f 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -57,7 +57,7 @@ static void verify_track_tag(Track *track, unsigned int size) static void test_track_tag_constructor() { - file f("track_tag", 0); + file f; album = tags :: get_album("Hyrule Symphony", 1998); artist = tags :: get_artist("Koji Kondo"); @@ -72,6 +72,7 @@ static void test_track_tag_constructor() verify_track_tag(&a, 1); + file_init(&f, "track_tag", 0); file_open(&f, OPEN_WRITE); a.write(f); file_close(&f);