1 /* $NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $ */
3 * Copyright (c) 2012 The NetBSD Foundation, Inc.
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
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
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
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $");
48 static const char database_name
[] = "test.cdb";
50 uint32_t keys
[MAXKEYS
];
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);
64 uint32_t sorted_keys
[MAXKEYS
];
67 assert(len
<= MAXKEYS
);
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
])
85 write_database(size_t len
)
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
) {
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);
102 ATF_REQUIRE(close(fd
) == 0);
106 check_database(size_t len
)
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
]);
126 ATF_TC_WITH_CLEANUP(cdb
);
131 atf_tc_set_md_var(tc
, "descr", "Test cdb(5) reading and writing");
136 size_t i
, sizes
[] = { 0, 16, 64, 1024, 2048 };
137 for (i
= 0; i
< __arraycount(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
);
154 ATF_TP_ADD_TC(tp
, cdb
);
156 return atf_no_error();