-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeHexChar_test.go
38 lines (34 loc) · 961 Bytes
/
DecodeHexChar_test.go
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
package convert
import (
"testing"
)
func TestDecodeHexChar(t *testing.T) {
tests := []struct {
hexByte byte
expected byte
shouldError bool
}{
{hexByte: '0', expected: 0, shouldError: false},
{hexByte: '5', expected: 5, shouldError: false},
{hexByte: 'A', expected: 10, shouldError: false},
{hexByte: 'f', expected: 15, shouldError: false},
{hexByte: 'x', shouldError: true}, // Invalid hexadecimal digit
{hexByte: '@', shouldError: true}, // Invalid hexadecimal digit
}
for _, test := range tests {
var result byte
ok := DecodeHexChar(&result, test.hexByte)
if test.shouldError {
if ok {
t.Fatalf("Expected failure on input '%c', but got ok", test.hexByte)
}
} else {
if !ok {
t.Fatalf("Expected ok but failed on '%c'", test.hexByte)
} else if result != test.expected {
t.Fatalf("Unexpected result for input '%c'. Expected: %d, Got: %d",
test.hexByte, test.expected, result)
}
}
}
}