git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/barcode/twooffive/encoder_test.go (about)

     1  package twooffive
     2  
     3  import (
     4  	"image/color"
     5  	"testing"
     6  )
     7  
     8  func Test_AddCheckSum(t *testing.T) {
     9  	if sum, err := AddCheckSum("1234567"); err != nil || sum != "12345670" {
    10  		t.Fail()
    11  	}
    12  	if _, err := AddCheckSum("1ABC"); err == nil {
    13  		t.Fail()
    14  	}
    15  	if _, err := AddCheckSum(""); err == nil {
    16  		t.Fail()
    17  	}
    18  }
    19  
    20  func Test_Encode(t *testing.T) {
    21  	_, err := Encode("FOOBAR", false)
    22  	if err == nil {
    23  		t.Error("\"FOOBAR\" should not be encodable")
    24  	}
    25  
    26  	testEncode := func(interleaved bool, txt, testResult string) {
    27  		code, err := Encode(txt, interleaved)
    28  		if err != nil || code == nil {
    29  			t.Fail()
    30  		} else {
    31  			if code.Bounds().Max.X != len(testResult) {
    32  				t.Errorf("%v: length missmatch! %v != %v", txt, code.Bounds().Max.X, len(testResult))
    33  			} else {
    34  				for i, r := range testResult {
    35  					if (code.At(i, 0) == color.Black) != (r == '1') {
    36  						t.Errorf("%v: code missmatch on position %d", txt, i)
    37  					}
    38  				}
    39  			}
    40  		}
    41  	}
    42  
    43  	testEncode(false, "12345670", "1101101011101010101110101110101011101110111010101010101110101110111010111010101011101110101010101011101110101011101110101101011")
    44  	testEncode(true, "12345670", "10101110100010101110001110111010001010001110100011100010101010100011100011101101")
    45  }