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