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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-10 10:33:24 -04:00
parent 09c7164aee
commit 074339040b
13 changed files with 84 additions and 59 deletions

View File

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

View File

@ -22,7 +22,7 @@ public:
static std::list<TempQueue> 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<TempQueue>::iterator it;
file_init(&deck_file, "deck", 1);
if (!file_open(&deck_file, OPEN_READ))
return;

View File

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

View File

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

View File

@ -9,8 +9,9 @@
template <class T>
Database<T> :: Database(std::string filepath, bool autosave)
: _size(0), _autosave(autosave), _file(filepath, 0)
: _size(0), _autosave(autosave)
{
file_init(&_file, filepath, 0);
}
template <class T>

View File

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

View File

@ -12,7 +12,7 @@ static Track *TRACK_NULL = NULL;
static void test_init()
{
unsigned int val;
file f("deck", 0);
file f;
std::list<TempQueue>::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 */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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