Add tests for julian package
This commit is contained in:
parent
bd5c9fe692
commit
cc6e52d352
147
julian/julian_test.go
Normal file
147
julian/julian_test.go
Normal file
|
@ -0,0 +1,147 @@
|
||||||
|
package julian
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMonthLength(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
y int
|
||||||
|
m time.Month
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{2001, time.January, 31},
|
||||||
|
{2001, time.February, 28},
|
||||||
|
{2000, time.February, 29},
|
||||||
|
{2001, time.March, 31},
|
||||||
|
{2001, time.April, 30},
|
||||||
|
{2001, time.May, 31},
|
||||||
|
{2001, time.June, 30},
|
||||||
|
{2001, time.July, 31},
|
||||||
|
{2001, time.August, 31},
|
||||||
|
{2001, time.September, 30},
|
||||||
|
{2001, time.October, 31},
|
||||||
|
{2001, time.November, 30},
|
||||||
|
{2001, time.December, 31},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("%v, %d", tt.m, tt.y)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
ans := monthLength(tt.y, tt.m)
|
||||||
|
if ans != tt.want {
|
||||||
|
t.Errorf("got %d, want %d", ans, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateDiff(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
d Date
|
||||||
|
days int
|
||||||
|
want Date
|
||||||
|
}{
|
||||||
|
{Date{2001, time.January, 20}, 5, Date{2001, time.January, 25}},
|
||||||
|
{Date{2001, time.February, 28}, 5, Date{2001, time.March, 5}},
|
||||||
|
{Date{2004, time.February, 28}, 5, Date{2004, time.March, 4}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("%v, %d", tt.d, tt.days)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
ans := dateDiff(tt.d, tt.days)
|
||||||
|
if ans != tt.want {
|
||||||
|
t.Errorf("got %d, want %d", ans, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextMonth(t *testing.T) {
|
||||||
|
ans1, ans2 := nextMonth(2000, time.December)
|
||||||
|
if ans1 != 2001 || ans2 != time.January {
|
||||||
|
t.Errorf("nextMonth(2000, time.December) = %d, %v; want 2001, time.January", ans1, ans2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToTime(t *testing.T) {
|
||||||
|
gDate := func(year int, month time.Month, day int) time.Time {
|
||||||
|
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
|
||||||
|
}
|
||||||
|
var tests = []struct {
|
||||||
|
d Date
|
||||||
|
want time.Time
|
||||||
|
}{
|
||||||
|
{Date{100, time.February, 29}, gDate(100, time.February, 27)},
|
||||||
|
{Date{100, time.March, 1}, gDate(100, time.February, 28)},
|
||||||
|
{Date{100, time.March, 2}, gDate(100, time.March, 1)},
|
||||||
|
{Date{200, time.February, 28}, gDate(200, time.February, 27)},
|
||||||
|
{Date{200, time.February, 29}, gDate(200, time.February, 28)},
|
||||||
|
{Date{200, time.March, 1}, gDate(200, time.March, 1)},
|
||||||
|
{Date{300, time.February, 28}, gDate(300, time.February, 28)},
|
||||||
|
{Date{300, time.February, 29}, gDate(300, time.March, 1)},
|
||||||
|
{Date{300, time.March, 1}, gDate(300, time.March, 2)},
|
||||||
|
{Date{500, time.February, 28}, gDate(500, time.March, 1)},
|
||||||
|
{Date{500, time.February, 29}, gDate(500, time.March, 2)},
|
||||||
|
{Date{500, time.March, 1}, gDate(500, time.March, 3)},
|
||||||
|
{Date{600, time.February, 28}, gDate(600, time.March, 2)},
|
||||||
|
{Date{600, time.February, 29}, gDate(600, time.March, 3)},
|
||||||
|
{Date{600, time.March, 1}, gDate(600, time.March, 4)},
|
||||||
|
{Date{700, time.February, 28}, gDate(700, time.March, 3)},
|
||||||
|
{Date{700, time.February, 29}, gDate(700, time.March, 4)},
|
||||||
|
{Date{700, time.March, 1}, gDate(700, time.March, 5)},
|
||||||
|
{Date{900, time.February, 28}, gDate(900, time.March, 4)},
|
||||||
|
{Date{900, time.February, 29}, gDate(900, time.March, 5)},
|
||||||
|
{Date{900, time.March, 1}, gDate(900, time.March, 6)},
|
||||||
|
{Date{1000, time.February, 28}, gDate(1000, time.March, 5)},
|
||||||
|
{Date{1000, time.February, 29}, gDate(1000, time.March, 6)},
|
||||||
|
{Date{1000, time.March, 1}, gDate(1000, time.March, 7)},
|
||||||
|
{Date{1100, time.February, 28}, gDate(1100, time.March, 6)},
|
||||||
|
{Date{1100, time.February, 29}, gDate(1100, time.March, 7)},
|
||||||
|
{Date{1100, time.March, 1}, gDate(1100, time.March, 8)},
|
||||||
|
{Date{1300, time.February, 28}, gDate(1300, time.March, 7)},
|
||||||
|
{Date{1300, time.February, 29}, gDate(1300, time.March, 8)},
|
||||||
|
{Date{1300, time.March, 1}, gDate(1300, time.March, 9)},
|
||||||
|
{Date{1400, time.February, 28}, gDate(1400, time.March, 8)},
|
||||||
|
{Date{1400, time.February, 29}, gDate(1400, time.March, 9)},
|
||||||
|
{Date{1400, time.March, 1}, gDate(1400, time.March, 10)},
|
||||||
|
{Date{1500, time.February, 28}, gDate(1500, time.March, 9)},
|
||||||
|
{Date{1500, time.February, 29}, gDate(1500, time.March, 10)},
|
||||||
|
{Date{1500, time.March, 1}, gDate(1500, time.March, 11)},
|
||||||
|
{Date{1582, time.October, 4}, gDate(1582, time.October, 14)},
|
||||||
|
{Date{1582, time.October, 5}, gDate(1582, time.October, 15)},
|
||||||
|
{Date{1582, time.October, 6}, gDate(1582, time.October, 16)},
|
||||||
|
{Date{1700, time.February, 18}, gDate(1700, time.February, 28)},
|
||||||
|
{Date{1700, time.February, 19}, gDate(1700, time.March, 1)},
|
||||||
|
{Date{1700, time.February, 28}, gDate(1700, time.March, 10)},
|
||||||
|
{Date{1700, time.February, 29}, gDate(1700, time.March, 11)},
|
||||||
|
{Date{1700, time.March, 1}, gDate(1700, time.March, 12)},
|
||||||
|
{Date{1800, time.February, 17}, gDate(1800, time.February, 28)},
|
||||||
|
{Date{1800, time.February, 18}, gDate(1800, time.March, 1)},
|
||||||
|
{Date{1800, time.February, 28}, gDate(1800, time.March, 11)},
|
||||||
|
{Date{1800, time.February, 29}, gDate(1800, time.March, 12)},
|
||||||
|
{Date{1800, time.March, 1}, gDate(1800, time.March, 13)},
|
||||||
|
{Date{1900, time.February, 16}, gDate(1900, time.February, 28)},
|
||||||
|
{Date{1900, time.February, 17}, gDate(1900, time.March, 1)},
|
||||||
|
{Date{1900, time.February, 28}, gDate(1900, time.March, 12)},
|
||||||
|
{Date{1900, time.February, 29}, gDate(1900, time.March, 13)},
|
||||||
|
{Date{1900, time.March, 1}, gDate(1900, time.March, 14)},
|
||||||
|
{Date{2100, time.February, 15}, gDate(2100, time.February, 28)},
|
||||||
|
{Date{2100, time.February, 16}, gDate(2100, time.March, 1)},
|
||||||
|
{Date{2100, time.February, 28}, gDate(2100, time.March, 13)},
|
||||||
|
{Date{2100, time.February, 29}, gDate(2100, time.March, 14)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("%v, %v", tt.d, tt.want)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
ans := ToTime(tt.d)
|
||||||
|
if ans != tt.want {
|
||||||
|
t.Errorf("got %v, want %v", ans, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue