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