From de46e306721643c6ba845829018a83457adb3aba Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 19 Dec 2012 00:05:33 -0500 Subject: Add some unit tests for dictionaries. --- libdimension/dimension/refcount.h | 2 +- libdimension/tests/Makefile.am | 4 ++ libdimension/tests/dictionary.c | 93 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 libdimension/tests/dictionary.c diff --git a/libdimension/dimension/refcount.h b/libdimension/dimension/refcount.h index f94815c..337147f 100644 --- a/libdimension/dimension/refcount.h +++ b/libdimension/dimension/refcount.h @@ -38,6 +38,6 @@ /* Suppress "address will always evaluate to true" warning */ \ void *testptr = (object); \ if (testptr) { \ - ++(object)->DMNSN_REFCOUNT_FIELD; \ + ++(object)->DMNSN_REFCOUNT_FIELD; \ } \ } while (0) diff --git a/libdimension/tests/Makefile.am b/libdimension/tests/Makefile.am index 46fc6a2..0673023 100644 --- a/libdimension/tests/Makefile.am +++ b/libdimension/tests/Makefile.am @@ -26,6 +26,7 @@ check_PROGRAMS = warning.test \ warning-as-error.test \ error.test \ custom-error-fn.test \ + dictionary.test \ polynomial.test \ prtree.test \ png.test \ @@ -72,6 +73,9 @@ error_test_LDADD = libdimension-tests.la custom_error_fn_test_SOURCES = custom-error-fn.c custom_error_fn_test_LDADD = libdimension-tests.la +dictionary_test_SOURCES = dictionary.c +dictionary_test_LDADD = libdimension-unit-test.la + polynomial_test_SOURCES = polynomial.c polynomial_test_LDADD = libdimension-unit-test.la diff --git a/libdimension/tests/dictionary.c b/libdimension/tests/dictionary.c new file mode 100644 index 0000000..63969d3 --- /dev/null +++ b/libdimension/tests/dictionary.c @@ -0,0 +1,93 @@ +/************************************************************************* + * Copyright (C) 2012 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @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_overwrites) +{ + const int value1 = 123, value2 = 456; + int value; + + /* Insert and read back value1 */ + dmnsn_dictionary_insert(dict, "key", &value1); + ck_assert(dmnsn_dictionary_get(dict, "key", &value)); + ck_assert_int_eq(value, value1); + + /* Insert and read back value2 */ + dmnsn_dictionary_insert(dict, "key", &value2); + ck_assert(dmnsn_dictionary_get(dict, "key", &value)); + ck_assert_int_eq(value, 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); +} -- cgit v1.2.3