Release 2.3.3.1.
[mLib] / hash / t / unihash-test.c
1 /* -*-c-*-
2 *
3 * Test driver for universal hashing
4 *
5 * (c) 2009 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31
32 #include "unihash.h"
33 #include "testrig.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 static int verify(dstr *v)
38 {
39 unihash_info ui;
40 uint32 k;
41 uint32 h, hh;
42 size_t n;
43 int i, c;
44 const char *p;
45 int ok = 1;
46
47 static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
48
49 /* --- Set up for using this key --- */
50
51 k = *(uint32 *)v[0].buf;
52 h = *(uint32 *)v[2].buf;
53 unihash_setkey(&ui, k);
54
55 /* --- Hash the data a lot --- */
56
57 for (i = 0; step[i] >= 0; i++) {
58 c = step[i];
59 if (!c)
60 hh = unihash(&ui, v[1].buf, v[1].len);
61 else {
62 hh = UNIHASH_INIT(&ui);
63 p = v[1].buf;
64 n = v[1].len;
65 while (n) {
66 if (c > n) c = n;
67 hh = unihash_hash(&ui, hh, p, c);
68 p += c;
69 n -= c;
70 }
71 }
72 if (h != hh) {
73 ok = 0;
74 fprintf(stderr, "\nunihash failed\n");
75 fprintf(stderr, " key = %08lx\n", (unsigned long)k);
76 fprintf(stderr, " data = %s\n", v[1].buf);
77 fprintf(stderr, " step = %d\n", step[i]);
78 fprintf(stderr, " expected = %08lx\n", (unsigned long)h);
79 fprintf(stderr, " computed = %08lx\n", (unsigned long)hh);
80 }
81 }
82 return (ok);
83 }
84
85 static const test_chunk tests[] = {
86 { "hash", verify, { &type_uint32, &type_string, &type_uint32 } },
87 { 0, 0, { 0 } }
88 };
89
90 int main(int argc, char *argv[])
91 {
92 test_run(argc, argv, tests, "unihash.in");
93 return (0);
94 }
95
96 /*----- That's all, folks -------------------------------------------------*/