-
Notifications
You must be signed in to change notification settings - Fork 1
/
tai_test.go
executable file
·351 lines (328 loc) · 9.83 KB
/
tai_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package tai_test
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/brandondube/tai"
)
func TestRegisterLeapSecondFunctions(t *testing.T) {
err := tai.RegisterLeapSecond(1e12, 40)
if err != nil {
t.Fatal("non-nil err registering a leapsecond in the distant future", err)
}
tai.RemoveLeapSecond(1e12) // cleanup
}
func TestFuzzTaiToGreg(t *testing.T) {
fuzzTaiToGreg(t, 1e6)
}
func fuzzTaiToGreg(t *testing.T, cases int) {
for i := 0; i < cases; i++ {
year := rand.Intn(2000)
month := rand.Intn(12)
day := rand.Intn(28) // min # of days in a month
hour := rand.Intn(24)
minute := rand.Intn(60)
second := rand.Intn(60)
if month == 0 {
month = 1
}
if day == 0 {
day = 1
}
ta := tai.Date(year, month, day).AddHMS(hour, minute, second)
date := ta.AsGregorian()
var failparts []string
if date.Year != year {
failparts = append(failparts, fmt.Sprintf("wrong year: got %d expected %d", date.Year, year))
}
if date.Month != month {
failparts = append(failparts, fmt.Sprintf("wrong month: got %d expected %d", date.Month, month))
}
if date.Day != day {
failparts = append(failparts, fmt.Sprintf("wrong day: got %d expected %d", date.Day, day))
}
if date.Hour != hour {
failparts = append(failparts, fmt.Sprintf("wrong hour: got %d expected %d", date.Hour, hour))
}
if date.Min != minute {
failparts = append(failparts, fmt.Sprintf("wrong minute: got %d expected %d", date.Min, minute))
}
if date.Sec != second {
failparts = append(failparts, fmt.Sprintf("wrong sec: got %d expected %d", date.Sec, second))
}
if date.Asec != 0 {
failparts = append(failparts, fmt.Sprintf("wrong subsec: got %d expected %d", date.Asec, 0))
}
if len(failparts) != 0 {
failparts = append(failparts, fmt.Sprintf("input Year=%d, Month=%d, Day=%d, Hour=%d, Min=%d, Sec=%d", year, month, day, hour, minute, second))
t.Fatal(strings.Join(failparts, "\n"))
}
}
}
func TestLessSpecialCasesGreg(t *testing.T) {
cases := []struct {
descr string
inp tai.TAI
exp tai.Gregorian
}{
{"Positive1Y", tai.Date(1959, 1, 1), tai.Gregorian{Year: 1959, Month: 1, Day: 1}}, // all others zero
{"Positive2Y", tai.Date(1960, 1, 1), tai.Gregorian{Year: 1960, Month: 1, Day: 1}}, // all others zero
{"Negative1Y", tai.Date(1957, 1, 1), tai.Gregorian{Year: 1957, Month: 1, Day: 1}}, // all others zero
{"Negative2Y", tai.Date(1956, 1, 1), tai.Gregorian{Year: 1956, Month: 1, Day: 1}}, // all others zero
{"Negative3Y", tai.Date(1955, 1, 1), tai.Gregorian{Year: 1955, Month: 1, Day: 1}}, // all others zero
{"Negative4Y", tai.Date(1954, 1, 1), tai.Gregorian{Year: 1954, Month: 1, Day: 1}}, // all others zero
{"Positive1Y1M1D", tai.Date(1959, 2, 2), tai.Gregorian{Year: 1959, Month: 2, Day: 2}},
{"Negative1Y1M1D", tai.Date(1956, 2, 2), tai.Gregorian{Year: 1956, Month: 2, Day: 2}},
{"DayOfChangeToGregorian", tai.Date(1582, tai.October, 15), tai.Gregorian{Year: 1582, Month: 10, Day: 15}},
{"LastJulianDay", tai.Date(1582, tai.October, 4), tai.Gregorian{Year: 1582, Month: 10, Day: 4}},
{"BrokenFuzzCase1NoHMS", tai.Date(81, 3, 15), tai.Gregorian{Year: 81, Month: 3, Day: 15}},
{"BrokenFuzzCase1", tai.Date(81, 3, 15).AddHMS(11, 1, 18), tai.Gregorian{Year: 81, Month: 3, Day: 15, Hour: 11, Min: 1, Sec: 18}},
}
for _, tc := range cases {
t.Run(tc.descr, func(t *testing.T) {
actual := tc.inp.AsGregorian()
if !actual.Eq(tc.exp) {
t.Fatalf("expected %+v, got %+v", tc.exp, actual)
}
})
}
}
func TestZeroTaiIsEpoch(t *testing.T) {
var ta tai.TAI
date := ta.AsGregorian()
var failparts []string
if date.Year != 1958 {
failparts = append(failparts, fmt.Sprintf("wrong year: got %d expected %d", date.Year, 1958))
}
if date.Month != tai.January {
failparts = append(failparts, fmt.Sprintf("wrong month: got %d expected %d", date.Month, tai.January))
}
if date.Day != 1 {
failparts = append(failparts, fmt.Sprintf("wrong day: got %d expected %d", date.Day, 1))
}
if date.Hour != 0 {
failparts = append(failparts, fmt.Sprintf("wrong hour: got %d expected %d", date.Hour, 0))
}
if date.Min != 0 {
failparts = append(failparts, fmt.Sprintf("wrong minute: got %d expected %d", date.Min, 0))
}
if date.Sec != 0 {
failparts = append(failparts, fmt.Sprintf("wrong sec: got %d expected %d", date.Sec, 0))
}
if date.Asec != 0 {
failparts = append(failparts, fmt.Sprintf("wrong subsec: got %d expected %d", date.Asec, 0))
}
if len(failparts) != 0 {
t.Fatal(strings.Join(failparts, "\n"))
}
}
func TestTaiFormat(t *testing.T) {
//2009-11-10 23:00:00
ta := tai.Date(2009, 11, 10).AddHMS(23, 0, 0)
out := ta.Format(tai.RFC3339)
if out != "2009-11-10T23:00:00Z" {
t.Fail()
}
out = ta.Format(tai.RFC3339Micro)
if out != "2009-11-10T23:00:00.000000Z" {
t.Fail()
}
out = ta.Format(tai.RFC3339Nano)
if out != "2009-11-10T23:00:00.000000000Z" {
t.Fail()
}
}
func TestNowAsTimeEq(t *testing.T) {
now := tai.Now()
nowT := now.AsTime()
nowT2 := time.Now()
diff := nowT2.Sub(nowT)
if diff < 0 {
diff = -diff
}
if diff > 100*time.Millisecond {
t.Fatal("tai now and stdlib now differ by > 100 msec")
}
}
func TestFromTimeAsTimeRoundTrip(t *testing.T) {
now := time.Now()
now2 := tai.FromTime(now).AsTime()
if !now.Equal(now2) {
t.Fatal()
}
}
func TestTaiBeforeAfterEq(t *testing.T) {
t1 := tai.Tai(1, 0)
t2 := tai.Tai(1, 1)
t3 := tai.Tai(0, 1)
t4 := tai.Tai(-1, 0)
t5 := tai.Tai(0, 1)
if !t1.Before(t2) {
t.Fatal()
}
if !t1.After(t4) {
t.Fatal()
}
if !t2.After(t1) {
t.Fatal()
}
if !t3.Before(t1) {
t.Fatal()
}
if !t5.Eq(t3) {
t.Fatal()
}
}
func TestTaiNegativeAsecs(t *testing.T) {
t1 := tai.Tai(10, 1e18)
t2 := tai.Tai(11, 0)
t3 := tai.Tai(12, -1e18)
t4 := tai.Tai(12, -1e18-10)
if !t1.Eq(t2) {
t.Fatalf("t1: %+v is not equal to t2: %+v", t1, t2)
}
if !t3.Eq(t2) {
t.Fatalf("t2: %+v is not equal to t3: %+v", t2, t3)
}
// remaining attoseconds should be positive and seconds decremented
if !t2.After(t4) {
t.Fatalf("t2: %+v is not after to t4: %+v", t2, t4)
}
}
func TestTaiAdd(t *testing.T) {
t1 := tai.Tai(10, 5)
t2 := tai.Tai(13, 6)
t3 := t1.Add(3, 1)
t4 := tai.Tai(12, -1e18-10)
t5 := t4.Add(0, 16+2*1e18)
t6 := tai.Tai(100, 1)
t7 := t6.Add(-78, -9*1e18).Add(0, -9*1e18).Add(0, 9*1e18+5)
if !t3.Eq(t2) {
t.Fatalf("t2: %+v not equal to t3: %+v", t2, t3)
}
if !t5.Eq(t2) {
t.Fatalf("t2: %+v not equal to t5: %+v", t2, t5)
}
if !t7.Eq(t2) {
t.Fatalf("t2: %+v not equal to t7: %+v", t2, t7)
}
}
func TestTaiAddMilliseconds(t *testing.T) {
t1 := tai.Tai(0, 0)
t2 := tai.Tai(9e15, 0)
t3 := t1.AddMilliseconds(9e18)
t4 := tai.Tai(100, 100)
t5 := t4.AddMilliseconds(-1e5)
t6 := tai.Tai(0, 100)
if !t3.Eq(t2) {
t.Fatalf("t2: %+v not equal to t3: %+v", t2, t3)
}
if !t5.Eq(t6) {
t.Fatalf("t6: %+v not equal to t5: %+v", t6, t5)
}
}
func TestTaiAddMicroseconds(t *testing.T) {
t1 := tai.Tai(0, 0)
t2 := tai.Tai(9e12, 0)
t3 := t1.AddMicroseconds(9e18)
t4 := tai.Tai(100, 100)
t5 := t4.AddMicroseconds(-1e8)
t6 := tai.Tai(0, 100)
if !t3.Eq(t2) {
t.Fatalf("t2: %+v not equal to t3: %+v", t2, t3)
}
if !t5.Eq(t6) {
t.Fatalf("t6: %+v not equal to t5: %+v", t6, t5)
}
}
func TestTaiAddNanoseconds(t *testing.T) {
t1 := tai.Tai(0, 0)
t2 := tai.Tai(9e9, 0)
t3 := t1.AddNanoseconds(9e18)
t4 := tai.Tai(100, 100)
t5 := t4.AddNanoseconds(-1e11)
t6 := tai.Tai(0, 100)
if !t3.Eq(t2) {
t.Fatalf("t2: %+v not equal to t3: %+v", t2, t3)
}
if !t5.Eq(t6) {
t.Fatalf("t6: %+v not equal to t5: %+v", t6, t5)
}
}
func TestUnixEpoch(t *testing.T) {
ta := tai.Tai(4383*tai.Day, 0)
date := ta.AsGregorian()
var failparts []string
if date.Year != 1970 {
failparts = append(failparts, fmt.Sprintf("wrong year: got %d expected %d", date.Year, 1970))
}
if date.Month != tai.January {
failparts = append(failparts, fmt.Sprintf("wrong month: got %d expected %d", date.Month, tai.January))
}
if date.Day != 1 {
failparts = append(failparts, fmt.Sprintf("wrong day: got %d expected %d", date.Day, 1))
}
if date.Hour != 0 {
failparts = append(failparts, fmt.Sprintf("wrong hour: got %d expected %d", date.Hour, 0))
}
if date.Min != 0 {
failparts = append(failparts, fmt.Sprintf("wrong minute: got %d expected %d", date.Min, 0))
}
if date.Sec != 0 {
failparts = append(failparts, fmt.Sprintf("wrong sec: got %d expected %d", date.Sec, 0))
}
if date.Asec != 0 {
failparts = append(failparts, fmt.Sprintf("wrong subsec: got %d expected %d", date.Asec, 0))
}
if len(failparts) != 0 {
t.Fatal(strings.Join(failparts, "\n"))
}
}
func BenchmarkTaiAsTime(b *testing.B) {
now := tai.Now()
for i := 0; i < b.N; i++ {
now.AsTime()
}
}
func BenchmarkTaiFormat(b *testing.B) {
now := tai.Now()
for i := 0; i < b.N; i++ {
now.Format(tai.RFC3339Micro)
}
}
// top level result of these two benchmarks: can reduce space by > 50% without
// compromising time -> do so (keep changes)
func BenchmarkAsGregorianWithoutFmt(b *testing.B) {
// 23.67 ns with all int64s
// 22.99 ns with some uint8s
now := tai.Now()
for i := 0; i < b.N; i++ {
now.AsGregorian()
}
}
func BenchmarkAsGregorianWithFmt(b *testing.B) {
// 369.8 ns with all int64s
// 364.6 ns with some uint8s
now := tai.Now()
for i := 0; i < b.N; i++ {
g := now.AsGregorian()
fmt.Sprintf("%d %d %d %d %d %d %d", g.Year, g.Month, g.Day, g.Hour, g.Min, g.Sec, g.Asec)
}
}
func BenchmarkTimeWithoutFmt(b *testing.B) {
// 35.92 ns; tai ~= 33% faster
now := time.Now()
for i := 0; i < b.N; i++ {
now.Date()
now.Second()
now.Nanosecond()
}
}
func BenchmarkTimeFormat(b *testing.B) {
now := time.Now()
for i := 0; i < b.N; i++ {
now.Format(time.RFC3339Nano)
}
}