Module Name: src Committed By: joerg Date: Thu Sep 27 00:38:57 UTC 2012
Modified Files: src/distrib/sets/lists/tests: mi src/tests/lib/libc: Makefile Added Files: src/tests/lib/libc: t_cdb.c Log Message: Add regression test for cdbr(3) and cdbw(3). To generate a diff of this commit: cvs rdiff -u -r1.495 -r1.496 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.42 -r1.43 src/tests/lib/libc/Makefile cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/t_cdb.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.495 src/distrib/sets/lists/tests/mi:1.496 --- src/distrib/sets/lists/tests/mi:1.495 Tue Sep 18 08:30:35 2012 +++ src/distrib/sets/lists/tests/mi Thu Sep 27 00:38:57 2012 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.495 2012/09/18 08:30:35 martin Exp $ +# $NetBSD: mi,v 1.496 2012/09/27 00:38:57 joerg Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -591,6 +591,7 @@ ./usr/libdata/debug/usr/tests/lib/libc/t_clone.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/libc/t_context.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/libc/t_convfp.debug tests-lib-debug debug,atf +./usr/libdata/debug/usr/tests/lib/libc/t_cdb.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/t_gdtoa.debug tests-lib-debug debug,atf ./usr/libdata/debug/usr/tests/lib/libc/t_hsearch.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/libc/t_inet.debug tests-obsolete obsolete @@ -2510,6 +2511,7 @@ ./usr/tests/lib/libc/t_clone tests-obsolete obsolete ./usr/tests/lib/libc/t_context tests-obsolete obsolete ./usr/tests/lib/libc/t_convfp tests-lib-tests atf +./usr/tests/lib/libc/t_cdb tests-lib-tests atf ./usr/tests/lib/libc/t_gdtoa tests-lib-tests atf ./usr/tests/lib/libc/t_hsearch tests-obsolete obsolete ./usr/tests/lib/libc/t_inet tests-obsolete obsolete Index: src/tests/lib/libc/Makefile diff -u src/tests/lib/libc/Makefile:1.42 src/tests/lib/libc/Makefile:1.43 --- src/tests/lib/libc/Makefile:1.42 Mon Sep 19 05:42:13 2011 +++ src/tests/lib/libc/Makefile Thu Sep 27 00:38:57 2012 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.42 2011/09/19 05:42:13 jruoho Exp $ +# $NetBSD: Makefile,v 1.43 2012/09/27 00:38:57 joerg Exp $ .include "Makefile.inc" .include <bsd.sys.mk> @@ -17,5 +17,6 @@ TESTSDIR= ${TESTSBASE}/lib/libc TESTS_C+= t_convfp TESTS_C+= t_gdtoa +TESTS_C+= t_cdb .include <bsd.test.mk> Added files: Index: src/tests/lib/libc/t_cdb.c diff -u /dev/null src/tests/lib/libc/t_cdb.c:1.1 --- /dev/null Thu Sep 27 00:38:57 2012 +++ src/tests/lib/libc/t_cdb.c Thu Sep 27 00:38:57 2012 @@ -0,0 +1,158 @@ +/* $NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $ */ +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Joerg Sonnenberger. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $"); + +#include <atf-c.h> +#include <assert.h> +#include <cdbr.h> +#include <cdbw.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define MAXKEYS 16384 + +static const char database_name[] = "test.cdb"; + +uint32_t keys[MAXKEYS]; + +static int +cmp_keys(const void *a_, const void *b_) +{ + uint32_t a = *(const uint32_t *)a_; + uint32_t b = *(const uint32_t *)b_; + + return a > b ? 1 : (a < b ? 1 : 0); +} + +static void +init_keys(size_t len) +{ + uint32_t sorted_keys[MAXKEYS]; + size_t i; + + assert(len <= MAXKEYS); + + if (len == 0) + return; + + do { + for (i = 0; i < len; ++i) + sorted_keys[i] = keys[i] = arc4random(); + + qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys); + for (i = 1; i < len; ++i) { + if (sorted_keys[i - 1] == sorted_keys[i]) + break; + } + } while (i != len); +} + +static void +write_database(size_t len) +{ + struct cdbw *db; + int fd; + size_t i; + uint32_t buf[2]; + + ATF_REQUIRE((db = cdbw_open()) != NULL); + ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1); + for (i = 0; i < len; ++i) { + buf[0] = i; + buf[1] = keys[i]; + ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]), + buf, sizeof(buf)) == 0); + } + ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0); + cdbw_close(db); + ATF_REQUIRE(close(fd) == 0); +} + +static void +check_database(size_t len) +{ + struct cdbr *db; + size_t i, data_len; + const void *data; + uint32_t buf[2]; + + ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL); + ATF_REQUIRE_EQ(cdbr_entries(db), len); + for (i = 0; i < len; ++i) { + ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]), + &data, &data_len) != -1); + ATF_REQUIRE_EQ(data_len, sizeof(buf)); + memcpy(buf, data, sizeof(buf)); + ATF_REQUIRE_EQ(buf[0], i); + ATF_REQUIRE_EQ(buf[1], keys[i]); + } + cdbr_close(db); +} + +ATF_TC_WITH_CLEANUP(cdb); + +ATF_TC_HEAD(cdb, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing"); +} + +ATF_TC_BODY(cdb, tc) +{ + size_t i, sizes[] = { 0, 16, 64, 1024, 2048 }; + for (i = 0; i < __arraycount(sizes); ++i) { + init_keys(sizes[i]); + write_database(sizes[i]); + check_database(sizes[i]); + unlink(database_name); + } +} + +ATF_TC_CLEANUP(cdb, tc) +{ + + unlink(database_name); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, cdb); + + return atf_no_error(); +} +