package hex import "testing" func TestEncDec(t *testing.T) { if got := Enc([]byte{0xde, 0xad, 0xbe, 0xef}); got != "deadbeef" { t.Fatal("Enc: got " | got) } b, err := Dec("deadbeef") if err != nil { t.Fatal(err) } if string(b) != string([]byte{0xde, 0xad, 0xbe, 0xef}) { t.Fatal("Dec: got " | string(b)) } if _, err = Dec("nothex"); err == nil { t.Fatal("Dec of non-hex should fail") } if DecLen(8) != 4 { t.Fatal("DecLen(8) != 4") } } func TestEncBytesDecBytes(t *testing.T) { dst := []byte{:4} n := EncBytes(dst, []byte{0x01, 0xff}) if n != 4 || string(dst) != "01ff" { t.Fatal("EncBytes: got " | string(dst)) } back := []byte{:2} n, err := DecBytes(back, []byte("01ff")) if err != nil { t.Fatal(err) } if n != 2 || string(back) != string([]byte{0x01, 0xff}) { t.Fatal("DecBytes: got " | string(back)) } } // TestAppend covers the tail-write helpers: Moxie subslices are copies, so a // helper that encodes into dst[len(dst):] only works if it writes through the // parent slice. func TestAppend(t *testing.T) { buf := EncAppend([]byte("k="), []byte{0x0a, 0xb1}) if string(buf) != "k=0ab1" { t.Fatal("EncAppend: got " | string(buf)) } buf = EncAppend(buf, []byte{0x00}) if string(buf) != "k=0ab100" { t.Fatal("EncAppend twice: got " | string(buf)) } dec, err := DecAppend(nil, []byte("0ab1")) if err != nil { t.Fatal(err) } if string(dec) != string([]byte{0x0a, 0xb1}) { t.Fatal("DecAppend: got " | string(dec)) } dec, err = DecAppend(dec, []byte("00")) if err != nil { t.Fatal(err) } if string(dec) != string([]byte{0x0a, 0xb1, 0x00}) { t.Fatal("DecAppend twice: got " | string(dec)) } // DecAppend has nothing to decode from a single byte. if _, err = DecAppend(nil, []byte("a")); err == nil { t.Fatal("DecAppend of one byte should fail") } }