3 * Test driver for universal hashing
5 * (c) 2009 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
41 struct step
{ size_t s
; };
43 static void test_crc32(const struct tvec_reg
*in
, struct tvec_reg
*out
,
46 const struct step
*step
= ctx
;
47 const unsigned char *p
= in
[RM
].v
.bytes
.p
; size_t sz
= in
[RM
].v
.bytes
.sz
;
51 out
[RH
].v
.u
= crc32(0, p
, sz
);
53 for (h
= 0; sz
> step
->s
; p
+= step
->s
, sz
-= step
->s
)
54 h
= crc32(h
, p
, step
->s
);
55 out
[RH
].v
.u
= crc32(h
, p
, sz
);
59 static void before_hash(struct tvec_state
*tv
, void *ctx
)
60 { tvec_allocbuffer(&tv
->in
[RM
].v
); }
62 static void bench_crc32(const struct tvec_reg
*in
, struct tvec_reg
*out
,
64 { crc32(0, in
[RM
].v
.bytes
.p
, in
[RM
].v
.bytes
.sz
); }
65 static const struct tvec_env crc32_benchenv
=
66 { 0, 0, 0, before_hash
};
68 static void test_unihash(const struct tvec_reg
*in
, struct tvec_reg
*out
,
71 const struct step
*step
= ctx
;
73 const unsigned char *p
= in
[RM
].v
.bytes
.p
; size_t sz
= in
[RM
].v
.bytes
.sz
;
76 unihash_setkey(&ui
, in
[RK
].v
.u
);
78 out
[RH
].v
.u
= unihash(&ui
, p
, sz
);
80 for (h
= UNIHASH_INIT(&ui
); sz
> step
->s
; p
+= step
->s
, sz
-= step
->s
)
81 h
= unihash_hash(&ui
, h
, p
, step
->s
);
82 out
[RH
].v
.u
= unihash_hash(&ui
, h
, p
, sz
);
86 static void bench_unihash(const struct tvec_reg
*in
, struct tvec_reg
*out
,
88 { unihash_hash(ctx
, 0, in
[RM
].v
.bytes
.p
, in
[RM
].v
.bytes
.sz
); }
89 static void setup_unihash(struct tvec_state
*tv
,
90 const struct tvec_env
*env
, void *pctx
, void *ctx
)
91 { tvec_allocbuffer(&tv
->in
[RM
].v
); unihash_setkey(ctx
, 0); }
92 static const struct tvec_env unihash_benchenv
=
93 { sizeof(unihash_info
), setup_unihash
, 0, before_hash
};
95 static void run_step(struct tvec_state
*tv
, tvec_testfn
*fn
, void *ctx
)
97 static const size_t steps
[] = { 1, 5, 6, 7, 8, 23 };
101 fn(tv
->in
, tv
->out
, 0);
102 tvec_check(tv
, "whole buffer");
104 for (i
= 0; i
< N(steps
); i
++) {
106 fn(tv
->in
, tv
->out
, &step
);
107 tvec_check(tv
, "step = %lu", (unsigned long)steps
[i
]);
111 static const struct tvec_env step_testenv
= { 0, 0, 0, 0, run_step
, 0, 0 };
113 static const struct tvec_regdef unihash_regs
[] = {
114 { "k", RK
, &tvty_uint
, 0, { &tvrange_u32
} },
115 { "m", RM
, &tvty_bytes
, 0 },
116 { "h", RH
, &tvty_uint
, 0, { &tvrange_u32
} },
120 static const struct tvec_regdef crc32_regs
[] = {
121 { "m", RM
, &tvty_bytes
, 0 },
122 { "h", RH
, &tvty_uint
, 0, { &tvrange_u32
} },
126 static const struct tvec_regdef bench_regs
[] = {
127 { "msz", RM
, &tvty_buffer
, TVRF_ID
},
131 static const struct tvec_benchenv crc32_bench
=
132 { TVEC_BENCHINIT
, 1, -1, RM
, &crc32_benchenv
};
133 static const struct tvec_benchenv unihash_bench
=
134 { TVEC_BENCHINIT
, 1, -1, RM
, &unihash_benchenv
};
136 static const struct tvec_test tests
[] = {
137 { "crc32", crc32_regs
, &step_testenv
, test_crc32
},
138 { "unihash", unihash_regs
, &step_testenv
, test_unihash
},
139 { "crc32-bench", bench_regs
, &crc32_bench
._env
, bench_crc32
},
140 { "unihash-bench", bench_regs
, &unihash_bench
._env
, bench_unihash
},
144 static const struct tvec_config testconfig
=
145 { tests
, NROUT
, NREG
, sizeof(struct tvec_reg
) };
147 int main(int argc
, char *argv
[])
148 { return (tvec_main(argc
, argv
, &testconfig
, 0)); }
150 /*----- That's all, folks -------------------------------------------------*/