-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_test.go
86 lines (74 loc) · 1.63 KB
/
math_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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import "testing"
func TestIsHex(t *testing.T) {
var cases = []struct {
hex string
exp bool
}{
{"0x0", true},
{"0x1", true},
{"123", false},
{"x1", false},
{"", false},
}
for _, c := range cases {
act := IsHex(c.hex)
if act != c.exp {
t.Errorf("Expected IsHex(%q) to return %t, %t given", c.hex, c.exp, act)
}
}
}
func TestHexToDec(t *testing.T) {
var cases = []struct {
hex string
exp string
err bool
}{
{"0x0", "0", false},
{"0x00", "0", false},
{"0x01", "1", false},
{"0x10", "16", false},
{"0x", "", true},
{"hello", "", true},
{"", "", true},
}
for _, c := range cases {
act, err := HexToDec(c.hex)
if err == nil && c.err {
t.Errorf("Expected HexToDec(%q) to return error, none given.", c.hex)
continue
} else if err != nil && !c.err {
t.Errorf("Expected HexToDec(%q) to return %q, error given: %v", c.hex, c.exp, err)
continue
}
if act != c.exp {
t.Errorf("Expected HexToDec(%q) to return %q, %q given", c.hex, c.exp, act)
}
}
}
func TestDecToHex(t *testing.T) {
var cases = []struct {
dec string
exp string
err bool
}{
{"0", "0x0", false},
{"1", "0x1", false},
{"16", "0x10", false},
{"hello", "", true},
{"", "", true},
}
for _, c := range cases {
act, err := DecToHex(c.dec)
if err == nil && c.err {
t.Errorf("Expected DecToHex(%q) to return error, none given.", c.dec)
continue
} else if err != nil && !c.err {
t.Errorf("Expected DecToHex(%q) to return %q, error given: %v", c.dec, c.exp, err)
continue
}
if act != c.exp {
t.Errorf("Expected DecToHex(%q) to return %q, %q given", c.dec, c.exp, act)
}
}
}