File: types_test.go

package info (click to toggle)
golang-github-adrianmo-go-nmea 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: makefile: 15
file content (306 lines) | stat: -rw-r--r-- 6,890 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package nmea

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

var nearDistance = 0.001

func TestParseLatLong(t *testing.T) {
	var tests = []struct {
		value    string
		expected float64
		err      bool
	}{
		{"33\u00B0 12' 34.3423\"", 33.209540, false}, // dms
		{"3345.1232 N", 33.752054, false},            // gps
		{"151.234532", 151.234532, false},            // decimal
	}
	for _, tt := range tests {
		t.Run(tt.value, func(t *testing.T) {
			l, err := ParseLatLong(tt.value)
			if tt.err {
				assert.Error(t, err)
			} else {
				assert.InDelta(t, tt.expected, l, nearDistance)
			}
		})
	}
}

func TestParseGPS(t *testing.T) {
	var tests = []struct {
		value    string
		expected float64
		err      bool
	}{
		{"3345.1232 N", 33.752054, false},
		{"15145.9877 S", -151.76646, false},
		{"12345.1234 X", 0, true},
		{"1234.1234", 0, true},
	}
	for _, tt := range tests {
		t.Run(tt.value, func(t *testing.T) {
			l, err := ParseGPS(tt.value)
			if tt.err {
				assert.Error(t, err)
			} else {
				assert.InDelta(t, tt.expected, l, nearDistance)
			}
		})
	}
}

func TestParseDMS(t *testing.T) {
	var tests = []struct {
		value    string
		expected float64
		err      bool
	}{
		{"33\u00B0 12' 34.3423\"", 33.209540, false},
		{"33\u00B0 1.1' 34.3423\"", 0, true},
		{"3.3\u00B0 1' 34.3423\"", 0, true},
		{"33\u00B0 1' 34.34.23\"", 0, true},
		{"33 1 3434.23", 0, true},
		{"123", 0, true},
	}
	for _, tt := range tests {
		t.Run(tt.value, func(t *testing.T) {
			l, err := ParseDMS(tt.value)
			if tt.err {
				assert.Error(t, err)
			} else {
				assert.InDelta(t, tt.expected, l, nearDistance)
			}
		})
	}
}

func TestParseDecimal(t *testing.T) {
	var tests = []struct {
		value    string
		expected float64
		err      bool
	}{
		{"151.234532", 151.234532, false},
		{"-151.234532", -151.234532, false},
	}
	for _, tt := range tests {
		t.Run(tt.value, func(t *testing.T) {
			l, err := ParseDecimal(tt.value)
			if tt.err {
				assert.Error(t, err)
			} else {
				assert.InDelta(t, tt.expected, l, nearDistance)
			}
		})
	}
}

func TestLatLongPrint(t *testing.T) {
	var tests = []struct {
		value float64
		dms   string
		gps   string
	}{
		{
			value: 151.434367,
			gps:   "15126.0620",
			dms:   "151° 26' 3.721200\"",
		},
		{
			value: 33.94057166666666,
			gps:   "3356.4343",
			dms:   "33° 56' 26.058000\"",
		},
		{
			value: 45.0,
			dms:   "45° 0' 0.000000\"",
			gps:   "4500.0000",
		},
	}

	for _, tt := range tests {
		t.Run(fmt.Sprintf("%f", tt.value), func(t *testing.T) {
			assert.Equal(t, tt.dms, FormatDMS(tt.value))
			assert.Equal(t, tt.gps, FormatGPS(tt.value))
		})
	}
}

func TestTimeParse(t *testing.T) {
	timetests := []struct {
		value    string
		expected Time
		ok       bool
	}{
		{"123456", Time{true, 12, 34, 56, 0}, true},
		{"", Time{}, true},
		{"112233.123", Time{true, 11, 22, 33, 123}, true},
		{"010203.04", Time{true, 1, 2, 3, 40}, true},
		{"10203.04", Time{}, false},
		{"x0u2xd", Time{}, false},
		{"xx2233.123", Time{}, false},
		{"11xx33.123", Time{}, false},
		{"1122xx.123", Time{}, false},
		{"112233.xxx", Time{}, false},
	}
	for _, tt := range timetests {
		actual, err := ParseTime(tt.value)
		if !tt.ok {
			if err == nil {
				t.Errorf("ParseTime(%s) expected error", tt.value)
			}
		} else {
			if err != nil {
				t.Errorf("ParseTime(%s) %s", tt.value, err)
			}
			if actual != tt.expected {
				t.Errorf("ParseTime(%s) got %s expected %s", tt.value, actual, tt.expected)
			}
		}
	}
}

func TestTimeString(t *testing.T) {
	d := Time{
		Hour:        1,
		Minute:      2,
		Second:      3,
		Millisecond: 4,
	}
	expected := "01:02:03.0040"
	if s := d.String(); s != expected {
		t.Fatalf("got %s, expected %s", s, expected)
	}
}

func TestDateParse(t *testing.T) {
	datetests := []struct {
		value    string
		expected Date
		ok       bool
	}{
		{"010203", Date{true, 1, 2, 3}, true},
		{"01003", Date{}, false},
		{"", Date{}, true},
		{"xx0203", Date{}, false},
		{"01xx03", Date{}, false},
		{"0102xx", Date{}, false},
	}
	for _, tt := range datetests {
		actual, err := ParseDate(tt.value)
		if !tt.ok {
			if err == nil {
				t.Errorf("ParseDate(%s) expected error", tt.value)
			}
		} else {
			if err != nil {
				t.Errorf("ParseDate(%s) %s", tt.value, err)
			}
			if actual != tt.expected {
				t.Errorf("ParseDate(%s) got %s expected %s", tt.value, actual, tt.expected)
			}
		}
	}
}

func TestDateString(t *testing.T) {
	d := Date{
		DD: 1,
		MM: 2,
		YY: 3,
	}
	expected := "01/02/03"
	if s := d.String(); s != expected {
		t.Fatalf("got %s expected %s", s, expected)
	}
}

func TestDateTime(t *testing.T) {
	for i, testCase := range []struct {
		refYear int
		date    Date
		time    Time
		expect  time.Time
	}{
		{
			refYear: 2024,
			date:    Date{DD: 1, MM: 2, YY: 3, Valid: true},
			time:    Time{Hour: 1, Minute: 2, Second: 3, Millisecond: 4, Valid: true},
			expect:  time.Date(2003, time.February, 1, 1, 2, 3, 4e6, time.UTC),
		},
		{
			refYear: 1999,
			date:    Date{DD: 1, MM: 2, YY: 3, Valid: true},
			time:    Time{Hour: 1, Minute: 2, Second: 3, Millisecond: 4, Valid: true},
			expect:  time.Date(1903, time.February, 1, 1, 2, 3, 4e6, time.UTC),
		},
		{
			refYear: 2025,
			date:    Date{DD: 23, MM: 7, YY: 24, Valid: true},
			time:    Time{Hour: 18, Minute: 5, Second: 12, Millisecond: 1, Valid: true},
			expect:  time.Date(2024, time.July, 23, 18, 5, 12, 1e6, time.UTC),
		},
		// zero reference year; this test will fail starting in the year 3000
		{
			refYear: 0,
			date:    Date{DD: 1, MM: 2, YY: 3, Valid: true},
			time:    Time{Hour: 1, Minute: 2, Second: 3, Millisecond: 4, Valid: true},
			expect:  time.Date(2003, time.February, 1, 1, 2, 3, 4e6, time.UTC),
		},
		// invalid date
		{
			refYear: 2024,
			date:    Date{DD: 1, MM: 2, YY: 3},
			time:    Time{Hour: 1, Minute: 2, Second: 3, Millisecond: 4, Valid: true},
			expect:  time.Time{},
		},
		// invalid time
		{
			refYear: 2024,
			date:    Date{DD: 1, MM: 2, YY: 3, Valid: true},
			time:    Time{Hour: 1, Minute: 2, Second: 3, Millisecond: 4},
			expect:  time.Time{},
		},
	} {
		actual := DateTime(testCase.refYear, testCase.date, testCase.time)
		if !actual.Equal(testCase.expect) {
			t.Fatalf("Test %d (refYear=%d date=%s time=%s): Expected %s but got %s", i, testCase.refYear, testCase.date, testCase.time, testCase.expect, actual)
		}
	}
}

func TestLatDir(t *testing.T) {
	tests := []struct {
		value    float64
		expected string
	}{
		{50.0, "N"},
		{-50.0, "S"},
	}
	for _, tt := range tests {
		if s := LatDir(tt.value); s != tt.expected {
			t.Fatalf("got %s expected %s", s, tt.expected)
		}
	}
}

func TestLonDir(t *testing.T) {
	tests := []struct {
		value    float64
		expected string
	}{
		{100.0, "E"},
		{-100.0, "W"},
	}
	for _, tt := range tests {
		if s := LonDir(tt.value); s != tt.expected {
			t.Fatalf("got %s expected %s", s, tt.expected)
		}
	}
}