gf_poly_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package reedsolomon
  4. import (
  5. "testing"
  6. )
  7. func TestGFPolyAdd(t *testing.T) {
  8. // a + b == result
  9. var tests = []struct {
  10. a gfPoly
  11. b gfPoly
  12. result gfPoly
  13. }{
  14. {
  15. gfPoly{[]gfElement{0, 0, 0}},
  16. gfPoly{[]gfElement{0}},
  17. gfPoly{[]gfElement{}},
  18. },
  19. {
  20. gfPoly{[]gfElement{1, 0}},
  21. gfPoly{[]gfElement{1, 0}},
  22. gfPoly{[]gfElement{0, 0}},
  23. },
  24. {
  25. gfPoly{[]gfElement{0xA0, 0x80, 0xFF, 0x00}},
  26. gfPoly{[]gfElement{0x0A, 0x82}},
  27. gfPoly{[]gfElement{0xAA, 0x02, 0xFF}},
  28. },
  29. }
  30. for _, test := range tests {
  31. result := gfPolyAdd(test.a, test.b)
  32. if !test.result.equals(result) {
  33. t.Errorf("%s * %s != %s (got %s)\n", test.a.string(false), test.b.string(false),
  34. test.result.string(false), result.string(false))
  35. }
  36. if len(result.term) > 0 && result.term[len(result.term)-1] == 0 {
  37. t.Errorf("Result's maximum term coefficient is zero")
  38. }
  39. }
  40. }
  41. func TestGFPolyequals(t *testing.T) {
  42. // a == b if isEqual
  43. var tests = []struct {
  44. a gfPoly
  45. b gfPoly
  46. isEqual bool
  47. }{
  48. {
  49. gfPoly{[]gfElement{0}},
  50. gfPoly{[]gfElement{0}},
  51. true,
  52. },
  53. {
  54. gfPoly{[]gfElement{1}},
  55. gfPoly{[]gfElement{0}},
  56. false,
  57. },
  58. {
  59. gfPoly{[]gfElement{1, 0, 1, 0, 1}},
  60. gfPoly{[]gfElement{1, 0, 1, 0, 1}},
  61. true,
  62. },
  63. {
  64. gfPoly{[]gfElement{1, 0, 1}},
  65. gfPoly{[]gfElement{1, 0, 1, 0, 0}},
  66. true,
  67. },
  68. }
  69. for _, test := range tests {
  70. isEqual := test.a.equals(test.b)
  71. if isEqual != test.isEqual {
  72. t.Errorf("%s and %s equality is %t (got %t)\n", test.a.string(false), test.b.string(false),
  73. test.isEqual, isEqual)
  74. }
  75. }
  76. }
  77. func TestGFPolyMultiply(t *testing.T) {
  78. // a * b == result
  79. var tests = []struct {
  80. a gfPoly
  81. b gfPoly
  82. result gfPoly
  83. }{
  84. {
  85. gfPoly{[]gfElement{0, 0, 1}},
  86. gfPoly{[]gfElement{9}},
  87. gfPoly{[]gfElement{0, 0, 9}},
  88. },
  89. {
  90. gfPoly{[]gfElement{0, 16, 1}},
  91. gfPoly{[]gfElement{128, 2}},
  92. gfPoly{[]gfElement{0, 232, 160, 2}},
  93. },
  94. {
  95. gfPoly{[]gfElement{254, 120, 88, 44, 11, 1}},
  96. gfPoly{[]gfElement{16, 2, 0, 51, 44}},
  97. gfPoly{[]gfElement{91, 50, 25, 184, 194, 105, 45, 244, 58, 44}},
  98. },
  99. }
  100. for _, test := range tests {
  101. result := gfPolyMultiply(test.a, test.b)
  102. if !test.result.equals(result) {
  103. t.Errorf("%s * %s = %s (got %s)\n",
  104. test.a.string(false),
  105. test.b.string(false),
  106. test.result.string(false),
  107. result.string(false))
  108. }
  109. }
  110. }
  111. func TestGFPolyRemainder(t *testing.T) {
  112. // numerator / denominator == quotient + remainder.
  113. var tests = []struct {
  114. numerator gfPoly
  115. denominator gfPoly
  116. remainder gfPoly
  117. }{
  118. {
  119. gfPoly{[]gfElement{1}},
  120. gfPoly{[]gfElement{1}},
  121. gfPoly{[]gfElement{0}},
  122. },
  123. {
  124. gfPoly{[]gfElement{1, 0}},
  125. gfPoly{[]gfElement{1}},
  126. gfPoly{[]gfElement{0}},
  127. },
  128. {
  129. gfPoly{[]gfElement{1}},
  130. gfPoly{[]gfElement{1, 0}},
  131. gfPoly{[]gfElement{1}},
  132. },
  133. {
  134. gfPoly{[]gfElement{1, 0, 1}},
  135. gfPoly{[]gfElement{0, 1}},
  136. gfPoly{[]gfElement{1}},
  137. },
  138. // (x^12 + x^10) / (x^10 + x^8 + x^5 + x^4 + x^2 + x^1 + x^0) =
  139. // (x^10 + x^8 + x^5 + x^4 + x^2 + x^1 + x^0) * x^2 +
  140. // (x^7 + x^6 + x^4 + x^3 + x^2) (the remainder)
  141. {
  142. gfPoly{[]gfElement{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}},
  143. gfPoly{[]gfElement{1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1}},
  144. gfPoly{[]gfElement{0, 0, 1, 1, 1, 0, 1, 1}},
  145. },
  146. {
  147. gfPoly{[]gfElement{91, 50, 25, 184, 194, 105, 45, 244, 58, 44}},
  148. gfPoly{[]gfElement{254, 120, 88, 44, 11, 1}},
  149. gfPoly{[]gfElement{}},
  150. },
  151. {
  152. gfPoly{[]gfElement{0, 0, 0, 0, 0, 0, 195, 172, 24, 64}},
  153. gfPoly{[]gfElement{116, 147, 63, 198, 31, 1}},
  154. gfPoly{[]gfElement{48, 174, 34, 13, 134}},
  155. },
  156. }
  157. for _, test := range tests {
  158. remainder := gfPolyRemainder(test.numerator, test.denominator)
  159. if !test.remainder.equals(remainder) {
  160. t.Errorf("%s / %s, remainder = %s (got %s)\n",
  161. test.numerator.string(false),
  162. test.denominator.string(false),
  163. test.remainder.string(false),
  164. remainder.string(false))
  165. }
  166. }
  167. }