123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- // go-qrcode
- // Copyright 2014 Tom Harwood
- package qrcode
- import "testing"
- func TestSymbolBasic(t *testing.T) {
- size := 10
- quietZoneSize := 4
- m := newSymbol(size, quietZoneSize)
- if m.size != size+quietZoneSize*2 {
- t.Errorf("Symbol size is %d, expected %d", m.size, size+quietZoneSize*2)
- }
- for i := 0; i < size; i++ {
- for j := 0; j < size; j++ {
- v := m.get(i, j)
- if v != false {
- t.Errorf("New symbol not empty")
- }
- if !m.empty(i, j) {
- t.Errorf("New symbol is not empty")
- }
- value := i*j%2 == 0
- m.set(i, j, value)
- v = m.get(i, j)
- if v != value {
- t.Errorf("Symbol ignores set bits")
- }
- if m.empty(i, j) {
- t.Errorf("Symbol ignores set bits")
- }
- }
- }
- }
- func TestSymbolPenalties(t *testing.T) {
- tests := []struct {
- pattern [][]bool
- expectedPenalty1 int
- expectedPenalty2 int
- expectedPenalty3 int
- expectedPenalty4 int
- }{
- {
- [][]bool{
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- },
- 0, // No adjacent modules of same color.
- 0, // No 2x2+ sized blocks.
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- },
- 0, // 5 adjacent modules of same colour, score = 0.
- 0, // No 2x2+ sized blocks.
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- },
- 4, // 6 adjacent modules of same colour, score = 3 + (6-5)
- 0, // No 2x2+ sized blocks.
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0},
- {b1, b0, b1, b0, b1, b0, b1},
- {b1, b0, b0, b0, b0, b0, b1},
- {b1, b0, b1, b0, b1, b0, b1},
- {b1, b0, b0, b0, b0, b0, b1},
- {b1, b0, b1, b0, b1, b0, b1},
- {b1, b0, b0, b0, b0, b0, b0},
- },
- 28, // 3+(7-5) + 3+(6-5) + 3+(6-5) + 3+(6-5) + 3+(7-5) + 3+(7-5) = 28
- 0, // No 2x2+ sized blocks.
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b1, b0, b1},
- {b0, b0, b1, b0, b1, b0},
- {b0, b1, b0, b1, b0, b1},
- {b1, b0, b1, b1, b1, b0},
- {b0, b1, b1, b1, b0, b1},
- {b1, b0, b1, b0, b1, b0},
- },
- -1,
- 6, // 3*(2-1)*(2-1) + 3(2-1)*(2-1)
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- },
- -1,
- 60, // 3 * (5-1) * (6-1)
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b1},
- {b0, b0, b0, b0, b0, b1},
- {b1, b1, b0, b1, b0, b1},
- {b1, b1, b0, b1, b0, b1},
- {b1, b1, b0, b1, b0, b1},
- {b1, b1, b0, b1, b0, b1},
- },
- -1,
- 21, // 3*(5-1)*(2-1) + 3*(2-1)*(4-1) = 3*4 + 3*3
- 0, // No 1:1:3:1:1 pattern.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- {b0, b0, b0, b0, b1, b0, b1, b1, b1, b0, b1, b0},
- },
- -1,
- -1,
- 480, // 12* 1:1:3:1:1 patterns, 12 * 40.
- -1,
- },
- {
- [][]bool{
- {b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b1, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- },
- -1,
- -1,
- 80, // 2* 1:1:3:1:1 patterns, 2 * 40.
- -1,
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- },
- -1,
- -1,
- -1,
- 100, // 10 * (10 steps of 5% deviation from 50% black/white).
- },
- {
- [][]bool{
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- },
- -1,
- -1,
- -1,
- 100, // 10 * (10 steps of 5% deviation from 50% black/white).
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- },
- -1,
- -1,
- -1,
- 0, // Exactly 50%/50% black/white.
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- },
- -1,
- -1,
- -1,
- 20, // 10 * (2 steps of 5% deviation towards white).
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- },
- -1,
- -1,
- -1,
- 30, // 10 * (3 steps of 5% deviation towards white).
- },
- {
- [][]bool{
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b0},
- {b0, b0, b0, b0, b0, b0, b0, b0, b0, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- {b1, b1, b1, b1, b1, b1, b1, b1, b1, b1},
- },
- -1,
- -1,
- -1,
- 30, // 10 * (3 steps of 5% deviation towards white).
- },
- }
- for i, test := range tests {
- s := newSymbol(len(test.pattern[0]), 4)
- s.set2dPattern(0, 0, test.pattern)
- penalty1 := s.penalty1()
- penalty2 := s.penalty2()
- penalty3 := s.penalty3()
- penalty4 := s.penalty4()
- ok := true
- if test.expectedPenalty1 != -1 && test.expectedPenalty1 != penalty1 {
- ok = false
- }
- if test.expectedPenalty2 != -1 && test.expectedPenalty2 != penalty2 {
- ok = false
- }
- if test.expectedPenalty3 != -1 && test.expectedPenalty3 != penalty3 {
- ok = false
- }
- if test.expectedPenalty4 != -1 && test.expectedPenalty4 != penalty4 {
- ok = false
- }
- if !ok {
- t.Fatalf("Penalty test #%d p1=%d, p2=%d, p3=%d, p4=%d (expected p1=%d, p2=%d, p3=%d, p4=%d)", i, penalty1, penalty2, penalty3, penalty4,
- test.expectedPenalty1, test.expectedPenalty2, test.expectedPenalty3,
- test.expectedPenalty4)
- }
- }
- }
|