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.
12
type encodeTest struct {
16
var encodeTests = []encodeTest{
18
{[]byte{0x01}, []byte{'0', '1'}},
19
{[]byte{0xff}, []byte{'f', 'f'}},
20
{[]byte{0xff, 00}, []byte{'f', 'f', '0', '0'}},
21
{[]byte{0}, []byte{'0', '0'}},
22
{[]byte{1}, []byte{'0', '1'}},
23
{[]byte{2}, []byte{'0', '2'}},
24
{[]byte{3}, []byte{'0', '3'}},
25
{[]byte{4}, []byte{'0', '4'}},
26
{[]byte{5}, []byte{'0', '5'}},
27
{[]byte{6}, []byte{'0', '6'}},
28
{[]byte{7}, []byte{'0', '7'}},
29
{[]byte{8}, []byte{'0', '8'}},
30
{[]byte{9}, []byte{'0', '9'}},
31
{[]byte{10}, []byte{'0', 'a'}},
32
{[]byte{11}, []byte{'0', 'b'}},
33
{[]byte{12}, []byte{'0', 'c'}},
34
{[]byte{13}, []byte{'0', 'd'}},
35
{[]byte{14}, []byte{'0', 'e'}},
36
{[]byte{15}, []byte{'0', 'f'}},
39
func TestEncode(t *testing.T) {
40
for i, test := range encodeTests {
41
dst := make([]byte, EncodedLen(len(test.in)))
42
n := Encode(dst, test.in)
44
t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst))
46
if bytes.Compare(dst, test.out) != 0 {
47
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.out)
52
type decodeTest struct {
57
var decodeTests = []decodeTest{
58
{[]byte{}, []byte{}, true},
59
{[]byte{'0'}, []byte{}, false},
60
{[]byte{'0', 'g'}, []byte{}, false},
61
{[]byte{'0', '\x01'}, []byte{}, false},
62
{[]byte{'0', '0'}, []byte{0}, true},
63
{[]byte{'0', '1'}, []byte{1}, true},
64
{[]byte{'0', '2'}, []byte{2}, true},
65
{[]byte{'0', '3'}, []byte{3}, true},
66
{[]byte{'0', '4'}, []byte{4}, true},
67
{[]byte{'0', '5'}, []byte{5}, true},
68
{[]byte{'0', '6'}, []byte{6}, true},
69
{[]byte{'0', '7'}, []byte{7}, true},
70
{[]byte{'0', '8'}, []byte{8}, true},
71
{[]byte{'0', '9'}, []byte{9}, true},
72
{[]byte{'0', 'a'}, []byte{10}, true},
73
{[]byte{'0', 'b'}, []byte{11}, true},
74
{[]byte{'0', 'c'}, []byte{12}, true},
75
{[]byte{'0', 'd'}, []byte{13}, true},
76
{[]byte{'0', 'e'}, []byte{14}, true},
77
{[]byte{'0', 'f'}, []byte{15}, true},
78
{[]byte{'0', 'A'}, []byte{10}, true},
79
{[]byte{'0', 'B'}, []byte{11}, true},
80
{[]byte{'0', 'C'}, []byte{12}, true},
81
{[]byte{'0', 'D'}, []byte{13}, true},
82
{[]byte{'0', 'E'}, []byte{14}, true},
83
{[]byte{'0', 'F'}, []byte{15}, true},
86
func TestDecode(t *testing.T) {
87
for i, test := range decodeTests {
88
dst := make([]byte, DecodedLen(len(test.in)))
89
n, err := Decode(dst, test.in)
90
if err == nil && n != len(dst) {
91
t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst))
93
if test.ok != (err == nil) {
94
t.Errorf("#%d: unexpected err value: %s", i, err)
96
if err == nil && bytes.Compare(dst, test.out) != 0 {
97
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.out)
102
type encodeStringTest struct {
107
var encodeStringTests = []encodeStringTest{
110
{[]byte{0, 1}, "0001"},
111
{[]byte{0, 1, 255}, "0001ff"},
114
func TestEncodeToString(t *testing.T) {
115
for i, test := range encodeStringTests {
116
s := EncodeToString(test.in)
118
t.Errorf("#%d got:%s want:%s", i, s, test.out)
123
type decodeStringTest struct {
129
var decodeStringTests = []decodeStringTest{
130
{"", []byte{}, true},
131
{"0", []byte{}, false},
132
{"00", []byte{0}, true},
133
{"0\x01", []byte{}, false},
134
{"0g", []byte{}, false},
135
{"00ff00", []byte{0, 255, 0}, true},
136
{"0000ff", []byte{0, 0, 255}, true},
139
func TestDecodeString(t *testing.T) {
140
for i, test := range decodeStringTests {
141
dst, err := DecodeString(test.in)
142
if test.ok != (err == nil) {
143
t.Errorf("#%d: unexpected err value: %s", i, err)
145
if err == nil && bytes.Compare(dst, test.out) != 0 {
146
t.Errorf("#%d: got: %#v want: #%v", i, dst, test.out)