]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/isa-l/crc/crc16_t10dif_test.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / isa-l / crc / crc16_t10dif_test.c
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 <string.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include "crc.h"
35 #include "types.h"
36 #include "crc_ref.h"
37
38 #ifndef TEST_SEED
39 # define TEST_SEED 0x1234
40 #endif
41
42 #define MAX_BUF 4096
43 #define TEST_SIZE 20
44
45 typedef uint32_t u32;
46 typedef uint16_t u16;
47 typedef uint8_t u8;
48
49 uint16_t crc16_t10dif_ref(uint16_t seed, uint8_t * buf, uint64_t len);
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 = 0;
62 int verbose = argc - 1;
63 int i, s;
64 void *buf_raw;
65 unsigned char *buf;
66
67 printf("Test crc16_t10dif_test ");
68 if (posix_memalign(&buf_raw, 32, MAX_BUF * TEST_SIZE)) {
69 printf("alloc error: Fail");
70 return -1;
71 }
72 buf = (unsigned char *)buf_raw;
73
74 srand(TEST_SEED);
75
76 // Test of all zeros
77 memset(buf, 0, MAX_BUF * 10);
78 u16 crc_ref = crc16_t10dif_ref(TEST_SEED, buf, MAX_BUF);
79 u16 crc_base = crc16_t10dif_base(TEST_SEED, buf, MAX_BUF);
80 u16 crc = crc16_t10dif(TEST_SEED, buf, MAX_BUF);
81 if ((crc_base != crc_ref) || (crc != crc_ref)) {
82 fail++;
83 printf("\n opt ref\n");
84 printf(" ------ ------\n");
85 printf("crc zero = 0x%4x 0x%4x 0x%4x \n", crc_ref, crc_base, crc);
86 } else
87 printf(".");
88
89 // Another simple test pattern
90 memset(buf, 0x8a, MAX_BUF);
91 crc_ref = crc16_t10dif_ref(TEST_SEED, buf, MAX_BUF);
92 crc_base = crc16_t10dif_base(TEST_SEED, buf, MAX_BUF);
93 crc = crc16_t10dif(TEST_SEED, buf, MAX_BUF);
94 if ((crc_base != crc_ref) || (crc != crc_ref)) {
95 fail++;
96 printf("crc all 8a = 0x%4x 0x%4x 0x%4x\n", crc_ref, crc_base, crc);
97 } else
98 printf(".");
99
100 // Do a few random tests
101
102 rand_buffer(buf, MAX_BUF * TEST_SIZE);
103
104 for (i = 0; i < TEST_SIZE; i++) {
105 crc_ref = crc16_t10dif_ref(TEST_SEED, buf, MAX_BUF);
106 crc_base = crc16_t10dif_base(TEST_SEED, buf, MAX_BUF);
107 crc = crc16_t10dif(TEST_SEED, buf, MAX_BUF);
108 if ((crc_base != crc_ref) || (crc != crc_ref))
109 fail++;
110 if (verbose)
111 printf("crc rand%3d = 0x%4x 0x%4x 0x%4x\n", i, crc_ref, crc_base, crc);
112 else if (i % (TEST_SIZE / 8) == 0)
113 printf(".");
114 buf += MAX_BUF;
115 }
116
117 // Do a few random sizes
118 buf = (unsigned char *)buf_raw; //reset buf
119 r = rand();
120
121 for (i = MAX_BUF; i >= 0; i--) {
122 crc_ref = crc16_t10dif_ref(r, buf, i);
123 crc_base = crc16_t10dif_base(r, buf, i);
124 crc = crc16_t10dif(r, buf, i);
125 if ((crc_base != crc_ref) || (crc != crc_ref)) {
126 fail++;
127 printf("fail random size%i 0x%8x 0x%8x 0x%8x\n", i, crc_ref, crc_base,
128 crc);
129 } else if (i % (MAX_BUF / 8) == 0)
130 printf(".");
131 }
132
133 // Try different seeds
134 for (s = 0; s < 20; s++) {
135 buf = (unsigned char *)buf_raw; //reset buf
136
137 r = rand(); // just to get a new seed
138 rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data
139
140 if (verbose)
141 printf("seed = 0x%x\n", r);
142
143 for (i = 0; i < TEST_SIZE; i++) {
144 crc_ref = crc16_t10dif_ref(r, buf, MAX_BUF);
145 crc_base = crc16_t10dif_base(r, buf, MAX_BUF);
146 crc = crc16_t10dif(r, buf, MAX_BUF);
147 if ((crc_base != crc_ref) || (crc != crc_ref))
148 fail++;
149 if (verbose)
150 printf("crc rand%3d = 0x%4x 0x%4x 0x%4x\n", i, crc_ref,
151 crc_base, crc);
152 else if (i % (TEST_SIZE * 20 / 8) == 0)
153 printf(".");
154 buf += MAX_BUF;
155 }
156 }
157
158 // Run tests at end of buffer
159 buf = (unsigned char *)buf_raw; //reset buf
160 buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end
161 for (i = 0; i < TEST_SIZE; i++) {
162 crc_ref = crc16_t10dif_ref(TEST_SEED, buf + i, TEST_SIZE - i);
163 crc_base = crc16_t10dif_base(TEST_SEED, buf + i, TEST_SIZE - i);
164 crc = crc16_t10dif(TEST_SEED, buf + i, TEST_SIZE - i);
165 if ((crc_base != crc_ref) || (crc != crc_ref))
166 fail++;
167 if (verbose)
168 printf("crc eob rand%3d = 0x%4x 0x%4x 0x%4x\n", i, crc_ref, crc_base,
169 crc);
170 else
171 printf(".");
172 }
173
174 printf("Test done: %s\n", fail ? "Fail" : "Pass");
175 if (fail)
176 printf("\nFailed %d tests\n", fail);
177
178 return fail;
179 }