chiark / gitweb /
Release 2.4.2.
[mLib] / hash / t / crc32-test.c
1 /* -*-c-*-
2  *
3  * Test driver for CRC
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 "crc32.h"
33 #include "testrig.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 static int verify(dstr *v)
38 {
39   uint32 h, hh;
40   size_t n;
41   int i, c;
42   const char *p;
43   int ok = 1;
44
45   static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
46
47   /* --- Set up for using this key --- */
48
49   h = *(uint32 *)v[1].buf;
50
51   /* --- Hash the data a lot --- */
52
53   for (i = 0; step[i] >= 0; i++) {
54     c = step[i];
55     if (!c)
56       hh = crc32(0, v[0].buf, v[0].len);
57     else {
58       hh = 0;
59       p = v[0].buf;
60       n = v[0].len;
61       while (n) {
62         if (c > n) c = n;
63         hh = crc32(hh, p, c);
64         p += c;
65         n -= c;
66       }
67     }
68     if (h != hh) {
69       ok = 0;
70       fprintf(stderr, "\ncrc32 failed\n");
71       fprintf(stderr, "  data = %s\n", v[0].buf);
72       fprintf(stderr, "  step = %d\n", step[i]);
73       fprintf(stderr, "  expected = %08lx\n", (unsigned long)h);
74       fprintf(stderr, "  computed = %08lx\n", (unsigned long)hh);
75     }
76   }
77   return (ok);
78 }
79
80 static const test_chunk tests[] = {
81   { "hash", verify, { &type_string, &type_uint32 } },
82   { 0, 0, { 0 } }
83 };
84
85 int main(int argc, char *argv[])
86 {
87   test_run(argc, argv, tests, "crc32.in");
88   return (0);
89 }
90
91 /*----- That's all, folks -------------------------------------------------*/