1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 type encDecTest
struct {
18 var encDecTests
= []encDecTest
{
20 {"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}},
21 {"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}},
22 {"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}},
23 {"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}},
25 {"e3a1", []byte{0xe3, 0xa1}},
28 func TestEncode(t
*testing
.T
) {
29 for i
, test
:= range encDecTests
{
30 dst
:= make([]byte, EncodedLen(len(test
.dec
)))
31 n
:= Encode(dst
, test
.dec
)
33 t
.Errorf("#%d: bad return value: got: %d want: %d", i
, n
, len(dst
))
35 if string(dst
) != test
.enc
{
36 t
.Errorf("#%d: got: %#v want: %#v", i
, dst
, test
.enc
)
41 func TestDecode(t
*testing
.T
) {
42 // Case for decoding uppercase hex characters, since
43 // Encode always uses lowercase.
44 decTests
:= append(encDecTests
, encDecTest
{"F8F9FAFBFCFDFEFF", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}})
45 for i
, test
:= range decTests
{
46 dst
:= make([]byte, DecodedLen(len(test
.enc
)))
47 n
, err
:= Decode(dst
, []byte(test
.enc
))
49 t
.Errorf("#%d: bad return value: got:%d want:%d", i
, n
, len(dst
))
50 } else if !bytes
.Equal(dst
, test
.dec
) {
51 t
.Errorf("#%d: got: %#v want: %#v", i
, dst
, test
.dec
)
56 func TestEncodeToString(t
*testing
.T
) {
57 for i
, test
:= range encDecTests
{
58 s
:= EncodeToString(test
.dec
)
60 t
.Errorf("#%d got:%s want:%s", i
, s
, test
.enc
)
65 func TestDecodeString(t
*testing
.T
) {
66 for i
, test
:= range encDecTests
{
67 dst
, err
:= DecodeString(test
.enc
)
69 t
.Errorf("#%d: unexpected err value: %s", i
, err
)
72 if !bytes
.Equal(dst
, test
.dec
) {
73 t
.Errorf("#%d: got: %#v want: #%v", i
, dst
, test
.dec
)
83 var errTests
= []errTest
{
84 {"0", "encoding/hex: odd length hex string"},
85 {"0g", "encoding/hex: invalid byte: U+0067 'g'"},
86 {"00gg", "encoding/hex: invalid byte: U+0067 'g'"},
87 {"0\x01", "encoding/hex: invalid byte: U+0001"},
90 func TestInvalidErr(t
*testing
.T
) {
91 for i
, test
:= range errTests
{
92 dst
:= make([]byte, DecodedLen(len(test
.in
)))
93 _
, err
:= Decode(dst
, []byte(test
.in
))
95 t
.Errorf("#%d: expected error; got none", i
)
96 } else if err
.Error() != test
.err
{
97 t
.Errorf("#%d: got: %v want: %v", i
, err
, test
.err
)
102 func TestInvalidStringErr(t
*testing
.T
) {
103 for i
, test
:= range errTests
{
104 _
, err
:= DecodeString(test
.in
)
106 t
.Errorf("#%d: expected error; got none", i
)
107 } else if err
.Error() != test
.err
{
108 t
.Errorf("#%d: got: %v want: %v", i
, err
, test
.err
)
113 func TestDumper(t
*testing
.T
) {
119 for stride
:= 1; stride
< len(in
); stride
++ {
121 dumper
:= Dumper(&out
)
124 todo
:= done
+ stride
128 dumper
.Write(in
[done
:todo
])
133 if !bytes
.Equal(out
.Bytes(), expectedHexDump
) {
134 t
.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride
, out
.Bytes(), expectedHexDump
)
139 func TestDump(t
*testing
.T
) {
145 out
:= []byte(Dump(in
[:]))
146 if !bytes
.Equal(out
, expectedHexDump
) {
147 t
.Errorf("got:\n%s\nwant:\n%s", out
, expectedHexDump
)
151 var expectedHexDump
= []byte(`00000000 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d |.. !"#$%&'()*+,-|
152 00000010 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d |./0123456789:;<=|
153 00000020 3e 3f 40 41 42 43 44 45 |>?@ABCDE|
158 func BenchmarkEncode(b
*testing
.B
) {
159 for _
, size
:= range []int{256, 1024, 4096, 16384} {
160 src
:= bytes
.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size
/8)
161 sink
= make([]byte, 2*size
)
163 b
.Run(fmt
.Sprintf("%v", size
), func(b
*testing
.B
) {
164 for i
:= 0; i
< b
.N
; i
++ {