/* * Copyright 2014 (c) Anna Schumaker. */ #include #include static void test_verify_empty(struct artist *artist) { const struct db_ops *artist_ops = test_artist_ops(); test_equal(artist->ar_name, ""); test_equal(artist->ar_lower, ""); test_equal(artist_ops->dbe_key(&artist->ar_dbe), ""); } static void test_verify_koji(struct artist *artist) { const struct db_ops *artist_ops = test_artist_ops(); test_equal(artist->ar_name, "Koji Kondo"); test_equal(artist->ar_lower, "koji kondo"); test_equal(artist_ops->dbe_key(&artist->ar_dbe), "Koji Kondo"); } static void test_artist() { const struct db_ops *artist_ops = test_artist_ops(); struct artist *artist; unsigned int i; struct file f; artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo")); test_verify_koji(artist); file_init(&f, "artist_tag", 0, 0); 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_readf(&f, "%u", &i); artist = ARTIST(artist_ops->dbe_read(&f)); test_verify_empty(artist); g_free(artist->ar_name); artist_ops->dbe_free(&artist->ar_dbe); file_readf(&f, "%u", &i); artist = ARTIST(artist_ops->dbe_read(&f)); 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")); hajime = ARTIST(artist_ops->dbe_alloc("hajime wakai")); test_equal(artist_compare(koji, koji), 0); test_equal(artist_compare(koji, hajime), 1); test_equal(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; artist_db_init(); artist = artist_find("Koji Kondo"); test_verify_koji(artist); test_equal((void *)artist_find("Koji Kondo"), (void *)artist); test_equal((void *)artist_get(0), (void *)artist); test_equal((void *)artist_get(1), (void *)NULL); db_init(&artist_db, "artist.db", false, artist_ops); db_load(&artist_db); test_equal(artist_db.db_size, 1); db_deinit(&artist_db); artist_db_deinit(); } DECLARE_UNIT_TESTS( UNIT_TEST("Artist Tag", test_artist), UNIT_TEST("Artist Comparison", test_artist_compare), UNIT_TEST("Artist Database", test_artist_db), );