-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance_test.go
161 lines (132 loc) · 3.59 KB
/
distance_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package units_test
/**
* Copyright (c) 2024, Starboard Maritime Intelligence
* All rights reserved. Use is subject to License terms.
* See LICENSE in the root directory of this source tree.
*/
import (
"fmt"
"testing"
"math/rand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/starboard-nz/units"
)
const δ = 0.000001
func TestDistance(t *testing.T) {
t.Run("NauticalMiles", func(t *testing.T) {
d := units.Metre(10000)
assert.InDelta(t, 5.399568034557236, float64(d.NM()), δ)
})
t.Run("Miles", func(t *testing.T) {
d := units.NM(5.399568034557236)
assert.InDelta(t, 6.2137119223733395, float64(d.Mile()), δ)
})
t.Run("Kms", func(t *testing.T) {
d := units.Km(235)
assert.InDelta(t, 146.0222301757, float64(d.Mile()), δ)
})
}
func TestParseDistance(t *testing.T) {
var testData = map[string]units.Distance{
"-10.4m": units.Metre(-10.4),
"32 inch": units.Metre(32 * units.InchInMetres),
"120km": units.Km(120),
"23.6mile": units.Mile(23.6),
"-17.3\"": units.Metre(-17.3 * units.InchInMetres),
".5'": units.Metre(0.5 * units.FootInMetres),
"79.3NM": units.NM(79.3),
}
for str, exp := range testData {
d, err := units.ParseDistance(str)
require.NoError(t, err)
assert.InDelta(t, float64(exp.Metre()), float64(d.Metre()), δ, str)
assert.InDelta(t, float64(exp.Meter()), float64(d.Meter()), δ, str)
assert.InDelta(t, float64(exp.Km()), float64(d.Km()), δ, str)
assert.InDelta(t, float64(exp.NM()), float64(d.NM()), δ, str)
assert.InDelta(t, float64(exp.Mile()), float64(d.Mile()), δ, str)
tExp := fmt.Sprintf("%T", exp)
tUnit := fmt.Sprintf("%T", d)
assert.Equal(t, tExp, tUnit)
}
var errTests = []string{"hello 6' world", "0.1.2m", "--123NM", "42"}
for _, str := range errTests {
_, err := units.ParseDistance(str)
assert.Error(t, err)
}
}
func randomDistanceConversion(d units.Distance) units.Distance {
u := rand.Intn(5)
switch u {
case 0:
return d.Metre()
case 1:
return d.Meter()
case 2:
return d.Km()
case 3:
return d.NM()
case 4:
return d.Mile()
}
return d.Metre()
}
func randomDistance() units.Distance {
val := rand.Float64()*200000 - 100000 // nolint:gosec
u := rand.Intn(5)
switch u {
case 0:
return units.Metre(val)
case 1:
return units.Meter(val)
case 2:
return units.Km(val)
case 3:
return units.NM(val)
case 4:
return units.Mile(val)
}
return units.Metre(0)
}
func TestDistanceRandom(t *testing.T) {
const N = 10000
for i := 0; i < N; i++ {
d0 := randomDistance()
d := d0
// do 5 random conversions
for j := 0; j < 5; j++ {
d = randomDistanceConversion(d)
}
assert.InDelta(t, float64(d0.Metre()), float64(d.Metre()), δ)
assert.InDelta(t, float64(d0.Meter()), float64(d.Meter()), δ)
assert.InDelta(t, float64(d0.Km()), float64(d.Km()), δ)
assert.InDelta(t, float64(d0.NM()), float64(d.NM()), δ)
assert.InDelta(t, float64(d0.Mile()), float64(d.Mile()), δ)
}
}
func TestDistanceString(t *testing.T) {
var testData = map[units.Distance]string{
units.Metre(-10.4): "-10.40 m",
units.Km(120): "120.00 km",
units.Mile(23.6): "23.60 mi",
units.Meter(0.5): "0.50 m",
units.NM(79.3): "79.30 NM",
}
for d, exp := range testData {
str := fmt.Sprintf("%v", d)
assert.Equal(t, exp, str)
}
}
func TestDistanceShort(t *testing.T) {
var testData = map[units.Distance]string{
units.Metre(-10.4): "-10 m",
units.Km(120): "120 km",
units.Mile(23.6): "24 mi",
units.Meter(0.5): "0 m",
units.NM(79.3): "79 NM",
}
for d, exp := range testData {
str := fmt.Sprintf("%.0f %s", d, d.Short())
assert.Equal(t, exp, str)
}
}