-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabs_test.go
183 lines (172 loc) · 4.38 KB
/
abs_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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package time
type testingT interface {
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
Failed() bool
Fatal(args ...any)
Fatalf(format string, args ...any)
Helper()
Log(args ...any)
Logf(format string, args ...any)
Skip(args ...any)
SkipNow()
Skipf(format string, args ...any)
}
var InternalTests = []struct {
Name string
Test func(testingT)
}{
{"AbsDaysSplit", testAbsDaysSplit},
{"AbsYdaySplit", testAbsYdaySplit},
{"AbsDate", testAbsDate},
{"DateToAbsDays", testDateToAbsDays},
{"DaysIn", testDaysIn},
{"DaysBefore", testDaysBefore},
}
func testAbsDaysSplit(t testingT) {
isLeap := func(year uint64) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
bad := 0
wantYear := uint64(0)
wantYday := absYday(0)
for days := range absDays(1e6) {
century, cyear, yday := days.split()
if century != absCentury(wantYear/100) || cyear != absCyear(wantYear%100) || yday != wantYday {
t.Errorf("absDays(%d).split() = %d, %d, %d, want %d, %d, %d",
days, century, cyear, yday,
wantYear/100, wantYear%100, wantYday)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
end := absYday(365)
if isLeap(wantYear + 1) {
end = 366
}
if wantYday++; wantYday == end {
wantYear++
wantYday = 0
}
}
}
func testAbsYdaySplit(t testingT) {
ends := []int{31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29}
bad := 0
wantMonth := absMonth(3)
wantDay := 1
for yday := range absYday(366) {
month, day := yday.split()
if month != wantMonth || day != wantDay {
t.Errorf("absYday(%d).split() = %d, %d, want %d, %d", yday, month, day, wantMonth, wantDay)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
if wantDay++; wantDay > ends[wantMonth-3] {
wantMonth++
wantDay = 1
}
}
}
func testAbsDate(t testingT) {
ends := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
isLeap := func(year int) bool {
y := uint64(year) + absoluteYears
return y%4 == 0 && (y%100 != 0 || y%400 == 0)
}
wantYear := 0
wantMonth := March
wantMday := 1
wantYday := 31 + 29 + 1
bad := 0
absoluteYears := int64(absoluteYears)
for days := range absDays(1e6) {
year, month, mday := days.date()
year += int(absoluteYears)
if year != wantYear || month != wantMonth || mday != wantMday {
t.Errorf("days(%d).date() = %v, %v, %v, want %v, %v, %v", days,
year, month, mday,
wantYear, wantMonth, wantMday)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
year, yday := days.yearYday()
year += int(absoluteYears)
if year != wantYear || yday != wantYday {
t.Errorf("days(%d).yearYday() = %v, %v, want %v, %v, ", days,
year, yday,
wantYear, wantYday)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
if wantMday++; wantMday == ends[wantMonth-1]+1 || wantMonth == February && wantMday == 29 && !isLeap(year) {
wantMonth++
wantMday = 1
}
wantYday++
if wantMonth == December+1 {
wantYear++
wantMonth = January
wantMday = 1
wantYday = 1
}
}
}
func testDateToAbsDays(t testingT) {
isLeap := func(year int64) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
wantDays := absDays(marchThruDecember)
bad := 0
for year := int64(1); year < 10000; year++ {
days := dateToAbsDays(year-absoluteYears, January, 1)
if days != wantDays {
t.Errorf("dateToAbsDays(abs %d, Jan, 1) = %d, want %d", year, days, wantDays)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
wantDays += 365
if isLeap(year) {
wantDays++
}
}
}
func testDaysIn(t testingT) {
isLeap := func(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
want := []int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
bad := 0
for year := 0; year <= 1600; year++ {
for m := January; m <= December; m++ {
w := want[m]
if m == February && isLeap(year) {
w++
}
d := daysIn(m, year-800)
if d != w {
t.Errorf("daysIn(%v, %d) = %d, want %d", m, year-800, d, w)
if bad++; bad >= 20 {
t.Fatalf("too many errors")
}
}
}
}
}
func testDaysBefore(t testingT) {
for m, want := range []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365} {
d := daysBefore(Month(m + 1))
if d != want {
t.Errorf("daysBefore(%d) = %d, want %d", m, d, want)
}
}
}