]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/isa-l/mem/mem_zero_detect_test.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / isa-l / mem / mem_zero_detect_test.c
CommitLineData
9f95a23c
TL
1/**********************************************************************
2 Copyright(c) 2011-2018 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 <stdlib.h>
33#include <limits.h>
34#include "mem_routines.h"
35#include "types.h"
36
37#define TEST_MEM 10*1024*1024
38#define TEST_LEN 8*1024
39#define RAND_ALIGN 32
40#define BORDER_BYTES (5*RAND_ALIGN + 7)
41
42#ifndef RANDOMS
43# define RANDOMS 2000
44#endif
45#ifndef TEST_SEED
46# define TEST_SEED 0x1234
47#endif
48
49int main(int argc, char *argv[])
50{
f67539c2
TL
51 int i, j, sign;
52 long long r, l;
9f95a23c
TL
53 void *buf;
54 unsigned char *a;
55 int failures = 0, ret_neg = 1;
56
57 printf("mem_zero_detect_test %d bytes, %d randoms, seed=0x%x ", TEST_MEM, RANDOMS,
58 TEST_SEED);
59 if (posix_memalign(&buf, 64, TEST_MEM)) {
60 printf("alloc error: Fail");
61 return -1;
62 }
63
64 srand(TEST_SEED);
65
66 // Test full zero buffer
67 memset(buf, 0, TEST_MEM);
68 failures = isal_zero_detect(buf, TEST_MEM);
69
70 if (failures) {
71 printf("Fail large buf test\n");
72 return failures;
73 }
74 putchar('.');
75
76 // Test small buffers
77 for (i = 0; i < TEST_LEN; i++) {
78 failures |= isal_zero_detect(buf, i);
79 if (failures) {
80 printf("Fail len=%d\n", i);
81 return failures;
82 }
83 }
84 putchar('.');
85
86 // Test small buffers near end of alloc region
87 a = buf;
88 for (i = 0; i < TEST_LEN; i++)
89 failures |= isal_zero_detect(&a[TEST_LEN - i], i);
90
91 if (failures) {
92 printf("Fail:\n");
93 return failures;
94 }
95 putchar('.');
96
97 // Test for detect non zero
98 a[TEST_MEM / 2] = 1;
99 ret_neg = isal_zero_detect(a, TEST_MEM);
100 if (ret_neg == 0) {
101 printf("Fail on not detect\n");
102 return -1;
103 }
104 a[TEST_MEM / 2] = 0;
105 putchar('.');
106
107 // Test various non-zero offsets
108 for (i = 0; i < BORDER_BYTES; i++) {
109 for (j = 0; j < CHAR_BIT; j++) {
110 a[i] = 1 << j;
111 ret_neg = isal_zero_detect(a, TEST_MEM);
112 if (ret_neg == 0) {
113 printf("Fail on not detect offsets %d, %d\n", i, j);
114 return -1;
115 }
116 a[i] = 0;
117 }
118 }
119 putchar('.');
120 fflush(0);
121
122 // Test random non-zero offsets
123 for (i = 0; i < RANDOMS; i++) {
124 r = rand();
125 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
126 if (r >= TEST_LEN)
127 continue;
128
129 a[r] = 1 << (r & (CHAR_BIT - 1));
130 ret_neg = isal_zero_detect(a, TEST_MEM);
131 if (ret_neg == 0) {
132 printf("Fail on not detect rand %d, e=%lld\n", i, r);
133 return -1;
134 }
135 a[r] = 0;
136 }
137 putchar('.');
138 fflush(0);
139
140 // Test putting non-zero byte at end of buffer
141 for (i = 1; i < BORDER_BYTES; i++) {
142 for (j = 0; j < CHAR_BIT; j++) {
143 a[TEST_MEM - i] = 1 << j;
144 ret_neg = isal_zero_detect(a, TEST_MEM);
145 if (ret_neg == 0) {
146 printf("Fail on not detect rand offset=%d, idx=%d\n", i, j);
147 return -1;
148 }
149 a[TEST_MEM - i] = 0;
150 }
151 }
152 putchar('.');
153
154 // Test various size buffers and non-zero offsets
155 for (l = 1; l < TEST_LEN; l++) {
156 for (i = 0; i < l + BORDER_BYTES; i++) {
157 failures = isal_zero_detect(a, l);
158
159 if (failures) {
f67539c2 160 printf("Fail on detect non-zero with l=%lld\n", l);
9f95a23c
TL
161 return -1;
162 }
163
164 a[i] = 1;
165 ret_neg = isal_zero_detect(a, l);
166
167 if ((i < l) && (ret_neg == 0)) {
f67539c2 168 printf("Fail on non-zero buffer l=%lld err=%d\n", l, i);
9f95a23c
TL
169 return -1;
170 }
171 if ((i >= l) && (ret_neg != 0)) {
f67539c2 172 printf("Fail on bad pass detect l=%lld err=%d\n", l, i);
9f95a23c
TL
173 return -1;
174 }
175 a[i] = 0;
176 }
177 }
178 putchar('.');
179
180 // Test random test size and non-zero error offsets
181 for (i = 0; i < RANDOMS; i++) {
182 r = rand();
183 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
184 l = r + 1 + (rand() & (CHAR_BIT - 1));
185 a[r] = 1 << (r & (CHAR_BIT - 1));
186 ret_neg = isal_zero_detect(a, l);
187 if (ret_neg == 0) {
f67539c2 188 printf("Fail on not detect rand %d, l=%lld, e=%lld\n", i, l, r);
9f95a23c
TL
189 return -1;
190 }
191 a[r] = 0;
192 }
193 putchar('.');
194 fflush(0);
195
196 // Test combinations of zero and non-zero buffers
197 for (i = 0; i < RANDOMS; i++) {
198 r = rand();
199 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
200 sign = rand() & 1 ? 1 : -1;
201 l = r + sign * (rand() & (2 * RAND_ALIGN - 1));
202
203 if ((l >= TEST_LEN) || (l < 0) || (r >= TEST_LEN))
204 continue;
205
206 a[r] = 1 << (r & (CHAR_BIT - 1));
207 ret_neg = isal_zero_detect(a, l);
208
209 if ((r < l) && (ret_neg == 0)) {
f67539c2 210 printf("Fail on non-zero rand buffer %d, l=%lld, e=%lld\n", i, l, r);
9f95a23c
TL
211 return -1;
212 }
213 if ((r >= l) && (ret_neg != 0)) {
f67539c2 214 printf("Fail on bad pass zero detect rand %d, l=%lld, e=%lld\n", i, l,
9f95a23c
TL
215 r);
216 return -1;
217 }
218
219 a[r] = 0;
220 }
221 putchar('.');
222 fflush(0);
223
224 printf(failures == 0 ? " Pass\n" : " Fail\n");
225 return failures;
226}