-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
81 lines (71 loc) · 2.01 KB
/
main_test.go
File metadata and controls
81 lines (71 loc) · 2.01 KB
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
package main
import (
"testing"
"time"
)
func TestCalculateMoonPhase(t *testing.T) {
tests := []struct {
name string
date time.Time
expectedAge float64
expectedIllum int
tolerance float64
}{
{
name: "Known new moon",
date: time.Date(2000, 1, 6, 18, 14, 0, 0, time.UTC),
expectedAge: 0.0,
expectedIllum: 0,
tolerance: 0.1,
},
{
name: "Approximately full moon",
date: time.Date(2000, 1, 21, 0, 0, 0, 0, time.UTC),
expectedAge: 14.2,
expectedIllum: 100,
tolerance: 1.0,
},
{
name: "Quarter moon",
date: time.Date(2000, 1, 14, 0, 0, 0, 0, time.UTC),
expectedAge: 7.2,
expectedIllum: 50,
tolerance: 1.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateMoonPhase(tt.date)
if abs(result.Age-tt.expectedAge) > tt.tolerance {
t.Errorf("Age = %.2f, expected %.2f ± %.2f", result.Age, tt.expectedAge, tt.tolerance)
}
if abs(float64(result.Illumination-tt.expectedIllum)) > tt.tolerance*10 {
t.Errorf("Illumination = %d%%, expected %d%% ± %.0f%%", result.Illumination, tt.expectedIllum, tt.tolerance*10)
}
})
}
}
func TestCalculateMoonPhaseRange(t *testing.T) {
// Test that age is always between 0 and ~29.5 days
// and illumination is always between 0 and 100
testDates := []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2023, 6, 15, 12, 0, 0, 0, time.UTC),
time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC),
}
for _, date := range testDates {
result := calculateMoonPhase(date)
if result.Age < 0 || result.Age > 30 {
t.Errorf("Age %.2f is out of valid range [0, 30] for date %s", result.Age, date.Format("2006-01-02"))
}
if result.Illumination < 0 || result.Illumination > 100 {
t.Errorf("Illumination %d%% is out of valid range [0, 100] for date %s", result.Illumination, date.Format("2006-01-02"))
}
}
}
func abs(x float64) float64 {
if x < 0 {
return -x
}
return x
}