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