1package datamatrix
2
3import (
4	"bytes"
5	"testing"
6)
7
8func codeFromStr(str string, size *dmCodeSize) *datamatrixCode {
9	code := newDataMatrixCode(size)
10	idx := 0
11	for _, r := range str {
12		x := idx % size.Columns
13		y := idx / size.Columns
14
15		switch r {
16		case '#':
17			code.set(x, y, true)
18		case '.':
19			code.set(x, y, false)
20		default:
21			continue
22		}
23
24		idx++
25	}
26	return code
27}
28
29func Test_Issue12(t *testing.T) {
30	data := `{"po":12,"batchAction":"start_end"}`
31	realData := addPadding(encodeText(data), 36)
32	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}
33
34	if bytes.Compare(realData, wantedData) != 0 {
35		t.Error("Data Encoding failed")
36		return
37	}
38
39	var codeSize *dmCodeSize
40	for _, s := range codeSizes {
41		if s.DataCodewords() >= len(wantedData) {
42			codeSize = s
43			break
44		}
45	}
46	realECC := ec.calcECC(realData, codeSize)[len(realData):]
47	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}
48	if bytes.Compare(realECC, wantedECC) != 0 {
49		t.Errorf("Error correction calculation failed\nGot: %v", realECC)
50		return
51	}
52
53	barcode := `
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	bc, err := Encode(data)
80
81	if err != nil {
82		t.Error(err)
83		return
84	}
85	realResult := bc.(*datamatrixCode)
86	if realResult.Columns != 24 || realResult.Rows != 24 {
87		t.Errorf("Got wrong barcode size %dx%d", realResult.Columns, realResult.Rows)
88		return
89	}
90
91	wantedResult := codeFromStr(barcode, realResult.dmCodeSize)
92
93	for x := 0; x < wantedResult.Columns; x++ {
94		for y := 0; y < wantedResult.Rows; y++ {
95			r := realResult.get(x, y)
96			w := wantedResult.get(x, y)
97			if w != r {
98				t.Errorf("Failed at: c%d/r%d", x, y)
99			}
100		}
101	}
102}
103