203 lines
5.7 KiB
C++
203 lines
5.7 KiB
C++
/**
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
extern "C" {
|
|
#include <core/string.h>
|
|
}
|
|
#include <core/filter.h>
|
|
#include <core/tags/tags.h>
|
|
#include <core/tags/track.h>
|
|
#include "../test.h"
|
|
|
|
#include <set>
|
|
#include <sstream>
|
|
|
|
|
|
static struct artist *artist = NULL;
|
|
static struct album *album = NULL;
|
|
static struct genre *genre = NULL;
|
|
static struct library *library = NULL;
|
|
const std::string MUSIC_DIR = "/home/Zelda/Music";
|
|
|
|
|
|
static void test_track_tag_default()
|
|
{
|
|
struct track track;
|
|
|
|
test_equal(track.album(), (struct album *)NULL);
|
|
test_equal(track.artist(), (struct artist *)NULL);
|
|
test_equal(track.genre(), (struct genre *)NULL);
|
|
test_equal(track.library(), (struct library *)NULL);
|
|
|
|
test_equal(track.name(), (std::string)"");
|
|
test_equal(track.lowercase(), (std::string)"");
|
|
test_equal(track.primary_key(), (std::string)"");
|
|
test_equal(track.date(), (std::string)"Never");
|
|
test_equal(track.path(), (std::string)"");
|
|
|
|
test_equal(track.track_nr(), (unsigned)0);
|
|
test_equal(track.length(), (unsigned)0);
|
|
test_equal(track.count(), (unsigned)0);
|
|
}
|
|
|
|
static void verify_track_tag(struct track *track, unsigned int size)
|
|
{
|
|
test_equal(track->album(), album);
|
|
test_equal(track->artist(), artist);
|
|
test_equal(track->genre(), genre);
|
|
test_equal(track->library(), library);
|
|
|
|
test_equal(track->name(), (std::string)"Legend of Zelda Medley");
|
|
test_equal(track->lowercase(), (std::string)"legend of zelda medley");
|
|
test_equal(track->date(), (std::string)"Never");
|
|
test_equal(track->path(), (std::string)MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3");
|
|
test_equal(track->primary_key(), (std::string)"0/Hyrule Symphony/13 - Legend of Zelda Medley.mp3");
|
|
|
|
test_equal(track->track_nr(), (unsigned)13);
|
|
test_equal(track->length(), (unsigned)288);
|
|
test_equal(track->count(), (unsigned)0);
|
|
test_equal(library->li_size, size);
|
|
}
|
|
|
|
static void test_track_tag_constructor()
|
|
{
|
|
file f;
|
|
|
|
filter_init();
|
|
tags :: init();
|
|
album = album_find("Hyrule Symphony", 1998);
|
|
artist = artist_find("Koji Kondo");
|
|
genre = genre_find("Video Game Music");
|
|
library = library_find(MUSIC_DIR);
|
|
|
|
struct track a(album, artist, genre, library,
|
|
"Hyrule Symphony/13 - Legend of Zelda Medley.mp3",
|
|
"Legend of Zelda Medley", 288, 13);
|
|
struct set search = SET_INIT();
|
|
struct track b;
|
|
|
|
verify_track_tag(&a, 1);
|
|
|
|
file_init(&f, "track_tag", 0);
|
|
file_open(&f, OPEN_WRITE);
|
|
a.write(f);
|
|
file_close(&f);
|
|
|
|
file_open(&f, OPEN_READ);
|
|
b.read(f);
|
|
file_close(&f);
|
|
|
|
verify_track_tag(&b, 2);
|
|
|
|
|
|
filter :: search("Legend of Zelda Medley", &search);
|
|
test_equal(set_size(&search), (size_t)1);
|
|
test_equal(set_has(&search, 0), true);
|
|
|
|
filter :: search("Koji Kondo", &search);
|
|
test_equal(set_size(&search), (size_t)1);
|
|
test_equal(set_has(&search, 0), true);
|
|
|
|
filter :: search("Hyrule Symphony", &search);
|
|
test_equal(set_size(&search), (size_t)1);
|
|
test_equal(set_has(&search, 0), true);
|
|
}
|
|
|
|
static void test_track_tag_destructor()
|
|
{
|
|
test_equal(library->li_size, (unsigned)0);
|
|
}
|
|
|
|
static void test_track_tag_load_db(unsigned int size)
|
|
{
|
|
database<struct track> track_db;
|
|
db_init(&track_db, "track.db", false);
|
|
db_load(&track_db);
|
|
test_equal(track_db.db_size, size);
|
|
db_deinit(&track_db);
|
|
}
|
|
|
|
static void test_track_tag_lookup()
|
|
{
|
|
unsigned int index;
|
|
struct track *a, *b;
|
|
|
|
a = tags :: add_track(album, artist, genre, library,
|
|
MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3",
|
|
"Legend of Zelda Medley", 288, 13);
|
|
test_not_equal(a, (struct track *)NULL);
|
|
verify_track_tag(a, 1);
|
|
|
|
b = tags :: add_track(album, artist, genre, library,
|
|
MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3",
|
|
"Legend of Zelda Medley", 288, 13);
|
|
test_equal(b, (struct track *)NULL);
|
|
|
|
b = tags :: add_track(album, artist, genre, library,
|
|
MUSIC_DIR + "/Hyrule Symphony/12 - Ocarina Medley.mp3",
|
|
"Ocarina Medley", 232, 12);
|
|
test_not_equal(b, a);
|
|
test_equal(library->li_size, (unsigned)2);
|
|
test_equal(tags :: track_size(), b->index() + 1);
|
|
|
|
|
|
test_equal(track_get(a->index()), a);
|
|
test_equal(track_get(a->index() + 1), b);
|
|
test_equal(track_get(a->index() + 2), (struct track *)NULL);
|
|
|
|
test_track_tag_load_db(0);
|
|
tags :: commit_track_db();
|
|
test_track_tag_load_db(2);
|
|
|
|
|
|
index = a->index();
|
|
tags :: remove_track(a);
|
|
test_equal(track_get(index), (struct track *)NULL);
|
|
test_track_tag_load_db(2);
|
|
tags :: commit_track_db();
|
|
test_track_tag_load_db(1);
|
|
test_equal(library->li_size, (unsigned)1);
|
|
test_equal(tags :: track_size(), b->index() + 1);
|
|
|
|
|
|
a = tags :: add_track(album, artist, genre, library,
|
|
MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3",
|
|
"Legend of Zelda Medley", 288, 13);
|
|
test_not_equal(a, (struct track *)NULL);
|
|
verify_track_tag(a, 2);
|
|
test_equal(tags :: track_size(), a->index() + 1);
|
|
|
|
tags :: remove_library_tracks(library);
|
|
test_track_tag_load_db(0);
|
|
test_equal(library->li_size, (unsigned)0);
|
|
}
|
|
|
|
static void test_track_tag_locale()
|
|
{
|
|
time_t rawtime = time(NULL);
|
|
struct tm *now = localtime(&rawtime);
|
|
struct track *track = tags :: add_track(album, artist, genre, library,
|
|
MUSIC_DIR + "/Hyrule Symphony/6 - Kakariko Village.mp3",
|
|
"Kakariko Village", 186, 6);
|
|
gchar *date;
|
|
|
|
std::setlocale(LC_TIME, "C");
|
|
date = string_tm2str(now);
|
|
|
|
test_equal(track->count(), (unsigned)0);
|
|
test_equal(track->date(), "Never");
|
|
|
|
track->played();
|
|
test_equal(track->count(), (unsigned)1);
|
|
test_equal(track->date(), date);
|
|
g_free(date);
|
|
}
|
|
|
|
DECLARE_UNIT_TESTS(
|
|
UNIT_TEST("Track Tag Default Constructor", test_track_tag_default),
|
|
UNIT_TEST("Track Tag Constructor", test_track_tag_constructor),
|
|
UNIT_TEST("Track Tag Destructor Test", test_track_tag_destructor),
|
|
UNIT_TEST("Track Tag Lookup Test", test_track_tag_lookup),
|
|
UNIT_TEST("Track Tag Date Test", test_track_tag_locale),
|
|
);
|