encode_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. for i, r := range testResult {
  15. if (code.At(i, 0) == color.Black) != (r == '1') {
  16. t.Errorf("%v: code missmatch on position %d", txt, i)
  17. }
  18. }
  19. }
  20. }
  21. }
  22. func Test_EncodeFunctionChars(t *testing.T) {
  23. encFNC1 := "11110101110"
  24. encFNC2 := "11110101000"
  25. encFNC3 := "10111100010"
  26. encFNC4 := "10111101110"
  27. encStartB := "11010010000"
  28. encStop := "1100011101011"
  29. testEncode(t, string(FNC1)+"123", encStartB+encFNC1+"10011100110"+"11001110010"+"11001011100"+"11001000010"+encStop)
  30. testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
  31. testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
  32. testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
  33. }
  34. func Test_Unencodable(t *testing.T) {
  35. if _, err := Encode(""); err == nil {
  36. t.Fail()
  37. }
  38. if _, err := Encode("ä"); err == nil {
  39. t.Fail()
  40. }
  41. }
  42. func Test_EncodeCTable(t *testing.T) {
  43. testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
  44. testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
  45. }