package modhex import ( "bytes" "testing" ) type encDecTest struct { enc string dec []byte } var encDecTests = []encDecTest{ {"", []byte{}}, {"dteffuje", []byte{0x2d, 0x34, 0x4e, 0x83}}, { "hknhfjbrjnlnldnhcujvddbikngjrtgh", []byte{0x69, 0xb6, 0x48, 0x1c, 0x8b, 0xab, 0xa2, 0xb6, 0x0e, 0x8f, 0x22, 0x17, 0x9b, 0x58, 0xcd, 0x56}, }, { "urtubjtnuihvntcreeeecvbregfjibtn", []byte{0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f, 0xbd, 0x0c, 0x33, 0x33, 0x0f, 0x1c, 0x35, 0x48, 0x71, 0xdb}, }, } func TestEncode(t *testing.T) { for i, test := range encDecTests { dst := make([]byte, EncodedLen(len(test.dec))) n := Encode(dst, test.dec) if n != len(dst) { t.Errorf("#%d: bad return value: got %d want: %d", i, n, len(dst)) } if string(dst) != test.enc { t.Errorf("#%d: got: %#v want %#v", i, dst, []byte(test.enc)) } } } func TestDecode(t *testing.T) { for i, test := range encDecTests { dst := make([]byte, DecodedLen(len(test.enc))) n, err := Decode(dst, []byte(test.enc)) if err != nil { t.Errorf("#%d: unexpected err value: %s", i, err) continue } if n != len(dst) { t.Errorf("#%d: bad return value got: %d want:%d", i, n, len(dst)) } if !bytes.Equal(dst, test.dec) { t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec) } } } func TestEncodeToString(t *testing.T) { for i, test := range encDecTests { s := EncodeToString(test.dec) if s != test.enc { t.Errorf("#%d: got: %s want: %s", i, s, test.enc) } } } func TestDecodeString(t *testing.T) { for i, test := range encDecTests { dst, err := DecodeString(test.enc) if err != nil { t.Errorf("#%d: unexpected err value: %s", i, err) continue } if !bytes.Equal(dst, test.dec) { t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec) } } }