This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
sunrisesunset_test.go
198 lines (173 loc) · 4.53 KB
/
sunrisesunset_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
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
package sunrisesunset
import (
"testing"
"time"
)
func TestGetSunriseSunset(t *testing.T) {
date := time.Date(2017, 3, 23, 0, 0, 0, 0, time.UTC)
// Test invalid parameters
// Table tests
var invalidParameters = []struct {
latitude float64
longitude float64
utcOffset float64
date time.Time
expectedError string
}{
{-95.0, -46.704082, -3.0, date, "Latitude invalid"},
{100.0, -46.704082, -3.0, date, "Latitude invalid"},
{-23.545570, -185.0, -3.0, date, "Longitude invalid"},
{-23.545570, 190.0, -3.0, date, "Longitude invalid"},
{-23.545570, -46.704082, -15.0, date, "UTC offset invalid"},
{-23.545570, -46.704082, 18.0, date, "UTC offset invalid"},
{-23.545570, -46.704082, -3.0, time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC), "Date invalid"},
{-23.545570, -46.704082, -3.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC), "Date invalid"},
}
// Test with all values in the table
for _, pair := range invalidParameters {
_, _, err := GetSunriseSunset(pair.latitude, pair.longitude, pair.utcOffset, pair.date)
if err == nil {
t.Error(
"Expect an error",
)
}
p := Parameters{
Latitude: pair.latitude,
Longitude: pair.longitude,
UtcOffset: pair.utcOffset,
Date: pair.date,
}
_, _, err = p.GetSunriseSunset()
if err == nil {
t.Error(
"Expect an error",
)
}
}
// Test with valid values
// Table tests
var tTests = []struct {
latitude float64
longitude float64
utcOffset float64
date time.Time
sunrise time.Time
sunset time.Time
}{
{-23.545570, -46.704082, -3.0, date, time.Date(2017, 3, 23, 6, 11, 44, 0, time.UTC), time.Date(2017, 3, 23, 18, 14, 27, 0, time.UTC)}, // Sao Paulo - Brazil
{36.7201600, -4.4203400, 1.0, date, time.Date(2017, 3, 23, 7, 16, 45, 0, time.UTC), time.Date(2017, 3, 23, 19, 32, 10, 0, time.UTC)}, // Málaga - Spain
{28.613084, 77.209168, 5.5, date, time.Date(2017, 3, 23, 6, 21, 45, 0, time.UTC), time.Date(2017, 3, 23, 18, 34, 07, 0, time.UTC)}, // Nova Delhi - India
{32.755701, -96.797296, -5.0, date, time.Date(2017, 3, 23, 7, 26, 34, 0, time.UTC), time.Date(2017, 3, 23, 19, 41, 07, 0, time.UTC)}, // Dallas - USA
}
// Test with all values in the table
for _, pair := range tTests {
sunrise, sunset, err := GetSunriseSunset(pair.latitude, pair.longitude, pair.utcOffset, pair.date)
if err != nil {
t.Error(
"Expect: nil",
"Received: ", err,
)
}
if !sunrise.Equal(pair.sunrise) {
t.Error(
"Expected: ", pair.sunrise,
"Received: ", sunrise,
)
}
if !sunset.Equal(pair.sunset) {
t.Error(
"Expected: ", pair.sunset,
"Received: ", sunset,
)
}
p := Parameters{
Latitude: pair.latitude,
Longitude: pair.longitude,
UtcOffset: pair.utcOffset,
Date: pair.date,
}
sunrise, sunset, err = p.GetSunriseSunset()
if err != nil {
t.Error(
"Expect: nil",
"Received: ", err,
)
}
if !sunrise.Equal(pair.sunrise) {
t.Error(
"Expected: ", pair.sunrise,
"Received: ", sunrise,
)
}
if !sunset.Equal(pair.sunset) {
t.Error(
"Expected: ", pair.sunset,
"Received: ", sunset,
)
}
}
}
func TestDifferentLength(t *testing.T) {
var slice1 []float64
var slice2 []float64
slice1 = append(slice1, 16.0)
slice2 = append(slice2, 32.0)
slice2 = append(slice2, 64.0)
// Table tests
var tTests = []struct {
result []float64
}{
{calcSunEqCtr(slice1, slice2)},
{calcSunTrueLong(slice1, slice2)},
{calcSunAppLong(slice1, slice2)},
{calcObliqCorr(slice1, slice2)},
{calcSunDeclination(slice1, slice2)},
{calcEquationOfTime(slice1, slice2, slice2, slice2)},
{calcEquationOfTime(slice2, slice2, slice2, slice1)},
}
// Test with all values in the table
for _, pair := range tTests {
if len(pair.result) != 0 {
t.Error("Expected: length == 0")
}
}
}
func TestMinIndex(t *testing.T) {
var slice []float64
result := minIndex(slice)
if result != -1 {
t.Error("Expected: minIndex == -1")
}
slice = append(slice, 2.10)
slice = append(slice, 1.00)
slice = append(slice, 0.99)
slice = append(slice, 1.50)
result = minIndex(slice)
if result != 2 {
t.Error("Expected: minIndex == 2")
}
}
func TestRound(t *testing.T) {
// Table tests
var tTests = []struct {
parameter float64
result int
}{
{-0.002, 0},
{-0.510, -1},
{-4.290, -4},
{0.490, 0},
{0.510, 1},
{10.280, 10},
}
// Test with all values in the table
for _, pair := range tTests {
result := round(pair.parameter)
if result != pair.result {
t.Error(
"Expected: ", pair.result,
"Received: ", result,
)
}
}
}