reed_solomon_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package reedsolomon
  4. import (
  5. "testing"
  6. bitset "github.com/skip2/go-qrcode/bitset"
  7. )
  8. func TestGeneratorPoly(t *testing.T) {
  9. var tests = []struct {
  10. degree int
  11. generator gfPoly
  12. }{
  13. // x^2 + 3x^1 + 2x^0 (the shortest generator poly)
  14. {
  15. 2,
  16. gfPoly{term: []gfElement{2, 3, 1}},
  17. },
  18. // x^5 + 31x^4 + 198x^3 + 63x^2 + 147x^1 + 116x^0
  19. {
  20. 5,
  21. gfPoly{term: []gfElement{116, 147, 63, 198, 31, 1}},
  22. },
  23. // x^68 + 131x^67 + 115x^66 + 9x^65 + 39x^64 + 18x^63 + 182x^62 + 60x^61 +
  24. // 94x^60 + 223x^59 + 230x^58 + 157x^57 + 142x^56 + 119x^55 + 85x^54 +
  25. // 107x^53 + 34x^52 + 174x^51 + 167x^50 + 109x^49 + 20x^48 + 185x^47 +
  26. // 112x^46 + 145x^45 + 172x^44 + 224x^43 + 170x^42 + 182x^41 + 107x^40 +
  27. // 38x^39 + 107x^38 + 71x^37 + 246x^36 + 230x^35 + 225x^34 + 144x^33 +
  28. // 20x^32 + 14x^31 + 175x^30 + 226x^29 + 245x^28 + 20x^27 + 219x^26 +
  29. // 212x^25 + 51x^24 + 158x^23 + 88x^22 + 63x^21 + 36x^20 + 199x^19 + 4x^18 +
  30. // 80x^17 + 157x^16 + 211x^15 + 239x^14 + 255x^13 + 7x^12 + 119x^11 + 11x^10
  31. // + 235x^9 + 12x^8 + 34x^7 + 149x^6 + 204x^5 + 8x^4 + 32x^3 + 29x^2 + 99x^1
  32. // + 11x^0 (the longest generator poly)
  33. {
  34. 68,
  35. gfPoly{term: []gfElement{11, 99, 29, 32, 8, 204, 149, 34, 12,
  36. 235, 11, 119, 7, 255, 239, 211, 157, 80, 4, 199, 36, 63, 88, 158, 51, 212,
  37. 219, 20, 245, 226, 175, 14, 20, 144, 225, 230, 246, 71, 107, 38, 107, 182,
  38. 170, 224, 172, 145, 112, 185, 20, 109, 167, 174, 34, 107, 85, 119, 142,
  39. 157, 230, 223, 94, 60, 182, 18, 39, 9, 115, 131, 1}},
  40. },
  41. }
  42. for _, test := range tests {
  43. generator := rsGeneratorPoly(test.degree)
  44. if !generator.equals(test.generator) {
  45. t.Errorf("degree=%d generator=%s, want %s", test.degree,
  46. generator.string(true), test.generator.string(true))
  47. }
  48. }
  49. }
  50. func TestEncode(t *testing.T) {
  51. var tests = []struct {
  52. numECBytes int
  53. data string
  54. rsCode string
  55. }{
  56. {
  57. 5,
  58. "01000000 00011000 10101100 11000011 00000000",
  59. "01000000 00011000 10101100 11000011 00000000 10000110 00001101 00100010 10101110 00110000",
  60. },
  61. {
  62. 10,
  63. "00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001",
  64. "00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 10100101 00100100 11010100 11000001 11101101 00110110 11000111 10000111 00101100 01010101",
  65. },
  66. }
  67. for _, test := range tests {
  68. data := bitset.NewFromBase2String(test.data)
  69. rsCode := bitset.NewFromBase2String(test.rsCode)
  70. result := Encode(data, test.numECBytes)
  71. if !rsCode.Equals(result) {
  72. t.Errorf("data=%s, numECBytes=%d, encoded=%s, want %s",
  73. data.String(),
  74. test.numECBytes,
  75. result.String(),
  76. rsCode)
  77. }
  78. }
  79. }