Home | History | Annotate | Line # | Download | only in libc
t_cdb.c revision 1.1
      1  1.1  joerg /*	$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $	*/
      2  1.1  joerg /*-
      3  1.1  joerg  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      4  1.1  joerg  * All rights reserved.
      5  1.1  joerg  *
      6  1.1  joerg  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  joerg  * by Joerg Sonnenberger.
      8  1.1  joerg  *
      9  1.1  joerg  * Redistribution and use in source and binary forms, with or without
     10  1.1  joerg  * modification, are permitted provided that the following conditions
     11  1.1  joerg  * are met:
     12  1.1  joerg  *
     13  1.1  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.1  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.1  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  joerg  *    notice, this list of conditions and the following disclaimer in
     17  1.1  joerg  *    the documentation and/or other materials provided with the
     18  1.1  joerg  *    distribution.
     19  1.1  joerg  *
     20  1.1  joerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.1  joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.1  joerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.1  joerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     24  1.1  joerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  joerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1  joerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  joerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.1  joerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.1  joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  1.1  joerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  joerg  * SUCH DAMAGE.
     32  1.1  joerg  */
     33  1.1  joerg 
     34  1.1  joerg #include <sys/cdefs.h>
     35  1.1  joerg __RCSID("$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $");
     36  1.1  joerg 
     37  1.1  joerg #include <atf-c.h>
     38  1.1  joerg #include <assert.h>
     39  1.1  joerg #include <cdbr.h>
     40  1.1  joerg #include <cdbw.h>
     41  1.1  joerg #include <fcntl.h>
     42  1.1  joerg #include <stdlib.h>
     43  1.1  joerg #include <string.h>
     44  1.1  joerg #include <unistd.h>
     45  1.1  joerg 
     46  1.1  joerg #define	MAXKEYS	16384
     47  1.1  joerg 
     48  1.1  joerg static const char database_name[] = "test.cdb";
     49  1.1  joerg 
     50  1.1  joerg uint32_t keys[MAXKEYS];
     51  1.1  joerg 
     52  1.1  joerg static int
     53  1.1  joerg cmp_keys(const void *a_, const void *b_)
     54  1.1  joerg {
     55  1.1  joerg 	uint32_t a = *(const uint32_t *)a_;
     56  1.1  joerg 	uint32_t b = *(const uint32_t *)b_;
     57  1.1  joerg 
     58  1.1  joerg 	return a > b ? 1 : (a < b ? 1 : 0);
     59  1.1  joerg }
     60  1.1  joerg 
     61  1.1  joerg static void
     62  1.1  joerg init_keys(size_t len)
     63  1.1  joerg {
     64  1.1  joerg 	uint32_t sorted_keys[MAXKEYS];
     65  1.1  joerg 	size_t i;
     66  1.1  joerg 
     67  1.1  joerg 	assert(len <= MAXKEYS);
     68  1.1  joerg 
     69  1.1  joerg 	if (len == 0)
     70  1.1  joerg 		return;
     71  1.1  joerg 
     72  1.1  joerg 	do {
     73  1.1  joerg 		for (i = 0; i < len; ++i)
     74  1.1  joerg 			sorted_keys[i] = keys[i] = arc4random();
     75  1.1  joerg 
     76  1.1  joerg 		qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys);
     77  1.1  joerg 		for (i = 1; i < len; ++i) {
     78  1.1  joerg 			if (sorted_keys[i - 1] == sorted_keys[i])
     79  1.1  joerg 				break;
     80  1.1  joerg 		}
     81  1.1  joerg 	} while (i != len);
     82  1.1  joerg }
     83  1.1  joerg 
     84  1.1  joerg static void
     85  1.1  joerg write_database(size_t len)
     86  1.1  joerg {
     87  1.1  joerg 	struct cdbw *db;
     88  1.1  joerg 	int fd;
     89  1.1  joerg 	size_t i;
     90  1.1  joerg 	uint32_t buf[2];
     91  1.1  joerg 
     92  1.1  joerg 	ATF_REQUIRE((db = cdbw_open()) != NULL);
     93  1.1  joerg 	ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1);
     94  1.1  joerg 	for (i = 0; i < len; ++i) {
     95  1.1  joerg 		buf[0] = i;
     96  1.1  joerg 		buf[1] = keys[i];
     97  1.1  joerg 		ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
     98  1.1  joerg 		    buf, sizeof(buf)) == 0);
     99  1.1  joerg 	}
    100  1.1  joerg 	ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
    101  1.1  joerg 	cdbw_close(db);
    102  1.1  joerg 	ATF_REQUIRE(close(fd) == 0);
    103  1.1  joerg }
    104  1.1  joerg 
    105  1.1  joerg static void
    106  1.1  joerg check_database(size_t len)
    107  1.1  joerg {
    108  1.1  joerg 	struct cdbr *db;
    109  1.1  joerg 	size_t i, data_len;
    110  1.1  joerg 	const void *data;
    111  1.1  joerg 	uint32_t buf[2];
    112  1.1  joerg 
    113  1.1  joerg 	ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL);
    114  1.1  joerg 	ATF_REQUIRE_EQ(cdbr_entries(db), len);
    115  1.1  joerg 	for (i = 0; i < len; ++i) {
    116  1.1  joerg 		ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]),
    117  1.1  joerg 		    &data, &data_len) != -1);
    118  1.1  joerg 		ATF_REQUIRE_EQ(data_len, sizeof(buf));
    119  1.1  joerg 		memcpy(buf, data, sizeof(buf));
    120  1.1  joerg 		ATF_REQUIRE_EQ(buf[0], i);
    121  1.1  joerg 		ATF_REQUIRE_EQ(buf[1], keys[i]);
    122  1.1  joerg 	}
    123  1.1  joerg 	cdbr_close(db);
    124  1.1  joerg }
    125  1.1  joerg 
    126  1.1  joerg ATF_TC_WITH_CLEANUP(cdb);
    127  1.1  joerg 
    128  1.1  joerg ATF_TC_HEAD(cdb, tc)
    129  1.1  joerg {
    130  1.1  joerg 
    131  1.1  joerg 	atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
    132  1.1  joerg }
    133  1.1  joerg 
    134  1.1  joerg ATF_TC_BODY(cdb, tc)
    135  1.1  joerg {
    136  1.1  joerg 	size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
    137  1.1  joerg 	for (i = 0; i < __arraycount(sizes); ++i) {
    138  1.1  joerg 		init_keys(sizes[i]);
    139  1.1  joerg 		write_database(sizes[i]);
    140  1.1  joerg 		check_database(sizes[i]);
    141  1.1  joerg 		unlink(database_name);
    142  1.1  joerg 	}
    143  1.1  joerg }
    144  1.1  joerg 
    145  1.1  joerg ATF_TC_CLEANUP(cdb, tc)
    146  1.1  joerg {
    147  1.1  joerg 
    148  1.1  joerg 	unlink(database_name);
    149  1.1  joerg }
    150  1.1  joerg 
    151  1.1  joerg ATF_TP_ADD_TCS(tp)
    152  1.1  joerg {
    153  1.1  joerg 
    154  1.1  joerg 	ATF_TP_ADD_TC(tp, cdb);
    155  1.1  joerg 
    156  1.1  joerg 	return atf_no_error();
    157  1.1  joerg }
    158  1.1  joerg 
    159