ocarina/tests/library/library.cpp

269 lines
5.4 KiB
C++

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <idle.h>
#include <library.h>
#include <print.h>
#include <stdlib.h>
void gen_library()
{
system("tests/library/gen_library.sh");
print("\n");
}
void run_idle_tasks()
{
while (idle :: run_task());
}
bool check_add_dir(const std::string &dir)
{
try {
library :: add_path(dir.c_str());
return true;
} catch (int error) {
return false;
}
}
void test_add_dir(const std::string &test, const std::string &dir, bool expected)
{
print("Test %s: ", test.c_str());
if (check_add_dir(dir.c_str()) == expected)
print("PASSED\n");
else
print("FAILED\n");
run_idle_tasks();
library :: print_db(library :: DB_LIBRARY);
}
void test_del_dir(const std::string &test, const unsigned int path_id)
{
print("Test %s\n", test.c_str());
library :: del_path(path_id);
library :: print_db(library :: DB_LIBRARY);
}
bool check_lookup(const unsigned int track_id, library :: Song *song)
{
try {
library :: lookup(track_id, song);
return true;
} catch (int error) {
return false;
}
}
void test_lookup(const std::string &test, const unsigned int track_id, bool expected)
{
library :: Song song;
bool res;
print("Test %s (track_id == %u): ", test.c_str(), track_id);
res = check_lookup(track_id, &song);
if (res == expected)
print("PASSED\n");
else
print("FAILED\n");
if (res == true) {
print(" %s (%s) by %s (%s) from %s (%s) Length: %s\n",
song.track->title.c_str(),
song.track->title_lower.c_str(),
song.artist->primary_key.c_str(),
song.artist->key_lower.c_str(),
song.album->name.c_str(),
song.album->name_lower.c_str(),
song.track->length_str.c_str());
print(" Genre: %s (%s), Library: %s\n",
song.genre->primary_key.c_str(),
song.genre->key_lower.c_str(),
song.library->root_path.c_str());
}
}
void test_path_lookup(const std::string &test, unsigned int lib_id, bool expected)
{
library :: Library *lib;
print("Test %s (lib_id == %u): ", test.c_str(), lib_id);
try {
lib = library :: lookup_path(lib_id);
if (expected == true)
print("PASSED\n");
else
print("FAILED, no error produced\n");
print("%s (", lib->root_path.c_str());
if (lib->enabled)
print("enabled");
else
print("disabled");
print(") size = %u\n", lib->size);
} catch (int error) {
if (expected == true)
print("FAILED, unexpected error %d\n", error);
else
print("PASSED\n");
}
}
void test_print_dbs(const std::string &test)
{
print("Test %s\n", test.c_str());
library :: print_db(library :: DB_GENRE);
print("\n");
}
/* Add paths library that SHOULD fail */
void test_0()
{
test_add_dir("0a", "/tmp/library/error", false);
test_add_dir("0b", "/tmp/library/file", false);
print("\n");
}
/* Simple library path operations */
void test_1()
{
test_add_dir("1a", "/tmp/library/0", true);
test_del_dir("1b", 0);
print("\n");
}
/* Test multiple paths */
void test_2()
{
library :: reset();
test_add_dir("2a", "/tmp/library/0", true);
test_add_dir("2b", "/tmp/library/1", true);
test_add_dir("2c", "/tmp/library/2", true);
test_del_dir("2d", 1);
test_del_dir("2e", 0);
test_del_dir("2f", 2);
print("\n");
}
/* Test load and save of library db */
void test_3()
{
library :: reset();
test_add_dir("3a", "/tmp/library/0", true);
test_add_dir("3b", "/tmp/library/1", true);
test_add_dir("3c", "/tmp/library/2", true);
print("Test 3d\n");
library :: reset();
library :: print_db(library :: DB_LIBRARY);
print("Test 3e\n");
library :: init();
library :: print_db(library :: DB_LIBRARY);
print("\n");
}
/* Test scanning a single path */
void test_4()
{
library :: reset();
test_add_dir("4a", "/tmp/library/0", true);
library :: print_db(library :: DB_ARTIST);
print("\n");
library :: print_db(library :: DB_ALBUM);
print("\n");
library :: print_db(library :: DB_GENRE);
print("\n");
library :: print_db(library :: DB_TRACK);
print("\n");
}
/* Test lookup() */
void test_5()
{
library :: reset();
/* Lookup on empty DB */
test_lookup("5a", 0, false);
test_add_dir("5b", "/tmp/library/0", true);
/* Lookup on DB[0] */
test_lookup("5c", 0, true);
/* Lookup on DB[10] */
test_lookup("5d", 42, true);
/* Lookup beyond db */
test_lookup("5e", 100000, false);
/* Lookup path id = 0 */
test_path_lookup("5f", 0, true);
/* Lookup path id that doesn't exist */
test_path_lookup("5g", 1, false);
print("\n");
}
/* Test validation code */
void test_6()
{
library :: reset();
test_add_dir("6a", "/tmp/library/0", true);
print("\n");
print("6b: Updating library 0 (nothing should change)\n");
library :: update_path(0);
run_idle_tasks();
library :: print_db(library :: DB_TRACK);
print("\n");
print("6c: Delete /tmp/library/0/Artist 2\n");
system("rm -rf /tmp/library/0/Artist\\ 2/");
library :: update_path(0);
run_idle_tasks();
library :: print_db(library :: DB_LIBRARY);
library :: print_db(library :: DB_TRACK);
print("\n");
print("6d: Regenerate Artist 2\n");
fflush(stdout);
gen_library();
fflush(stdout);
library :: update_all();
run_idle_tasks();
library :: print_db(library :: DB_TRACK);
print("\n");
}
/* Test importing Ocarina 5.11 libraries */
void test_7()
{
library :: reset();
test_add_dir("7a", "/tmp/library/2", true);
print("\n");
library :: import();
run_idle_tasks();
library :: print_db(library :: DB_LIBRARY);
}
int main(int argc, char **argv)
{
gen_library();
test_0();
test_1();
test_2();
test_3();
test_4();
test_5();
test_6();
test_7();
return 0;
}