1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
38 # define TEST_SEED 0x1234
49 typedef uint64_t(*crc64_func_t
) (uint64_t, const uint8_t *, uint64_t);
51 typedef struct func_case
{
53 crc64_func_t crc64_func_call
;
54 crc64_func_t crc64_ref_call
;
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
}
66 // Generates pseudo-random data
68 void rand_buffer(unsigned char *buf
, long buffer_size
)
71 for (i
= 0; i
< buffer_size
; i
++)
76 int zeros_test(func_case_t
* test_func
);
78 int simple_pattern_test(func_case_t
* test_func
);
80 int seeds_sizes_test(func_case_t
* test_func
);
82 int eob_test(func_case_t
* test_func
);
84 int update_test(func_case_t
* test_func
);
87 void *buf_alloc
= NULL
;
89 int main(int argc
, char *argv
[])
91 int fail
= 0, fail_case
;
93 func_case_t
*test_func
;
97 // Align to MAX_BUF boundary
98 ret
= posix_memalign(&buf_alloc
, MAX_BUF
, MAX_BUF
* TEST_SIZE
);
100 printf("alloc error: Fail");
104 printf("CRC64 Tests\n");
106 for (i
= 0; i
< sizeof(test_funcs
) / sizeof(test_funcs
[0]); i
++) {
108 test_func
= &test_funcs
[i
];
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");
119 printf("\n%s Failed %d tests\n", test_func
->note
, fail_case
);
124 printf("CRC64 Tests all done: %s\n", fail
? "Fail" : "Pass");
130 int zeros_test(func_case_t
* test_func
)
132 uint64_t crc
, crc_ref
;
134 unsigned char *buf
= NULL
;
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);
141 if (crc
!= crc_ref
) {
143 printf("\n opt ref\n");
144 printf(" ------ ------\n");
145 printf("crc zero = 0x%16lx 0x%16lx \n", crc
, crc_ref
);
152 // Another simple test pattern
153 int simple_pattern_test(func_case_t
* test_func
)
155 uint64_t crc
, crc_ref
;
157 unsigned char *buf
= NULL
;
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
);
166 printf("crc all 8a = 0x%16lx 0x%16lx\n", crc
, crc_ref
);
173 int seeds_sizes_test(func_case_t
* test_func
)
175 uint64_t crc
, crc_ref
;
179 unsigned char *buf
= NULL
;
181 // Do a few random tests
182 buf
= (unsigned char *)buf_alloc
; //reset buf
184 rand_buffer(buf
, MAX_BUF
* TEST_SIZE
);
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
);
192 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i
, crc
, crc_ref
);
198 // Do a few random sizes
199 buf
= (unsigned char *)buf_alloc
; //reset buf
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
) {
207 printf("fail random size%i 0x%16lx 0x%16lx\n", i
, crc
, crc_ref
);
212 // Try different seeds
213 for (s
= 0; s
< 20; s
++) {
214 buf
= (unsigned char *)buf_alloc
; //reset buf
216 r
= rand(); // just to get a new seed
217 rand_buffer(buf
, MAX_BUF
* TEST_SIZE
); // new pseudo-rand data
220 printf("seed = 0x%lx\n", r
);
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
);
228 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i
, crc
, crc_ref
);
238 // Run tests at end of buffer
239 int eob_test(func_case_t
* test_func
)
241 uint64_t crc
, crc_ref
;
244 unsigned char *buf
= NULL
;
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
);
254 printf("crc eob rand%3d = 0x%16lx 0x%16lx\n", i
, crc
, crc_ref
);
262 int update_test(func_case_t
* test_func
)
264 uint64_t crc
, crc_ref
;
268 unsigned char *buf
= NULL
;
270 buf
= (unsigned char *)buf_alloc
; //reset buf
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.
285 printf("crc rand%3d = 0x%16lx 0x%16lx\n", i
, crc
, crc_ref
);