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 "raid.h" 35 #include "test.h" 36 37 #define TEST_SOURCES 16 38 #define TEST_LEN 1024 39 #define TEST_MEM ((TEST_SOURCES + 1)*(TEST_LEN)) 40 #ifndef TEST_SEED 41 # define TEST_SEED 0x1234 42 #endif 43 44 // Generates pseudo-random data 45 46 void rand_buffer(unsigned char *buf, long buffer_size) 47 { 48 long i; 49 for (i = 0; i < buffer_size; i++) 50 buf[i] = rand(); 51 } 52 53 int main(int argc, char *argv[]) 54 { 55 int i, j, k, ret, fail = 0; 56 void *buffs[TEST_SOURCES + 1]; 57 char *tmp_buf[TEST_SOURCES + 1]; 58 59 printf("Test xor_gen_test "); 60 61 srand(TEST_SEED); 62 63 // Allocate the arrays 64 for (i = 0; i < TEST_SOURCES + 1; i++) { 65 void *buf; 66 ret = posix_memalign(&buf, 32, TEST_LEN); 67 if (ret) { 68 printf("alloc error: Fail"); 69 return 1; 70 } 71 buffs[i] = buf; 72 } 73 74 // Test of all zeros 75 for (i = 0; i < TEST_SOURCES + 1; i++) 76 memset(buffs[i], 0, TEST_LEN); 77 78 xor_gen(TEST_SOURCES + 1, TEST_LEN, buffs); 79 80 for (i = 0; i < TEST_LEN; i++) { 81 if (((char *)buffs[TEST_SOURCES])[i] != 0) 82 fail++; 83 } 84 85 if (fail > 0) { 86 printf("fail zero test"); 87 return 1; 88 } else 89 putchar('.'); 90 91 // Test rand1 92 for (i = 0; i < TEST_SOURCES + 1; i++) 93 rand_buffer(buffs[i], TEST_LEN); 94 95 xor_gen(TEST_SOURCES + 1, TEST_LEN, buffs); 96 97 fail |= xor_check_base(TEST_SOURCES + 1, TEST_LEN, buffs); 98 99 if (fail > 0) { 100 printf("fail rand test %d\n", fail); 101 return 1; 102 } else 103 putchar('.'); 104 105 // Test various number of sources 106 for (j = 3; j <= TEST_SOURCES + 1; j++) { 107 for (i = 0; i < j; i++) 108 rand_buffer(buffs[i], TEST_LEN); 109 110 xor_gen(j, TEST_LEN, buffs); 111 fail |= xor_check_base(j, TEST_LEN, buffs); 112 113 if (fail > 0) { 114 printf("fail rand test %d sources\n", j); 115 return 1; 116 } else 117 putchar('.'); 118 } 119 120 fflush(0); 121 122 // Test various number of sources and len 123 k = 0; 124 while (k <= TEST_LEN) { 125 for (j = 3; j <= TEST_SOURCES + 1; j++) { 126 for (i = 0; i < j; i++) 127 rand_buffer(buffs[i], k); 128 129 xor_gen(j, k, buffs); 130 fail |= xor_check_base(j, k, buffs); 131 132 if (fail > 0) { 133 printf("fail rand test %d sources, len=%d, ret=%d\n", j, k, 134 fail); 135 return 1; 136 } 137 } 138 putchar('.'); 139 k += 1; 140 } 141 142 // Test at the end of buffer 143 for (i = 0; i < TEST_LEN; i += 32) { 144 for (j = 0; j < TEST_SOURCES + 1; j++) { 145 rand_buffer((unsigned char *)buffs[j] + i, TEST_LEN - i); 146 tmp_buf[j] = (char *)buffs[j] + i; 147 } 148 149 xor_gen(TEST_SOURCES + 1, TEST_LEN - i, (void *)tmp_buf); 150 fail |= xor_check_base(TEST_SOURCES + 1, TEST_LEN - i, (void *)tmp_buf); 151 152 if (fail > 0) { 153 printf("fail end test - offset: %d, len: %d\n", i, TEST_LEN - i); 154 return 1; 155 } 156 157 putchar('.'); 158 fflush(0); 159 } 160 161 if (!fail) 162 printf(" done: Pass\n"); 163 164 return fail; 165 } 166