gf_vect_mul_base_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <string.h> // for memset
  30. #include "erasure_code.h"
  31. #define TEST_SIZE 8192
  32. #define TEST_MEM TEST_SIZE
  33. #define TEST_LOOPS 100000
  34. #define TEST_TYPE_STR ""
  35. typedef unsigned char u8;
  36. int main(int argc, char *argv[])
  37. {
  38. int i;
  39. u8 *buff1, *buff2, *buff3, gf_const_tbl[64], a = 2;
  40. int align, size;
  41. unsigned char *efence_buff1;
  42. unsigned char *efence_buff2;
  43. printf("gf_vect_mul_base_test:\n");
  44. gf_vect_mul_init(a, gf_const_tbl);
  45. buff1 = (u8 *) malloc(TEST_SIZE);
  46. buff2 = (u8 *) malloc(TEST_SIZE);
  47. buff3 = (u8 *) malloc(TEST_SIZE);
  48. if (NULL == buff1 || NULL == buff2 || NULL == buff3) {
  49. printf("buffer alloc error\n");
  50. return -1;
  51. }
  52. // Fill with rand data
  53. for (i = 0; i < TEST_SIZE; i++)
  54. buff1[i] = rand();
  55. if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
  56. printf("fail fill with rand data\n");
  57. return 1;
  58. }
  59. for (i = 0; i < TEST_SIZE; i++)
  60. if (gf_mul_erasure(a, buff1[i]) != buff2[i]) {
  61. printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n", i, buff1[i], buff2[i],
  62. gf_mul_erasure(2, buff1[i]));
  63. return 1;
  64. }
  65. if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff3) != 0) {
  66. printf("fail fill with rand data for buff1\n");
  67. return -1;
  68. }
  69. // Check reference function
  70. for (i = 0; i < TEST_SIZE; i++)
  71. if (buff2[i] != buff3[i]) {
  72. printf("fail at %d, 0x%x x 0x%d = 0x%x (0x%x)\n",
  73. i, a, buff1[i], buff2[i], gf_mul_erasure(a, buff1[i]));
  74. return 1;
  75. }
  76. for (i = 0; i < TEST_SIZE; i++)
  77. buff1[i] = rand();
  78. // Check each possible constant
  79. printf("Random tests ");
  80. for (a = 0; a != 255; a++) {
  81. gf_vect_mul_init(a, gf_const_tbl);
  82. if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
  83. printf("fail random tests\n");
  84. return 1;
  85. }
  86. for (i = 0; i < TEST_SIZE; i++)
  87. if (gf_mul_erasure(a, buff1[i]) != buff2[i]) {
  88. printf("fail at %d, 0x%x x %d = 0x%x (0x%x)\n",
  89. i, a, buff1[i], buff2[i], gf_mul_erasure(2, buff1[i]));
  90. return 1;
  91. }
  92. #ifdef TEST_VERBOSE
  93. putchar('.');
  94. #endif
  95. }
  96. // Run tests at end of buffer for Electric Fence
  97. align = 32;
  98. a = 2;
  99. gf_vect_mul_init(a, gf_const_tbl);
  100. for (size = 0; size < TEST_SIZE; size += align) {
  101. // Line up TEST_SIZE from end
  102. efence_buff1 = buff1 + size;
  103. efence_buff2 = buff2 + size;
  104. if (gf_vect_mul_base
  105. (TEST_SIZE - size, gf_const_tbl, efence_buff1, efence_buff2) != 0) {
  106. printf("fail tests at end of buffer\n");
  107. return -1;
  108. }
  109. for (i = 0; i < TEST_SIZE - size; i++)
  110. if (gf_mul_erasure(a, efence_buff1[i]) != efence_buff2[i]) {
  111. printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n",
  112. i, efence_buff1[i], efence_buff2[i], gf_mul_erasure(2,
  113. efence_buff1
  114. [i]));
  115. return 1;
  116. }
  117. #ifdef TEST_VERBOSE
  118. putchar('.');
  119. #endif
  120. }
  121. printf(" done: Pass\n");
  122. return 0;
  123. }