From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51877) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anJa1-0001eD-67 for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1anJZy-0005Tk-8D for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:09 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:48174) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anJZy-0005T9-3t for qemu-devel@nongnu.org; Tue, 05 Apr 2016 01:31:06 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id C25EB20DDD for ; Tue, 5 Apr 2016 01:31:05 -0400 (EDT) From: "Emilio G. Cota" Date: Tue, 5 Apr 2016 01:30:52 -0400 Message-Id: <1459834253-8291-10-git-send-email-cota@braap.org> In-Reply-To: <1459834253-8291-1-git-send-email-cota@braap.org> References: <1459834253-8291-1-git-send-email-cota@braap.org> Subject: [Qemu-devel] [PATCH 09/10] qht: add test program List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: QEMU Developers , MTTCG Devel Cc: Peter Maydell , Peter Crosthwaite , Sergey Fedorov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Richard Henderson Signed-off-by: Emilio G. Cota --- tests/.gitignore | 1 + tests/Makefile | 6 +++- tests/test-qht.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/test-qht.c diff --git a/tests/.gitignore b/tests/.gitignore index b7bf13e..d6d0700 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -47,6 +47,7 @@ test-qapi-visit.[ch] test-qdev-global-props test-qemu-opts test-qga +test-qht test-qmp-commands test-qmp-commands.h test-qmp-event diff --git a/tests/Makefile b/tests/Makefile index 45b9048..b5a99d6 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -68,6 +68,8 @@ check-unit-y += tests/rcutorture$(EXESUF) gcov-files-rcutorture-y = util/rcu.c check-unit-y += tests/test-rcu-list$(EXESUF) gcov-files-test-rcu-list-y = util/rcu.c +check-unit-y += tests/test-qht$(EXESUF) +gcov-files-test-qht-y = util/qht.c check-unit-y += tests/test-bitops$(EXESUF) check-unit-$(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) += tests/test-qdev-global-props$(EXESUF) check-unit-y += tests/check-qom-interface$(EXESUF) @@ -387,7 +389,8 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/test-qmp-commands.o tests/test-visitor-serialization.o \ tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \ tests/test-opts-visitor.o tests/test-qmp-event.o \ - tests/rcutorture.o tests/test-rcu-list.o + tests/rcutorture.o tests/test-rcu-list.o \ + tests/test-qht.o $(test-obj-y): QEMU_INCLUDES += -Itests QEMU_CFLAGS += -I$(SRC_PATH)/tests @@ -425,6 +428,7 @@ tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o tests/test-int128$(EXESUF): tests/test-int128.o tests/rcutorture$(EXESUF): tests/rcutorture.o $(test-util-obj-y) tests/test-rcu-list$(EXESUF): tests/test-rcu-list.o $(test-util-obj-y) +tests/test-qht$(EXESUF): tests/test-qht.o $(test-util-obj-y) tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \ hw/core/qdev.o hw/core/qdev-properties.o hw/core/hotplug.o\ diff --git a/tests/test-qht.c b/tests/test-qht.c new file mode 100644 index 0000000..f553fbc --- /dev/null +++ b/tests/test-qht.c @@ -0,0 +1,100 @@ +#include "qemu/osdep.h" +#include "qemu/xxhash.h" +#include "qemu/qht.h" + +#define N 5000 +#define SEED 1 + +static struct qht ht; +static int32_t arr[N]; + +static bool is_equal(const void *obj, const void *userp) +{ + const int32_t *a = obj; + const int32_t *b = userp; + + return *a == *b; +} + +static void insert(int a, int b) +{ + int i; + + for (i = a; i < b; i++) { + uint32_t hash; + + arr[i] = i; + hash = qemu_xxh32((uint32_t *)&arr[i], 1, SEED); + + qht_insert(&ht, &arr[i], hash); + } +} + +static void rm(int init, int end) +{ + int i; + + for (i = init; i < end; i++) { + uint32_t hash; + + hash = qemu_xxh32((uint32_t *)&arr[i], 1, SEED); + assert(qht_remove(&ht, &arr[i], hash)); + } +} + +static void check(int a, int b, bool expected) +{ + int i; + + for (i = a; i < b; i++) { + void *p; + uint32_t hash; + int32_t val; + + val = i; + hash = qemu_xxh32((uint32_t *)&val, 1, SEED); + p = qht_lookup(&ht, is_equal, &val, hash); + assert(!!p == expected); + } +} + +static void count_func(struct qht *ht, void *p, uint32_t hash, void *userp) +{ + unsigned int *curr = userp; + + (*curr)++; +} + +static void iter_check(unsigned int count) +{ + unsigned int curr = 0; + + qht_iter(&ht, count_func, &curr); + assert(curr == count); +} + +static void qht_test(unsigned int mode) +{ + qht_init(&ht, 0, mode); + + insert(0, N); + check(0, N, true); + check(-N, -1, false); + iter_check(N); + rm(1, 2); + qht_reset_size(&ht, 0); + check(0, N, false); + + qht_destroy(&ht); +} + +int main(int argc, char *argv[]) +{ + qht_test(0); + qht_test(QHT_MODE_MRU_LOOKUP); + qht_test(QHT_MODE_MRU_LOOKUP | QHT_MODE_MRU_INSERT); + qht_test(QHT_MODE_MRU_LOOKUP | QHT_MODE_MRU_INSERT | QHT_MODE_AUTO_RESIZE); + qht_test(QHT_MODE_AUTO_RESIZE | QHT_MODE_MRU_INSERT); + qht_test(QHT_MODE_MRU_LOOKUP | QHT_MODE_MRU_INSERT); + return 0; +} -- 2.5.0