From 4dafe7270f39baeb93619f9ce93400a6ae35bd66 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 6 May 2014 22:44:40 -0400 Subject: [PATCH] tags: Update the unit test While I'm at it, I also fix a refcounting issue with the Library pointer each track has. Signed-off-by: Anna Schumaker --- include/tags.h | 1 + lib/tags.cpp | 9 ++ tests/.gitignore | 1 + tests/Sconscript | 3 +- tests/src/tagdb.cpp | 131 ------------------- tests/src/tags.cpp | 307 -------------------------------------------- tests/tag_db | 60 --------- tests/tags.cpp | 301 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 314 insertions(+), 499 deletions(-) delete mode 100644 tests/src/tagdb.cpp delete mode 100644 tests/src/tags.cpp delete mode 100755 tests/tag_db create mode 100644 tests/tags.cpp diff --git a/include/tags.h b/include/tags.h index 6cc3ca89..431a3007 100644 --- a/include/tags.h +++ b/include/tags.h @@ -98,6 +98,7 @@ public: Track(); Track(const std::string &, Library *); + ~Track(); const std::string primary_key() const; void read(File &); void write(File &); diff --git a/lib/tags.cpp b/lib/tags.cpp index 8ca7edd1..571268fd 100644 --- a/lib/tags.cpp +++ b/lib/tags.cpp @@ -159,6 +159,12 @@ Track :: Track(const std::string &f, Library *l) play_count(0), last_year(0), last_month(0), last_day(0), filepath(f.substr(l->root_path.size() + 1)) { + library->count++; +} + +Track :: ~Track() +{ + library->count--; } const std::string Track :: primary_key() const @@ -183,6 +189,7 @@ void Track :: read(File &f) genre = genre_db.at(genre_id); title_lower = filter :: add(title, id); + library->count++; set_length_str(); } @@ -248,6 +255,8 @@ void Track :: tag() filter :: add(artist->name, id); filter :: add(album->name, id); + + library->count++; } const std::string Track :: path() const diff --git a/tests/.gitignore b/tests/.gitignore index 3f676dd5..5f5223fc 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -4,3 +4,4 @@ database index filter idle +tags diff --git a/tests/Sconscript b/tests/Sconscript index c2d4b1af..02703643 100644 --- a/tests/Sconscript +++ b/tests/Sconscript @@ -15,6 +15,7 @@ tests = [ ("index.cpp", True, []), ("filter.cpp", True, []), ("idle.cpp", False, [ "idle.cpp" ]), + ("tags.cpp", True, []), ] @@ -39,7 +40,7 @@ def make_program(src, name, extra_files): def make_test(src, name): test = Command("%s.out" % name, [], - "./tests/%s | tee ./tests/%s.out" % (name, name)) + "set -o pipefail; ./tests/%s | tee ./tests/%s.out" % (name, name)) Alias("tests/%s" % name, test) AlwaysBuild(test) return test diff --git a/tests/src/tagdb.cpp b/tests/src/tagdb.cpp deleted file mode 100644 index 28631d22..00000000 --- a/tests/src/tagdb.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - * Test a DatabaseEntry - */ - -#include -#include - -#include -#include - -unsigned int test_num = 0; - -void test_results(bool success, unsigned int line) -{ - print(" %u: ", test_num); - if (success) - print("Success!\n"); - else { - print("FAILED (%u) =(\n", line); - exit(1); - } - test_num++; -} - -void test_library_size(unsigned int expected, unsigned int line) -{ - test_results(tagdb :: get_library_db().size() == expected, line); -} - -void test_track_size(unsigned int expected, unsigned int line) -{ - test_results(tagdb :: get_track_db().size() == expected, line); -} - -int main(int argc, char **argv) -{ - char c; - bool init_called = false; - unsigned int id; - - while ((c = getopt(argc, argv, "i")) != -1) { - switch (c) { - case 'i': - init_called = true; - tagdb :: init(); - test_library_size(1, __LINE__); - test_track_size(0, __LINE__); - default: - break; - } - } - - - /** - * Initial library checks - */ - Library *library, *library_null; - if (init_called == true) { - library = *(tagdb :: get_library_db().begin()); - goto test_tracks; - } - - library = tagdb :: add_library("Music"); - test_results(library->root_path == "Music", __LINE__); - test_library_size(1, __LINE__); - - library_null = tagdb :: add_library("Music"); - test_results(library_null == NULL, __LINE__); - test_library_size(1, __LINE__); - - id = library->id; - tagdb :: remove_library(id); - test_library_size(0, __LINE__); - tagdb :: remove_library(id); - test_library_size(0, __LINE__); - - - library = tagdb :: add_library("Music"); -test_tracks: - /** - * Test adding / removing tracks - */ - Track *track = tagdb :: add_track("Music/1.ogg", library); - test_track_size(1, __LINE__); - test_results(track->track == 1, __LINE__); - Track *track_null = tagdb :: add_track("Music/1.ogg", library); - test_track_size(1, __LINE__); - - test_results(track->path() == "Music/1.ogg", __LINE__); - test_results(track_null == NULL, __LINE__); - - id = track->id; - tagdb :: remove_track(id); - test_track_size(0, __LINE__); - tagdb :: remove_track(id); - test_track_size(0, __LINE__); - - - /** - * Test adding and removing multiple tracks - */ - Track *track1 = tagdb :: add_track("Music/1.ogg", library); - Track *track2 = tagdb :: add_track("Music/10.ogg", library); - Track *track3 = tagdb :: add_track("Music/15.ogg", library); - Track *track4 = tagdb :: add_track("Music/60.ogg", library); - Track *track5 = tagdb :: add_track("Music/90.ogg", library); - Track *track6 = tagdb :: add_track("Music/600.ogg", library); - Track *track7 = tagdb :: add_track("Music/666.ogg", library); - test_track_size(7, __LINE__); - - test_results(tagdb :: lookup(0) == NULL, __LINE__); - test_results(tagdb :: lookup(1) == track1, __LINE__); - test_results(tagdb :: lookup(2) == track2, __LINE__); - test_results(tagdb :: lookup(3) == track3, __LINE__); - test_results(tagdb :: lookup(4) == track4, __LINE__); - test_results(tagdb :: lookup(5) == track5, __LINE__); - test_results(tagdb :: lookup(6) == track6, __LINE__); - test_results(tagdb :: lookup(7) == track7, __LINE__); - test_results(tagdb :: lookup(8) == NULL, __LINE__); - - - tagdb :: remove_library(library->id); - test_track_size(0, __LINE__); - - library = tagdb :: add_library("Music"); - if (init_called == true) - tagdb :: commit(); - - return 0; -} diff --git a/tests/src/tags.cpp b/tests/src/tags.cpp deleted file mode 100644 index 5d89bfe6..00000000 --- a/tests/src/tags.cpp +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - * Test a DatabaseEntry - */ - -#include -#include -#include - -#include -#include -#include - - -unsigned int test_num = 0; - -void test_results(bool success, unsigned int line) -{ - print(" %u: ", test_num); - if (success) - print("Success!\n"); - else { - print("FAILED (%u) =(\n", line); - exit(1); - } - test_num++; -} - -template -void save_tag(const std::string &file, T &tag) -{ - File f(file); - f.open(OPEN_WRITE); - tag.write(f); - f.close(); -} - -template -void load_tag(const std::string &file, T &tag) -{ - File f(file); - f.open(OPEN_READ); - tag.read(f); - f.close(); -} - - -void artist_test_tags(Artist &artist) -{ - test_results(artist.name == "Artist Name", __LINE__); - test_results(artist.lower == "artist name", __LINE__); - test_results(artist.primary_key() == "Artist Name", __LINE__); -} - -void artist_test() -{ - Artist artist("Artist Name"), artist2; - - artist_test_tags(artist); - save_tag("artist.txt", artist); - load_tag("artist.txt", artist2); - artist_test_tags(artist2); -} - - -void album_test_tags(Album &album) -{ - test_results(album.name == "Album Name", __LINE__); - test_results(album.lower == "album name", __LINE__); - test_results(album.year == 2014, __LINE__); - test_results(album.primary_key() == "2014.Album Name", __LINE__); -} - -void album_test() -{ - Album album("Album Name", 2014), album2; - - album_test_tags(album); - save_tag("album.txt", album); - load_tag("album.txt", album2); - album_test_tags(album2); -} - - -void genre_test_tags(Genre &genre) -{ - test_results(genre.name == "Genre Name", __LINE__); - test_results(genre.lower == "genre name", __LINE__); - test_results(genre.primary_key() == "Genre Name", __LINE__); -} - -void genre_test() -{ - Genre genre("Genre Name"), genre2; - - genre_test_tags(genre); - save_tag("genre.txt", genre); - load_tag("genre.txt", genre2); - genre_test_tags(genre2); -} - - -void library_test_tags(Library &library) -{ - test_results(library.root_path == "/home/user/Music", __LINE__); - test_results(library.count == 0, __LINE__); - test_results(library.enabled == true, __LINE__); - test_results(library.primary_key() == "/home/user/Music", __LINE__); -} - -void library_test() -{ - Library library("/home/user/Music"), library2; - - library_test_tags(library); - test_results(library2.enabled == false, __LINE__); - save_tag("library.txt", library); - load_tag("library.txt", library2); - library_test_tags(library2); -} - - -void track_test_tags(Track &track, Library *library) -{ - test_results(track.filepath == "1.ogg", __LINE__); - test_results(track.path() == "Music/1.ogg", __LINE__); - test_results(track.primary_key() == "Music/1.ogg", __LINE__); - test_results(track.library == library, __LINE__); - test_results(track.play_count == 0, __LINE__); - test_results(track.last_year == 0, __LINE__); - test_results(track.last_month == 0, __LINE__); - test_results(track.last_day == 0, __LINE__); -} - -void track_test_search(const std::string &term, unsigned int expected, - unsigned int line) -{ - std::set s; - filter :: search(term, s); - test_results(s.size() == 1 && s.find(expected) != s.end(), line); -} - -void track_test_basics(Library *library) -{ - Track track("Music/1.ogg", library), track2; - track_test_tags(track, library); - test_results(track.artist == NULL, __LINE__); - test_results(track.album == NULL, __LINE__); - test_results(track.genre == NULL, __LINE__); - - track.tag(); - - track_test_tags(track, library); - test_results(track.artist->name == "Artist", __LINE__); - test_results(track.album->name == "Album", __LINE__); - test_results(track.album->year == 2014, __LINE__); - test_results(track.genre->name == "Silence", __LINE__); - test_results(track.track == 1, __LINE__); - test_results(track.length == 1, __LINE__); - test_results(track.title == "One", __LINE__); - test_results(track.title_lower == "one", __LINE__); - - track_test_search("One", track.id, __LINE__); - track_test_search("Artist", track.id, __LINE__); - track_test_search("Album", track.id, __LINE__); - - save_tag("track.txt", track); - load_tag("track.txt", track2); - track_test_tags(track2, library); - test_results(track.artist == track2.artist, __LINE__); - test_results(track.album == track2.album, __LINE__); - test_results(track.genre == track2.genre, __LINE__); - test_results(track.track == track2.track, __LINE__); - test_results(track.length == track2.length, __LINE__); - test_results(track.length_str == track2.length_str, __LINE__); - test_results(track.title == track2.title, __LINE__); - test_results(track.title_lower == track2.title_lower, __LINE__); -} - -void track_test_lenstrs(const std::string &file, const std::string &expected, - Library *library, unsigned int line) -{ - Track track(file, library); - track.tag(); - test_results(track.length_str == expected, line); -} - -void track_test_lenstrs(Library *library) -{ - track_test_lenstrs("Music/1.ogg", "0:01", library, __LINE__); - track_test_lenstrs("Music/10.ogg", "0:10", library, __LINE__); - track_test_lenstrs("Music/15.ogg", "0:15", library, __LINE__); - track_test_lenstrs("Music/60.ogg", "1:00", library, __LINE__); - track_test_lenstrs("Music/90.ogg", "1:30", library, __LINE__); - track_test_lenstrs("Music/600.ogg", "10:00", library, __LINE__); - track_test_lenstrs("Music/666.ogg", "11:06", library, __LINE__); -} - -void test_track_sorting() -{ - Track track1, track2; - Artist artist1("a"), artist2("b"); - Album album1("a", 2000), album2("b", 2014); - Genre genre1("a"), genre2("b"); - - track1.artist = &artist1; - track1.album = &album1; - track1.genre = &genre1; - track1.length = 10; - track1.play_count = 0; - track1.last_year = 2013; - track1.title_lower = "a"; - track1.track = 1; - - track2.artist = &artist2; - track2.album = &album2; - track2.genre = &genre2; - track2.length = 20; - track2.play_count = 1; - track2.last_year = 2014; - track2.title_lower = "b"; - track2.track = 2; - - test_results(track1.less_than(&track1, SORT_ARTIST) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_ARTIST) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_ARTIST) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_ALBUM) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_ALBUM) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_ALBUM) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_GENRE) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_GENRE) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_GENRE) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_LENGTH) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_LENGTH) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_LENGTH) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_PLAYED) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_PLAYED) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_PLAYED) > 0, __LINE__); - - track1.last_year = 2014; - track1.last_month = 3; - track2.last_month = 4; - - test_results(track1.less_than(&track1, SORT_PLAYED) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_PLAYED) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_PLAYED) > 0, __LINE__); - - track1.last_month = 4; - track1.last_day = 10; - track2.last_day = 11; - - test_results(track1.less_than(&track1, SORT_PLAYED) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_PLAYED) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_PLAYED) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_TITLE) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_TITLE) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_TITLE) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_TRACK) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_TRACK) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_TRACK) > 0, __LINE__); - - test_results(track1.less_than(&track1, SORT_YEAR) == 0, __LINE__); - test_results(track1.less_than(&track2, SORT_YEAR) < 0, __LINE__); - test_results(track2.less_than(&track1, SORT_YEAR) > 0, __LINE__); -} - -void track_test() -{ - Library *library = tagdb :: add_library("Music"); - track_test_basics(library); - track_test_lenstrs(library); - test_track_sorting(); -} - - -int main(int argc, char **argv) -{ - char c; - - while ((c = getopt(argc, argv, "aAglt")) != -1) { - switch (c) { - case 'a': - artist_test(); - break; - case 'A': - album_test(); - break; - case 'g': - genre_test(); - break; - case 'l': - library_test(); - break; - case 't': - track_test(); - break; - } - } - - return 0; -} diff --git a/tests/tag_db b/tests/tag_db deleted file mode 100755 index a03482a0..00000000 --- a/tests/tag_db +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash -# Copyright 2014 (c) Anna Schumaker - -. $(dirname $0)/_functions - -function test_tag -{ - new_test "Test $1 Tag" - ./src/tags.run "$2" -} - -function test_db_exists -{ - if [ ! -f $DATA_DIR/$1.db ]; then - echo "ERROR: $DATA_DIR/$1.db doesn't exist!" - exit 1 - fi -} - -function test_db_not_exists -{ - if [ -f $DATA_DIR/$1.db ]; then - echo "ERROR: $DATA_DIR/$1.db shouldn't exist!" - exit 1 - fi -} - -test_tag "Artist" "-a" -echo -test_tag "Album" "-A" -echo -test_tag "Genre" "-g" -echo -test_tag "Library" "-l" -echo -test_tag "Track" "-t" - - -rm -rf $DATA_DIR/*.db - -echo -new_test "Test TagDB" -./src/tagdb.run - -test_db_exists artist -test_db_exists album -test_db_exists genre -test_db_exists library -test_db_not_exists track - - -echo -new_test "Test TagDB (call init)" -./src/tagdb.run -i - -test_db_exists artist -test_db_exists album -test_db_exists genre -test_db_exists library -test_db_exists track diff --git a/tests/tags.cpp b/tests/tags.cpp new file mode 100644 index 00000000..2695f2e1 --- /dev/null +++ b/tests/tags.cpp @@ -0,0 +1,301 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + * Test a DatabaseEntry + */ + +#include +#include "test.h" + +static Library *LIB_NULL = NULL; +static Track *TRACK_NULL = NULL; + +struct TagArgs { + struct Library *library; + std::string full_path; + + std::string artist; + std::string artist_lower; + + std::string album; + std::string album_lower; + unsigned int year; + + std::string genre; + std::string genre_lower; + + unsigned int track; + unsigned int length; + unsigned int play_count; + unsigned int last_year; + unsigned int last_month; + unsigned int last_day; + std::string title; + std::string title_lower; + std::string filepath; + std::string length_str; +}; + +static void test_library() +{ + Library *lib = tagdb :: add_library("tests/Music/"); + + test_not_equal(lib, LIB_NULL); + test_equal(lib->root_path, (std::string)"tests/Music/"); + test_equal(lib->count, (unsigned)0); + test_equal(lib->enabled, true); + + test_equal(tagdb :: add_library("tests/Music/"), LIB_NULL); + test_equal(lib, tagdb :: lookup_library(0)); + + tagdb :: remove_library(0); + test_equal(tagdb :: get_library_db().size(), (unsigned)0); +} + +static void test_track(struct TagArgs *args) +{ + Track *track = tagdb :: add_track(args->full_path, args->library); + + test_not_equal(track, TRACK_NULL); + test_equal(track->path(), args->full_path); + + /* + * Check tags + */ + test_equal(track->artist->name, args->artist); + test_equal(track->artist->lower, args->artist_lower); + test_equal(track->album->name, args->album); + test_equal(track->album->lower, args->album_lower); + test_equal(track->album->year, args->year); + test_equal(track->genre->name, args->genre); + test_equal(track->genre->lower, args->genre_lower); + test_equal(track->track, args->track); + test_equal(track->length, args->length); + test_equal(track->play_count, args->play_count); + test_equal(track->last_year, args->last_year); + test_equal(track->last_month, args->last_month); + test_equal(track->last_day, args->last_day); + test_equal(track->title, args->title); + test_equal(track->title_lower, args->title_lower); + test_equal(track->filepath, args->filepath); + test_equal(track->length_str, args->length_str); + + test_equal(tagdb :: lookup(args->track - 1), track); + test_equal(args->library->count, (unsigned)1); + tagdb :: remove_track(args->track - 1); + test_equal(tagdb :: lookup(args->track - 1), TRACK_NULL); + test_equal(tagdb :: get_track_db().size(), (unsigned)0); + test_equal(args->library->count, (unsigned)0); +} + +static void test_all_tracks() +{ + struct TagArgs expected; + + expected.library = tagdb :: add_library("tests/Music"); + expected.full_path = "tests/Music/1.ogg"; + expected.artist = "Artist"; + expected.artist_lower = "artist"; + expected.album = "Album"; + expected.album_lower = "album"; + expected.year = 2014; + expected.genre = "Silence"; + expected.genre_lower = "silence"; + expected.track = 1; + expected.length = 1; + expected.play_count = 0; + expected.last_year = 0; + expected.last_month = 0; + expected.last_day = 0; + expected.title = "One"; + expected.title_lower = "one"; + expected.filepath = "1.ogg"; + expected.length_str = "0:01"; + run_test("Tags Track Test (1.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/10.ogg"; + expected.track = 2; + expected.length = 10; + expected.title = "Ten"; + expected.title_lower = "ten"; + expected.filepath = "10.ogg"; + expected.length_str = "0:10"; + run_test("Tags Track Test (10.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/15.ogg"; + expected.track = 3; + expected.length = 15; + expected.title = "Fifteen"; + expected.title_lower = "fifteen"; + expected.filepath = "15.ogg"; + expected.length_str = "0:15"; + run_test("Tags Track Test (15.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/60.ogg"; + expected.track = 4; + expected.length = 60; + expected.title = "Sixty"; + expected.title_lower = "sixty"; + expected.filepath = "60.ogg"; + expected.length_str = "1:00"; + run_test("Tags Track Test (60.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/90.ogg"; + expected.album = "Album Two"; + expected.album_lower = "album two"; + expected.track = 5; + expected.length = 90; + expected.title = "Ninety"; + expected.title_lower = "ninety"; + expected.filepath = "90.ogg"; + expected.length_str = "1:30"; + run_test("Tags Track Test (90.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/600.ogg"; + expected.track = 6; + expected.length = 600; + expected.title = "Six Hundred"; + expected.title_lower = "six hundred"; + expected.filepath = "600.ogg"; + expected.length_str = "10:00"; + run_test("Tags Track Test (600.ogg)", test_track, &expected); + + expected.full_path = "tests/Music/666.ogg"; + expected.track = 7; + expected.length = 666; + expected.title = "Six-Six-Six"; + expected.title_lower = "six six six"; + expected.filepath = "666.ogg"; + expected.length_str = "11:06"; + run_test("Tags Track Test (666.ogg)", test_track, &expected); +} + +static void test_comparison() +{ + Library *lib = tagdb :: lookup_library(1); + Track *track1 = tagdb :: add_track("tests/Music/1.ogg", lib); + Track *track10 = tagdb :: add_track("tests/Music/10.ogg", lib); + Track *track15 = tagdb :: add_track("tests/Music/15.ogg", lib); + Track *track60 = tagdb :: add_track("tests/Music/60.ogg", lib); + Track *track90 = tagdb :: add_track("tests/Music/90.ogg", lib); + Track *track600 = tagdb :: add_track("tests/Music/600.ogg", lib); + Track *track666 = tagdb :: add_track("tests/Music/666.ogg", lib); + + Artist art2(""); + track10->artist = &art2; + test_equal(track1->less_than(track10, SORT_ARTIST), -1); + test_equal(track10->less_than(track1, SORT_ARTIST), 1); + + Artist art3("Artist Three"); + track10->artist = &art3; + test_equal(track1->less_than(track1, SORT_ARTIST), 0); + test_equal(track1->less_than(track10, SORT_ARTIST) < 0, true); + test_equal(track10->less_than(track1, SORT_ARTIST) > 0, true); + + test_equal(track1->less_than(track1, SORT_ALBUM), 0); + test_equal(track1->less_than(track90, SORT_ALBUM) < 0, true); + test_equal(track90->less_than(track1, SORT_ALBUM) > 0, true); + + track15->play_count++; + test_equal(track1->less_than(track1, SORT_COUNT), 0); + test_equal(track1->less_than(track15, SORT_COUNT) < 0, true); + test_equal(track15->less_than(track1, SORT_COUNT) > 0, true); + + Genre gen2("X-Treme Silence!!!"); + track60->genre = &gen2; + test_equal(track1->less_than(track1, SORT_GENRE), 0); + test_equal(track1->less_than(track60, SORT_GENRE) < 0, true); + test_equal(track60->less_than(track1, SORT_GENRE) > 0, true); + + test_equal(track1->less_than(track1, SORT_LENGTH), 0); + test_equal(track1->less_than(track600, SORT_LENGTH) < 0, true); + test_equal(track600->less_than(track1, SORT_LENGTH) > 0, true); + + track15->last_year = 2014; + test_equal(track1->less_than(track1, SORT_PLAYED), 0); + test_equal(track1->less_than(track15, SORT_PLAYED) < 0, true); + test_equal(track15->less_than(track1, SORT_PLAYED) > 0, true); + + track1->last_year = 2014; + track15->last_month = 5; + test_equal(track1->less_than(track15, SORT_PLAYED) < 0, true); + test_equal(track15->less_than(track1, SORT_PLAYED) > 0, true); + + track1->last_month = 5; + track15->last_day = 6; + test_equal(track1->less_than(track15, SORT_PLAYED) < 0, true); + test_equal(track15->less_than(track1, SORT_PLAYED) > 0, true); + + test_equal(track1->less_than(track1, SORT_TITLE), 0); + test_equal(track1->less_than(track666, SORT_TITLE) < 0, true); + test_equal(track666->less_than(track1, SORT_TITLE) > 0, true); + + test_equal(track1->less_than(track1, SORT_TRACK), 0); + test_equal(track1->less_than(track60, SORT_TRACK) < 0, true); + test_equal(track60->less_than(track1, SORT_TRACK) > 0, true); + + track666->album->year = 2048; + test_equal(track1->less_than(track1, SORT_YEAR), 0); + test_equal(track1->less_than(track666, SORT_YEAR) < 0, true); + test_equal(track666->less_than(track1, SORT_YEAR) > 0, true); +} + +static void test_lib_removal() +{ + test_equal(tagdb :: get_track_db().size(), (unsigned)7); + test_equal(tagdb :: lookup_library(1)->count, (unsigned)7); + tagdb :: remove_library(1); + test_equal(tagdb :: get_track_db().size(), (unsigned)0); +} + +static void test_save_load() +{ + Database artist("artist.db", false); + Database album("album.db", false); + Database genre("genre.db", false); + + artist.load(); + album.load(); + genre.load(); + + test_equal(artist.size(), (unsigned)1); + test_equal(album.size(), (unsigned)2); + test_equal(genre.size(), (unsigned)1); + + + Database library("library.db", false); + library.load(); + test_equal(library.size(), (unsigned)0); + + Library *lib = tagdb :: add_library("tests/Music"); + library.load(); + test_equal(library.size(), (unsigned)1); + + Database library2("library.db", false); + lib->enabled = false; + tagdb :: commit_library(); + library2.load(); + test_equal(library2.at(2)->enabled, false); + + + tagdb :: add_track("tests/Music/1.ogg", lib); + tagdb :: add_track("tests/Music/15.ogg", lib); + tagdb :: add_track("tests/Music/60.ogg", lib); + + Database track("track.db", false); + track.load(); + test_equal(track.size(), (unsigned)0); + + tagdb :: commit(); + track.load(); + test_equal(track.size(), (unsigned)3); +} + +int main(int argc, char **argv) +{ + run_test("Tags Library Test", test_library); + test_all_tracks(); + run_test("Tags Comparison Test", test_comparison); + run_test("Tags Library Removal Test", test_lib_removal); + run_test("Tags Save and Load Test", test_save_load); + return 0; +}