git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/barcode/qr/alphanumeric_test.go (about) 1 package qr 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func makeString(length int, content string) string { 9 res := "" 10 11 for i := 0; i < length; i++ { 12 res += content 13 } 14 15 return res 16 } 17 18 func Test_AlphaNumericEncoding(t *testing.T) { 19 encode := AlphaNumeric.getEncoder() 20 21 x, vi, err := encode("HELLO WORLD", M) 22 23 if x == nil || vi == nil || vi.Version != 1 || bytes.Compare(x.GetBytes(), []byte{32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17}) != 0 { 24 t.Errorf("\"HELLO WORLD\" failed to encode: %s", err) 25 } 26 27 x, vi, err = encode(makeString(4296, "A"), L) 28 if x == nil || vi == nil || err != nil { 29 t.Fail() 30 } 31 x, vi, err = encode(makeString(4297, "A"), L) 32 if x != nil || vi != nil || err == nil { 33 t.Fail() 34 } 35 x, vi, err = encode("ABc", L) 36 if x != nil || vi != nil || err == nil { 37 t.Fail() 38 } 39 x, vi, err = encode("hello world", M) 40 41 if x != nil || vi != nil || err == nil { 42 t.Error("\"hello world\" should not be encodable in alphanumeric mode") 43 } 44 }