From 5ce95fe490f66bde5b805abac7fe2786166b482b Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 6 Feb 2019 17:00:48 +0700 Subject: lib/bytes: add function to convert hexadecimal into byte --- lib/bytes/bytes_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'lib/bytes/bytes_test.go') diff --git a/lib/bytes/bytes_test.go b/lib/bytes/bytes_test.go index 3b79aced..2e1b3a24 100644 --- a/lib/bytes/bytes_test.go +++ b/lib/bytes/bytes_test.go @@ -164,6 +164,52 @@ func TestIsTokenAt(t *testing.T) { } } +func TestReadHexByte(t *testing.T) { + cases := []struct { + in []byte + exp byte + expOK bool + }{{ + in: []byte{}, + }, { + in: []byte("x0"), + }, { + in: []byte("0x"), + }, { + in: []byte("00"), + expOK: true, + }, { + in: []byte("01"), + exp: 1, + expOK: true, + }, { + in: []byte("10"), + exp: 16, + expOK: true, + }, { + in: []byte("1A"), + exp: 26, + expOK: true, + }, { + in: []byte("1a"), + exp: 26, + expOK: true, + }, { + in: []byte("a1"), + exp: 161, + expOK: true, + }} + + for _, c := range cases { + t.Log(c.in) + + got, ok := ReadHexByte(c.in, 0) + + test.Assert(t, "b", c.exp, got, true) + test.Assert(t, "ok", c.expOK, ok, true) + } +} + func TestSkipAfterToken(t *testing.T) { line := []byte(`abc \def ghi`) -- cgit v1.3-6-g1900