xref: /isa-l/mem/mem_zero_detect_test.c (revision aaa78d6a7c0a3e3a48e4997bd5b8f78e71070835)
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 "test.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 
49 int
main(int argc,char * argv[])50 main(int argc, char *argv[])
51 {
52         int i, j, sign;
53         long long r, l;
54         void *buf = NULL;
55         unsigned char *a;
56         int failures = 0, ret_neg = 1;
57 
58         printf("mem_zero_detect_test %d bytes, %d randoms, seed=0x%x ", TEST_MEM, RANDOMS,
59                TEST_SEED);
60         if (posix_memalign(&buf, 64, TEST_MEM)) {
61                 printf("alloc error: Fail");
62                 return -1;
63         }
64 
65         srand(TEST_SEED);
66 
67         // Test full zero buffer
68         memset(buf, 0, TEST_MEM);
69         failures = isal_zero_detect(buf, TEST_MEM);
70 
71         if (failures) {
72                 printf("Fail large buf test\n");
73                 goto exit;
74         }
75 #ifdef TEST_VERBOSE
76         putchar('.');
77 #endif
78 
79         // Test to help memory checkers
80         for (i = 1; i < 2345; i++) {
81                 uint8_t *newbuf = (uint8_t *) malloc(i);
82 
83                 if (newbuf == NULL) {
84                         printf("Fail alloc test - not enough memory\n");
85                         failures = -1;
86                         goto exit;
87                 }
88                 memset(newbuf, 0, i);
89                 failures = isal_zero_detect(newbuf, i);
90                 free(newbuf);
91                 if (failures) {
92                         printf("Fail alloc test\n");
93                         goto exit;
94                 }
95         }
96 
97         // Test small buffers
98         for (i = 0; i < TEST_LEN; i++) {
99                 failures |= isal_zero_detect(buf, i);
100                 if (failures) {
101                         printf("Fail len=%d\n", i);
102                         goto exit;
103                 }
104         }
105 #ifdef TEST_VERBOSE
106         putchar('.');
107 #endif
108 
109         // Test small buffers near end of alloc region
110         a = buf;
111         for (i = 0; i < TEST_LEN; i++)
112                 failures |= isal_zero_detect(&a[TEST_LEN - i], i);
113 
114         if (failures) {
115                 printf("Fail:\n");
116                 goto exit;
117         }
118 #ifdef TEST_VERBOSE
119         putchar('.');
120 #endif
121 
122         // Test for detect non zero
123         a[TEST_MEM / 2] = 1;
124         ret_neg = isal_zero_detect(a, TEST_MEM);
125         if (ret_neg == 0) {
126                 printf("Fail on not detect\n");
127                 failures = -1;
128                 goto exit;
129         }
130         a[TEST_MEM / 2] = 0;
131 #ifdef TEST_VERBOSE
132         putchar('.');
133 #endif
134 
135         // Test various non-zero offsets
136         for (i = 0; i < BORDER_BYTES; i++) {
137                 for (j = 0; j < CHAR_BIT; j++) {
138                         a[i] = 1 << j;
139                         ret_neg = isal_zero_detect(a, TEST_MEM);
140                         if (ret_neg == 0) {
141                                 printf("Fail on not detect offsets %d, %d\n", i, j);
142                                 failures = -1;
143                                 goto exit;
144                         }
145                         a[i] = 0;
146                 }
147         }
148 #ifdef TEST_VERBOSE
149         putchar('.');
150 #endif
151         fflush(0);
152 
153         // Test random non-zero offsets
154         for (i = 0; i < RANDOMS; i++) {
155                 r = rand();
156                 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
157                 if (r >= TEST_LEN)
158                         continue;
159 
160                 a[r] = 1 << (r & (CHAR_BIT - 1));
161                 ret_neg = isal_zero_detect(a, TEST_MEM);
162                 if (ret_neg == 0) {
163                         printf("Fail on not detect rand %d, e=%lld\n", i, r);
164                         failures = -1;
165                         goto exit;
166                 }
167                 a[r] = 0;
168         }
169 #ifdef TEST_VERBOSE
170         putchar('.');
171 #endif
172         fflush(0);
173 
174         // Test putting non-zero byte at end of buffer
175         for (i = 1; i < BORDER_BYTES; i++) {
176                 for (j = 0; j < CHAR_BIT; j++) {
177                         a[TEST_MEM - i] = 1 << j;
178                         ret_neg = isal_zero_detect(a, TEST_MEM);
179                         if (ret_neg == 0) {
180                                 printf("Fail on not detect rand offset=%d, idx=%d\n", i, j);
181                                 failures = -1;
182                                 goto exit;
183                         }
184                         a[TEST_MEM - i] = 0;
185                 }
186         }
187 #ifdef TEST_VERBOSE
188         putchar('.');
189 #endif
190 
191         // Test various size buffers and non-zero offsets
192         for (l = 1; l < TEST_LEN; l++) {
193                 for (i = 0; i < l + BORDER_BYTES; i++) {
194                         failures = isal_zero_detect(a, l);
195 
196                         if (failures) {
197                                 printf("Fail on detect non-zero with l=%lld\n", l);
198                                 goto exit;
199                         }
200 
201                         a[i] = 1;
202                         ret_neg = isal_zero_detect(a, l);
203 
204                         if ((i < l) && (ret_neg == 0)) {
205                                 printf("Fail on non-zero buffer l=%lld err=%d\n", l, i);
206                                 failures = -1;
207                                 goto exit;
208                         }
209                         if ((i >= l) && (ret_neg != 0)) {
210                                 printf("Fail on bad pass detect l=%lld err=%d\n", l, i);
211                                 failures = -1;
212                                 goto exit;
213                         }
214                         a[i] = 0;
215                 }
216         }
217 #ifdef TEST_VERBOSE
218         putchar('.');
219 #endif
220 
221         // Test random test size and non-zero error offsets
222         for (i = 0; i < RANDOMS; i++) {
223                 r = rand();
224                 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
225                 l = r + 1 + (rand() & (CHAR_BIT - 1));
226                 a[r] = 1 << (r & (CHAR_BIT - 1));
227                 ret_neg = isal_zero_detect(a, l);
228                 if (ret_neg == 0) {
229                         printf("Fail on not detect rand %d, l=%lld, e=%lld\n", i, l, r);
230                         failures = -1;
231                         goto exit;
232                 }
233                 a[r] = 0;
234         }
235 #ifdef TEST_VERBOSE
236         putchar('.');
237 #endif
238         fflush(0);
239 
240         // Test combinations of zero and non-zero buffers
241         for (i = 0; i < RANDOMS; i++) {
242                 r = rand();
243                 r = (r % TEST_LEN) ^ (r & (RAND_ALIGN - 1));
244                 sign = rand() & 1 ? 1 : -1;
245                 l = r + sign * (rand() & (2 * RAND_ALIGN - 1));
246 
247                 if ((l >= TEST_LEN) || (l < 0) || (r >= TEST_LEN))
248                         continue;
249 
250                 a[r] = 1 << (r & (CHAR_BIT - 1));
251                 ret_neg = isal_zero_detect(a, l);
252 
253                 if ((r < l) && (ret_neg == 0)) {
254                         printf("Fail on non-zero rand buffer %d, l=%lld, e=%lld\n", i, l, r);
255                         failures = -1;
256                         goto exit;
257                 }
258                 if ((r >= l) && (ret_neg != 0)) {
259                         printf("Fail on bad pass zero detect rand %d, l=%lld, e=%lld\n", i, l, r);
260                         failures = -1;
261                         goto exit;
262                 }
263 
264                 a[r] = 0;
265         }
266 #ifdef TEST_VERBOSE
267         putchar('.');
268 #endif
269         fflush(0);
270 
271 exit:
272         aligned_free(buf);
273         printf(failures == 0 ? " Pass\n" : " Fail\n");
274         return failures;
275 }
276