summaryrefslogtreecommitdiff
path: root/src/lib/tests/crc32_test.c
blob: f96ca57a802945e570092bd7e1f0e2a7e981d9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Ouroboros - Copyright (C) 2016 - 2021
 *
 * Test of the CRC32 function
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#include <ouroboros/crc32.h>

#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

/*
 * Test vectors calculated at
 * https://www.lammertbies.nl/comm/info/crc-calculation.html
 */

int crc32_test(int     argc,
               char ** argv)
{
        uint32_t crc = 0;
        int i = 0;

        (void) argc;
        (void) argv;

        crc32(&crc, "0", 1);
        if (crc != 0xF4DBDF21)
                return -1;

        crc = 0;

        crc32(&crc, "123456789", 9);
        if (crc != 0xCBF43926)
                return -1;

        crc = 0;

        crc32(&crc, "987654321", 9);
        if (crc != 0x015F0201)
                return -1;

        crc32(&crc, "123456789", 9);
        if (crc != 0x806B60E3)
                return -1;

        crc = 0;

        crc32(&crc, &i , 1);
        if (crc != 0xD202EF8D)
                return -1;

        return 0;
}