1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  *
21  * Author: Dustin J. Mitchell <dustin@zmanda.com>
22  */
23 
24 #include "amanda.h"
25 #include "testutils.h"
26 #include "util.h"
27 #include "amcrc32chw.h"
28 
29 /* Utilities */
30 
31 #define SIZE_BUF 33819
32 static uint8_t test_buf[SIZE_BUF];
33 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 };
34 
35 static void
init_test_buf(void)36 init_test_buf(void)
37 {
38     int i;
39 
40     for(i=0; i<SIZE_BUF; i++) {
41 	test_buf[i] = rand();
42     }
43 }
44 
45 static int
test_size(size_t size)46 test_size(
47     size_t size)
48 {
49     crc_t crc1;
50     crc_t crc16;
51 #if defined __GNUC__ && GCC_VERSION > 40300 && (defined __x86_64__ || defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__)
52     crc_t crchw;
53 #endif
54 
55     crc32_init(&crc1);
56     crc32_init(&crc16);
57 #if defined __GNUC__ && GCC_VERSION > 40300 && (defined __x86_64__ || defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__)
58     crc32_init(&crchw);
59 #endif
60 
61     crc32_add_1byte(test_buf, size, &crc1);
62     crc32_add_16bytes(test_buf, size, &crc16);
63 #if defined __GNUC__ && GCC_VERSION > 40300 && (defined __x86_64__ || defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__)
64     if (have_sse42) {
65 	crc32c_add_hw(test_buf, size, &crchw);
66     }
67 #endif
68 
69 #if defined __GNUC__ && GCC_VERSION > 40300 && (defined __x86_64__ || defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__)
70     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);
71 #else
72     g_fprintf(stderr, " %08x:%lld  %08x:%lld\n", crc32_finish(&crc1), (long long)crc1.size, crc32_finish(&crc16), (long long)crc16.size);
73 #endif
74 
75     if (crc1.crc != crc16.crc ||
76 	crc1.size != crc16.size) {
77 	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);
78 	return FALSE;
79     }
80 #if defined __GNUC__ && GCC_VERSION > 40300 && (defined __x86_64__ || defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__)
81     if (have_sse42) {
82 	if (crc1.crc != crchw.crc ||
83 	    crc1.size != crchw.size) {
84 	    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);
85 	    return FALSE;
86 	}
87     }
88 #endif
89     return TRUE;
90 }
91 
92 
93 /*
94  * Main driver
95  */
96 
97 int
main(int argc,char ** argv)98 main(int argc, char **argv)
99 {
100     int i;
101     int nb_error = 0;
102     argc =argc;
103     argv =argv;
104 
105     make_crc_table();
106     init_test_buf();
107 
108     for (i=0; size_of_test[i] != 0; i++) {
109 	if (!test_size(size_of_test[i])) {
110 	    nb_error++;
111 	}
112     }
113     if (nb_error) {
114 	g_fprintf(stderr, " FAIL CRC \n");
115     } else {
116 	g_fprintf(stderr, " PASS CRC\n");
117     }
118     return nb_error;
119 }
120