From 49e4105259111da534fa91d1ff6ea20bfa100150 Mon Sep 17 00:00:00 2001 From: Florian Sundermann Date: Thu, 12 Dec 2013 14:04:21 +0100 Subject: [PATCH] more tests --- qr/qrcode_test.go | 67 +++++++++++++++++++++++++++++++++++++++++- qr/versioninfo_test.go | 11 +++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/qr/qrcode_test.go b/qr/qrcode_test.go index a661622..8cc17e3 100644 --- a/qr/qrcode_test.go +++ b/qr/qrcode_test.go @@ -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) if qr.ColorModel() != color.Gray16Model { 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) { qr := newBarcode(3) diff --git a/qr/versioninfo_test.go b/qr/versioninfo_test.go index ec0237d..28f2a2f 100644 --- a/qr/versioninfo_test.go +++ b/qr/versioninfo_test.go @@ -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 +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) { v1 := &versionInfo{5, M, 0, 0, 0, 0, 0} v2 := &versionInfo{15, M, 0, 0, 0, 0, 0}