github.com/muyo/sno@v1.2.1/internal/encoding_test.go (about) 1 package internal 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func testEncoding(t *testing.T) { 9 runEncodingWithFallback("encode", t, testEncodingEncode) 10 runEncodingWithFallback("decode", t, testEncodingDecode) 11 } 12 13 var encdec = [...]struct { 14 dec string 15 enc [10]byte 16 }{ 17 {"2222222222222222", [10]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, 18 {"brpk4q72xwf2m63l", [10]byte{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}}, 19 {"xxxxxxxxxxxxxxxx", [10]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255}}, 20 } 21 22 func testEncodingEncode(t *testing.T) { 23 for _, c := range encdec { 24 var ( 25 actual = Encode(&c.enc) 26 expected = []byte(c.dec) 27 ) 28 29 if !bytes.Equal(actual[:], expected) { 30 t.Errorf("expected [%s], got [%s]", expected, actual) 31 } 32 } 33 } 34 35 func testEncodingDecode(t *testing.T) { 36 for _, c := range encdec { 37 var ( 38 actual = Decode([]byte(c.dec)) 39 expected = c.enc 40 ) 41 42 if actual != expected { 43 t.Errorf("expected [%v], got [%v]", expected, actual) 44 } 45 } 46 } 47 48 func runEncodingWithFallback(name string, t *testing.T, f func(t *testing.T)) { 49 t.Run(name, func(t *testing.T) { 50 var actualVectorSupport = hasVectorSupport 51 if actualVectorSupport { 52 t.Run("vectorized", f) 53 } 54 55 hasVectorSupport = false 56 t.Run("fallback", f) 57 hasVectorSupport = actualVectorSupport 58 }) 59 }