2014-11-19 09:08:38 -05:00
|
|
|
/**
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-12-01 10:33:19 -05:00
|
|
|
#include <core/filter.h>
|
2014-11-19 09:08:38 -05:00
|
|
|
#include <core/tags/track.h>
|
2015-08-27 13:17:08 -04:00
|
|
|
#include "../test.h"
|
2014-12-01 10:33:19 -05:00
|
|
|
|
|
|
|
#include <set>
|
2014-11-27 10:37:46 -05:00
|
|
|
#include <sstream>
|
2014-11-19 09:08:38 -05:00
|
|
|
|
2014-11-28 20:27:08 -05:00
|
|
|
|
|
|
|
static Artist *artist = NULL;
|
|
|
|
static Album *album = NULL;
|
|
|
|
static Genre *genre = NULL;
|
|
|
|
static Library *library = NULL;
|
|
|
|
const std::string MUSIC_DIR = "/home/Zelda/Music";
|
|
|
|
|
|
|
|
|
2014-11-20 08:22:50 -05:00
|
|
|
static void test_track_tag_default()
|
2014-11-19 09:08:38 -05:00
|
|
|
{
|
|
|
|
Track track;
|
|
|
|
|
2014-11-21 08:11:03 -05:00
|
|
|
test_equal(track.album(), (Album *)NULL);
|
2014-11-20 08:41:18 -05:00
|
|
|
test_equal(track.artist(), (Artist *)NULL);
|
2014-11-21 08:25:35 -05:00
|
|
|
test_equal(track.genre(), (Genre *)NULL);
|
2014-11-20 08:16:13 -05:00
|
|
|
test_equal(track.library(), (Library *)NULL);
|
2014-11-19 09:08:38 -05:00
|
|
|
|
|
|
|
test_equal(track.name(), (std::string)"");
|
|
|
|
test_equal(track.lowercase(), (std::string)"");
|
|
|
|
test_equal(track.primary_key(), (std::string)"");
|
2014-11-27 10:37:46 -05:00
|
|
|
test_equal(track.date(), (std::string)"Never");
|
2014-11-26 21:25:39 -05:00
|
|
|
test_equal(track.path(), (std::string)"");
|
2014-11-19 09:08:38 -05:00
|
|
|
|
2014-11-27 10:37:46 -05:00
|
|
|
test_equal(track.track(), (unsigned)0);
|
|
|
|
test_equal(track.length(), (unsigned)0);
|
|
|
|
test_equal(track.count(), (unsigned)0);
|
2014-11-19 09:08:38 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 09:56:48 -05:00
|
|
|
static void verify_track_tag(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(), (unsigned)13);
|
|
|
|
test_equal(track->length(), (unsigned)288);
|
|
|
|
test_equal(track->count(), (unsigned)0);
|
|
|
|
test_equal(library->size(), size);
|
|
|
|
}
|
|
|
|
|
2014-11-20 08:22:50 -05:00
|
|
|
static void test_track_tag_constructor()
|
|
|
|
{
|
2015-09-10 10:33:24 -04:00
|
|
|
file f;
|
2015-08-27 13:17:08 -04:00
|
|
|
|
|
|
|
album = tags :: get_album("Hyrule Symphony", 1998);
|
|
|
|
artist = tags :: get_artist("Koji Kondo");
|
|
|
|
genre = tags :: get_genre("Video Game Music");
|
|
|
|
library = tags :: get_library(MUSIC_DIR);
|
|
|
|
|
2014-11-29 09:56:48 -05:00
|
|
|
Track a(album, artist, genre, library,
|
2014-11-29 10:17:21 -05:00
|
|
|
"Hyrule Symphony/13 - Legend of Zelda Medley.mp3",
|
2014-11-29 09:56:48 -05:00
|
|
|
"Legend of Zelda Medley", 288, 13);
|
2014-12-01 10:33:19 -05:00
|
|
|
std::set<unsigned int> search;
|
2014-11-29 09:56:48 -05:00
|
|
|
Track b;
|
2014-11-26 08:23:20 -05:00
|
|
|
|
2014-11-29 09:56:48 -05:00
|
|
|
verify_track_tag(&a, 1);
|
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&f, "track_tag", 0);
|
2015-09-10 09:46:33 -04:00
|
|
|
file_open(&f, OPEN_WRITE);
|
2014-11-29 09:56:48 -05:00
|
|
|
a.write(f);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-11-29 09:56:48 -05:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
file_open(&f, OPEN_READ);
|
2014-11-29 09:56:48 -05:00
|
|
|
b.read(f);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-11-29 09:56:48 -05:00
|
|
|
|
|
|
|
verify_track_tag(&b, 2);
|
2014-12-01 10:33:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
filter :: search("Legend of Zelda Medley", search);
|
|
|
|
test_equal(search.size(), (size_t)1);
|
|
|
|
test_equal((*search.begin()), (unsigned)0);
|
|
|
|
|
|
|
|
filter :: search("Koji Kondo", search);
|
|
|
|
test_equal(search.size(), (size_t)1);
|
|
|
|
test_equal((*search.begin()), (unsigned)0);
|
|
|
|
|
|
|
|
filter :: search("Hyrule Symphony", search);
|
|
|
|
test_equal(search.size(), (size_t)1);
|
|
|
|
test_equal((*search.begin()), (unsigned)0);
|
2014-11-21 08:36:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_track_tag_destructor()
|
|
|
|
{
|
|
|
|
test_equal(library->size(), (unsigned)0);
|
2014-11-20 08:22:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 08:24:56 -05:00
|
|
|
static void test_track_tag_load_db(unsigned int size)
|
2014-11-29 10:38:27 -05:00
|
|
|
{
|
2014-11-29 11:16:21 -05:00
|
|
|
Database<Track> track_db("track.db", false);
|
2014-12-01 08:24:56 -05:00
|
|
|
track_db.load();
|
|
|
|
test_equal(track_db.size(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_track_tag_lookup()
|
|
|
|
{
|
|
|
|
unsigned int index;
|
2014-11-29 10:38:27 -05:00
|
|
|
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, (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, (Track *)NULL);
|
2014-11-29 11:11:49 -05:00
|
|
|
|
2014-12-01 08:24:56 -05:00
|
|
|
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);
|
2014-12-01 09:36:37 -05:00
|
|
|
test_equal(library->size(), (unsigned)2);
|
2014-12-01 09:44:32 -05:00
|
|
|
test_equal(tags :: track_size(), b->index() + 1);
|
2014-12-01 09:36:37 -05:00
|
|
|
|
2014-12-01 08:24:56 -05:00
|
|
|
|
2014-11-29 11:11:49 -05:00
|
|
|
test_equal(tags :: get_track(a->index()), a);
|
2014-12-01 08:24:56 -05:00
|
|
|
test_equal(tags :: get_track(a->index() + 1), b);
|
|
|
|
test_equal(tags :: get_track(a->index() + 2), (Track *)NULL);
|
2014-11-29 11:16:21 -05:00
|
|
|
|
2014-12-01 08:24:56 -05:00
|
|
|
test_track_tag_load_db(0);
|
|
|
|
tags :: commit_track_db();
|
|
|
|
test_track_tag_load_db(2);
|
2014-11-29 11:16:21 -05:00
|
|
|
|
2014-12-01 09:36:37 -05:00
|
|
|
|
2014-12-01 08:24:56 -05:00
|
|
|
index = a->index();
|
|
|
|
tags :: remove_track(a);
|
|
|
|
test_equal(tags :: get_track(index), (Track *)NULL);
|
|
|
|
test_track_tag_load_db(2);
|
2014-11-29 11:16:21 -05:00
|
|
|
tags :: commit_track_db();
|
2014-12-01 08:24:56 -05:00
|
|
|
test_track_tag_load_db(1);
|
2014-12-01 09:36:37 -05:00
|
|
|
test_equal(library->size(), (unsigned)1);
|
2014-12-01 09:44:32 -05:00
|
|
|
test_equal(tags :: track_size(), b->index() + 1);
|
2014-12-01 09:36:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
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, (Track *)NULL);
|
|
|
|
verify_track_tag(a, 2);
|
2014-12-01 09:44:32 -05:00
|
|
|
test_equal(tags :: track_size(), a->index() + 1);
|
2014-12-01 09:36:37 -05:00
|
|
|
|
|
|
|
tags :: remove_library_tracks(library);
|
|
|
|
test_track_tag_load_db(0);
|
|
|
|
test_equal(library->size(), (unsigned)0);
|
2014-11-29 10:38:27 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 10:32:06 -05:00
|
|
|
static std::string find_expected_date(const std::string &locale)
|
2014-11-26 08:55:06 -05:00
|
|
|
{
|
2014-12-01 10:32:06 -05:00
|
|
|
std::stringstream ss;
|
2014-11-28 20:27:08 -05:00
|
|
|
time_t rawtime = time(NULL);
|
|
|
|
struct tm *now = localtime(&rawtime);
|
2014-11-27 10:37:46 -05:00
|
|
|
|
|
|
|
if ((now->tm_mon + 1) < 10)
|
|
|
|
ss << "0";
|
|
|
|
ss << (now->tm_mon + 1) << "/";
|
|
|
|
if (now->tm_mday < 10)
|
|
|
|
ss << "0";
|
2014-12-01 10:32:06 -05:00
|
|
|
ss << now->tm_mday << "/";
|
|
|
|
|
|
|
|
if (locale == "C")
|
|
|
|
ss << (now->tm_year % 100);
|
|
|
|
else if (locale == "en_US")
|
|
|
|
ss << (now->tm_year + 1900);
|
|
|
|
else
|
|
|
|
ss << "????";
|
|
|
|
return ss.str();
|
|
|
|
}
|
2014-11-27 10:37:46 -05:00
|
|
|
|
2014-12-01 10:32:06 -05:00
|
|
|
static void test_track_tag_locale()
|
|
|
|
{
|
|
|
|
Track *a, *b;
|
2014-11-27 10:37:46 -05:00
|
|
|
|
2014-12-01 10:32:06 -05:00
|
|
|
a = tags :: add_track(album, artist, genre, library,
|
|
|
|
MUSIC_DIR + "/Hyrule Symphony/6 - Kakariko Village.mp3",
|
|
|
|
"Kakariko Village", 186, 6);
|
|
|
|
test_equal(a->count(), (unsigned)0);
|
|
|
|
a->played();
|
|
|
|
test_equal(a->count(), (unsigned)1);
|
|
|
|
test_track_tag_load_db(1);
|
|
|
|
|
|
|
|
|
|
|
|
std::setlocale(LC_TIME, "C");
|
|
|
|
test_equal(a->date(), find_expected_date("C"));
|
2014-11-27 10:37:46 -05:00
|
|
|
std::setlocale(LC_TIME, "en_US");
|
2014-12-01 10:32:06 -05:00
|
|
|
test_equal(a->date(), find_expected_date("en_US"));
|
2014-11-27 10:37:46 -05:00
|
|
|
|
2014-11-26 08:55:06 -05:00
|
|
|
|
2014-12-01 10:32:06 -05:00
|
|
|
b = tags :: add_track(album, artist, genre, library,
|
|
|
|
MUSIC_DIR + "/Hyrule Symphony/0 - intro.mp3",
|
|
|
|
"Intro", 56, 0);
|
2014-11-28 10:21:35 -05:00
|
|
|
|
2014-12-01 10:32:06 -05:00
|
|
|
test_equal(a->compare_date(a), 0);
|
2015-01-02 10:26:29 -05:00
|
|
|
test_equal(a->compare_date(b), 2015);
|
|
|
|
test_equal(b->compare_date(a), -2015);
|
2014-11-26 08:55:06 -05:00
|
|
|
}
|
|
|
|
|
2015-08-27 13:17:08 -04:00
|
|
|
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 Locale Test", test_track_tag_locale),
|
|
|
|
);
|