fixed bug in ean encoder and added some tests
This commit is contained in:
parent
b63e15f7ff
commit
e8eca3ad75
|
@ -78,14 +78,14 @@ var encoderTable map[rune]encodedNumber = map[rune]encodedNumber{
|
||||||
}
|
}
|
||||||
|
|
||||||
func runeToInt(r rune) int {
|
func runeToInt(r rune) int {
|
||||||
if r >= '0' || r <= '9' {
|
if r >= '0' && r <= '9' {
|
||||||
return int(r - '0')
|
return int(r - '0')
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func intToRune(i int) rune {
|
func intToRune(i int) rune {
|
||||||
if i >= 0 || i <= 9 {
|
if i >= 0 && i <= 9 {
|
||||||
return rune(i + '0')
|
return rune(i + '0')
|
||||||
}
|
}
|
||||||
return 'F'
|
return 'F'
|
||||||
|
|
|
@ -5,19 +5,63 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_EncodeEAN13(t *testing.T) {
|
func testHelper(t *testing.T, testCode, testResult, kind string, checkMetadata bool) {
|
||||||
testResult := "10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101"
|
|
||||||
testCode := "5901234123457"
|
|
||||||
code, err := Encode(testCode)
|
code, err := Encode(testCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if code.Metadata().Dimensions != 1 || code.Content() != testCode || code.Metadata().CodeKind != "EAN 13" {
|
if checkMetadata && (code.Metadata().Dimensions != 1 || code.Content() != testCode || code.Metadata().CodeKind != kind) {
|
||||||
t.Error("Metadata missmatch")
|
t.Error("Metadata missmatch")
|
||||||
}
|
}
|
||||||
|
if len(testResult) != code.Bounds().Max.X {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
for i, r := range testResult {
|
for i, r := range testResult {
|
||||||
if (code.At(i, 0) == color.Black) != (r == '1') {
|
if (code.At(i, 0) == color.Black) != (r == '1') {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_EncodeEAN(t *testing.T) {
|
||||||
|
testHelper(t, "5901234123457", "10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101", "EAN 13", true)
|
||||||
|
testHelper(t, "55123457", "1010110001011000100110010010011010101000010101110010011101000100101", "EAN 8", true)
|
||||||
|
testHelper(t, "5512345", "1010110001011000100110010010011010101000010101110010011101000100101", "EAN 8", false)
|
||||||
|
_, err := Encode("55123458") //<-- Invalid checksum
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Invalid checksum not detected")
|
||||||
|
}
|
||||||
|
_, err = Encode("invalid")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("\"invalid\" should not be encodable")
|
||||||
|
}
|
||||||
|
_, err = Encode("invalid")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("\"invalid\" should not be encodable")
|
||||||
|
}
|
||||||
|
bits := encodeEAN13("invalid error")
|
||||||
|
if bits != nil {
|
||||||
|
t.Error("\"invalid error\" should not be encodable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_RuneToIntIntToRune(t *testing.T) {
|
||||||
|
if intToRune(0) != '0' {
|
||||||
|
t.Errorf("failed intToRune(0) returned %d", string(intToRune(0)))
|
||||||
|
}
|
||||||
|
if intToRune(9) != '9' {
|
||||||
|
t.Errorf("failed intToRune(9) returned %d", intToRune(9))
|
||||||
|
}
|
||||||
|
if intToRune(10) != 'F' {
|
||||||
|
t.Errorf("failed intToRune(10) returned %d", intToRune(10))
|
||||||
|
}
|
||||||
|
if runeToInt('0') != 0 {
|
||||||
|
t.Error("failed runeToInt('0') returned %d", runeToInt(0))
|
||||||
|
}
|
||||||
|
if runeToInt('9') != 9 {
|
||||||
|
t.Error("failed runeToInt('9') returned %d", runeToInt(9))
|
||||||
|
}
|
||||||
|
if runeToInt('F') != -1 {
|
||||||
|
t.Error("failed runeToInt('F') returned %d", runeToInt('F'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue