File: crc32-test.c

package info (click to toggle)
amanda 1%3A3.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,420 kB
  • sloc: ansic: 197,218; perl: 109,331; xml: 16,126; sh: 4,180; makefile: 2,810; awk: 502; lex: 407; yacc: 347; tcl: 118; sql: 19; sed: 16; php: 2
file content (120 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download | duplicates (5)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
 * Copyright (c) 2013-2016 Carbonite, Inc.  All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * Contact information: Carbonite Inc., 756 N Pastoria Ave
 * Sunnyvale, CA 94085, or: http://www.zmanda.com
 *
 * Author: Dustin J. Mitchell <dustin@zmanda.com>
 */

#include "amanda.h"
#include "testutils.h"
#include "amutil.h"
#include "amcrc32chw.h"

/* Utilities */

#define SIZE_BUF 33819
static uint8_t test_buf[SIZE_BUF];
static size_t size_of_test[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 63, 64, 65, 255, 256, 257, 258, 767, 768, 769, 1023, 1024, 1027, 32767, 32768, 32769, 33791, 33792, 33793, 33794, 33795, 33796, 33797, 33798, 33799, 33800, 33801, 33802, 33803, 33804, 33805, 33806, 33807, 33808, 33809, 33810, 33811, 33812, 33813, 33814, 33815, 33816, 33817, 33818, 0 };

static void
init_test_buf(void)
{
    int i;

    for(i=0; i<SIZE_BUF; i++) {
	test_buf[i] = rand();
    }
}

static int
test_size(
    size_t size)
{
    crc_t crc1;
    crc_t crc16;
#ifdef __SSE4_2__
    crc_t crchw;
#endif

    crc32_init(&crc1);
    crc32_init(&crc16);
#ifdef __SSE4_2__
    crc32_init(&crchw);
#endif

    crc32_add_1byte(test_buf, size, &crc1);
    crc32_add_16bytes(test_buf, size, &crc16);
#ifdef __SSE4_2__
    if (have_sse42) {
	crc32c_add_hw(test_buf, size, &crchw);
    }
#endif

#ifdef __SSE4_2__
    g_fprintf(stderr, " %08x:%lld  %08x:%lld  %08x:%lld\n", crc32_finish(&crc1), (long long)crc1.size, crc32_finish(&crc16), (long long)crc16.size, crc32_finish(&crchw), (long long)crchw.size);
#else
    g_fprintf(stderr, " %08x:%lld  %08x:%lld\n", crc32_finish(&crc1), (long long)crc1.size, crc32_finish(&crc16), (long long)crc16.size);
#endif

    if (crc1.crc != crc16.crc ||
	crc1.size != crc16.size) {
	g_fprintf(stderr, " CRC16 %zu %08x:%lld != %08x:%lld\n", size, crc32_finish(&crc1), (long long)crc1.size, crc32_finish(&crc16), (long long)crc16.size);
	return FALSE;
    }
#ifdef __SSE4_2__
    if (have_sse42) {
	if (crc1.crc != crchw.crc ||
	    crc1.size != crchw.size) {
	    g_fprintf(stderr, " CRChw %zu %08x:%lld != %08x:%lld\n", size, crc32_finish(&crc1), (long long)crc1.size, crc32_finish(&crchw), (long long)crchw.size);
	    return FALSE;
	}
    }
#endif
    return TRUE;
}


/*
 * Main driver
 */

int
main(
    int    argc G_GNUC_UNUSED,
    char **argv G_GNUC_UNUSED)
{
    int i;
    int nb_error = 0;

    make_crc_table();
    init_test_buf();

    for (i=0; size_of_test[i] != 0; i++) {
	if (!test_size(size_of_test[i])) {
	    nb_error++;
	}
    }
    if (nb_error) {
	g_fprintf(stderr, " FAIL CRC \n");
    } else {
	g_fprintf(stderr, " PASS CRC\n");
    }
    return nb_error;
}