-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanotime_test.go
291 lines (262 loc) · 10.9 KB
/
nanotime_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package smalltime
import "testing"
import "time"
//import "fmt"
func assertEncodeDecodeNanotime(t *testing.T, year, month, day, hour, minute, second, nsec int) {
time := NewNanotime(year, month, day, hour, minute, second, nsec)
if time.Year() != year || time.Month() != month || time.Day() != day ||
time.Minute() != minute || time.Second() != second || time.Nanosecond() != nsec {
t.Errorf("Expected: %04d-%02d-%02dT%02d:%02d:%02d.%06d, Actual: %04d-%02d-%02dT%02d:%02d:%02d.%06d (doy %d)",
year, month, day, hour, minute, second, nsec,
time.Year(), time.Month(), time.Day(), time.Hour(), time.Minute(), time.Second(), time.Nanosecond(), time.Doy())
}
}
func assertEncodeNanotime(t *testing.T, year, month, day, hour, minute, second, nsec int, encoded Nanotime) {
time := NewNanotime(year, month, day, hour, minute, second, nsec)
if time != encoded {
t.Errorf("Expected %04d-%02d-%02dT%02d:%02d:%02d.%06d to encode to %016x. Actual: %016x",
year, month, day, hour, minute, second, nsec, encoded, time)
}
}
func assertDecodeNanotime(t *testing.T, time Nanotime, year, month, day, hour, minute, second, nsec int) {
if time.Year() != year || time.Month() != month || time.Day() != day ||
time.Minute() != minute || time.Second() != second || time.Nanosecond() != nsec {
t.Errorf("Expected %016x to decode to %04d-%02d-%02dT%02d:%02d:%02d.%06d. Actual: %04d-%02d-%02dT%02d:%02d:%02d.%06d",
time, year, month, day, hour, minute, second, nsec,
time.Year(), time.Month(), time.Day(), time.Hour(), time.Minute(), time.Second(), time.Nanosecond())
}
}
func assertNanotimeYdToMd(t *testing.T, year, doy, month, day int) {
mm, dd := doyToYmd(year, doy)
if mm != month || dd != day {
t.Errorf("Expected Y %d DOY %d to give M %d D %d, but got %d %d", year, doy, month, day, mm, dd)
}
}
func assertNanotimeYmdToDoy(t *testing.T, year, month, day, doy int) {
dd := ymdToDoy(year, month, day)
if dd != doy {
t.Errorf("Expected Y %d M %d D %d to give DOY %d, but got %d", year, month, day, doy, dd)
}
}
func assertNanotimeGotimeEquivalence(t *testing.T, year, month, day, hour, minute, second, nsec int) {
// fmt.Println(year, month, day, hour, minute, second, nsec)
smtime := NewNanotime(year, month, day, hour, minute, second, nsec)
gotime := time.Date(year, time.Month(month), day, hour, minute, second, nsec, time.UTC)
if smtime.Year() != gotime.Year() ||
smtime.Month() != int(gotime.Month()) ||
smtime.Day() != gotime.Day() ||
smtime.Hour() != gotime.Hour() ||
smtime.Minute() != gotime.Minute() ||
smtime.Second() != gotime.Second() ||
smtime.Nanosecond() != gotime.Nanosecond() {
t.Errorf("%04d-%02d-%02dT%02d:%02d:%02d.%06d != %v",
smtime.Year(), smtime.Month(), smtime.Day(), smtime.Hour(),
smtime.Minute(), smtime.Second(), smtime.Nanosecond(), gotime)
}
if smtime.AsTime() != gotime {
t.Errorf("%04d-%02d-%02dT%02d:%02d:%02d.%06d did not convert cleanly to %v",
smtime.Year(), smtime.Month(), smtime.Day(), smtime.Hour(),
smtime.Minute(), smtime.Second(), smtime.Nanosecond(), gotime)
}
if NanotimeFromTime(gotime) != smtime {
t.Errorf("%v did not convert cleanly to %04d-%02d-%02dT%02d:%02d:%02d.%06d",
gotime, smtime.Year(), smtime.Month(), smtime.Day(), smtime.Hour(),
smtime.Minute(), smtime.Second(), smtime.Nanosecond())
}
}
func assertNanotimeGreater(t *testing.T, greater, smaller Nanotime) {
if greater <= smaller {
t.Errorf("%04d-%02d-%02dT%02d:%02d:%02d.%06d is not smaller than %04d-%02d-%02dT%02d:%02d:%02d.%06d",
greater.Year(), greater.Month(), greater.Day(), greater.Hour(),
greater.Minute(), greater.Second(), greater.Nanosecond(),
smaller.Year(), smaller.Month(), smaller.Day(), smaller.Hour(),
smaller.Minute(), smaller.Second(), smaller.Nanosecond())
}
}
func TestNanosecondsNanotime(t *testing.T) {
year := 2003
month := 11
day := 15
hour := 8
minute := 30
second := 55
for nanosecond := 0; nanosecond < 1000000000; nanosecond += 111 {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestSecondsNanotime(t *testing.T) {
year := 2003
month := 11
day := 15
hour := 8
minute := 30
nanosecond := 1402778
for second := 0; second < 60; second++ {
// fmt.Println(">",year, month, day, hour, minute, second, nanosecond)
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestMinutesNanotime(t *testing.T) {
year := 2003
month := 11
day := 15
hour := 8
second := 30
nanosecond := 1402991
for minute := 0; minute < 60; minute++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestHoursNanotime(t *testing.T) {
year := 2003
month := 11
day := 15
minute := 30
second := 55
nanosecond := 1402889
for hour := 0; hour < 24; hour++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestDaysNanotime(t *testing.T) {
year := 2003
month := 11
hour := 8
minute := 30
second := 55
nanosecond := 1402345
for day := 1; day < 30; day++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
year = 2004
for day := 1; day < 30; day++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestMonthsNanotime(t *testing.T) {
year := 2003
day := 15
hour := 8
minute := 30
second := 55
nanosecond := 1402988
for month := 1; month < 12; month++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
year = 2004
for month := 1; month < 12; month++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestYearsNanotime(t *testing.T) {
month := 11
day := 15
hour := 8
minute := 15
second := 30
nanosecond := 1402567
for year := 1970; year < 2226; year++ {
assertNanotimeGotimeEquivalence(t, year, month, day, hour, minute, second, nanosecond)
}
}
func TestYmdToDoyNanotime(t *testing.T) {
assertNanotimeYmdToDoy(t, 1985, 1, 1, 1)
assertNanotimeYmdToDoy(t, 1985, 1, 2, 2)
assertNanotimeYmdToDoy(t, 1985, 1, 31, 31)
assertNanotimeYmdToDoy(t, 1985, 2, 1, 32)
assertNanotimeYmdToDoy(t, 1985, 2, 28, 59)
assertNanotimeYmdToDoy(t, 1985, 3, 1, 60)
assertNanotimeYmdToDoy(t, 1985, 3, 2, 61)
assertNanotimeYmdToDoy(t, 1985, 3, 31, 90)
assertNanotimeYmdToDoy(t, 1985, 4, 1, 91)
assertNanotimeYmdToDoy(t, 1985, 12, 30, 364)
assertNanotimeYmdToDoy(t, 1985, 12, 31, 365)
assertNanotimeYmdToDoy(t, 1986, 1, 1, 1)
assertNanotimeYmdToDoy(t, 2000, 1, 1, 1)
assertNanotimeYmdToDoy(t, 2000, 1, 2, 2)
assertNanotimeYmdToDoy(t, 2000, 1, 31, 31)
assertNanotimeYmdToDoy(t, 2000, 2, 1, 32)
assertNanotimeYmdToDoy(t, 2000, 2, 28, 59)
assertNanotimeYmdToDoy(t, 2000, 2, 29, 60)
assertNanotimeYmdToDoy(t, 2000, 3, 1, 61)
assertNanotimeYmdToDoy(t, 2000, 3, 2, 62)
assertNanotimeYmdToDoy(t, 2000, 3, 31, 91)
assertNanotimeYmdToDoy(t, 2000, 4, 1, 92)
assertNanotimeYmdToDoy(t, 2000, 12, 30, 365)
assertNanotimeYmdToDoy(t, 2000, 12, 31, 366)
assertNanotimeYmdToDoy(t, 2001, 1, 1, 1)
assertNanotimeYmdToDoy(t, 2100, 1, 1, 1)
assertNanotimeYmdToDoy(t, 2100, 1, 2, 2)
assertNanotimeYmdToDoy(t, 2100, 1, 31, 31)
assertNanotimeYmdToDoy(t, 2100, 2, 1, 32)
assertNanotimeYmdToDoy(t, 2100, 2, 28, 59)
assertNanotimeYmdToDoy(t, 2100, 3, 1, 60)
assertNanotimeYmdToDoy(t, 2100, 3, 2, 61)
assertNanotimeYmdToDoy(t, 2100, 3, 31, 90)
assertNanotimeYmdToDoy(t, 2100, 4, 1, 91)
assertNanotimeYmdToDoy(t, 2100, 12, 30, 364)
assertNanotimeYmdToDoy(t, 2100, 12, 31, 365)
assertNanotimeYmdToDoy(t, 2101, 1, 1, 1)
}
func TestDoyToYmdNanotime(t *testing.T) {
assertNanotimeYdToMd(t, 1985, 1, 1, 1)
assertNanotimeYdToMd(t, 1985, 2, 1, 2)
assertNanotimeYdToMd(t, 1985, 31, 1, 31)
assertNanotimeYdToMd(t, 1985, 32, 2, 1)
assertNanotimeYdToMd(t, 1985, 59, 2, 28)
assertNanotimeYdToMd(t, 1985, 60, 3, 1)
assertNanotimeYdToMd(t, 1985, 61, 3, 2)
assertNanotimeYdToMd(t, 1985, 90, 3, 31)
assertNanotimeYdToMd(t, 1985, 91, 4, 1)
assertNanotimeYdToMd(t, 1985, 364, 12, 30)
assertNanotimeYdToMd(t, 1985, 365, 12, 31)
assertNanotimeYdToMd(t, 1986, 1, 1, 1)
assertNanotimeYdToMd(t, 2000, 1, 1, 1)
assertNanotimeYdToMd(t, 2000, 2, 1, 2)
assertNanotimeYdToMd(t, 2000, 31, 1, 31)
assertNanotimeYdToMd(t, 2000, 32, 2, 1)
assertNanotimeYdToMd(t, 2000, 59, 2, 28)
assertNanotimeYdToMd(t, 2000, 60, 2, 29)
assertNanotimeYdToMd(t, 2000, 61, 3, 1)
assertNanotimeYdToMd(t, 2000, 62, 3, 2)
assertNanotimeYdToMd(t, 2000, 91, 3, 31)
assertNanotimeYdToMd(t, 2000, 92, 4, 1)
assertNanotimeYdToMd(t, 2000, 365, 12, 30)
assertNanotimeYdToMd(t, 2000, 366, 12, 31)
assertNanotimeYdToMd(t, 2001, 1, 1, 1)
assertNanotimeYdToMd(t, 2100, 1, 1, 1)
assertNanotimeYdToMd(t, 2100, 2, 1, 2)
assertNanotimeYdToMd(t, 2100, 31, 1, 31)
assertNanotimeYdToMd(t, 2100, 32, 2, 1)
assertNanotimeYdToMd(t, 2100, 59, 2, 28)
assertNanotimeYdToMd(t, 2100, 60, 3, 1)
assertNanotimeYdToMd(t, 2100, 61, 3, 2)
assertNanotimeYdToMd(t, 2100, 90, 3, 31)
assertNanotimeYdToMd(t, 2100, 91, 4, 1)
assertNanotimeYdToMd(t, 2100, 364, 12, 30)
assertNanotimeYdToMd(t, 2100, 365, 12, 31)
assertNanotimeYdToMd(t, 2101, 1, 1, 1)
}
func TestComparisonsNanotime(t *testing.T) {
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 0, 0, 0, 1), NewNanotime(2000, 1, 1, 0, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 0, 0, 1, 0), NewNanotime(2000, 1, 1, 0, 0, 0, 999999))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 0, 0, 2, 0), NewNanotime(2000, 1, 1, 0, 0, 1, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 0, 1, 0, 0), NewNanotime(2000, 1, 1, 0, 0, 60, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 0, 2, 0, 0), NewNanotime(2000, 1, 1, 0, 1, 0, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 1, 0, 0, 0), NewNanotime(2000, 1, 1, 0, 59, 0, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 1, 2, 0, 0, 0), NewNanotime(2000, 1, 1, 1, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 2, 0, 0, 0, 0), NewNanotime(2000, 1, 1, 23, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(2000, 1, 2, 0, 0, 0, 0), NewNanotime(2000, 1, 1, 0, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(2005, 1, 1, 0, 0, 0, 0), NewNanotime(2004, 12, 31, 0, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(1, 1, 1, 0, 0, 0, 0), NewNanotime(0, 1, 1, 0, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(0, 1, 1, 0, 0, 0, 0), NewNanotime(-1, 1, 1, 0, 0, 0, 0))
assertNanotimeGreater(t, NewNanotime(1, 1, 1, 0, 0, 0, 0), NewNanotime(-1, 1, 1, 0, 0, 0, 0))
}
func TestSpecExamplesNanotime(t *testing.T) {
assertDecodeNanotime(t, Nanotime(0x0fad2164076290ee), 1985, 10, 26, 8, 22, 16, 123900142)
assertDecodeNanotime(t, Nanotime(0x0fada164076290ee), 1985, 10, 27, 8, 22, 16, 123900142)
assertDecodeNanotime(t, Nanotime(0x0fad2154076290ee), 1985, 10, 26, 8, 21, 16, 123900142)
assertEncodeNanotime(t, 1985, 10, 26, 8, 22, 16, 123900142, Nanotime(0x0fad2164076290ee))
assertEncodeNanotime(t, 1985, 10, 27, 8, 22, 16, 123900142, Nanotime(0x0fada164076290ee))
assertEncodeNanotime(t, 1985, 10, 26, 8, 21, 16, 123900142, Nanotime(0x0fad2154076290ee))
}