| 1 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package time |
| 6 |
|
| 7 |
type testingT interface { |
| 8 |
Error(args ...any) |
| 9 |
Errorf(format string, args ...any) |
| 10 |
Fail() |
| 11 |
FailNow() |
| 12 |
Failed() bool |
| 13 |
Fatal(args ...any) |
| 14 |
Fatalf(format string, args ...any) |
| 15 |
Helper() |
| 16 |
Log(args ...any) |
| 17 |
Logf(format string, args ...any) |
| 18 |
Skip(args ...any) |
| 19 |
SkipNow() |
| 20 |
Skipf(format string, args ...any) |
| 21 |
} |
| 22 |
|
| 23 |
var InternalTests = []struct { |
| 24 |
Name string |
| 25 |
Test func(testingT) |
| 26 |
}{ |
| 27 |
{"AbsDaysSplit", testAbsDaysSplit}, |
| 28 |
{"AbsYdaySplit", testAbsYdaySplit}, |
| 29 |
{"AbsDate", testAbsDate}, |
| 30 |
{"DateToAbsDays", testDateToAbsDays}, |
| 31 |
{"DaysIn", testDaysIn}, |
| 32 |
{"DaysBefore", testDaysBefore}, |
| 33 |
} |
| 34 |
|
| 35 |
func testAbsDaysSplit(t testingT) { |
| 36 |
isLeap := func(year uint64) bool { |
| 37 |
return year%4 == 0 && (year%100 != 0 || year%400 == 0) |
| 38 |
} |
| 39 |
bad := 0 |
| 40 |
wantYear := uint64(0) |
| 41 |
wantYday := absYday(0) |
| 42 |
for days := range absDays(1e6) { |
| 43 |
century, cyear, yday := days.split() |
| 44 |
if century != absCentury(wantYear/100) || cyear != absCyear(wantYear%100) || yday != wantYday { |
| 45 |
t.Errorf("absDays(%d).split() = %d, %d, %d, want %d, %d, %d", |
| 46 |
days, century, cyear, yday, |
| 47 |
wantYear/100, wantYear%100, wantYday) |
| 48 |
if bad++; bad >= 20 { |
| 49 |
t.Fatalf("too many errors") |
| 50 |
} |
| 51 |
} |
| 52 |
end := absYday(365) |
| 53 |
if isLeap(wantYear + 1) { |
| 54 |
end = 366 |
| 55 |
} |
| 56 |
if wantYday++; wantYday == end { |
| 57 |
wantYear++ |
| 58 |
wantYday = 0 |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func testAbsYdaySplit(t testingT) { |
| 64 |
ends := []int{31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29} |
| 65 |
bad := 0 |
| 66 |
wantMonth := absMonth(3) |
| 67 |
wantDay := 1 |
| 68 |
for yday := range absYday(366) { |
| 69 |
month, day := yday.split() |
| 70 |
if month != wantMonth || day != wantDay { |
| 71 |
t.Errorf("absYday(%d).split() = %d, %d, want %d, %d", yday, month, day, wantMonth, wantDay) |
| 72 |
if bad++; bad >= 20 { |
| 73 |
t.Fatalf("too many errors") |
| 74 |
} |
| 75 |
} |
| 76 |
if wantDay++; wantDay > ends[wantMonth-3] { |
| 77 |
wantMonth++ |
| 78 |
wantDay = 1 |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func testAbsDate(t testingT) { |
| 84 |
ends := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
| 85 |
isLeap := func(year int) bool { |
| 86 |
y := uint64(year) + absoluteYears |
| 87 |
return y%4 == 0 && (y%100 != 0 || y%400 == 0) |
| 88 |
} |
| 89 |
wantYear := 0 |
| 90 |
wantMonth := March |
| 91 |
wantMday := 1 |
| 92 |
wantYday := 31 + 29 + 1 |
| 93 |
bad := 0 |
| 94 |
absoluteYears := int64(absoluteYears) |
| 95 |
for days := range absDays(1e6) { |
| 96 |
year, month, mday := days.date() |
| 97 |
year += int(absoluteYears) |
| 98 |
if year != wantYear || month != wantMonth || mday != wantMday { |
| 99 |
t.Errorf("days(%d).date() = %v, %v, %v, want %v, %v, %v", days, |
| 100 |
year, month, mday, |
| 101 |
wantYear, wantMonth, wantMday) |
| 102 |
if bad++; bad >= 20 { |
| 103 |
t.Fatalf("too many errors") |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
year, yday := days.yearYday() |
| 108 |
year += int(absoluteYears) |
| 109 |
if year != wantYear || yday != wantYday { |
| 110 |
t.Errorf("days(%d).yearYday() = %v, %v, want %v, %v, ", days, |
| 111 |
year, yday, |
| 112 |
wantYear, wantYday) |
| 113 |
if bad++; bad >= 20 { |
| 114 |
t.Fatalf("too many errors") |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if wantMday++; wantMday == ends[wantMonth-1]+1 || wantMonth == February && wantMday == 29 && !isLeap(year) { |
| 119 |
wantMonth++ |
| 120 |
wantMday = 1 |
| 121 |
} |
| 122 |
wantYday++ |
| 123 |
if wantMonth == December+1 { |
| 124 |
wantYear++ |
| 125 |
wantMonth = January |
| 126 |
wantMday = 1 |
| 127 |
wantYday = 1 |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func testDateToAbsDays(t testingT) { |
| 133 |
isLeap := func(year int64) bool { |
| 134 |
return year%4 == 0 && (year%100 != 0 || year%400 == 0) |
| 135 |
} |
| 136 |
wantDays := absDays(marchThruDecember) |
| 137 |
bad := 0 |
| 138 |
for year := int64(1); year < 10000; year++ { |
| 139 |
days := dateToAbsDays(year-absoluteYears, January, 1) |
| 140 |
if days != wantDays { |
| 141 |
t.Errorf("dateToAbsDays(abs %d, Jan, 1) = %d, want %d", year, days, wantDays) |
| 142 |
if bad++; bad >= 20 { |
| 143 |
t.Fatalf("too many errors") |
| 144 |
} |
| 145 |
} |
| 146 |
wantDays += 365 |
| 147 |
if isLeap(year) { |
| 148 |
wantDays++ |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func testDaysIn(t testingT) { |
| 154 |
isLeap := func(year int) bool { |
| 155 |
return year%4 == 0 && (year%100 != 0 || year%400 == 0) |
| 156 |
} |
| 157 |
want := []int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
| 158 |
bad := 0 |
| 159 |
for year := 0; year <= 1600; year++ { |
| 160 |
for m := January; m <= December; m++ { |
| 161 |
w := want[m] |
| 162 |
if m == February && isLeap(year) { |
| 163 |
w++ |
| 164 |
} |
| 165 |
d := daysIn(m, year-800) |
| 166 |
if d != w { |
| 167 |
t.Errorf("daysIn(%v, %d) = %d, want %d", m, year-800, d, w) |
| 168 |
if bad++; bad >= 20 { |
| 169 |
t.Fatalf("too many errors") |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func testDaysBefore(t testingT) { |
| 177 |
for m, want := range []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365} { |
| 178 |
d := daysBefore(Month(m + 1)) |
| 179 |
if d != want { |
| 180 |
t.Errorf("daysBefore(%d) = %d, want %d", m, d, want) |
| 181 |
} |
| 182 |
} |
| 183 |
} |