xref: /freebsd-src/contrib/netbsd-tests/lib/libc/stdlib/t_mi_vector_hash.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /*	$NetBSD: t_mi_vector_hash.c,v 1.3 2011/07/07 11:12:18 jruoho Exp $	*/
2*57718be8SEnji Cooper /*-
3*57718be8SEnji Cooper  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4*57718be8SEnji Cooper  * All rights reserved.
5*57718be8SEnji Cooper  *
6*57718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
7*57718be8SEnji Cooper  * by Joerg Sonnenberger.
8*57718be8SEnji Cooper  *
9*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper  * are met:
12*57718be8SEnji Cooper  *
13*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in
17*57718be8SEnji Cooper  *    the documentation and/or other materials provided with the
18*57718be8SEnji Cooper  *    distribution.
19*57718be8SEnji Cooper  *
20*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*57718be8SEnji Cooper  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*57718be8SEnji Cooper  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24*57718be8SEnji Cooper  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*57718be8SEnji Cooper  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*57718be8SEnji Cooper  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*57718be8SEnji Cooper  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*57718be8SEnji Cooper  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*57718be8SEnji Cooper  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*57718be8SEnji Cooper  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*57718be8SEnji Cooper  * SUCH DAMAGE.
32*57718be8SEnji Cooper  */
33*57718be8SEnji Cooper 
34*57718be8SEnji Cooper #include <sys/cdefs.h>
35*57718be8SEnji Cooper __RCSID("$NetBSD: t_mi_vector_hash.c,v 1.3 2011/07/07 11:12:18 jruoho Exp $");
36*57718be8SEnji Cooper 
37*57718be8SEnji Cooper #include <atf-c.h>
38*57718be8SEnji Cooper #include <stdlib.h>
39*57718be8SEnji Cooper #include <string.h>
40*57718be8SEnji Cooper 
41*57718be8SEnji Cooper ATF_TC(mi_vector_hash_basic);
ATF_TC_HEAD(mi_vector_hash_basic,tc)42*57718be8SEnji Cooper ATF_TC_HEAD(mi_vector_hash_basic, tc)
43*57718be8SEnji Cooper {
44*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
45*57718be8SEnji Cooper 	    "Test mi_vector_hash_vector_hash for consistent results");
46*57718be8SEnji Cooper }
47*57718be8SEnji Cooper 
48*57718be8SEnji Cooper static const struct testvector {
49*57718be8SEnji Cooper 	const char *vector;
50*57718be8SEnji Cooper 	uint32_t hashes[3];
51*57718be8SEnji Cooper } testv[] = {
52*57718be8SEnji Cooper 	{ "hello, world", { 0xd38f7f21, 0xbf6be9ab, 0x37a0e989 } },
53*57718be8SEnji Cooper 	{ "", { 0x9b2ec03d, 0xdb2b69ae, 0xbd49d10d } },
54*57718be8SEnji Cooper 	{ "a", { 0x9454baa3, 0xb711c708, 0x29eec818 } },
55*57718be8SEnji Cooper 	{ "ab", { 0x9a5dca90, 0xdd212644, 0x9879ac41 } },
56*57718be8SEnji Cooper 	{ "abc", { 0x0b91c470, 0x4770cdf5, 0x251e4793 } },
57*57718be8SEnji Cooper 	{ "abcd", { 0x5f128df3, 0xf5a667a6, 0x5ae61fa5 } },
58*57718be8SEnji Cooper 	{ "abcde", { 0x4cbae281, 0x799c0ed5, 0x03a96866 } },
59*57718be8SEnji Cooper 	{ "abcdef", { 0x507a54c8, 0xb6bd06f4, 0xde922732 } },
60*57718be8SEnji Cooper 	{ "abcdefg", { 0xae2bca5d, 0x61e960ef, 0xb9e6762c } },
61*57718be8SEnji Cooper 	{ "abcdefgh", { 0xd1021264, 0x87f6988f, 0x053f775e } },
62*57718be8SEnji Cooper 	{ "abcdefghi", { 0xe380defc, 0xfc35a811, 0x3a7b0a5f } },
63*57718be8SEnji Cooper 	{ "abcdefghij", { 0x9a504408, 0x70d2e89d, 0xc9cac242 } },
64*57718be8SEnji Cooper 	{ "abcdefghijk", { 0x376117d0, 0x89f434d4, 0xe52b8e4c } },
65*57718be8SEnji Cooper 	{ "abcdefghijkl", { 0x92253599, 0x7b6ff99e, 0x0b1b3ea5 } },
66*57718be8SEnji Cooper 	{ "abcdefghijklm", { 0x92ee6a52, 0x55587d47, 0x3122b031 } },
67*57718be8SEnji Cooper 	{ "abcdefghijklmn", { 0x827baf08, 0x1d0ada73, 0xfec330e0 } },
68*57718be8SEnji Cooper 	{ "abcdefghijklmno", { 0x06ab787d, 0xc1ad17c2, 0x11dccf31 } },
69*57718be8SEnji Cooper 	{ "abcdefghijklmnop", { 0x2cf18103, 0x638c9268, 0xfa1ecf51 } },
70*57718be8SEnji Cooper };
71*57718be8SEnji Cooper 
ATF_TC_BODY(mi_vector_hash_basic,tc)72*57718be8SEnji Cooper ATF_TC_BODY(mi_vector_hash_basic, tc)
73*57718be8SEnji Cooper {
74*57718be8SEnji Cooper 	size_t i, j, len;
75*57718be8SEnji Cooper 	uint32_t hashes[3];
76*57718be8SEnji Cooper 	char buf[256];
77*57718be8SEnji Cooper 
78*57718be8SEnji Cooper 	for (j = 0; j < 8; ++j) {
79*57718be8SEnji Cooper 		for (i = 0; i < sizeof(testv) / sizeof(testv[0]); ++i) {
80*57718be8SEnji Cooper 			len = strlen(testv[i].vector);
81*57718be8SEnji Cooper 			strcpy(buf + j, testv[i].vector);
82*57718be8SEnji Cooper 			mi_vector_hash(buf + j, len, 0, hashes);
83*57718be8SEnji Cooper 			ATF_CHECK_EQ(hashes[0], testv[i].hashes[0]);
84*57718be8SEnji Cooper 			ATF_CHECK_EQ(hashes[1], testv[i].hashes[1]);
85*57718be8SEnji Cooper 			ATF_CHECK_EQ(hashes[2], testv[i].hashes[2]);
86*57718be8SEnji Cooper 		}
87*57718be8SEnji Cooper 	}
88*57718be8SEnji Cooper }
89*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)90*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
91*57718be8SEnji Cooper {
92*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mi_vector_hash_basic);
93*57718be8SEnji Cooper 
94*57718be8SEnji Cooper 	return atf_no_error();
95*57718be8SEnji Cooper }
96