]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/crc/crc64_funcs_test.c
update sources to v12.1.1
[ceph.git] / ceph / src / isa-l / crc / crc64_funcs_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include "crc64.h"
35 #include "types.h"
36
37 #ifndef TEST_SEED
38 # define TEST_SEED 0x1234
39 #endif
40
41 #define MAX_BUF 512
42 #define TEST_SIZE 20
43
44 typedef uint64_t u64;
45 typedef uint32_t u32;
46 typedef uint16_t u16;
47 typedef uint8_t u8;
48
49 typedef uint64_t(*crc64_func_t) (uint64_t, const uint8_t *, uint64_t);
50
51 typedef struct func_case {
52 char *note;
53 crc64_func_t crc64_func_call;
54 crc64_func_t crc64_ref_call;
55 } func_case_t;
56
57 func_case_t test_funcs[] = {
58 {"crc64_ecma_norm", crc64_ecma_norm, crc64_ecma_norm_base},
59 {"crc64_ecma_refl", crc64_ecma_refl, crc64_ecma_refl_base},
60 {"crc64_iso_norm", crc64_iso_norm, crc64_iso_norm_base},
61 {"crc64_iso_refl", crc64_iso_refl, crc64_iso_refl_base},
62 {"crc64_jones_norm", crc64_jones_norm, crc64_jones_norm_base},
63 {"crc64_jones_refl", crc64_jones_refl, crc64_jones_refl_base}
64 };
65
66 // Generates pseudo-random data
67
68 void rand_buffer(unsigned char *buf, long buffer_size)
69 {
70 long i;
71 for (i = 0; i < buffer_size; i++)
72 buf[i] = rand();
73 }
74
75 // Test cases
76 int zeros_test(func_case_t * test_func);
77
78 int simple_pattern_test(func_case_t * test_func);
79
80 int seeds_sizes_test(func_case_t * test_func);
81
82 int eob_test(func_case_t * test_func);
83
84 int update_test(func_case_t * test_func);
85
86 int verbose = 0;
87 void *buf_alloc = NULL;
88
89 int main(int argc, char *argv[])
90 {
91 int fail = 0, fail_case;
92 int i, ret;
93 func_case_t *test_func;
94
95 verbose = argc - 1;
96
97 // Align to MAX_BUF boundary
98 ret = posix_memalign(&buf_alloc, MAX_BUF, MAX_BUF * TEST_SIZE);
99 if (ret) {
100 printf("alloc error: Fail");
101 return -1;
102 }
103 srand(TEST_SEED);
104 printf("CRC64 Tests\n");
105
106 for (i = 0; i < sizeof(test_funcs) / sizeof(test_funcs[0]); i++) {
107 fail_case = 0;
108 test_func = &test_funcs[i];
109
110 printf("Test %s ", test_func->note);
111 fail_case += zeros_test(test_func);
112 fail_case += simple_pattern_test(test_func);
113 fail_case += seeds_sizes_test(test_func);
114 fail_case += eob_test(test_func);
115 fail_case += update_test(test_func);
116 printf("Test %s done: %s\n", test_func->note, fail_case ? "Fail" : "Pass");
117
118 if (fail_case) {
119 printf("\n%s Failed %d tests\n", test_func->note, fail_case);
120 fail++;
121 }
122 }
123
124 printf("CRC64 Tests all done: %s\n", fail ? "Fail" : "Pass");
125
126 return fail;
127 }
128
129 // Test of all zeros
130 int zeros_test(func_case_t * test_func)
131 {
132 uint64_t crc, crc_ref;
133 int fail = 0;
134 unsigned char *buf = NULL;
135
136 buf = (unsigned char *)buf_alloc;
137 memset(buf, 0, MAX_BUF * 10);
138 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF * 10);
139 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF * 10);
140
141 if (crc != crc_ref) {
142 fail++;
143 printf("\n opt ref\n");
144 printf(" ------ ------\n");
145 printf("crc zero = 0x%16lx 0x%16lx \n", crc, crc_ref);
146 } else
147 printf(".");
148
149 return fail;
150 }
151
152 // Another simple test pattern
153 int simple_pattern_test(func_case_t * test_func)
154 {
155 uint64_t crc, crc_ref;
156 int fail = 0;
157 unsigned char *buf = NULL;
158
159 buf = (unsigned char *)buf_alloc;
160 memset(buf, 0x8a, MAX_BUF);
161 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF);
162 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF);
163 if (crc != crc_ref)
164 fail++;
165 if (verbose)
166 printf("crc all 8a = 0x%16lx 0x%16lx\n", crc, crc_ref);
167 else
168 printf(".");
169
170 return fail;
171 }
172
173 int seeds_sizes_test(func_case_t * test_func)
174 {
175 uint64_t crc, crc_ref;
176 int fail = 0;
177 int i;
178 uint64_t r, s;
179 unsigned char *buf = NULL;
180
181 // Do a few random tests
182 buf = (unsigned char *)buf_alloc; //reset buf
183 r = rand();
184 rand_buffer(buf, MAX_BUF * TEST_SIZE);
185
186 for (i = 0; i < TEST_SIZE; i++) {
187 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
188 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
189 if (crc != crc_ref)
190 fail++;
191 if (verbose)
192 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i, crc, crc_ref);
193 else
194 printf(".");
195 buf += MAX_BUF;
196 }
197
198 // Do a few random sizes
199 buf = (unsigned char *)buf_alloc; //reset buf
200 r = rand();
201
202 for (i = MAX_BUF; i >= 0; i--) {
203 crc = test_func->crc64_func_call(r, buf, i);
204 crc_ref = test_func->crc64_ref_call(r, buf, i);
205 if (crc != crc_ref) {
206 fail++;
207 printf("fail random size%i 0x%16lx 0x%16lx\n", i, crc, crc_ref);
208 } else
209 printf(".");
210 }
211
212 // Try different seeds
213 for (s = 0; s < 20; s++) {
214 buf = (unsigned char *)buf_alloc; //reset buf
215
216 r = rand(); // just to get a new seed
217 rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data
218
219 if (verbose)
220 printf("seed = 0x%lx\n", r);
221
222 for (i = 0; i < TEST_SIZE; i++) {
223 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
224 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
225 if (crc != crc_ref)
226 fail++;
227 if (verbose)
228 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i, crc, crc_ref);
229 else
230 printf(".");
231 buf += MAX_BUF;
232 }
233 }
234
235 return fail;
236 }
237
238 // Run tests at end of buffer
239 int eob_test(func_case_t * test_func)
240 {
241 uint64_t crc, crc_ref;
242 int fail = 0;
243 int i;
244 unsigned char *buf = NULL;
245
246 buf = (unsigned char *)buf_alloc; //reset buf
247 buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end
248 for (i = 0; i < TEST_SIZE; i++) {
249 crc = test_func->crc64_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
250 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
251 if (crc != crc_ref)
252 fail++;
253 if (verbose)
254 printf("crc eob rand%3d = 0x%16lx 0x%16lx\n", i, crc, crc_ref);
255 else
256 printf(".");
257 }
258
259 return fail;
260 }
261
262 int update_test(func_case_t * test_func)
263 {
264 uint64_t crc, crc_ref;
265 int fail = 0;
266 int i;
267 uint64_t r;
268 unsigned char *buf = NULL;
269
270 buf = (unsigned char *)buf_alloc; //reset buf
271 r = rand();
272 // Process the whole buf with reference func single call.
273 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF * TEST_SIZE);
274 // Process buf with update method.
275 for (i = 0; i < TEST_SIZE; i++) {
276 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
277 // Update crc seeds and buf pointer.
278 r = crc;
279 buf += MAX_BUF;
280 }
281
282 if (crc != crc_ref)
283 fail++;
284 if (verbose)
285 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i, crc, crc_ref);
286 else
287 printf(".");
288
289 return fail;
290 }