938bbc92f2
I want a way to lookup library paths without allocating new ones, so let's add this now. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
116 lines
3.3 KiB
C
116 lines
3.3 KiB
C
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/tags/library.h>
|
|
#include <tests/test.h>
|
|
|
|
static void test_verify_zelda(struct library *library)
|
|
{
|
|
const struct db_ops *library_ops = test_library_ops();
|
|
test_equal(library->li_enabled, (bool)true);
|
|
test_equal(library->li_size, 0);
|
|
test_equal(library->li_playlist, NULL);
|
|
test_equal(library_ops->dbe_key(&library->li_dbe), "/home/Zelda/Music");
|
|
}
|
|
|
|
static void test_verify_link(struct library *library)
|
|
{
|
|
const struct db_ops *library_ops = test_library_ops();
|
|
test_equal(library->li_enabled, (bool)false);
|
|
test_equal(library->li_size, 0);
|
|
test_equal(library->li_playlist, NULL);
|
|
test_equal(library_ops->dbe_key(&library->li_dbe), "/home/Link/Music");
|
|
}
|
|
|
|
static void test_library()
|
|
{
|
|
const struct db_ops *library_ops = test_library_ops();
|
|
struct library *link, *zelda, *library;
|
|
struct file f;
|
|
|
|
link = LIBRARY(library_ops->dbe_alloc("/home/Link/Music"));
|
|
zelda = LIBRARY(library_ops->dbe_alloc("/home/Zelda/Music"));
|
|
|
|
library_set_enabled(link, false);
|
|
test_verify_link(link);
|
|
test_verify_zelda(zelda);
|
|
|
|
link->li_size = 21;
|
|
zelda->li_size = 42;
|
|
|
|
file_init(&f, "library_tag", 0, 0);
|
|
file_open(&f, OPEN_WRITE);
|
|
library_ops->dbe_write(&f, &link->li_dbe);
|
|
file_writef(&f, "\n");
|
|
library_ops->dbe_write(&f, &zelda->li_dbe);
|
|
file_close(&f);
|
|
|
|
file_open(&f, OPEN_READ);
|
|
library = LIBRARY(library_ops->dbe_read(&f));
|
|
test_verify_link(library);
|
|
test_str_equal(library_file(library, "navi.mp3"),
|
|
"/home/Link/Music/navi.mp3");
|
|
g_free(library->li_path);
|
|
library_ops->dbe_free(&library->li_dbe);
|
|
|
|
library = LIBRARY(library_ops->dbe_read(&f));
|
|
file_close(&f);
|
|
test_verify_zelda(library);
|
|
test_str_equal(library_file(library, "impa.ogg"),
|
|
"/home/Zelda/Music/impa.ogg");
|
|
|
|
g_free(link->li_path);
|
|
g_free(zelda->li_path);
|
|
g_free(library->li_path);
|
|
library_ops->dbe_free(&link->li_dbe);
|
|
library_ops->dbe_free(&zelda->li_dbe);
|
|
library_ops->dbe_free(&library->li_dbe);
|
|
}
|
|
|
|
static void test_library_db()
|
|
{
|
|
const struct db_ops *library_ops = test_library_ops();
|
|
struct library *library, *library2;
|
|
struct database library_db;
|
|
|
|
test_not_equal((void *)library_db_get(), NULL);
|
|
test_equal(library_db_get()->db_size, 0);
|
|
|
|
library_db_init();
|
|
library = library_lookup("/home/Zelda/Music");
|
|
test_equal((void *)library, NULL);
|
|
library = library_find("/home/Zelda/Music");
|
|
test_verify_zelda(library);
|
|
|
|
test_equal(library_db_get()->db_size, 1);
|
|
test_equal((void *)library_lookup("/home/Zelda/Music"), (void *)library);
|
|
test_equal((void *)library_find("/home/Zelda/Music"), (void *)library);
|
|
test_equal((void *)library_get(0), (void *)library);
|
|
test_equal((void *)library_get(1), NULL);
|
|
|
|
db_init(&library_db, "library.db", false, library_ops);
|
|
db_load(&library_db);
|
|
test_equal(library_db.db_size, 1);
|
|
test_equal(db_actual_size(&library_db), 1);
|
|
|
|
library2 = LIBRARY(db_at(&library_db, 0));
|
|
test_not_equal((void *)library2, NULL);
|
|
test_verify_zelda(library2);
|
|
|
|
library_remove(library);
|
|
test_equal((void *)library_get(0), (void *)NULL);
|
|
test_equal(library_db_get()->db_size, 0);
|
|
|
|
library_db_deinit();
|
|
db_deinit(&library_db);
|
|
library_db.db_entries = g_ptr_array_new();
|
|
db_load(&library_db);
|
|
test_equal(library_db.db_size, 0);
|
|
g_ptr_array_free(library_db.db_entries, true);
|
|
}
|
|
|
|
DECLARE_UNIT_TESTS(
|
|
UNIT_TEST("Library Tag", test_library),
|
|
UNIT_TEST("Library Database", test_library_db),
|
|
);
|