gf_inverse_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, memcmp
  30. #include <assert.h>
  31. #include "erasure_code.h"
  32. #define TEST_LEN 8192
  33. #ifndef TEST_SOURCES
  34. # define TEST_SOURCES 128
  35. #endif
  36. #ifndef RANDOMS
  37. # define RANDOMS 200
  38. #endif
  39. #define KMAX TEST_SOURCES
  40. typedef unsigned char u8;
  41. void matrix_mult(u8 * a, u8 * b, u8 * c, int n)
  42. {
  43. int i, j, k;
  44. u8 d;
  45. for (i = 0; i < n; i++) {
  46. for (j = 0; j < n; j++) {
  47. d = 0;
  48. for (k = 0; k < n; k++) {
  49. d ^= gf_mul_erasure(a[n * i + k], b[n * k + j]);
  50. }
  51. c[i * n + j] = d;
  52. }
  53. }
  54. }
  55. void print_matrix(u8 * a, int n)
  56. {
  57. int i, j;
  58. for (i = 0; i < n; i++) {
  59. for (j = 0; j < n; j++) {
  60. printf(" %2x", a[i * n + j]);
  61. }
  62. printf("\n");
  63. }
  64. printf("\n");
  65. }
  66. int is_ident(u8 * a, const int n)
  67. {
  68. int i, j;
  69. u8 c;
  70. for (i = 0; i < n; i++) {
  71. for (j = 0; j < n; j++) {
  72. c = *a++;
  73. if (i == j)
  74. c--;
  75. if (c != 0)
  76. return -1;
  77. }
  78. }
  79. return 0;
  80. }
  81. int inv_test(u8 * in, u8 * inv, u8 * sav, int n)
  82. {
  83. memcpy(sav, in, n * n);
  84. if (gf_invert_matrix(in, inv, n)) {
  85. printf("Given singular matrix\n");
  86. print_matrix(sav, n);
  87. return -1;
  88. }
  89. matrix_mult(inv, sav, in, n);
  90. if (is_ident(in, n)) {
  91. printf("fail\n");
  92. print_matrix(sav, n);
  93. print_matrix(inv, n);
  94. print_matrix(in, n);
  95. return -1;
  96. }
  97. #ifdef TEST_VERBOSE
  98. putchar('.');
  99. #endif
  100. return 0;
  101. }
  102. int main(int argc, char *argv[])
  103. {
  104. int i, k, t;
  105. u8 *test_mat = NULL, *save_mat = NULL, *invr_mat = NULL;
  106. int ret = -1;
  107. u8 test1[] = { 1, 1, 6,
  108. 1, 1, 1,
  109. 7, 1, 9
  110. };
  111. u8 test2[] = { 0, 1, 6,
  112. 1, 0, 1,
  113. 0, 1, 9
  114. };
  115. u8 test3[] = { 0, 0, 1,
  116. 1, 0, 0,
  117. 0, 1, 1
  118. };
  119. u8 test4[] = { 0, 1, 6, 7,
  120. 1, 1, 0, 0,
  121. 0, 1, 2, 3,
  122. 3, 2, 2, 3
  123. }; // = row3+3*row2
  124. printf("gf_inverse_test: max=%d ", KMAX);
  125. test_mat = malloc(KMAX * KMAX);
  126. save_mat = malloc(KMAX * KMAX);
  127. invr_mat = malloc(KMAX * KMAX);
  128. if (NULL == test_mat || NULL == save_mat || NULL == invr_mat)
  129. goto exit;
  130. // Test with lots of leading 1's
  131. k = 3;
  132. memcpy(test_mat, test1, k * k);
  133. if (inv_test(test_mat, invr_mat, save_mat, k))
  134. goto exit;
  135. // Test with leading zeros
  136. k = 3;
  137. memcpy(test_mat, test2, k * k);
  138. if (inv_test(test_mat, invr_mat, save_mat, k))
  139. goto exit;
  140. // Test 3
  141. k = 3;
  142. memcpy(test_mat, test3, k * k);
  143. if (inv_test(test_mat, invr_mat, save_mat, k))
  144. goto exit;
  145. // Test 4 - try a singular matrix
  146. k = 4;
  147. memcpy(test_mat, test4, k * k);
  148. if (!gf_invert_matrix(test_mat, invr_mat, k)) {
  149. printf("Fail: didn't catch singular matrix\n");
  150. print_matrix(test4, 4);
  151. goto exit;
  152. }
  153. // Do random test of size KMAX
  154. k = KMAX;
  155. for (i = 0; i < k * k; i++)
  156. test_mat[i] = save_mat[i] = rand();
  157. if (gf_invert_matrix(test_mat, invr_mat, k)) {
  158. printf("rand picked a singular matrix, try again\n");
  159. goto exit;
  160. }
  161. matrix_mult(invr_mat, save_mat, test_mat, k);
  162. if (is_ident(test_mat, k)) {
  163. printf("fail\n");
  164. print_matrix(save_mat, k);
  165. print_matrix(invr_mat, k);
  166. print_matrix(test_mat, k);
  167. goto exit;
  168. }
  169. // Do Randoms. Random size and coefficients
  170. for (t = 0; t < RANDOMS; t++) {
  171. k = rand() % KMAX;
  172. for (i = 0; i < k * k; i++)
  173. test_mat[i] = save_mat[i] = rand();
  174. if (gf_invert_matrix(test_mat, invr_mat, k))
  175. continue;
  176. matrix_mult(invr_mat, save_mat, test_mat, k);
  177. if (is_ident(test_mat, k)) {
  178. printf("fail rand k=%d\n", k);
  179. print_matrix(save_mat, k);
  180. print_matrix(invr_mat, k);
  181. print_matrix(test_mat, k);
  182. goto exit;
  183. }
  184. #ifdef TEST_VERBOSE
  185. if (0 == (t % 8))
  186. putchar('.');
  187. #endif
  188. }
  189. printf(" Pass\n");
  190. ret = 0;
  191. exit:
  192. free(test_mat);
  193. free(save_mat);
  194. free(invr_mat);
  195. return ret;
  196. }