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 <stdint.h> 32 #include <string.h> 33 #include <stdlib.h> 34 #include <limits.h> 35 #include "raid.h" 36 #include "test.h" 37 38 #define TEST_SOURCES 16 39 #define TEST_LEN 1024 40 #define TEST_MEM ((TEST_SOURCES + 2) * (TEST_LEN)) 41 #ifndef TEST_SEED 42 #define TEST_SEED 0x1234 43 #endif 44 45 // Generates pseudo-random data 46 47 void 48 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 56 dump(unsigned char *buf, int len) 57 { 58 int i; 59 for (i = 0; i < len;) { 60 printf(" %2x", buf[i++]); 61 if (i % 16 == 0) 62 printf("\n"); 63 } 64 printf("\n"); 65 return 0; 66 } 67 68 int 69 main(int argc, char *argv[]) 70 { 71 int i, j, k, ret = 0, fail = 0; 72 void *buffs[TEST_SOURCES + 2] = { NULL }; // Pointers to src and dest 73 char *tmp_buf[TEST_SOURCES + 2] = { NULL }; 74 75 printf("Test pq_gen_test "); 76 77 srand(TEST_SEED); 78 79 // Allocate the arrays 80 for (i = 0; i < TEST_SOURCES + 2; i++) { 81 void *buf; 82 ret = posix_memalign(&buf, 32, TEST_LEN); 83 if (ret) { 84 printf("alloc error: Fail"); 85 fail = 1; 86 goto exit; 87 } 88 buffs[i] = buf; 89 } 90 91 // Test of all zeros 92 for (i = 0; i < TEST_SOURCES + 2; i++) 93 memset(buffs[i], 0, TEST_LEN); 94 95 pq_gen(TEST_SOURCES + 2, TEST_LEN, buffs); 96 97 for (i = 0; i < TEST_LEN; i++) { 98 if (((char *) buffs[TEST_SOURCES])[i] != 0) 99 fail++; 100 } 101 102 for (i = 0; i < TEST_LEN; i++) { 103 if (((char *) buffs[TEST_SOURCES + 1])[i] != 0) 104 fail++; 105 } 106 107 if (fail > 0) { 108 printf("fail zero test %d\n", fail); 109 goto exit; 110 } 111 #ifdef TEST_VERBOSE 112 putchar('.'); 113 #endif 114 115 // Test rand1 116 for (i = 0; i < TEST_SOURCES + 2; i++) 117 rand_buffer(buffs[i], TEST_LEN); 118 119 ret = pq_gen(TEST_SOURCES + 2, TEST_LEN, buffs); 120 fail |= pq_check_base(TEST_SOURCES + 2, TEST_LEN, buffs); 121 122 if (fail > 0) { 123 int t; 124 printf(" Fail rand test1 fail=%d, ret=%d\n", fail, ret); 125 for (t = 0; t < TEST_SOURCES + 2; t++) 126 dump(buffs[t], 15); 127 128 printf(" reference function p,q\n"); 129 pq_gen_base(TEST_SOURCES + 2, TEST_LEN, buffs); 130 for (t = TEST_SOURCES; t < TEST_SOURCES + 2; t++) 131 dump(buffs[t], 15); 132 133 goto exit; 134 } 135 #ifdef TEST_VERBOSE 136 putchar('.'); 137 #endif 138 139 // Test various number of sources 140 for (j = 4; j <= TEST_SOURCES + 2; j++) { 141 for (i = 0; i < j; i++) 142 rand_buffer(buffs[i], TEST_LEN); 143 144 pq_gen(j, TEST_LEN, buffs); 145 fail |= pq_check_base(j, TEST_LEN, buffs); 146 147 if (fail > 0) { 148 printf("fail rand test %d sources\n", j); 149 goto exit; 150 } 151 #ifdef TEST_VERBOSE 152 putchar('.'); 153 #endif 154 } 155 156 fflush(0); 157 158 // Test various number of sources and len 159 k = 0; 160 while (k <= TEST_LEN) { 161 for (j = 4; j <= TEST_SOURCES + 2; j++) { 162 for (i = 0; i < j; i++) 163 rand_buffer(buffs[i], k); 164 165 ret = pq_gen(j, k, buffs); 166 fail |= pq_check_base(j, k, buffs); 167 168 if (fail > 0) { 169 printf("fail rand test %d sources, len=%d, fail=" 170 "%d, ret=%d\n", 171 j, k, fail, ret); 172 goto exit; 173 } 174 } 175 #ifdef TEST_VERBOSE 176 putchar('.'); 177 #endif 178 k += 32; 179 } 180 181 // Test at the end of buffer 182 k = 0; 183 while (k <= TEST_LEN) { 184 for (j = 0; j < (TEST_SOURCES + 2); j++) { 185 rand_buffer(buffs[j], TEST_LEN - k); 186 tmp_buf[j] = (char *) buffs[j] + k; 187 } 188 189 ret = pq_gen(TEST_SOURCES + 2, TEST_LEN - k, (void *) tmp_buf); 190 fail |= pq_check_base(TEST_SOURCES + 2, TEST_LEN - k, (void *) tmp_buf); 191 192 if (fail > 0) { 193 printf("fail end test - offset: %d, len: %d, fail: %d, " 194 "ret: %d\n", 195 k, TEST_LEN - k, fail, ret); 196 goto exit; 197 } 198 #ifdef TEST_VERBOSE 199 putchar('.'); 200 fflush(0); 201 #endif 202 k += 32; 203 } 204 205 if (!fail) 206 printf(" done: Pass\n"); 207 208 exit: 209 for (i = 0; i < TEST_SOURCES + 2; i++) 210 aligned_free(buffs[i]); 211 return fail; 212 } 213