gf_vect_mul_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**********************************************************************
  2. Copyright(c) 2011-2015 Intel Corporation All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include "erasure_code.h"
  30. #define TEST_SIZE (128*1024)
  31. typedef unsigned char u8;
  32. int main(int argc, char *argv[])
  33. {
  34. int i, ret = -1;
  35. u8 *buff1 = NULL, *buff2 = NULL, *buff3 = NULL, gf_const_tbl[64], a = 2;
  36. int tsize;
  37. int align, size;
  38. unsigned char *efence_buff1;
  39. unsigned char *efence_buff2;
  40. unsigned char *efence_buff3;
  41. printf("gf_vect_mul_test: ");
  42. gf_vect_mul_init(a, gf_const_tbl);
  43. buff1 = (u8 *) malloc(TEST_SIZE);
  44. buff2 = (u8 *) malloc(TEST_SIZE);
  45. buff3 = (u8 *) malloc(TEST_SIZE);
  46. if (NULL == buff1 || NULL == buff2 || NULL == buff3) {
  47. printf("buffer alloc error\n");
  48. goto exit;
  49. }
  50. // Fill with rand data
  51. for (i = 0; i < TEST_SIZE; i++)
  52. buff1[i] = rand();
  53. if (gf_vect_mul(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
  54. printf("fail creating buff2\n");
  55. goto exit;
  56. }
  57. for (i = 0; i < TEST_SIZE; i++) {
  58. if (gf_mul_erasure(a, buff1[i]) != buff2[i]) {
  59. printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n", i,
  60. buff1[i], buff2[i], gf_mul_erasure(2, buff1[i]));
  61. goto exit;
  62. }
  63. }
  64. if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff3) != 0) {
  65. printf("fail fill with rand data\n");
  66. goto exit;
  67. }
  68. // Check reference function
  69. for (i = 0; i < TEST_SIZE; i++) {
  70. if (buff2[i] != buff3[i]) {
  71. printf("fail at %d, 0x%x x 0x%d = 0x%x (0x%x)\n",
  72. i, a, buff1[i], buff2[i], gf_mul_erasure(a, buff1[i]));
  73. goto exit;
  74. }
  75. }
  76. for (i = 0; i < TEST_SIZE; i++)
  77. buff1[i] = rand();
  78. // Check each possible constant
  79. for (a = 0; a != 255; a++) {
  80. gf_vect_mul_init(a, gf_const_tbl);
  81. if (gf_vect_mul(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
  82. printf("fail creating buff2\n");
  83. goto exit;
  84. }
  85. for (i = 0; i < TEST_SIZE; i++)
  86. if (gf_mul_erasure(a, buff1[i]) != buff2[i]) {
  87. printf("fail at %d, 0x%x x %d = 0x%x (0x%x)\n",
  88. i, a, buff1[i], buff2[i], gf_mul_erasure(2, buff1[i]));
  89. goto exit;
  90. }
  91. #ifdef TEST_VERBOSE
  92. putchar('.');
  93. #endif
  94. }
  95. // Check buffer len
  96. for (tsize = TEST_SIZE; tsize > 0; tsize -= 32) {
  97. a = rand();
  98. gf_vect_mul_init(a, gf_const_tbl);
  99. if (gf_vect_mul(tsize, gf_const_tbl, buff1, buff2) != 0) {
  100. printf("fail creating buff2 (len %d)\n", tsize);
  101. goto exit;
  102. }
  103. for (i = 0; i < tsize; i++)
  104. if (gf_mul_erasure(a, buff1[i]) != buff2[i]) {
  105. printf("fail at %d, 0x%x x %d = 0x%x (0x%x)\n",
  106. i, a, buff1[i], buff2[i], gf_mul_erasure(2, buff1[i]));
  107. goto exit;
  108. }
  109. #ifdef TEST_VERBOSE
  110. if (0 == tsize % (32 * 8)) {
  111. putchar('.');
  112. fflush(0);
  113. }
  114. #endif
  115. }
  116. // Run tests at end of buffer for Electric Fence
  117. align = 32;
  118. a = 2;
  119. gf_vect_mul_init(a, gf_const_tbl);
  120. for (size = 0; size < TEST_SIZE; size += align) {
  121. // Line up TEST_SIZE from end
  122. efence_buff1 = buff1 + size;
  123. efence_buff2 = buff2 + size;
  124. efence_buff3 = buff3 + size;
  125. gf_vect_mul(TEST_SIZE - size, gf_const_tbl, efence_buff1, efence_buff2);
  126. for (i = 0; i < TEST_SIZE - size; i++)
  127. if (gf_mul_erasure(a, efence_buff1[i]) != efence_buff2[i]) {
  128. printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n",
  129. i, efence_buff1[i], efence_buff2[i],
  130. gf_mul_erasure(2, efence_buff1[i]));
  131. goto exit;
  132. }
  133. if (gf_vect_mul_base
  134. (TEST_SIZE - size, gf_const_tbl, efence_buff1, efence_buff3) != 0) {
  135. printf("fail line up TEST_SIZE from end\n");
  136. goto exit;
  137. }
  138. // Check reference function
  139. for (i = 0; i < TEST_SIZE - size; i++)
  140. if (efence_buff2[i] != efence_buff3[i]) {
  141. printf("fail at %d, 0x%x x 0x%d = 0x%x (0x%x)\n",
  142. i, a, efence_buff2[i], efence_buff3[i],
  143. gf_mul_erasure(2, efence_buff1[i]));
  144. goto exit;
  145. }
  146. #ifdef TEST_VERBOSE
  147. putchar('.');
  148. #endif
  149. }
  150. // Test all unsupported sizes up to TEST_SIZE
  151. for (size = 0; size < TEST_SIZE; size++) {
  152. if (size % align != 0 && gf_vect_mul(size, gf_const_tbl, buff1, buff2) == 0) {
  153. printf
  154. ("fail expecting nonzero return code for unaligned size param (%d)\n",
  155. size);
  156. goto exit;
  157. }
  158. }
  159. printf(" done: Pass\n");
  160. fflush(0);
  161. ret = 0;
  162. exit:
  163. free(buff1);
  164. free(buff2);
  165. free(buff3);
  166. return ret;
  167. }