From patchwork Mon Aug 3 16:57:22 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 30677 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by bilbo.ozlabs.org (Postfix) with ESMTPS id 60095B6F20 for ; Tue, 4 Aug 2009 06:10:44 +1000 (EST) Received: from localhost ([127.0.0.1]:60820 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MY3re-00031J-PY for incoming@patchwork.ozlabs.org; Mon, 03 Aug 2009 16:10:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MY0sX-0006Np-CF for qemu-devel@nongnu.org; Mon, 03 Aug 2009 12:59:17 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MY0sS-0006NH-In for qemu-devel@nongnu.org; Mon, 03 Aug 2009 12:59:16 -0400 Received: from [199.232.76.173] (port=55303 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MY0sS-0006NC-9t for qemu-devel@nongnu.org; Mon, 03 Aug 2009 12:59:12 -0400 Received: from mx2.redhat.com ([66.187.237.31]:56402) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MY0sR-0003Fj-MZ for qemu-devel@nongnu.org; Mon, 03 Aug 2009 12:59:12 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n73GxBl3026432; Mon, 3 Aug 2009 12:59:11 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n73Gx9uR015518; Mon, 3 Aug 2009 12:59:09 -0400 Received: from localhost (vpn-10-1.bos.redhat.com [10.16.10.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n73Gx7pT030205; Mon, 3 Aug 2009 12:59:08 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Mon, 3 Aug 2009 13:57:22 -0300 Message-Id: <1249318642-19324-26-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1249318642-19324-1-git-send-email-lcapitulino@redhat.com> References: <1249318642-19324-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: jan.kiszka@siemens.com, aliguori@us.ibm.com, avi@redhat.com Subject: [Qemu-devel] [PATCH 25/25] Introduce QDict unit-tests X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This suite contains tests to assure that QDict API will work as expected. To execute it, you should build QEMU with check support enabled (--enable-check) and then run: $ ./check-qdict Signed-off-by: Luiz Capitulino --- Makefile | 1 + check-qdict.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ configure | 2 +- 3 files changed, 268 insertions(+), 1 deletions(-) create mode 100644 check-qdict.c diff --git a/Makefile b/Makefile index a929d2c..2ce539f 100644 --- a/Makefile +++ b/Makefile @@ -223,6 +223,7 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx ifdef CONFIG_CHECK LIBS += $(CHECK_LIBS) +check-qdict: check-qdict.o qdict.o qemu-malloc.o endif clean: diff --git a/check-qdict.c b/check-qdict.c new file mode 100644 index 0000000..6bf6f1a --- /dev/null +++ b/check-qdict.c @@ -0,0 +1,266 @@ +/* + * QDict unit-tests. + * + * Copyright (C) 2009 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * Not really the best unit-testing example (and not developed in a + * test-driven way), but does the job. + */ +#include +#include +#include + +#include "qdict.h" + +/* + * Basic API tests + */ + +START_TEST(create_test) +{ + QDict *qdict; + + qdict = qdict_new(); + fail_unless(qdict != NULL, "qdict is NULL"); + fail_unless(qdict_size(qdict) == 0, "size is not 0"); + + free(qdict); +} +END_TEST + +START_TEST(destroy_simple_test) +{ + QDict *qdict; + + qdict = qdict_new(); + fail_unless(qdict != NULL, "qdict is NULL"); + + qdict_destroy(qdict); + fail_unless(qdict_size(qdict) == 0, "size is not 0"); +} +END_TEST + +static QDict *tests_dict = NULL; + +static void qdict_setup(void) +{ + tests_dict = qdict_new(); + fail_unless(tests_dict != NULL, "tests_dict is NULL"); +} + +static void qdict_teardown(void) +{ + qdict_destroy(tests_dict); + tests_dict = NULL; +} + +/* two tests in one, not good but... */ +START_TEST(insert_new_test) +{ + int *ret, value = 42; + const char *key = "test"; + + qdict_add(tests_dict, key, &value); + fail_unless(qdict_size(tests_dict) == 1); + + ret = qdict_get(tests_dict, key); + fail_unless(*ret == value); +} +END_TEST + +START_TEST(insert_existing_test) +{ + int *ret, value1, value2; + const char *key = "test"; + + value1 = 42; + qdict_add(tests_dict, key, &value1); + fail_unless(qdict_size(tests_dict) == 1); + + value2 = 667; + qdict_add(tests_dict, key, &value2); + + ret = qdict_get(tests_dict, key); + fail_unless(*ret == value1); +} +END_TEST + +START_TEST(exists_not_exists_test) +{ + fail_unless(qdict_exists(tests_dict, "test") == 0); +} +END_TEST + +START_TEST(exists_exists_test) +{ + const char *key = "test"; + + qdict_add(tests_dict, key, NULL); + fail_unless(qdict_exists(tests_dict, key) == 1); +} +END_TEST + +START_TEST(get_not_exists_test) +{ + fail_unless(qdict_get(tests_dict, "test") == NULL); +} +END_TEST + +START_TEST(del_exists_test) +{ + int value = 42; + const char *key = "test"; + + qdict_add(tests_dict, key, &value); + fail_unless(qdict_size(tests_dict) == 1); + + qdict_del(tests_dict, key); + fail_unless(qdict_size(tests_dict) == 0); + fail_unless(qdict_get(tests_dict, key) == NULL); +} +END_TEST + +START_TEST(del_not_exists_test) +{ + const char *key = "test"; + + qdict_del(tests_dict, key); + fail_unless(qdict_size(tests_dict) == 0); + fail_unless(qdict_get(tests_dict, key) == NULL); +} +END_TEST + +/* + * Stress test-case + * + * This is a lot big for a unit-test, but there is no other place + * to have it. + */ + +static void remove_dots(char *string) +{ + char *p = strchr(string, ':'); + if (p) + *p = '\0'; +} + +static int read_chars(FILE *file, char *key, char *value) +{ + if (fscanf(file, "%s%s", key, value) == EOF) + return 1; + remove_dots(key); + return 0; +} + +#define reset_file(file) fseek(file, 0L, SEEK_SET) + +START_TEST(qdict_stress_test) +{ + size_t i; + FILE *test_file; + QDict *qdict; + char key[128], value[128]; + + test_file = fopen("qdict-test-data.txt", "r"); + fail_unless(test_file != NULL); + + // Create the dict + qdict = qdict_new(); + fail_unless(qdict != NULL); + + // Add everything from the test file + for (i = 0;; i++) { + if (read_chars(test_file, key, value)) + break; + qdict_add(qdict, key, strdup(value)); + } + fail_unless(qdict_size(qdict) == i); + + // Check if everything is really in there + reset_file(test_file); + for (;;) { + char *p; + + if (read_chars(test_file, key, value)) + break; + + p = qdict_get(qdict, key); + fail_unless(p != NULL); + fail_unless(strcmp(p, value) == 0); + } + + // Delete everything + reset_file(test_file); + for (;;) { + char *p; + + if (read_chars(test_file, key, value)) + break; + + p = qdict_del(qdict, key); + fail_unless(p != NULL); + fail_unless(strcmp(p, value) == 0); + free(p); + + p = qdict_get(qdict, key); + fail_unless(p == NULL); + } + + fail_unless(qdict_size(qdict) == 0); + qdict_destroy(qdict); +} +END_TEST + +static Suite *qdict_suite(void) +{ + Suite *s; + TCase *qdict_api_tcase; + TCase *qdict_api2_tcase; + TCase *qdict_stress_tcase; + + s = suite_create("QDict test-suite"); + + /* Very basic API test-case */ + qdict_api_tcase = tcase_create("Basic API"); + suite_add_tcase(s, qdict_api_tcase); + tcase_add_test(qdict_api_tcase, create_test); + tcase_add_test(qdict_api_tcase, destroy_simple_test); + + /* The same as above, but with fixture */ + qdict_api2_tcase = tcase_create("Basic API 2"); + suite_add_tcase(s, qdict_api2_tcase); + tcase_add_checked_fixture(qdict_api2_tcase, qdict_setup, qdict_teardown); + tcase_add_test(qdict_api2_tcase, insert_new_test); + tcase_add_test(qdict_api2_tcase, insert_existing_test); + tcase_add_test(qdict_api2_tcase, exists_not_exists_test); + tcase_add_test(qdict_api2_tcase, exists_exists_test); + tcase_add_test(qdict_api2_tcase, get_not_exists_test); + tcase_add_test(qdict_api2_tcase, del_exists_test); + tcase_add_test(qdict_api2_tcase, del_not_exists_test); + + /* The Big one */ + qdict_stress_tcase = tcase_create("Stress test"); + suite_add_tcase(s, qdict_stress_tcase); + tcase_add_test(qdict_stress_tcase, qdict_stress_test); + + return s; +} + +int main(void) +{ + int nf; + Suite *s; + SRunner *sr; + + s = qdict_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + nf = srunner_ntests_failed(sr); + srunner_free(sr); + + return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/configure b/configure index 95e5afc..61bb790 100755 --- a/configure +++ b/configure @@ -1765,7 +1765,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then if [ "$linux" = "yes" ] ; then tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools" if [ "$check" = "yes" ]; then - tools="$tools" + tools="check-qdict $tools" fi fi fi