File: encode_test.go

package info (click to toggle)
golang-barcode 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 3
file content (131 lines) | stat: -rw-r--r-- 3,961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package code128

import (
	"image/color"
	"testing"
)

func testEncode(t *testing.T, txt, testResult string) {
	code, err := Encode(txt)
	if err != nil || code == nil {
		t.Error(err)
	} else {
		if code.Bounds().Max.X != len(testResult) {
			t.Errorf("%v: length missmatch. Got %d expected %d", txt, code.Bounds().Max.X, len(testResult))
		} else {
			encoded := ""
			failed := false
			for i, r := range testResult {
				if code.At(i, 0) == color.Black {
					encoded += "1"
				} else {
					encoded += "0"
				}

				if (code.At(i, 0) == color.Black) != (r == '1') {
					failed = true
					t.Errorf("%v: code missmatch on position %d", txt, i)
				}
			}
			if failed {
				t.Log("Encoded: ", encoded)
			}
		}
	}
}

func Test_EncodeFunctionChars(t *testing.T) {
	encFNC1 := "11110101110"
	encFNC2 := "11110101000"
	encFNC3 := "10111100010"
	encFNC4 := "10111101110"
	encStartB := "11010010000"
	encStop := "1100011101011"

	// Special Case FC1 can also be encoded to C Table therefor using 123 as suffix might have unexpected results.
	testEncode(t, string(FNC1)+"A23", encStartB+encFNC1+"10100011000"+"11001110010"+"11001011100"+"10100011110"+encStop)
	testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
	testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
	testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
}

func Test_Unencodable(t *testing.T) {
	if _, err := Encode(""); err == nil {
		t.Fail()
	}
	if _, err := Encode("รค"); err == nil {
		t.Fail()
	}
}

func Test_EncodeCTable(t *testing.T) {
	testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
	testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")

	testEncode(t, string(FNC1)+"1234",
		"11010011100"+ // Start C
			"11110101110"+ // FNC1
			"10110011100"+ // 12
			"10001011000"+ // 34
			"11101001100"+ // CheckSum == 24
			"1100011101011") // Stop
}

func Test_shouldUseCTable(t *testing.T) {
	if !shouldUseCTable([]rune{FNC1, '1', '2'}, startCSymbol) {
		t.Error("[FNC1]12 failed")
	}
	if shouldUseCTable([]rune{FNC1, '1'}, startCSymbol) {
		t.Error("[FNC1]1 failed")
	}
	if shouldUseCTable([]rune{'0', FNC1, '1'}, startCSymbol) {
		t.Error("0[FNC1]1 failed")
	}
	if !shouldUseCTable([]rune{'0', '1', FNC1, '2', '3'}, startBSymbol) {
		t.Error("01[FNC1]23 failed")
	}
	if shouldUseCTable([]rune{'0', '1', FNC1}, startBSymbol) {
		t.Error("01[FNC1] failed")
	}
}

func Test_Issue16(t *testing.T) {
	if !shouldUseATable([]rune{'\r', 'A'}, 0) {
		t.Error("Code should start with A-Table if the text start with \\r")
	}
	if !shouldUseATable([]rune{FNC1, '\r'}, 0) {
		t.Error("Code should start with A-Table if the text start with <FNC1>\\r")
	}
	if shouldUseATable([]rune{FNC1, '1', '2', '3'}, 0) {
		t.Error("Code should not start with A-Table if the text start with <FNC1>123")
	}
	testEncode(t, string(FNC3)+"$P\rI", "110100001001011110001010010001100111011101101111011101011000100010110001010001100011101011")
}

func Test_Datalogic(t *testing.T) {
	// <Start A><FNC3>$P\r<checksum><STOP>
	testEncode(t, string(FNC3)+"$P\r",
		"11010000100"+ // <Start A>
			"10111100010"+ // <FNC3>
			"10010001100"+ // $
			"11101110110"+ // P
			"11110111010"+ // CR
			"11000100010"+ // checksum = 'I'
			"1100011101011") // STOP

	// <Start B><FNC3>$P,Ae,P<CR><checksum><STOP>
	testEncode(t, string(FNC3)+"$P,Ae,P\r",
		"11010010000"+ // <Start B>
			"10111100010"+ // <FNC3>
			"10010001100"+ // $
			"11101110110"+ // P
			"10110011100"+ // ,
			"10100011000"+ // A
			"10110010000"+ // e
			"10110011100"+ // ,
			"11101110110"+ // P
			"11101011110"+ // <Code A>
			"11110111010"+ // <CR>
			"10110001000"+ // checksum = 'D'
			"1100011101011") // STOP
}