forked from bcicen/go-units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits_test.go
93 lines (80 loc) · 1.99 KB
/
units_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
87
88
89
90
91
92
93
package units
import (
"testing"
"github.com/stretchr/testify/assert"
)
// aggregate all unit names, aliases, etc
func aggrNames() (a []string) {
for _, u := range All() {
a = append(a, u.Names()...)
}
return a
}
// aggregate units by quantity
func aggrByQuantity() map[string][]Unit {
m := make(map[string][]Unit)
for _, u := range All() {
if _, ok := m[u.Quantity]; !ok {
m[u.Quantity] = []Unit{}
}
m[u.Quantity] = append(m[u.Quantity], u)
}
return m
}
func TestUnitLookup(t *testing.T) {
for _, name := range aggrNames() {
u, err := Find(name)
if err != nil {
t.Errorf(err.Error())
continue
}
t.Logf("found unit by name: %s (%s)", name, u.Name)
}
}
func TestUnitNameOverlap(t *testing.T) {
nameMap := make(map[string]Unit)
var total, failed int
for _, u := range nameMap {
for _, name := range u.Names() {
if existing, ok := nameMap[name]; ok {
t.Errorf("overlap in unit names: %s, %s (%s)", u.Name, existing.Name, name)
failed++
} else {
nameMap[name] = u
}
total++
}
}
t.Logf("tested %d unit names, %d overlaps", total, failed)
}
func TestSimilarSymbolLookup(t *testing.T) {
// The casing of gb could match both gigabyte (GB) and gigabit (Gb)
symbol := "gb"
// Run the same assertion multiple times to ensure there is no inconsistency to the results based on random map ordering
for range make([]bool, 100) {
u, err := Find(symbol)
if err != nil {
t.Errorf("failed to find unit for symbol %s", symbol)
}
assert.Equal(t, GigaBit, u)
}
}
// ensure all units within the same quantity resolve
// a conversion path
func TestPathResolve(t *testing.T) {
for qname, qunits := range aggrByQuantity() {
t.Logf("testing conversion paths for quantity: %s", qname)
for _, u1 := range qunits {
v1 := NewValue(1.0, u1)
for _, u2 := range qunits {
if u1.Name == u2.Name {
continue
}
_, err := v1.Convert(u2)
if err != nil {
t.Errorf("failed to resolve path: %s -> %s", u1.Name, u2.Name)
}
}
}
}
}