From: Simon McVittie Date: Wed, 3 Jul 2019 09:58:06 +0000 (+0100) Subject: tests: Move mempool test out of libdbus X-Git-Tag: dbus-1.13.14~26^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1412056bb4b49a65348458794656ccc5f0a9f3e;p=thirdparty%2Fdbus.git tests: Move mempool test out of libdbus All the functions under test turn out to be DBUS_PRIVATE_EXPORT already. Signed-off-by: Simon McVittie --- diff --git a/dbus/dbus-mempool.c b/dbus/dbus-mempool.c index 638799e15..83aa2722a 100644 --- a/dbus/dbus-mempool.c +++ b/dbus/dbus-mempool.c @@ -2,6 +2,8 @@ /* dbus-mempool.h Memory pools * * Copyright (C) 2002, 2003 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * Copyright (C) 2011-2012 Collabora Ltd. * * Licensed under the Academic Free License version 2.1 * @@ -448,200 +450,3 @@ _dbus_mem_pool_get_stats (DBusMemPool *pool, #endif /* DBUS_ENABLE_STATS */ /** @} */ - -#ifdef DBUS_ENABLE_EMBEDDED_TESTS -#include "dbus-test.h" -#include -#include - -static void -time_for_size (int size) -{ - int i; - int j; -#ifdef DBUS_ENABLE_VERBOSE_MODE - clock_t start; - clock_t end; -#endif -#define FREE_ARRAY_SIZE 512 -#define N_ITERATIONS FREE_ARRAY_SIZE * 512 - void *to_free[FREE_ARRAY_SIZE]; - DBusMemPool *pool; - - _dbus_verbose ("Timings for size %d\n", size); - - _dbus_verbose (" malloc\n"); - -#ifdef DBUS_ENABLE_VERBOSE_MODE - start = clock (); -#endif - - i = 0; - j = 0; - while (i < N_ITERATIONS) - { - to_free[j] = dbus_malloc (size); - _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ - - ++j; - - if (j == FREE_ARRAY_SIZE) - { - j = 0; - while (j < FREE_ARRAY_SIZE) - { - dbus_free (to_free[j]); - ++j; - } - - j = 0; - } - - ++i; - } - -#ifdef DBUS_ENABLE_VERBOSE_MODE - end = clock (); - - _dbus_verbose (" created/destroyed %d elements in %g seconds\n", - N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); - - - - _dbus_verbose (" mempools\n"); - - start = clock (); -#endif - - pool = _dbus_mem_pool_new (size, FALSE); - - i = 0; - j = 0; - while (i < N_ITERATIONS) - { - to_free[j] = _dbus_mem_pool_alloc (pool); - _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ - - ++j; - - if (j == FREE_ARRAY_SIZE) - { - j = 0; - while (j < FREE_ARRAY_SIZE) - { - _dbus_mem_pool_dealloc (pool, to_free[j]); - ++j; - } - - j = 0; - } - - ++i; - } - - _dbus_mem_pool_free (pool); - -#ifdef DBUS_ENABLE_VERBOSE_MODE - end = clock (); - - _dbus_verbose (" created/destroyed %d elements in %g seconds\n", - N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); - - _dbus_verbose (" zeroed malloc\n"); - - start = clock (); -#endif - - i = 0; - j = 0; - while (i < N_ITERATIONS) - { - to_free[j] = dbus_malloc0 (size); - _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ - - ++j; - - if (j == FREE_ARRAY_SIZE) - { - j = 0; - while (j < FREE_ARRAY_SIZE) - { - dbus_free (to_free[j]); - ++j; - } - - j = 0; - } - - ++i; - } - -#ifdef DBUS_ENABLE_VERBOSE_MODE - end = clock (); - - _dbus_verbose (" created/destroyed %d elements in %g seconds\n", - N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); - - _dbus_verbose (" zeroed mempools\n"); - - start = clock (); -#endif - - pool = _dbus_mem_pool_new (size, TRUE); - - i = 0; - j = 0; - while (i < N_ITERATIONS) - { - to_free[j] = _dbus_mem_pool_alloc (pool); - _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ - - ++j; - - if (j == FREE_ARRAY_SIZE) - { - j = 0; - while (j < FREE_ARRAY_SIZE) - { - _dbus_mem_pool_dealloc (pool, to_free[j]); - ++j; - } - - j = 0; - } - - ++i; - } - - _dbus_mem_pool_free (pool); - -#ifdef DBUS_ENABLE_VERBOSE_MODE - end = clock (); - - _dbus_verbose (" created/destroyed %d elements in %g seconds\n", - N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); -#endif -} - -/** - * @ingroup DBusMemPoolInternals - * Unit test for DBusMemPool - * @returns #TRUE on success. - */ -dbus_bool_t -_dbus_mem_pool_test (const char *test_data_dir _DBUS_GNUC_UNUSED) -{ - int i; - int element_sizes[] = { 4, 8, 16, 50, 124 }; - - i = 0; - while (i < _DBUS_N_ELEMENTS (element_sizes)) - { - time_for_size (element_sizes[i]); - ++i; - } - - return TRUE; -} - -#endif /* DBUS_ENABLE_EMBEDDED_TESTS */ diff --git a/dbus/dbus-test.h b/dbus/dbus-test.h index e3d4645e7..dd99c9a43 100644 --- a/dbus/dbus-test.h +++ b/dbus/dbus-test.h @@ -40,9 +40,6 @@ DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_marshal_test (const char *test_data_dir); -DBUS_PRIVATE_EXPORT -dbus_bool_t _dbus_mem_pool_test (const char *test_data_dir); - DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_keyring_test (const char *test_data_dir); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 83b161ba5..08e7a8b0c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -127,6 +127,7 @@ if(DBUS_ENABLE_EMBEDDED_TESTS) internals/dbus-marshal-validate-util.c internals/dbus-string-util.c internals/dbus-sysdeps-util.c + internals/mempool.c internals/misc-internals.c internals/misc-internals.h internals/sha.c diff --git a/test/Makefile.am b/test/Makefile.am index 2819c5f9e..28be5cddd 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -248,6 +248,7 @@ test_misc_internals_SOURCES = \ internals/dbus-marshal-validate-util.c \ internals/dbus-string-util.c \ internals/dbus-sysdeps-util.c \ + internals/mempool.c \ internals/misc-internals.c \ internals/misc-internals.h \ internals/sha.c \ diff --git a/test/internals/mempool.c b/test/internals/mempool.c new file mode 100644 index 000000000..53a606fd9 --- /dev/null +++ b/test/internals/mempool.c @@ -0,0 +1,223 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-mempool.h Memory pools + * + * Copyright (C) 2002, 2003 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * Copyright (C) 2013 Chengwei Yang / Intel + * + * Licensed under the Academic Free License version 2.1 + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include "misc-internals.h" + +#include +#include + +#include +#include + +static void +time_for_size (int size) +{ + int i; + int j; +#ifdef DBUS_ENABLE_VERBOSE_MODE + clock_t start; + clock_t end; +#endif +#define FREE_ARRAY_SIZE 512 +#define N_ITERATIONS FREE_ARRAY_SIZE * 512 + void *to_free[FREE_ARRAY_SIZE]; + DBusMemPool *pool; + + _dbus_verbose ("Timings for size %d\n", size); + + _dbus_verbose (" malloc\n"); + +#ifdef DBUS_ENABLE_VERBOSE_MODE + start = clock (); +#endif + + i = 0; + j = 0; + while (i < N_ITERATIONS) + { + to_free[j] = dbus_malloc (size); + _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ + + ++j; + + if (j == FREE_ARRAY_SIZE) + { + j = 0; + while (j < FREE_ARRAY_SIZE) + { + dbus_free (to_free[j]); + ++j; + } + + j = 0; + } + + ++i; + } + +#ifdef DBUS_ENABLE_VERBOSE_MODE + end = clock (); + + _dbus_verbose (" created/destroyed %d elements in %g seconds\n", + N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); + + + + _dbus_verbose (" mempools\n"); + + start = clock (); +#endif + + pool = _dbus_mem_pool_new (size, FALSE); + + i = 0; + j = 0; + while (i < N_ITERATIONS) + { + to_free[j] = _dbus_mem_pool_alloc (pool); + _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ + + ++j; + + if (j == FREE_ARRAY_SIZE) + { + j = 0; + while (j < FREE_ARRAY_SIZE) + { + _dbus_mem_pool_dealloc (pool, to_free[j]); + ++j; + } + + j = 0; + } + + ++i; + } + + _dbus_mem_pool_free (pool); + +#ifdef DBUS_ENABLE_VERBOSE_MODE + end = clock (); + + _dbus_verbose (" created/destroyed %d elements in %g seconds\n", + N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); + + _dbus_verbose (" zeroed malloc\n"); + + start = clock (); +#endif + + i = 0; + j = 0; + while (i < N_ITERATIONS) + { + to_free[j] = dbus_malloc0 (size); + _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ + + ++j; + + if (j == FREE_ARRAY_SIZE) + { + j = 0; + while (j < FREE_ARRAY_SIZE) + { + dbus_free (to_free[j]); + ++j; + } + + j = 0; + } + + ++i; + } + +#ifdef DBUS_ENABLE_VERBOSE_MODE + end = clock (); + + _dbus_verbose (" created/destroyed %d elements in %g seconds\n", + N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); + + _dbus_verbose (" zeroed mempools\n"); + + start = clock (); +#endif + + pool = _dbus_mem_pool_new (size, TRUE); + + i = 0; + j = 0; + while (i < N_ITERATIONS) + { + to_free[j] = _dbus_mem_pool_alloc (pool); + _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */ + + ++j; + + if (j == FREE_ARRAY_SIZE) + { + j = 0; + while (j < FREE_ARRAY_SIZE) + { + _dbus_mem_pool_dealloc (pool, to_free[j]); + ++j; + } + + j = 0; + } + + ++i; + } + + _dbus_mem_pool_free (pool); + +#ifdef DBUS_ENABLE_VERBOSE_MODE + end = clock (); + + _dbus_verbose (" created/destroyed %d elements in %g seconds\n", + N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC); +#endif +} + +/** + * @ingroup DBusMemPoolInternals + * Unit test for DBusMemPool + * @returns #TRUE on success. + */ +dbus_bool_t +_dbus_mem_pool_test (const char *test_data_dir _DBUS_GNUC_UNUSED) +{ + int i; + int element_sizes[] = { 4, 8, 16, 50, 124 }; + + i = 0; + while (i < _DBUS_N_ELEMENTS (element_sizes)) + { + time_for_size (element_sizes[i]); + ++i; + } + + return TRUE; +} diff --git a/test/internals/misc-internals.h b/test/internals/misc-internals.h index d207ae9f3..7f4d2cdf3 100644 --- a/test/internals/misc-internals.h +++ b/test/internals/misc-internals.h @@ -31,6 +31,7 @@ dbus_bool_t _dbus_auth_test (const char *test_data_dir); dbus_bool_t _dbus_credentials_test (const char *test_data_dir); dbus_bool_t _dbus_marshal_byteswap_test (const char *test_data_dir); dbus_bool_t _dbus_marshal_validate_test (const char *test_data_dir); +dbus_bool_t _dbus_mem_pool_test (const char *test_data_dir); dbus_bool_t _dbus_string_test (const char *test_data_dir); dbus_bool_t _dbus_sysdeps_test (const char *test_data_dir); dbus_bool_t _dbus_sha_test (const char *test_data_dir);