summaryrefslogtreecommitdiffstats
path: root/libdimension/tests/base
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-08-19 17:49:02 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-10-25 11:03:56 -0400
commitda47b2858f6e64ec60dce218544647df32b49077 (patch)
tree6c40604dbf113e6b8e0a86bab9aebbe510118045 /libdimension/tests/base
parent654b98bafec4845e5d4af39cda58a2a1aa293d32 (diff)
downloaddimension-da47b2858f6e64ec60dce218544647df32b49077.tar.xz
libdimension: Modularize tests.
Diffstat (limited to 'libdimension/tests/base')
-rw-r--r--libdimension/tests/base/dictionary.c132
-rw-r--r--libdimension/tests/base/error/custom-error-fn.c37
-rw-r--r--libdimension/tests/base/error/error.c32
-rw-r--r--libdimension/tests/base/error/warning-as-error.c33
-rw-r--r--libdimension/tests/base/error/warning.c32
-rw-r--r--libdimension/tests/base/pool.c87
6 files changed, 353 insertions, 0 deletions
diff --git a/libdimension/tests/base/dictionary.c b/libdimension/tests/base/dictionary.c
new file mode 100644
index 0000000..117887a
--- /dev/null
+++ b/libdimension/tests/base/dictionary.c
@@ -0,0 +1,132 @@
+/*************************************************************************
+ * Copyright (C) 2012 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Tests for dmnsn_dictionary, a string->object map.
+ */
+
+#include "tests.h"
+
+static dmnsn_dictionary *dict;
+
+/*
+ * Dictionary creation/deletion.
+ */
+
+DMNSN_TEST(new_delete, delete_null)
+{
+ dmnsn_delete_dictionary(NULL);
+}
+
+/*
+ * Dictionary operations.
+ */
+
+DMNSN_TEST_SETUP(dictionary)
+{
+ dict = dmnsn_new_dictionary(sizeof(int));
+}
+
+DMNSN_TEST_TEARDOWN(dictionary)
+{
+ dmnsn_delete_dictionary(dict);
+}
+
+DMNSN_TEST(dictionary, insert_and_get)
+{
+ const int value_in = 123;
+ dmnsn_dictionary_insert(dict, "key", &value_in);
+
+ int value_out;
+ ck_assert(dmnsn_dictionary_get(dict, "key", &value_out));
+ ck_assert_int_eq(value_out, value_in);
+
+ int *value_at = dmnsn_dictionary_at(dict, "key");
+ ck_assert_int_eq(*value_at, value_in);
+}
+
+DMNSN_TEST(dictionary, insert_multiple)
+{
+ const int value1 = 123, value2 = 456, value3 = 789;
+ int value_out;
+
+ dmnsn_dictionary_insert(dict, "key1", &value1);
+ dmnsn_dictionary_insert(dict, "key2", &value2);
+ dmnsn_dictionary_insert(dict, "asdf", &value3);
+
+ ck_assert(dmnsn_dictionary_get(dict, "key1", &value_out));
+ ck_assert_int_eq(value_out, value1);
+
+ ck_assert(dmnsn_dictionary_get(dict, "key2", &value_out));
+ ck_assert_int_eq(value_out, value2);
+
+ ck_assert(dmnsn_dictionary_get(dict, "asdf", &value_out));
+ ck_assert_int_eq(value_out, value3);
+}
+
+DMNSN_TEST(dictionary, insert_overwrites)
+{
+ const int value1 = 123, value2 = 456;
+ int value_out;
+
+ // Insert and read back value1
+ dmnsn_dictionary_insert(dict, "key", &value1);
+ ck_assert(dmnsn_dictionary_get(dict, "key", &value_out));
+ ck_assert_int_eq(value_out, value1);
+
+ // Insert and read back value2
+ dmnsn_dictionary_insert(dict, "key", &value2);
+ ck_assert(dmnsn_dictionary_get(dict, "key", &value_out));
+ ck_assert_int_eq(value_out, value2);
+}
+
+DMNSN_TEST(dictionary, remove)
+{
+ const int value_in = 123;
+ dmnsn_dictionary_insert(dict, "key", &value_in);
+ ck_assert(dmnsn_dictionary_remove(dict, "key"));
+ ck_assert(!dmnsn_dictionary_remove(dict, "key"));
+
+ int value_out;
+ ck_assert(!dmnsn_dictionary_get(dict, "key", &value_out));
+
+ int *value_at = dmnsn_dictionary_at(dict, "key");
+ ck_assert(value_at == NULL);
+}
+
+static int sum = 0;
+
+static void
+dmnsn_dictionary_test_apply_callback(void *ptr)
+{
+ sum += *(int *)ptr;
+}
+
+DMNSN_TEST(dictionary, apply)
+{
+ const int value1 = 123, value2 = 456, value3 = 789;
+
+ dmnsn_dictionary_insert(dict, "key1", &value1);
+ dmnsn_dictionary_insert(dict, "key2", &value2);
+ dmnsn_dictionary_insert(dict, "asdf", &value3);
+
+ dmnsn_dictionary_apply(dict, dmnsn_dictionary_test_apply_callback);
+ ck_assert_int_eq(sum, value1 + value2 + value3);
+}
diff --git a/libdimension/tests/base/error/custom-error-fn.c b/libdimension/tests/base/error/custom-error-fn.c
new file mode 100644
index 0000000..49a0f9e
--- /dev/null
+++ b/libdimension/tests/base/error/custom-error-fn.c
@@ -0,0 +1,37 @@
+/*************************************************************************
+ * Copyright (C) 2011 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+// Make sure that custom fatal error handlers work.
+
+#include "dimension.h"
+#include <stdlib.h>
+
+void
+dmnsn_custom_fatal_error_fn(void)
+{
+ exit(EXIT_SUCCESS);
+}
+
+int
+main(void)
+{
+ dmnsn_set_fatal_error_fn(dmnsn_custom_fatal_error_fn);
+ dmnsn_error("This error is expected.");
+ return EXIT_FAILURE;
+}
diff --git a/libdimension/tests/base/error/error.c b/libdimension/tests/base/error/error.c
new file mode 100644
index 0000000..58a5b43
--- /dev/null
+++ b/libdimension/tests/base/error/error.c
@@ -0,0 +1,32 @@
+/*************************************************************************
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+// Make sure that errors terminate us - this test should fail
+
+#include "dimension.h"
+#include <stdlib.h>
+#include <errno.h>
+
+int
+main(void)
+{
+ errno = ENOMEM;
+ dmnsn_error("This error is expected.");
+ return EXIT_SUCCESS;
+}
diff --git a/libdimension/tests/base/error/warning-as-error.c b/libdimension/tests/base/error/warning-as-error.c
new file mode 100644
index 0000000..7d0fdb7
--- /dev/null
+++ b/libdimension/tests/base/error/warning-as-error.c
@@ -0,0 +1,33 @@
+/*************************************************************************
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+// Make sure warnings kill us in strict mode - this test should fail
+
+#include "dimension.h"
+#include <stdlib.h>
+#include <errno.h>
+
+int
+main(void)
+{
+ dmnsn_die_on_warnings(true);
+ errno = ENOMEM;
+ dmnsn_warning("This warning is expected.");
+ return EXIT_SUCCESS;
+}
diff --git a/libdimension/tests/base/error/warning.c b/libdimension/tests/base/error/warning.c
new file mode 100644
index 0000000..cc1aed1
--- /dev/null
+++ b/libdimension/tests/base/error/warning.c
@@ -0,0 +1,32 @@
+/*************************************************************************
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+// Make sure warnings don't kill us - this test should pass
+
+#include "dimension.h"
+#include <stdlib.h>
+#include <errno.h>
+
+int
+main(void)
+{
+ errno = ENOMEM;
+ dmnsn_warning("This warning is expected.");
+ return EXIT_SUCCESS;
+}
diff --git a/libdimension/tests/base/pool.c b/libdimension/tests/base/pool.c
new file mode 100644
index 0000000..f8b1259
--- /dev/null
+++ b/libdimension/tests/base/pool.c
@@ -0,0 +1,87 @@
+/*************************************************************************
+ * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Test Suite. *
+ * *
+ * The Dimension Test Suite is free software; you can redistribute it *
+ * and/or modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Test Suite is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Tests for memory pools.
+ */
+
+#include "../../concurrency/future.c"
+#include "../../concurrency/threads.c"
+#include "../../internal.h"
+#include "tests.h"
+
+static dmnsn_pool *pool;
+
+DMNSN_TEST_SETUP(pool)
+{
+ pool = dmnsn_new_pool();
+}
+
+DMNSN_TEST_TEARDOWN(pool)
+{
+ dmnsn_delete_pool(pool);
+}
+
+DMNSN_TEST(pool, simple)
+{
+ for (int i = 0; i < 10000; ++i) {
+ int *p = DMNSN_PALLOC(pool, int);
+ *p = i;
+ }
+
+ // Leak checking will tell us if something bad happened
+}
+
+static int counter = 0;
+
+static void
+callback(void *ptr)
+{
+ ++counter;
+}
+
+DMNSN_TEST(pool, callback)
+{
+ DMNSN_PALLOC(pool, int);
+ DMNSN_PALLOC_TIDY(pool, int, callback);
+ DMNSN_PALLOC_TIDY(pool, int, callback);
+ DMNSN_PALLOC(pool, int);
+
+ dmnsn_delete_pool(pool);
+ pool = NULL;
+
+ ck_assert_int_eq(counter, 2);
+}
+
+static int
+alloc_thread(void *ptr, unsigned int thread, unsigned int nthreads)
+{
+ for (unsigned int i = thread; i < 10000; i += nthreads) {
+ int *p = DMNSN_PALLOC(pool, int);
+ *p = i;
+ }
+ return 0;
+}
+
+DMNSN_TEST(pool, threaded)
+{
+ int ret = dmnsn_execute_concurrently(NULL, alloc_thread, NULL, 64);
+ ck_assert_int_eq(ret, 0);
+}