...

Source file src/github.com/boombuler/barcode/aztec/encoder_test.go

Documentation: github.com/boombuler/barcode/aztec

     1  package aztec
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/boombuler/barcode/utils"
     8  )
     9  
    10  func Test_StuffBits(t *testing.T) {
    11  	testStuffBits := func(wordSize int, bits string, expected string) {
    12  		bl := new(utils.BitList)
    13  		for _, r := range bits {
    14  			if r == 'X' {
    15  				bl.AddBit(true)
    16  			} else if r == '.' {
    17  				bl.AddBit(false)
    18  			}
    19  		}
    20  		stuffed := stuffBits(bl, wordSize)
    21  		expectedBits := strings.Replace(expected, " ", "", -1)
    22  		result := bitStr(stuffed)
    23  
    24  		if result != expectedBits {
    25  			t.Errorf("stuffBits failed for %q\nGot: %q", bits, result)
    26  		}
    27  	}
    28  
    29  	testStuffBits(5, ".X.X. X.X.X .X.X.",
    30  		".X.X. X.X.X .X.X.")
    31  	testStuffBits(5, ".X.X. ..... .X.X",
    32  		".X.X. ....X ..X.X")
    33  	testStuffBits(3, "XX. ... ... ..X XXX .X. ..",
    34  		"XX. ..X ..X ..X ..X .XX XX. .X. ..X")
    35  	testStuffBits(6, ".X.X.. ...... ..X.XX",
    36  		".X.X.. .....X. ..X.XX XXXX.")
    37  	testStuffBits(6, ".X.X.. ...... ...... ..X.X.",
    38  		".X.X.. .....X .....X ....X. X.XXXX")
    39  	testStuffBits(6, ".X.X.. XXXXXX ...... ..X.XX",
    40  		".X.X.. XXXXX. X..... ...X.X XXXXX.")
    41  	testStuffBits(6,
    42  		"...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX ..X... ....X. X..XXX X.XX.X",
    43  		".....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X..X.. .....X X.X..X XXX.XX .XXXXX")
    44  }
    45  
    46  func Test_ModeMessage(t *testing.T) {
    47  	testModeMessage := func(compact bool, layers, words int, expected string) {
    48  		result := bitStr(generateModeMessage(compact, layers, words))
    49  		expectedBits := strings.Replace(expected, " ", "", -1)
    50  		if result != expectedBits {
    51  			t.Errorf("generateModeMessage(%v, %d, %d) failed.\nGot:%s", compact, layers, words, result)
    52  		}
    53  	}
    54  	testModeMessage(true, 2, 29, ".X .XXX.. ...X XX.. ..X .XX. .XX.X")
    55  	testModeMessage(true, 4, 64, "XX XXXXXX .X.. ...X ..XX .X.. XX..")
    56  	testModeMessage(false, 21, 660, "X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... ..XXX")
    57  	testModeMessage(false, 32, 4096, "XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X.XXX")
    58  }
    59  

View as plain text