You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
1.6 KiB

  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package reedsolomon
  4. import "testing"
  5. func TestGFMultiplicationIdentities(t *testing.T) {
  6. for i := 0; i < 256; i++ {
  7. value := gfElement(i)
  8. if gfMultiply(gfZero, value) != gfZero {
  9. t.Errorf("0 . %d != 0", value)
  10. }
  11. if gfMultiply(value, gfOne) != value {
  12. t.Errorf("%d . 1 == %d, want %d", value, gfMultiply(value, gfOne), value)
  13. }
  14. }
  15. }
  16. func TestGFMultiplicationAndDivision(t *testing.T) {
  17. // a * b == result
  18. var tests = []struct {
  19. a gfElement
  20. b gfElement
  21. result gfElement
  22. }{
  23. {0, 29, 0},
  24. {1, 1, 1},
  25. {1, 32, 32},
  26. {2, 4, 8},
  27. {16, 128, 232},
  28. {17, 17, 28},
  29. {27, 9, 195},
  30. }
  31. for _, test := range tests {
  32. result := gfMultiply(test.a, test.b)
  33. if result != test.result {
  34. t.Errorf("%d * %d = %d, want %d", test.a, test.b, result, test.result)
  35. }
  36. if test.b != gfZero && test.result != gfZero {
  37. b := gfDivide(test.result, test.a)
  38. if b != test.b {
  39. t.Errorf("%d / %d = %d, want %d", test.result, test.a, b, test.b)
  40. }
  41. }
  42. }
  43. }
  44. func TestGFInverse(t *testing.T) {
  45. for i := 1; i < 256; i++ {
  46. a := gfElement(i)
  47. inverse := gfInverse(a)
  48. result := gfMultiply(a, inverse)
  49. if result != gfOne {
  50. t.Errorf("%d * %d^-1 == %d, want %d", a, inverse, result, gfOne)
  51. }
  52. }
  53. }
  54. func TestGFDivide(t *testing.T) {
  55. for i := 1; i < 256; i++ {
  56. for j := 1; j < 256; j++ {
  57. // a * b == product
  58. a := gfElement(i)
  59. b := gfElement(j)
  60. product := gfMultiply(a, b)
  61. // product / b == a
  62. result := gfDivide(product, b)
  63. if result != a {
  64. t.Errorf("%d / %d == %d, want %d", product, b, result, a)
  65. }
  66. }
  67. }
  68. }