Anna Schumaker
eb8a23f2ff
This lets up update track keys to prevent duplicates showing up during a library path update. Fixes #93: Library sometimes has duplicated tracks Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
319 lines
11 KiB
C
319 lines
11 KiB
C
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/idle.h>
|
|
#include <core/string.h>
|
|
#include <core/tags/tags.h>
|
|
#include <core/tags/track.h>
|
|
#include <tests/test.h>
|
|
#include <locale.h>
|
|
|
|
|
|
static struct track *test_alloc(const gchar *key)
|
|
{
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
struct db_entry *dbe = track_ops->dbe_alloc(key);
|
|
g_assert_nonnull(dbe);
|
|
return TRACK(dbe);
|
|
}
|
|
|
|
static void test_verify_tags(struct track *track)
|
|
{
|
|
g_assert_cmpstr(track->tr_album->al_name, ==, "Hyrule Symphony");
|
|
g_assert_cmpuint(track->tr_album->al_year, ==, 1998);
|
|
g_assert_cmpstr(track->tr_album->al_artist->ar_name, ==, "Koji Kondo");
|
|
g_assert_cmpstr(track->tr_album->al_genre->ge_name, ==, "Game");
|
|
g_assert_cmpstr(track->tr_library->li_path, ==, "tests/Music");
|
|
}
|
|
|
|
static void test_verify_track(struct track *track)
|
|
{
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
test_verify_tags(track);
|
|
|
|
g_assert_cmpstr(track->tr_title, ==, "Title Theme");
|
|
g_assert_cmpstr(track->tr_tokens[0], ==, "title");
|
|
g_assert_cmpstr(track->tr_tokens[1], ==, "theme");
|
|
g_assert_null(track->tr_tokens[2]);
|
|
g_assert_null(track->tr_alts[0]);
|
|
g_assert_cmpstr(track_ops->dbe_key(&track->tr_dbe), ==,
|
|
"0/Hyrule Symphony/01 - Title Theme.ogg");
|
|
g_assert_cmpstr_free(track_path(track), ==,
|
|
"tests/Music/Hyrule Symphony/01 - Title Theme.ogg");
|
|
g_assert_cmpstr_free(track_last_play(track), ==, "Never");
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, 1);
|
|
g_assert_cmpuint(track->tr_length, ==, 243);
|
|
g_assert_cmpuint(track->tr_count, ==, 0);
|
|
}
|
|
|
|
static void test_verify_notrack(struct track *track)
|
|
{
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
test_verify_tags(track);
|
|
|
|
g_assert_cmpstr(track->tr_title, ==, "");
|
|
g_assert_null(track->tr_tokens[0]);
|
|
g_assert_null(track->tr_alts[0]);
|
|
g_assert_cmpstr(track_ops->dbe_key(&track->tr_dbe), ==,
|
|
"0/Hyrule Symphony/00 - No Track.ogg");
|
|
g_assert_cmpstr_free(track_path(track), ==,
|
|
"tests/Music/Hyrule Symphony/00 - No Track.ogg");
|
|
g_assert_cmpstr_free(track_last_play(track), ==, "Never");
|
|
|
|
g_assert_cmpuint(track->tr_track, ==, 0);
|
|
g_assert_cmpuint(track->tr_length, ==, 0);
|
|
g_assert_cmpuint(track->tr_count, ==, 0);
|
|
}
|
|
|
|
static void test_track()
|
|
{
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
time_t rawtime = time(NULL);
|
|
struct tm *now = localtime(&rawtime);
|
|
struct track *track;
|
|
struct file f;
|
|
gchar *date;
|
|
|
|
file_init(&f, "track_tag", 1);
|
|
g_assert_nonnull(library_find("tests/Music"));
|
|
date = string_tm2str(now);
|
|
|
|
track = test_alloc("0/Hyrule Symphony/01 - Title Theme.ogg");
|
|
g_assert_nonnull(track);
|
|
track->tr_dbe.dbe_index = 0;
|
|
test_verify_track(track);
|
|
|
|
g_assert_true( track_match_token(track, "title"));
|
|
g_assert_true( track_match_token(track, "theme"));
|
|
g_assert_false(track_match_token(track, "hyrule"));
|
|
|
|
file_open(&f, OPEN_WRITE);
|
|
file_writef(&f, "0 0 0 0 0 0 \n");
|
|
file_writef(&f, "Hyrule Symphony/00 - No Track.ogg\n");
|
|
track_ops->dbe_write(&f, &track->tr_dbe);
|
|
file_close(&f);
|
|
|
|
g_free(track->tr_path);
|
|
track_ops->dbe_free(&track->tr_dbe);
|
|
|
|
file_open(&f, OPEN_READ);
|
|
track = TRACK(track_ops->dbe_read(&f));
|
|
track->tr_dbe.dbe_index = 0;
|
|
test_verify_notrack(track);
|
|
|
|
g_free(track->tr_path);
|
|
track_ops->dbe_free(&track->tr_dbe);
|
|
|
|
track = TRACK(track_ops->dbe_read(&f));
|
|
track->tr_dbe.dbe_index = 0;
|
|
test_verify_track(track);
|
|
file_close(&f);
|
|
|
|
track_played(track);
|
|
g_assert_cmpuint(track->tr_count, ==, 1);
|
|
g_assert_cmpstr_free(track_last_play(track), ==, date);
|
|
|
|
g_free(track->tr_path);
|
|
track_ops->dbe_free(&track->tr_dbe);
|
|
g_free(date);
|
|
}
|
|
|
|
static void test_track_compare()
|
|
{
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
struct track *title = test_alloc("0/Hyrule Symphony/01 - Title Theme.ogg");
|
|
struct track *kokiri = test_alloc("0/Ocarina of Time/05 - Kokiri Forest.ogg");
|
|
|
|
/* Compare artists. */
|
|
title->tr_album->al_artist = artist_find("Hajime Wakai");
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_ARTIST), ==, 0);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_ARTIST), ==, 1);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_ARTIST), ==, -1);
|
|
title->tr_album->al_artist = artist_find("Koji Kondo");
|
|
|
|
/* Compare albums. */
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_ALBUM), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_ALBUM), ==, -1);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_ALBUM), ==, 1);
|
|
|
|
/* Compare play counts. */
|
|
title->tr_count = 42;
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_COUNT), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_COUNT), ==, 42);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_COUNT), ==, -42);
|
|
title->tr_count = 0;
|
|
|
|
/* Compare genres. */
|
|
kokiri->tr_album->al_genre = genre_find("Video Game");
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_GENRE), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_GENRE), ==, -1);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_GENRE), ==, 1);
|
|
|
|
/* Compare lenghts (title = 243, kokiri = 130). */
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_LENGTH), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_LENGTH), ==, 131);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_LENGTH), ==, -131);
|
|
|
|
/* Compare last played dates. */
|
|
date_set(&kokiri->tr_date, 0, 0, 17);
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_PLAYED), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_PLAYED), ==, -17);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_PLAYED), ==, 17);
|
|
|
|
date_set(&kokiri->tr_date, 0, 6, 17);
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_PLAYED), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_PLAYED), ==, -6);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_PLAYED), ==, 6);
|
|
|
|
date_set(&kokiri->tr_date, 1988, 6, 17);
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_PLAYED), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_PLAYED), ==, -1988);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_PLAYED), ==, 1988);
|
|
|
|
/* Compare titles. */
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_TITLE), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_TITLE), ==, 1);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_TITLE), ==, -1);
|
|
|
|
/* Compare track numbers. */
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_TRACK), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_TRACK), ==, -4);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_TRACK), ==, 4);
|
|
|
|
/* Compare years. */
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_YEAR), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_YEAR), ==, -1);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_YEAR), ==, 1);
|
|
|
|
title->tr_album->al_year += 2;
|
|
g_assert_cmpint(track_compare(title, title, COMPARE_YEAR), ==, 0);
|
|
g_assert_cmpint(track_compare(title, kokiri, COMPARE_YEAR), ==, 2);
|
|
g_assert_cmpint(track_compare(kokiri, title, COMPARE_YEAR), ==, -2);
|
|
title->tr_album->al_year -= 2;
|
|
|
|
g_free(title->tr_path);
|
|
g_free(kokiri->tr_path);
|
|
track_ops->dbe_free(&title->tr_dbe);
|
|
track_ops->dbe_free(&kokiri->tr_dbe);
|
|
}
|
|
|
|
static void __test_track_db_subprocess()
|
|
{
|
|
const gchar *path = "tests/Music/Hyrule Symphony/01 - Title Theme.ogg";
|
|
struct library *library = library_find("tests/Music");
|
|
const struct db_ops *track_ops = test_track_ops();
|
|
struct database track_db;
|
|
struct track *track;
|
|
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 0);
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 0);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 0);
|
|
track = track_add(library, path);
|
|
test_verify_track(track);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
|
|
g_assert_null(track_add(library, path));
|
|
g_assert(track_get(0) == track);
|
|
g_assert_null(track_get(1));
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 1);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
|
|
db_init(&track_db, "track.db", false, track_ops, 0);
|
|
db_load(&track_db);
|
|
g_assert_cmpuint(track_db.db_size, ==, 0);
|
|
|
|
track_db_commit();
|
|
db_load(&track_db);
|
|
g_assert_cmpuint(track_db.db_size, ==, 1);
|
|
test_verify_track(TRACK(db_first(&track_db)));
|
|
/* Make sure our unplayed count isn't skewed */
|
|
db_deinit(&track_db);
|
|
|
|
track_remove(track);
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 0);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 0);
|
|
|
|
track = track_add(library, path);
|
|
g_assert_nonnull(track);
|
|
|
|
track = track_add(library,
|
|
"tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg");
|
|
g_assert_nonnull(track);
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 2);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 2);
|
|
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 0);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 0);
|
|
track_played(track);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 1);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 1);
|
|
track_played(track);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 2);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 2);
|
|
|
|
track = track_add(library, "tests/Music/invalid_track");
|
|
g_assert_null(track);
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 2);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
|
|
/* Reload the database */
|
|
track_db_deinit();
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 0);
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 0);
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 0);
|
|
track_db_init();
|
|
while (idle_run_task()) {};
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 2);
|
|
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 2);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 2);
|
|
|
|
track_remove(track_get(2));
|
|
g_assert_cmpuint(track_db_count_plays(), ==, 0);
|
|
g_assert_cmpuint(track_db_average_plays(), ==, 0);
|
|
|
|
/* Rekey the database */
|
|
track_get(1)->tr_library = library_find("tests/Music2");
|
|
track_db_rekey();
|
|
g_assert_cmpstr(track_get(1)->tr_path, ==, "1/Hyrule Symphony/01 - Title Theme.ogg");
|
|
|
|
track_remove_all(library_get(0));
|
|
track_remove_all(library_get(1));
|
|
g_assert_cmpuint(track_db_get()->db_size, ==, 0);
|
|
}
|
|
|
|
static void test_track_db()
|
|
{
|
|
if (g_test_subprocess()) {
|
|
__test_track_db_subprocess();
|
|
return;
|
|
}
|
|
|
|
g_test_trap_subprocess(NULL, 0, 0);
|
|
g_test_trap_assert_passed();
|
|
g_test_trap_assert_stderr("*WARNING: Could not read tags for:*");
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int ret;
|
|
|
|
setlocale(LC_TIME, "C");
|
|
idle_init_sync();
|
|
tags_init();
|
|
while (idle_run_task()) {}
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
g_test_add_func("/Core/Tags/Track", test_track);
|
|
g_test_add_func("/Core/Tags/Track/Comparison", test_track_compare);
|
|
g_test_add_func("/Core/Tags/Track/Database", test_track_db);
|
|
ret = g_test_run();
|
|
|
|
tags_deinit();
|
|
idle_deinit();
|
|
return ret;
|
|
}
|