datamatrix_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package datamatrix
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func codeFromStr(str string, size *dmCodeSize) *datamatrixCode {
  7. code := newDataMatrixCode(size)
  8. idx := 0
  9. for _, r := range str {
  10. x := idx % size.Columns
  11. y := idx / size.Columns
  12. switch r {
  13. case '#':
  14. code.set(x, y, true)
  15. case '.':
  16. code.set(x, y, false)
  17. default:
  18. continue
  19. }
  20. idx++
  21. }
  22. return code
  23. }
  24. func Test_Issue12(t *testing.T) {
  25. data := `{"po":12,"batchAction":"start_end"}`
  26. realData := addPadding(encodeText(data), 36)
  27. wantedData := []byte{124, 35, 113, 112, 35, 59, 142, 45, 35, 99, 98, 117, 100, 105, 66, 100, 117, 106, 112, 111, 35, 59, 35, 116, 117, 98, 115, 117, 96, 102, 111, 101, 35, 126, 129, 181}
  28. if bytes.Compare(realData, wantedData) != 0 {
  29. t.Error("Data Encoding failed")
  30. return
  31. }
  32. var codeSize *dmCodeSize
  33. for _, s := range codeSizes {
  34. if s.DataCodewords() >= len(wantedData) {
  35. codeSize = s
  36. break
  37. }
  38. }
  39. realECC := ec.calcECC(realData, codeSize)[len(realData):]
  40. wantedECC := []byte{196, 53, 147, 192, 151, 213, 107, 61, 98, 251, 50, 71, 186, 15, 43, 111, 165, 243, 209, 79, 128, 109, 251, 4}
  41. if bytes.Compare(realECC, wantedECC) != 0 {
  42. t.Errorf("Error correction calculation failed\nGot: %v", realECC)
  43. return
  44. }
  45. barcode := `
  46. #.#.#.#.#.#.#.#.#.#.#.#.
  47. #....###..#..#....#...##
  48. ##.......#...#.#.#....#.
  49. #.###...##..#...##.##..#
  50. ##...####..##..#.#.#.##.
  51. #.###.##.###..#######.##
  52. #..###...##.##..#.##.##.
  53. #.#.#.#.#.#.###....#.#.#
  54. ##.#...#.#.#..#...#####.
  55. #...####..#...##..#.#..#
  56. ##...#...##.###.#.....#.
  57. #.###.#.##.#.....###..##
  58. ##..#####...#..##...###.
  59. ###...#.####.##.#.#.#..#
  60. #..###..#.#.####.#.###..
  61. ###.#.#..#..#.###.#.##.#
  62. #####.##.###..#.####.#..
  63. #.##.#......#.#..#.#.###
  64. ###.#....######.#...##..
  65. ##...#..##.###..#...####
  66. #.######.###.##..#...##.
  67. #..#..#.##.#..####...#.#
  68. ###.###..#..##.#.##...#.
  69. ########################`
  70. bc, err := Encode(data)
  71. if err != nil {
  72. t.Error(err)
  73. return
  74. }
  75. realResult := bc.(*datamatrixCode)
  76. if realResult.Columns != 24 || realResult.Rows != 24 {
  77. t.Errorf("Got wrong barcode size %dx%d", realResult.Columns, realResult.Rows)
  78. return
  79. }
  80. wantedResult := codeFromStr(barcode, realResult.dmCodeSize)
  81. for x := 0; x < wantedResult.Columns; x++ {
  82. for y := 0; y < wantedResult.Rows; y++ {
  83. r := realResult.get(x, y)
  84. w := wantedResult.get(x, y)
  85. if w != r {
  86. t.Errorf("Failed at: c%d/r%d", x, y)
  87. }
  88. }
  89. }
  90. }