aboutsummaryrefslogtreecommitdiff
path: root/pkg/hex/hex_test.go
blob: 222bd5e9b11539a991766b7a6198dd6b97a55b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package hex

import (
	"bytes"
	"testing"
)

func TestSerialize(t *testing.T) {
	for _, table := range []struct {
		desc  string
		input []byte
		want  string
	}{
		{
			desc:  "valid",
			input: []byte{0, 9, 10, 15, 16, 17, 254, 255},
			want:  "00090a0f1011feff",
		},
	} {
		str := Serialize(table.input)
		if got, want := str, table.want; got != want {
			t.Errorf("got %q but wanted %q in test %q", got, want, table.desc)
		}
	}
}

func TestDeserialize(t *testing.T) {
	for _, table := range []struct {
		desc  string
		input string
		want  []byte
		err   bool
	}{
		{
			desc:  "invalid: length is odd",
			input: "0",
			err:   true,
		},
		{
			desc:  "invalid: even index has invalid character",
			input: "A0",
			err:   true,
		},
		{
			desc:  "invalid: odd index has invalid character",
			input: "0A",
			err:   true,
		},
		{
			desc:  "valid",
			input: "00090a0f1011feff",
			want:  []byte{0, 9, 10, 15, 16, 17, 254, 255},
		},
	} {
		buf, err := Deserialize(table.input)
		if got, want := err != nil, table.err; got != want {
			t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
		}
		if err != nil {
			continue
		}
		if got, want := buf, table.want; !bytes.Equal(got, want) {
			t.Errorf("got %v but wanted %v in test %q", got, want, table.desc)
		}
	}
}