1
0

matrix_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package reedsolomon
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestVerifyEncMatrixVand(t *testing.T) {
  7. a, err := genEncMatrixVand(4, 4)
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. e := []byte{1, 0, 0, 0,
  12. 0, 1, 0, 0,
  13. 0, 0, 1, 0,
  14. 0, 0, 0, 1,
  15. 27, 28, 18, 20,
  16. 28, 27, 20, 18,
  17. 18, 20, 27, 28,
  18. 20, 18, 28, 27}
  19. if !bytes.Equal(a, e) {
  20. t.Fatal("mismatch")
  21. }
  22. }
  23. func TestVerifyEncMatrixCauchy(t *testing.T) {
  24. a := genEncMatrixCauchy(4, 4)
  25. e := []byte{1, 0, 0, 0,
  26. 0, 1, 0, 0,
  27. 0, 0, 1, 0,
  28. 0, 0, 0, 1,
  29. 71, 167, 122, 186,
  30. 167, 71, 186, 122,
  31. 122, 186, 71, 167,
  32. 186, 122, 167, 71}
  33. if !bytes.Equal(a, e) {
  34. t.Fatal("mismatch")
  35. }
  36. }
  37. func TestMatrixInvert(t *testing.T) {
  38. testCases := []struct {
  39. matrixData []byte
  40. cols int
  41. expect []byte
  42. ok bool
  43. expectedErr error
  44. }{
  45. {
  46. []byte{56, 23, 98,
  47. 3, 100, 200,
  48. 45, 201, 123},
  49. 3,
  50. []byte{175, 133, 33,
  51. 130, 13, 245,
  52. 112, 35, 126},
  53. true,
  54. nil,
  55. },
  56. {
  57. []byte{0, 23, 98,
  58. 3, 100, 200,
  59. 45, 201, 123},
  60. 3,
  61. []byte{245, 128, 152,
  62. 188, 64, 135,
  63. 231, 81, 239},
  64. true,
  65. nil,
  66. },
  67. {
  68. []byte{1, 0, 0, 0, 0,
  69. 0, 1, 0, 0, 0,
  70. 0, 0, 0, 1, 0,
  71. 0, 0, 0, 0, 1,
  72. 7, 7, 6, 6, 1},
  73. 5,
  74. []byte{1, 0, 0, 0, 0,
  75. 0, 1, 0, 0, 0,
  76. 123, 123, 1, 122, 122,
  77. 0, 0, 1, 0, 0,
  78. 0, 0, 0, 1, 0},
  79. true,
  80. nil,
  81. },
  82. {
  83. []byte{4, 2,
  84. 12, 6},
  85. 2,
  86. nil,
  87. false,
  88. errSingular,
  89. },
  90. }
  91. for i, c := range testCases {
  92. m := matrix(c.matrixData)
  93. raw := make([]byte, 2*c.cols*c.cols)
  94. actual := make([]byte, c.cols*c.cols)
  95. actualErr := m.invert(raw, c.cols, actual)
  96. if actualErr != nil && c.ok {
  97. t.Errorf("case.%d, expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
  98. }
  99. if actualErr == nil && !c.ok {
  100. t.Errorf("case.%d, expected to fail with <ERROR> \"%s\", but passed", i+1, c.expectedErr)
  101. }
  102. if actualErr != nil && !c.ok {
  103. if c.expectedErr != actualErr {
  104. t.Errorf("case.%d, expected to fail with error \"%s\", but instead failed with error \"%s\"", i+1, c.expectedErr, actualErr)
  105. }
  106. }
  107. if actualErr == nil && c.ok {
  108. if !bytes.Equal(c.expect, actual) {
  109. t.Errorf("case.%d, mismatch", i+1)
  110. }
  111. }
  112. }
  113. }
  114. func benchmarkInvert(b *testing.B, size int) {
  115. m := genEncMatrixCauchy(size, 2)
  116. m.swap(0, size, size)
  117. m.swap(1, size+1, size)
  118. b.ResetTimer()
  119. buf := make([]byte, 3*size*size)
  120. raw := buf[:2*size*size]
  121. r := buf[2*size*size:]
  122. for i := 0; i < b.N; i++ {
  123. err := m.invert(raw, size, r)
  124. if err != nil {
  125. b.Fatal(err)
  126. }
  127. }
  128. }
  129. func BenchmarkInvert5x5(b *testing.B) {
  130. benchmarkInvert(b, 5)
  131. }
  132. func BenchmarkInvert10x10(b *testing.B) {
  133. benchmarkInvert(b, 10)
  134. }
  135. func BenchmarkInvert20x20(b *testing.B) {
  136. benchmarkInvert(b, 20)
  137. }