]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2015 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 "crc.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 | // Generates pseudo-random data | |
50 | ||
51 | void rand_buffer(unsigned char *buf, long buffer_size) | |
52 | { | |
53 | long i; | |
54 | for (i = 0; i < buffer_size; i++) | |
55 | buf[i] = rand(); | |
56 | } | |
57 | ||
58 | int main(int argc, char *argv[]) | |
59 | { | |
60 | int fail = 0; | |
61 | u32 r; | |
62 | int verbose = argc - 1; | |
63 | int i, s, ret; | |
64 | void *buf_alloc; | |
65 | unsigned char *buf; | |
66 | ||
67 | printf("Test crc32_ieee "); | |
68 | ||
69 | // Align to MAX_BUF boundary | |
70 | ret = posix_memalign(&buf_alloc, MAX_BUF, MAX_BUF * TEST_SIZE); | |
71 | if (ret) { | |
72 | printf("alloc error: Fail"); | |
73 | return -1; | |
74 | } | |
75 | buf = (unsigned char *)buf_alloc; | |
76 | ||
77 | srand(TEST_SEED); | |
78 | ||
79 | // Test of all zeros | |
80 | memset(buf, 0, MAX_BUF * 10); | |
81 | u32 crc = crc32_ieee(TEST_SEED, buf, MAX_BUF); | |
82 | u32 crc_ref = crc32_ieee_base(TEST_SEED, buf, MAX_BUF); | |
83 | if (crc != crc_ref) { | |
84 | fail++; | |
85 | printf("\n opt ref\n"); | |
86 | printf(" ------ ------\n"); | |
87 | printf("crc zero = 0x%8x 0x%8x \n", crc, crc_ref); | |
88 | } else | |
89 | printf("."); | |
90 | ||
91 | // Another simple test pattern | |
92 | memset(buf, 0x8a, MAX_BUF); | |
93 | crc = crc32_ieee(TEST_SEED, buf, MAX_BUF); | |
94 | crc_ref = crc32_ieee_base(TEST_SEED, buf, MAX_BUF); | |
95 | if (crc != crc_ref) | |
96 | fail++; | |
97 | if (verbose) | |
98 | printf("crc all 8a = 0x%8x 0x%8x\n", crc, crc_ref); | |
99 | else | |
100 | printf("."); | |
101 | ||
102 | // Do a few random tests | |
103 | r = rand(); | |
104 | rand_buffer(buf, MAX_BUF * TEST_SIZE); | |
105 | ||
106 | for (i = 0; i < TEST_SIZE; i++) { | |
107 | crc = crc32_ieee(r, buf, MAX_BUF); | |
108 | crc_ref = crc32_ieee_base(r, buf, MAX_BUF); | |
109 | if (crc != crc_ref) | |
110 | fail++; | |
111 | if (verbose) | |
112 | printf("crc rand%3d = 0x%8x 0x%8x\n", i, crc, crc_ref); | |
113 | else | |
114 | printf("."); | |
115 | buf += MAX_BUF; | |
116 | } | |
117 | ||
118 | // Do a few random sizes | |
119 | buf = (unsigned char *)buf_alloc; //reset buf | |
120 | r = rand(); | |
121 | ||
122 | for (i = MAX_BUF; i >= 0; i--) { | |
123 | crc = crc32_ieee(r, buf, i); | |
124 | crc_ref = crc32_ieee_base(r, buf, i); | |
125 | if (crc != crc_ref) { | |
126 | fail++; | |
127 | printf("fail random size%i 0x%8x 0x%8x\n", i, crc, crc_ref); | |
128 | } else | |
129 | printf("."); | |
130 | } | |
131 | ||
132 | // Try different seeds | |
133 | for (s = 0; s < 20; s++) { | |
134 | buf = (unsigned char *)buf_alloc; //reset buf | |
135 | ||
136 | r = rand(); // just to get a new seed | |
137 | rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data | |
138 | ||
139 | if (verbose) | |
140 | printf("seed = 0x%x\n", r); | |
141 | ||
142 | for (i = 0; i < TEST_SIZE; i++) { | |
143 | crc = crc32_ieee(r, buf, MAX_BUF); | |
144 | crc_ref = crc32_ieee_base(r, buf, MAX_BUF); | |
145 | if (crc != crc_ref) | |
146 | fail++; | |
147 | if (verbose) | |
148 | printf("crc rand%3d = 0x%8x 0x%8x\n", i, crc, crc_ref); | |
149 | else | |
150 | printf("."); | |
151 | buf += MAX_BUF; | |
152 | } | |
153 | } | |
154 | ||
155 | // Run tests at end of buffer | |
156 | buf = (unsigned char *)buf_alloc; //reset buf | |
157 | buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end | |
158 | for (i = 0; i < TEST_SIZE; i++) { | |
159 | crc = crc32_ieee(TEST_SEED, buf + i, TEST_SIZE - i); | |
160 | crc_ref = crc32_ieee_base(TEST_SEED, buf + i, TEST_SIZE - i); | |
161 | if (crc != crc_ref) | |
162 | fail++; | |
163 | if (verbose) | |
164 | printf("crc eob rand%3d = 0x%4x 0x%4x\n", i, crc, crc_ref); | |
165 | else | |
166 | printf("."); | |
167 | } | |
168 | ||
169 | printf("Test done: %s\n", fail ? "Fail" : "Pass"); | |
170 | if (fail) | |
171 | printf("\nFailed %d tests\n", fail); | |
172 | ||
173 | return fail; | |
174 | } |