ocarina/tests/core/tags/artist.c
Anna Schumaker e1f13a7ef4 core/file: Create new functions for reading data from files
The readd(), readu(), and readhu() functions are all used to read
various forms of integers.  The readl() and readw() are for reading
either lines or individual whitespace-delimited words

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2018-02-21 16:01:15 -05:00

119 lines
3.4 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/artist.h>
#include <tests/test.h>
static void test_verify_empty(struct artist *artist)
{
const struct db_ops *artist_ops = test_artist_ops();
g_assert_cmpstr(artist->ar_name, ==, "");
g_assert_null(artist->ar_tokens[0]);
g_assert_null(artist->ar_alts[0]);
g_assert_null(artist->ar_playlist);
g_assert_cmpstr(artist_ops->dbe_key(&artist->ar_dbe), ==, "");
}
static void test_verify_koji(struct artist *artist)
{
const struct db_ops *artist_ops = test_artist_ops();
g_assert_cmpstr(artist->ar_name, ==, "Koji Kondo");
g_assert_cmpstr(artist->ar_tokens[0], ==, "koji");
g_assert_cmpstr(artist->ar_tokens[1], ==, "kondo");
g_assert_null(artist->ar_tokens[2]);
g_assert_null(artist->ar_alts[0]);
g_assert_null(artist->ar_playlist);
g_assert_cmpstr(artist_ops->dbe_key(&artist->ar_dbe), ==, "Koji Kondo");
}
static void test_artist()
{
struct file f = FILE_INIT_DATA("", "artist_tag", 0);
const struct db_ops *artist_ops = test_artist_ops();
struct artist *artist;
artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0));
test_verify_koji(artist);
g_assert_true( artist_match_token(artist, "koji"));
g_assert_true( artist_match_token(artist, "kondo"));
g_assert_false(artist_match_token(artist, "hajime"));
file_open(&f, OPEN_WRITE);
file_writef(&f, "1 \n2 ");
artist_ops->dbe_write(&f, &artist->ar_dbe);
file_close(&f);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);
file_open(&f, OPEN_READ);
file_readu(&f);
artist = ARTIST(artist_ops->dbe_read(&f, 0));
test_verify_empty(artist);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);
file_readu(&f);
artist = ARTIST(artist_ops->dbe_read(&f, 0));
file_close(&f);
test_verify_koji(artist);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);
}
static void test_artist_compare()
{
const struct db_ops *artist_ops = test_artist_ops();
struct artist *koji, *hajime;
koji = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0));
hajime = ARTIST(artist_ops->dbe_alloc("hajime wakai", 0));
g_assert_cmpint(artist_compare(koji, koji), ==, 0);
g_assert_cmpint(artist_compare(koji, hajime), ==, 1);
g_assert_cmpint(artist_compare(hajime, koji), ==, -1);
g_free(koji->ar_name);
g_free(hajime->ar_name);
artist_ops->dbe_free(&koji->ar_dbe);
artist_ops->dbe_free(&hajime->ar_dbe);
}
static void test_artist_db()
{
const struct db_ops *artist_ops = test_artist_ops();
struct database artist_db;
struct artist *artist;
g_assert_nonnull(artist_db_get());
g_assert_cmpuint(artist_db_get()->db_size, ==, 0);
artist_db_init();
artist = artist_lookup("Koji Kondo");
g_assert_null(artist);
artist = artist_find("Koji Kondo");
test_verify_koji(artist);
g_assert_cmpuint(artist_db_get()->db_size, ==, 1);
g_assert(artist_lookup("Koji Kondo") == artist);
g_assert(artist_find("Koji Kondo") == artist);
g_assert(artist_get(0) == artist);
g_assert_null(artist_get(1));
db_init(&artist_db, "artist.db", false, artist_ops, 0);
db_load(&artist_db);
g_assert_cmpuint(artist_db.db_size, ==, 1);
db_deinit(&artist_db);
artist_db_deinit();
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Core/Tags/Artist", test_artist);
g_test_add_func("/Core/Tags/Artist/Comparison", test_artist_compare);
g_test_add_func("/Core/Tags/Artist/Database", test_artist_db);
return g_test_run();
}