tests: Build containers/database test with ctest
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
10f81461d9
commit
1b83978b09
|
@ -9,3 +9,4 @@ core_unit_test(String)
|
|||
core_unit_test(File)
|
||||
core_unit_test(Date)
|
||||
core_unit_test(Idle)
|
||||
core_unit_test(Database)
|
||||
|
|
|
@ -28,8 +28,7 @@ core_objs += [ env.Object("../../core/string.c") ]
|
|||
core_objs += [ env.Object("../../core/file.c") ]
|
||||
core_objs += [ env.Object("../../core/date.c") ]
|
||||
core_objs += [ env.Object("../../core/idle.c") ]
|
||||
|
||||
res += [ CoreTest("database") ]
|
||||
core_objs += [ env.Object("../../core/database.c") ]
|
||||
|
||||
res += SConscript("tags/Sconscript")
|
||||
res += [ CoreTest("queue") ]
|
||||
|
|
|
@ -78,10 +78,10 @@ static void test_db_entry()
|
|||
struct file f;
|
||||
|
||||
ent = INT_ENTRY(int_ops.dbe_alloc("1"));
|
||||
test_equal(ent->ie_dbe.dbe_index, 0);
|
||||
test_equal((void *)ent->ie_dbe.dbe_data, (void *)ent);
|
||||
test_equal(ent->ie_val, 1);
|
||||
test_str_equal(int_ops.dbe_key(&ent->ie_dbe), "1");
|
||||
g_assert_cmpuint(ent->ie_dbe.dbe_index, ==, 0);
|
||||
g_assert(ent->ie_dbe.dbe_data == ent);
|
||||
g_assert_cmpuint(ent->ie_val, ==, 1);
|
||||
g_assert_cmpstr_free(int_ops.dbe_key(&ent->ie_dbe), ==, "1");
|
||||
|
||||
file_init(&f, "test_db_entry", 0, 0);
|
||||
file_open(&f, OPEN_WRITE);
|
||||
|
@ -89,20 +89,20 @@ static void test_db_entry()
|
|||
file_close(&f);
|
||||
|
||||
int_ops.dbe_free(&ent->ie_dbe);
|
||||
test_equal(test_free_count, 1);
|
||||
g_assert_cmpuint(test_free_count, ==, 1);
|
||||
|
||||
file_open(&f, OPEN_READ);
|
||||
ent = INT_ENTRY(int_ops.dbe_read(&f));
|
||||
file_close(&f);
|
||||
|
||||
int_ops.dbe_setup(&ent->ie_dbe);
|
||||
test_equal((void *)ent->ie_dbe.dbe_data, (void *)ent);
|
||||
test_equal(ent->ie_val, 1);
|
||||
test_str_equal(int_ops.dbe_key(&ent->ie_dbe), "1");
|
||||
test_equal(test_setup_count, 1);
|
||||
g_assert(ent->ie_dbe.dbe_data == ent);
|
||||
g_assert_cmpuint(ent->ie_val, ==, 1);
|
||||
g_assert_cmpstr_free(int_ops.dbe_key(&ent->ie_dbe), ==, "1");
|
||||
g_assert_cmpuint(test_setup_count, ==, 1);
|
||||
|
||||
int_ops.dbe_free(&ent->ie_dbe);
|
||||
test_equal(test_free_count, 2);
|
||||
g_assert_cmpuint(test_free_count, ==, 2);
|
||||
}
|
||||
|
||||
static void test_init()
|
||||
|
@ -110,25 +110,22 @@ static void test_init()
|
|||
struct database db = DB_INIT("init.db", false, &int_ops);
|
||||
|
||||
/* Check initial sizes. */
|
||||
test_equal(db.db_entries->len, 0);
|
||||
test_equal(g_hash_table_size(db.db_keys), 0);
|
||||
test_equal(db.db_size, 0);
|
||||
test_equal(db.db_autosave, (bool)false);
|
||||
test_equal(db.db_file.f_version, 0);
|
||||
test_equal(db.db_file.f_name, "init.db");
|
||||
g_assert_cmpuint(db.db_entries->len, ==, 0);
|
||||
g_assert_cmpuint(g_hash_table_size(db.db_keys), ==, 0);
|
||||
g_assert_cmpuint(db.db_size, ==, 0);
|
||||
g_assert_false(db.db_autosave);
|
||||
g_assert_cmpuint(db.db_file.f_version, ==, 0);
|
||||
g_assert_cmpstr(db.db_file.f_name, ==, "init.db");
|
||||
|
||||
db_deinit(&db);
|
||||
}
|
||||
|
||||
#define PTRS_AT(db, index) \
|
||||
g_ptr_array_index(db, index)
|
||||
|
||||
static void test_stress(unsigned int N)
|
||||
static void test_database(gconstpointer arg)
|
||||
{
|
||||
unsigned int N = GPOINTER_TO_UINT(arg);
|
||||
struct database db = DB_INIT("stress.db", false, &int_ops);;
|
||||
struct db_entry *dbe, *next;
|
||||
struct int_entry rmv;
|
||||
GPtrArray *ptrs;
|
||||
unsigned int i;
|
||||
gchar *key;
|
||||
|
||||
|
@ -136,106 +133,99 @@ static void test_stress(unsigned int N)
|
|||
rmv.ie_dbe.dbe_index = 0;
|
||||
test_free_count = 0;
|
||||
test_setup_count = 0;
|
||||
ptrs = g_ptr_array_new();
|
||||
|
||||
/* db_insert() */
|
||||
for (i = 0; i < N; i++) {
|
||||
key = g_strdup_printf("%u", i);
|
||||
dbe = db_insert(&db, key);
|
||||
test_loop_not_equal((void *)dbe, NULL, i);
|
||||
test_loop_equal(dbe->dbe_index, i, i);
|
||||
test_loop_equal(dbe->dbe_key, key, i);
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_ptr_array_add(ptrs, INT_ENTRY(dbe));
|
||||
g_assert_nonnull(dbe);
|
||||
g_assert_cmpuint(dbe->dbe_index, ==, i);
|
||||
g_assert_cmpstr(dbe->dbe_key, ==, key);
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
g_free(key);
|
||||
} test_loop_passed();
|
||||
}
|
||||
|
||||
dbe = db_insert(&db, NULL);
|
||||
test_equal((void *)dbe, NULL);
|
||||
test_equal(db.db_size, N);
|
||||
test_equal(db_actual_size(&db), N);
|
||||
test_equal(test_setup_count, N);
|
||||
g_assert_null(dbe);
|
||||
g_assert_cmpuint(db.db_size, ==, N);
|
||||
g_assert_cmpuint(db_actual_size(&db), ==, N);
|
||||
g_assert_cmpuint(test_setup_count, ==, N);
|
||||
|
||||
/* db_at() */
|
||||
for (i = 0; i < N; i++) {
|
||||
dbe = db_at(&db, i);
|
||||
test_loop_not_equal((void *)dbe, NULL, i);
|
||||
test_loop_equal((void *)INT_ENTRY(dbe), PTRS_AT(ptrs, i), i);
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
} test_loop_passed();
|
||||
test_equal((void *)db_at(&db, N), NULL);
|
||||
g_assert_nonnull(dbe);
|
||||
g_assert(dbe == g_ptr_array_index(db.db_entries, i));
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
}
|
||||
g_assert_null(db_at(&db, N));
|
||||
|
||||
/* db_get() */
|
||||
for (i = 0; i < N; i++) {
|
||||
key = g_strdup_printf("%u", i);
|
||||
dbe = db_get(&db, key);
|
||||
test_loop_not_equal((void *)dbe, NULL, i);
|
||||
test_loop_str_equal(int_ops.dbe_key(dbe), key, i);
|
||||
test_loop_equal((void *)INT_ENTRY(dbe), PTRS_AT(ptrs, i), i);
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_assert_nonnull(dbe);
|
||||
g_assert_cmpstr_free(int_ops.dbe_key(dbe), ==, key);
|
||||
g_assert(dbe == db_at(&db, i));
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
g_free(key);
|
||||
} test_loop_passed();
|
||||
}
|
||||
key = g_strdup_printf("%u", N);
|
||||
test_equal((void *)db_get(&db, key), NULL);
|
||||
g_assert_null(db_get(&db, key));
|
||||
g_free(key);
|
||||
|
||||
/* db_find() */
|
||||
for (i = 0; i <= N; i++) {
|
||||
key = g_strdup_printf("%u", i);
|
||||
dbe = db_find(&db, key);
|
||||
test_loop_not_equal((void *)dbe, NULL, i);
|
||||
test_loop_str_equal(int_ops.dbe_key(dbe), key, i);
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_assert_nonnull(dbe);
|
||||
g_assert_cmpstr_free(int_ops.dbe_key(dbe), ==, key);
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
if (i < N)
|
||||
test_loop_equal((void *)INT_ENTRY(dbe), PTRS_AT(ptrs, i), i);
|
||||
g_assert(dbe == db_at(&db, i));
|
||||
g_free(key);
|
||||
} test_loop_passed();
|
||||
test_equal(db.db_size, N + 1);
|
||||
test_equal(db_actual_size(&db), N + 1);
|
||||
}
|
||||
g_assert_cmpuint(db.db_size, ==, N + 1);
|
||||
g_assert_cmpuint(db_actual_size(&db), ==, N + 1);
|
||||
|
||||
/* db_remove(): Even indices only! */
|
||||
for (i = 0; i <= (N + 2); i += 2) {
|
||||
key = g_strdup_printf("%u", i);
|
||||
dbe = db_get(&db, key);
|
||||
db_remove(&db, dbe);
|
||||
test_loop_equal((void *)INT_ENTRY(db_at(&db, i)), NULL, i);
|
||||
test_loop_equal((void *)INT_ENTRY(db_get(&db, key)), NULL, i);
|
||||
g_assert_null(INT_ENTRY(db_at(&db, i)));
|
||||
g_assert_null(INT_ENTRY(db_get(&db, key)));
|
||||
g_free(key);
|
||||
} test_loop_passed();
|
||||
}
|
||||
db_remove(&db, &rmv.ie_dbe);
|
||||
test_equal(db.db_size, N / 2);
|
||||
test_equal(db_actual_size(&db), N + 1);
|
||||
test_equal(test_free_count, (N / 2) + 1);
|
||||
g_assert_cmpuint(db.db_size, ==, N / 2);
|
||||
g_assert_cmpuint(db_actual_size(&db), ==, N + 1);
|
||||
g_assert_cmpuint(test_free_count, ==, (N / 2) + 1);
|
||||
|
||||
/* db_for_each() (db_first() / db_next()) */
|
||||
i = 1;
|
||||
db_for_each(dbe, next, &db) {
|
||||
test_loop_not_equal((void *)dbe, NULL, i);
|
||||
g_assert_nonnull(dbe);
|
||||
if (i == (N - 1)) {
|
||||
test_loop_equal((void *)next, NULL, i);
|
||||
g_assert_null(next);
|
||||
} else {
|
||||
test_loop_not_equal((void *)next, NULL, i);
|
||||
test_loop_equal(INT_ENTRY(next)->ie_val, i + 2, i);
|
||||
g_assert_nonnull(next);
|
||||
g_assert_cmpuint(INT_ENTRY(next)->ie_val, ==, i + 2);
|
||||
}
|
||||
test_loop_equal((void *)INT_ENTRY(dbe), PTRS_AT(ptrs, i), i);
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
test_loop_equal((void *)db_next(&db, dbe), (void *)next, i);
|
||||
g_assert(dbe == db_at(&db, i));
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
g_assert(db_next(&db, dbe) == next);
|
||||
i += 2;
|
||||
} test_loop_passed();
|
||||
test_equal(i, N + 1);
|
||||
}
|
||||
g_assert_cmpuint(i, ==, N + 1);
|
||||
|
||||
db_deinit(&db);
|
||||
g_ptr_array_free(ptrs, true);
|
||||
test_equal(db.db_size, 0);
|
||||
test_equal(db_actual_size(&db), 0);
|
||||
test_equal((void *)db_first(&db), NULL);
|
||||
test_equal(test_free_count, N + 1);
|
||||
g_assert_cmpuint(db.db_size, ==, 0);
|
||||
g_assert_cmpuint(db_actual_size(&db), ==, 0);
|
||||
g_assert_null(db_first(&db));
|
||||
g_assert_cmpuint(test_free_count, ==, N + 1);
|
||||
}
|
||||
|
||||
static void test_basics() { test_stress(10); }
|
||||
static void test_stress_0() { test_stress(0); }
|
||||
static void test_stress_100K() { test_stress(100000); }
|
||||
|
||||
static void test_save_load()
|
||||
{
|
||||
struct database db1 = DB_INIT("save_load.db", true, &int_ops);
|
||||
|
@ -255,12 +245,12 @@ static void test_save_load()
|
|||
/* Load using the standard db_load() function */
|
||||
i = 0;
|
||||
db_load(&db2);
|
||||
test_equal(db2.db_size, N);
|
||||
test_equal(db_actual_size(&db2), N);
|
||||
g_assert_cmpuint(db2.db_size, ==, N);
|
||||
g_assert_cmpuint(db_actual_size(&db2), ==, N);
|
||||
db_for_each(dbe, next, &db2) {
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
i++;
|
||||
} test_loop_passed();
|
||||
}
|
||||
|
||||
/* Removing 5 items, should also trigger autosaving. */
|
||||
for (i = 0; i < N; i += 2)
|
||||
|
@ -272,14 +262,14 @@ static void test_save_load()
|
|||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
test_setup_count = 0;
|
||||
db_load(&db2);
|
||||
test_equal(db2.db_size, N / 2);
|
||||
test_equal(test_setup_count, N / 2);
|
||||
g_assert_cmpuint(db2.db_size, ==, N / 2);
|
||||
g_assert_cmpuint(test_setup_count, ==, N / 2);
|
||||
|
||||
i = 1;
|
||||
db_for_each(dbe, next, &db2) {
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
i += 2;
|
||||
} test_loop_passed();
|
||||
}
|
||||
|
||||
/* Manually turn autosave off. */
|
||||
db1.db_autosave = false;
|
||||
|
@ -297,9 +287,9 @@ static void test_save_load()
|
|||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
|
||||
db_load_idle(&db2);
|
||||
test_equal(db2.db_size, 0);
|
||||
g_assert_cmpuint(db2.db_size, ==, 0);
|
||||
while (idle_run_task()) {};
|
||||
test_equal(db2.db_size, N / 2);
|
||||
g_assert_cmpuint(db2.db_size, ==, N / 2);
|
||||
|
||||
/* Use db_load_idle() again */
|
||||
db_save(&db1);
|
||||
|
@ -308,26 +298,30 @@ static void test_save_load()
|
|||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
|
||||
db_load_idle(&db2);
|
||||
test_equal(db2.db_size, 0);
|
||||
g_assert_cmpuint(db2.db_size, ==, 0);
|
||||
while (idle_run_task()) {};
|
||||
test_equal(db2.db_size, N);
|
||||
test_equal(db_actual_size(&db2), 2 * N);
|
||||
g_assert_cmpuint(db2.db_size, ==, N);
|
||||
g_assert_cmpuint(db_actual_size(&db2), ==, 2 * N);
|
||||
|
||||
i = 1;
|
||||
db_for_each(dbe, next, &db2) {
|
||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||
g_assert_cmpuint(INT_ENTRY(dbe)->ie_val, ==, i);
|
||||
i += 2;
|
||||
} test_loop_passed();
|
||||
}
|
||||
|
||||
db_deinit(&db1);
|
||||
db_deinit(&db2);
|
||||
}
|
||||
|
||||
DECLARE_UNIT_TESTS(
|
||||
UNIT_TEST("Database Entry", test_db_entry),
|
||||
UNIT_TEST("Database Initialization", test_init),
|
||||
UNIT_TEST("Database Basics", test_basics),
|
||||
UNIT_TEST("Database Stress (n = 0)", test_stress_0),
|
||||
UNIT_TEST("Database Stress (n = 100,000)", test_stress_100K),
|
||||
UNIT_TEST("Database Save and Load", test_save_load),
|
||||
);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
g_test_init(&argc, &argv, NULL);
|
||||
g_test_add_func("/Core/Database/Entry", test_db_entry);
|
||||
g_test_add_func("/Core/Database/Initialization", test_init);
|
||||
g_test_add_data_func("/Core/Database/n = 0", GUINT_TO_POINTER( 0), test_database);
|
||||
g_test_add_data_func("/Core/Database/n = 10", GUINT_TO_POINTER( 10), test_database);
|
||||
g_test_add_data_func("/Core/Database/n = 100,000", GUINT_TO_POINTER(100000), test_database);
|
||||
g_test_add_func("/Core/Database/Save and Load", test_save_load);
|
||||
return g_test_run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue