encode_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package code128
  2. import (
  3. "image/color"
  4. "testing"
  5. )
  6. func testEncode(t *testing.T, txt, testResult string) {
  7. code, err := Encode(txt)
  8. if err != nil || code == nil {
  9. t.Error(err)
  10. } else {
  11. if code.Bounds().Max.X != len(testResult) {
  12. t.Errorf("%v: length missmatch. Got %d expected %d", txt, code.Bounds().Max.X, len(testResult))
  13. } else {
  14. encoded := ""
  15. failed := false
  16. for i, r := range testResult {
  17. if code.At(i, 0) == color.Black {
  18. encoded += "1"
  19. } else {
  20. encoded += "0"
  21. }
  22. if (code.At(i, 0) == color.Black) != (r == '1') {
  23. failed = true
  24. t.Errorf("%v: code missmatch on position %d", txt, i)
  25. }
  26. }
  27. if failed {
  28. t.Log("Encoded: ", encoded)
  29. }
  30. }
  31. }
  32. }
  33. func Test_EncodeFunctionChars(t *testing.T) {
  34. encFNC1 := "11110101110"
  35. encFNC2 := "11110101000"
  36. encFNC3 := "10111100010"
  37. encFNC4 := "10111101110"
  38. encStartB := "11010010000"
  39. encStop := "1100011101011"
  40. // Special Case FC1 can also be encoded to C Table therefor using 123 as suffix might have unexpected results.
  41. testEncode(t, string(FNC1)+"A23", encStartB+encFNC1+"10100011000"+"11001110010"+"11001011100"+"10100011110"+encStop)
  42. testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
  43. testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
  44. testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
  45. }
  46. func Test_Unencodable(t *testing.T) {
  47. if _, err := Encode(""); err == nil {
  48. t.Fail()
  49. }
  50. if _, err := Encode("ä"); err == nil {
  51. t.Fail()
  52. }
  53. }
  54. func Test_EncodeCTable(t *testing.T) {
  55. testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
  56. testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
  57. testEncode(t, string(FNC1)+"1234",
  58. "11010011100"+ // Start C
  59. "11110101110"+ // FNC1
  60. "10110011100"+ // 12
  61. "10001011000"+ // 34
  62. "11101001100"+ // CheckSum == 24
  63. "1100011101011") // Stop
  64. }
  65. func Test_shouldUseCTable(t *testing.T) {
  66. if !shouldUseCTable([]rune{FNC1, '1', '2'}, startCSymbol) {
  67. t.Error("[FNC1]12 failed")
  68. }
  69. if shouldUseCTable([]rune{FNC1, '1'}, startCSymbol) {
  70. t.Error("[FNC1]1 failed")
  71. }
  72. if shouldUseCTable([]rune{'0', FNC1, '1'}, startCSymbol) {
  73. t.Error("0[FNC1]1 failed")
  74. }
  75. if !shouldUseCTable([]rune{'0', '1', FNC1, '2', '3'}, startBSymbol) {
  76. t.Error("01[FNC1]23 failed")
  77. }
  78. if shouldUseCTable([]rune{'0', '1', FNC1}, startBSymbol) {
  79. t.Error("01[FNC1] failed")
  80. }
  81. }
  82. func Test_Issue16(t *testing.T) {
  83. if !shouldUseATable([]rune{'\r', 'A'}, 0) {
  84. t.Error("Code should start with A-Table if the text start with \\r")
  85. }
  86. if !shouldUseATable([]rune{FNC1, '\r'}, 0) {
  87. t.Error("Code should start with A-Table if the text start with <FNC1>\\r")
  88. }
  89. if shouldUseATable([]rune{FNC1, '1', '2', '3'}, 0) {
  90. t.Error("Code should not start with A-Table if the text start with <FNC1>123")
  91. }
  92. testEncode(t, string(FNC3)+"$P\rI", "110100001001011110001010010001100111011101101111011101011000100010110001010001100011101011")
  93. }
  94. func Test_Datalogic(t *testing.T) {
  95. // <Start A><FNC3>$P\r<checksum><STOP>
  96. testEncode(t, string(FNC3)+"$P\r",
  97. "11010000100"+ // <Start A>
  98. "10111100010"+ // <FNC3>
  99. "10010001100"+ // $
  100. "11101110110"+ // P
  101. "11110111010"+ // CR
  102. "11000100010"+ // checksum = 'I'
  103. "1100011101011") // STOP
  104. // <Start B><FNC3>$P,Ae,P<CR><checksum><STOP>
  105. testEncode(t, string(FNC3)+"$P,Ae,P\r",
  106. "11010010000"+ // <Start B>
  107. "10111100010"+ // <FNC3>
  108. "10010001100"+ // $
  109. "11101110110"+ // P
  110. "10110011100"+ // ,
  111. "10100011000"+ // A
  112. "10110010000"+ // e
  113. "10110011100"+ // ,
  114. "11101110110"+ // P
  115. "11101011110"+ // <Code A>
  116. "11110111010"+ // <CR>
  117. "10110001000"+ // checksum = 'D'
  118. "1100011101011") // STOP
  119. }