From e4feb9600e16aebe06539360aa38a2248d4029ba Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 12 Dec 2019 09:34:58 +0100 Subject: Add a trivial sanity-check for the atomic primitives This doesn't verify that they're atomic, but does verify that they return the right things. This commit adds a new test function _dbus_test_check (a) to make writing tests easier. It checks the given boolean expression and generates a "not ok" test result if the expression is false. Due to the current design of the test api, the test is only compiled if embedded tests were enabled at the time of configuration. It was also necessary to move the test_atomic target definitions in test/Makefile.am to the --enable-embedded-tests section to avoid a make distcheck build error. The test case itself has been authored by smcv. Co-authored-by: Simon McVittie --- dbus/dbus-test-tap.h | 2 ++ test/CMakeLists.txt | 5 +++ test/Makefile.am | 4 +++ test/internals/atomic.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 test/internals/atomic.c diff --git a/dbus/dbus-test-tap.h b/dbus/dbus-test-tap.h index ea116921..423cf48e 100644 --- a/dbus/dbus-test-tap.h +++ b/dbus/dbus-test-tap.h @@ -59,6 +59,8 @@ void _dbus_test_check_memleaks (const char *test_name); DBUS_PRIVATE_EXPORT int _dbus_test_done_testing (void); +#define _dbus_test_check(a) if (!(a)) _dbus_test_not_ok ("%s:%d - '%s' failed\n", __FILE__, __LINE__, #a) + #endif #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 08e7a8b0..d0cc8d4d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -39,6 +39,10 @@ set(test-shell_SOURCES shell-test.c ) +set(test-atomic_SOURCES + internals/atomic.c +) + set(test-spawn_SOURCES spawn-test.c ) @@ -92,6 +96,7 @@ if(WIN32) endif() if(DBUS_ENABLE_EMBEDDED_TESTS) + add_test_executable(test-atomic ${test-atomic_SOURCES} dbus-testutils) add_test_executable(test-hash internals/hash.c dbus-testutils) set_target_properties(test-hash PROPERTIES COMPILE_FLAGS ${DBUS_INTERNAL_CLIENT_DEFINITIONS}) diff --git a/test/Makefile.am b/test/Makefile.am index 28be5cdd..9566f900 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -85,6 +85,7 @@ TEST_BINARIES += test-spawn endif uninstallable_test_programs += \ + test-atomic \ test-bus \ test-bus-dispatch \ test-bus-dispatch-sha1 \ @@ -106,6 +107,9 @@ endif DBUS_UNIX noinst_PROGRAMS += $(uninstallable_test_programs) TESTS += $(uninstallable_test_programs) +test_atomic_SOURCES = internals/atomic.c +test_atomic_LDADD = libdbus-testutils.la + else !DBUS_ENABLE_EMBEDDED_TESTS TEST_BINARIES= diff --git a/test/internals/atomic.c b/test/internals/atomic.c new file mode 100644 index 00000000..d595dc17 --- /dev/null +++ b/test/internals/atomic.c @@ -0,0 +1,91 @@ +/* Regression test for atomic ops + * + * Author: Simon McVittie + * Copyright © 2013 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include + +static dbus_bool_t +atomic_test_inc_dec (const char *test_data_dir _DBUS_GNUC_UNUSED) +{ + DBusAtomic a = { 0 }; + DBusAtomic b = { 123 }; + dbus_int32_t old; + + _dbus_test_check (_dbus_atomic_get (&a) == 0); + _dbus_test_check (_dbus_atomic_get (&b) == 123); + + _dbus_test_check (a.value == 0); + old = _dbus_atomic_dec (&a); + _dbus_test_check (old == 0); + _dbus_test_check (a.value == -1); + + _dbus_test_check (b.value == 123); + old = _dbus_atomic_inc (&b); + _dbus_test_check (old == 123); + _dbus_test_check (b.value == 124); + return TRUE; +} + +static dbus_bool_t +atomic_test_zero (const char *test_data_dir _DBUS_GNUC_UNUSED) +{ + DBusAtomic a = { 0 }; + DBusAtomic b = { 123 }; + + _dbus_atomic_set_nonzero (&a); + /* careful: this is not necessarily 1 */ + _dbus_test_check (a.value != 0); + + _dbus_atomic_set_nonzero (&b); + _dbus_test_check (b.value != 0); + + _dbus_atomic_set_zero (&a); + _dbus_test_check (a.value == 0); + + _dbus_atomic_set_zero (&b); + _dbus_test_check (b.value == 0); + return TRUE; +} + +static DBusTestCase tests[] = +{ + { "atomic_inc/dec", atomic_test_inc_dec }, + { "atomic_set_[non]zero", atomic_test_zero }, + { NULL } +}; + +int +main (int argc, char **argv) +{ + return _dbus_test_main (argc, argv, _DBUS_N_ELEMENTS (tests), tests, + DBUS_TEST_FLAGS_NONE, + NULL, NULL); +} -- cgit v1.2.1