encode_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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", txt)
  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. }