more tests

This commit is contained in:
Florian Sundermann 2013-12-12 14:04:21 +01:00
parent 3ddff87dab
commit 49e4105259
2 changed files with 77 additions and 1 deletions

View File

@ -18,14 +18,79 @@ func Test_NewQRCode(t *testing.T) {
} }
} }
func Test_ImageBasics(t *testing.T) { func Test_QRBasics(t *testing.T) {
qr := newBarcode(10) qr := newBarcode(10)
if qr.ColorModel() != color.Gray16Model { if qr.ColorModel() != color.Gray16Model {
t.Fail() t.Fail()
} }
code, _ := Encode("test", L, Unicode)
if code.Content() != "test" {
t.Fail()
}
if code.Metadata().Dimensions != 2 {
t.Fail()
}
bounds := code.Bounds()
if bounds.Min.X != 0 || bounds.Min.Y != 0 || bounds.Max.X != 21 || bounds.Max.Y != 21 {
t.Fail()
}
if code.At(0, 0) != color.Black || code.At(0, 7) != color.White {
t.Fail()
}
qr = code.(*qrcode)
if !qr.Get(0, 0) || qr.Get(0, 7) {
t.Fail()
}
sum := qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
if qr.calcPenalty() != sum {
t.Fail()
}
} }
func Test_Penalty1(t *testing.T) {
qr := newBarcode(7)
if qr.calcPenaltyRule1() != 70 {
t.Fail()
}
qr.Set(0, 0, true)
if qr.calcPenaltyRule1() != 68 {
t.Fail()
}
qr.Set(0, 6, true)
if qr.calcPenaltyRule1() != 66 {
t.Fail()
}
}
func Test_Penalty2(t *testing.T) {
qr := newBarcode(3)
if qr.calcPenaltyRule2() != 12 {
t.Fail()
}
qr.Set(0, 0, true)
qr.Set(1, 1, true)
qr.Set(2, 0, true)
if qr.calcPenaltyRule2() != 0 {
t.Fail()
}
qr.Set(1, 1, false)
if qr.calcPenaltyRule2() != 6 {
t.Fail()
}
}
func Test_Penalty3(t *testing.T) {
runTest := func(content string, result uint) {
code, _ := Encode(content, L, AlphaNumeric)
qr := code.(*qrcode)
if qr.calcPenaltyRule3() != result {
t.Errorf("Failed Penalty Rule 3 for content \"%s\" got %d but expected %d", content, qr.calcPenaltyRule3(), result)
}
}
runTest("A", 80)
runTest("FOO", 40)
runTest("0815", 0)
}
func Test_Penalty4(t *testing.T) { func Test_Penalty4(t *testing.T) {
qr := newBarcode(3) qr := newBarcode(3)

View File

@ -4,6 +4,17 @@ import "testing"
var testvi *versionInfo = &versionInfo{7, M, 0, 1, 10, 2, 5} // Fake versionInfo to run some of the tests var testvi *versionInfo = &versionInfo{7, M, 0, 1, 10, 2, 5} // Fake versionInfo to run some of the tests
func Test_ErrorCorrectionStringer(t *testing.T) {
tests := map[ErrorCorrectionLevel]string{
L: "L", M: "M", Q: "Q", H: "H", ErrorCorrectionLevel(99): "unknown",
}
for ecl, str := range tests {
if ecl.String() != str {
t.Fail()
}
}
}
func Test_CharCountBits(t *testing.T) { func Test_CharCountBits(t *testing.T) {
v1 := &versionInfo{5, M, 0, 0, 0, 0, 0} v1 := &versionInfo{5, M, 0, 0, 0, 0, 0}
v2 := &versionInfo{15, M, 0, 0, 0, 0, 0} v2 := &versionInfo{15, M, 0, 0, 0, 0, 0}