diff --git a/DESIGN b/DESIGN index 8122a01c..15d57a1d 100644 --- a/DESIGN +++ b/DESIGN @@ -577,22 +577,24 @@ Tag Database: last played variables. Library *tagdb :: add_library(const std::string &filepath); - Add a new path to library_db and return its library_id. + Add a new path to library_db. Return a pointer to the new path + or return NULL if the path is already in the database. void tagdb :: remove_track(unsigned int track_id); Remove the track with id equal to track_id from the track_db. void tagdb :: remove_library(unsigned int library_id); - Remove the library with id equal to library_id from the - library_db. + Remove all tracks associated with this library from the + track_db, then remove this library from the library_db. Track *tagdb :: lookup(unsigned int track_id); - Look up the track_id in the track database. + Look up the track_id in the track database. Return NULL if + there is no matching track. - const Database &tagdb :: get_track_db(); + Database &tagdb :: get_track_db(); Return a reference to the track_db. - const Database &tagdb :: get_library_db(); + Database &tagdb :: get_library_db(); Return a reference to the library_db. diff --git a/include/tags.h b/include/tags.h index c9175eee..9e2c8627 100644 --- a/include/tags.h +++ b/include/tags.h @@ -105,9 +105,16 @@ public: }; -namespace tagdb { +namespace tagdb +{ + Track *add_track(const std::string &, Library *); Library *add_library(const std::string &); + void remove_track(unsigned int); + void remove_library(unsigned int); + Track *lookup(unsigned int); + Database &get_track_db(); + Database &get_library_db(); } diff --git a/lib/tags.cpp b/lib/tags.cpp index c5d43711..94b8d47f 100644 --- a/lib/tags.cpp +++ b/lib/tags.cpp @@ -12,6 +12,7 @@ Database artist_db("artist.db", true); Database album_db("album.db", true); Database genre_db("genre.db", true); Database library_db("library.db", true); +Database track_db("track.db", false); /** @@ -193,8 +194,53 @@ bool Track :: less_than(Track *rhs, sort_t field) * */ -Library *tagdb :: add_library(const std::string &filepath) +Track *tagdb :: add_track(const std::string &filepath, Library *library) { - Database::iterator it = library_db.insert(Library(filepath)); + unsigned int size = track_db.size(); + Database::iterator it = track_db.insert(Track(filepath, library)); + if (size == track_db.size()) + return NULL; return &(*it); } + +Library *tagdb :: add_library(const std::string &filepath) +{ + unsigned int size = library_db.size(); + Database::iterator it = library_db.insert(Library(filepath)); + if (size == library_db.size()) + return NULL; + return &(*it); +} + +void tagdb :: remove_track(unsigned int track_id) +{ + track_db.remove(track_id); +} + +void tagdb :: remove_library(unsigned int library_id) +{ + Database::iterator it; + for (it = track_db.begin(); it != track_db.end(); it++) { + if (it->library->id == library_id) + track_db.remove(it->id); + } + library_db.remove(library_id); +} + +Track *tagdb :: lookup(unsigned int track_id) +{ + Database::iterator it = track_db.at(track_id); + if (it == track_db.end()) + return NULL; + return &(*it); +} + +Database &tagdb :: get_track_db() +{ + return track_db; +} + +Database &tagdb :: get_library_db() +{ + return library_db; +} diff --git a/tests/Music/1.ogg b/tests/Music/1.ogg index b869165d..5e94d369 100644 Binary files a/tests/Music/1.ogg and b/tests/Music/1.ogg differ diff --git a/tests/Music/10.ogg b/tests/Music/10.ogg new file mode 100644 index 00000000..6f2e4fcd Binary files /dev/null and b/tests/Music/10.ogg differ diff --git a/tests/Music/15.ogg b/tests/Music/15.ogg new file mode 100644 index 00000000..99bb15b7 Binary files /dev/null and b/tests/Music/15.ogg differ diff --git a/tests/Music/60.ogg b/tests/Music/60.ogg new file mode 100644 index 00000000..16b85beb Binary files /dev/null and b/tests/Music/60.ogg differ diff --git a/tests/Music/600.ogg b/tests/Music/600.ogg new file mode 100644 index 00000000..c7e36082 Binary files /dev/null and b/tests/Music/600.ogg differ diff --git a/tests/Music/666.ogg b/tests/Music/666.ogg new file mode 100644 index 00000000..3586665e Binary files /dev/null and b/tests/Music/666.ogg differ diff --git a/tests/Music/90.ogg b/tests/Music/90.ogg new file mode 100644 index 00000000..abeceb9a Binary files /dev/null and b/tests/Music/90.ogg differ diff --git a/tests/src/tagdb.cpp b/tests/src/tagdb.cpp new file mode 100644 index 00000000..4c96ff96 --- /dev/null +++ b/tests/src/tagdb.cpp @@ -0,0 +1,101 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + * Test a DatabaseEntry + */ + +#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) +{ + /** + * Initial library checks + */ + Library *library = tagdb :: add_library("Music"); + test_results(library->root_path == "Music", __LINE__); + test_library_size(1, __LINE__); + + Library *library_null = tagdb :: add_library("Music"); + test_results(library_null == NULL, __LINE__); + test_library_size(1, __LINE__); + + unsigned int id = library->id; + tagdb :: remove_library(id); + test_library_size(0, __LINE__); + tagdb :: remove_library(id); + test_library_size(0, __LINE__); + + + /** + * Test adding / removing tracks + */ + library = tagdb :: add_library("Music"); + Track *track = tagdb :: add_track("Music/1.ogg", library); + test_track_size(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 + */ + tagdb :: add_track("Music/1.ogg", library); + tagdb :: add_track("Music/10.ogg", library); + tagdb :: add_track("Music/15.ogg", library); + tagdb :: add_track("Music/60.ogg", library); + tagdb :: add_track("Music/90.ogg", library); + tagdb :: add_track("Music/600.ogg", library); + tagdb :: add_track("Music/666.ogg", library); + test_track_size(7, __LINE__); + + test_results(tagdb :: lookup(0) == NULL, __LINE__); + test_results(tagdb :: lookup(1)->id == 1, __LINE__); + test_results(tagdb :: lookup(2)->id == 2, __LINE__); + test_results(tagdb :: lookup(3)->id == 3, __LINE__); + test_results(tagdb :: lookup(4)->id == 4, __LINE__); + test_results(tagdb :: lookup(5)->id == 5, __LINE__); + test_results(tagdb :: lookup(6)->id == 6, __LINE__); + test_results(tagdb :: lookup(7)->id == 7, __LINE__); + test_results(tagdb :: lookup(8) == NULL, __LINE__); + + + tagdb :: remove_library(library->id); + test_track_size(0, __LINE__); + + return 0; +} diff --git a/tests/tag_db b/tests/tag_db index ffe5cd54..2e29ff50 100755 --- a/tests/tag_db +++ b/tests/tag_db @@ -18,3 +18,8 @@ echo test_tag "Library" "-l" echo test_tag "Track" "-t" + + +echo +new_test "Test TagDB" +./src/tagdb.run