OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [time/] [time_test.go] - Blame information for rev 761

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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_test
6
 
7
import (
8
        "bytes"
9
        "encoding/gob"
10
        "encoding/json"
11
        "math/rand"
12
        "strconv"
13
        "strings"
14
        "testing"
15
        "testing/quick"
16
        . "time"
17
)
18
 
19
// We should be in PST/PDT, but if the time zone files are missing we
20
// won't be. The purpose of this test is to at least explain why some of
21
// the subsequent tests fail.
22
func TestZoneData(t *testing.T) {
23
        lt := Now()
24
        // PST is 8 hours west, PDT is 7 hours west.  We could use the name but it's not unique.
25
        if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 {
26
                t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off)
27
                t.Error("Likely problem: the time zone files have not been installed.")
28
        }
29
}
30
 
31
// parsedTime is the struct representing a parsed time value.
32
type parsedTime struct {
33
        Year                 int
34
        Month                Month
35
        Day                  int
36
        Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
37
        Nanosecond           int // Fractional second.
38
        Weekday              Weekday
39
        ZoneOffset           int    // seconds east of UTC, e.g. -7*60*60 for -0700
40
        Zone                 string // e.g., "MST"
41
}
42
 
43
type TimeTest struct {
44
        seconds int64
45
        golden  parsedTime
46
}
47
 
48
var utctests = []TimeTest{
49
        {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}},
50
        {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}},
51
        {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UTC"}},
52
        {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC"}},
53
        {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC"}},
54
        {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"}},
55
}
56
 
57
var nanoutctests = []TimeTest{
58
        {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}},
59
        {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}},
60
}
61
 
62
var localtests = []TimeTest{
63
        {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}},
64
        {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}},
65
}
66
 
67
var nanolocaltests = []TimeTest{
68
        {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 60, "PST"}},
69
        {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}},
70
}
71
 
72
func same(t Time, u *parsedTime) bool {
73
        // Check aggregates.
74
        year, month, day := t.Date()
75
        hour, min, sec := t.Clock()
76
        name, offset := t.Zone()
77
        if year != u.Year || month != u.Month || day != u.Day ||
78
                hour != u.Hour || min != u.Minute || sec != u.Second ||
79
                name != u.Zone || offset != u.ZoneOffset {
80
                return false
81
        }
82
        // Check individual entries.
83
        return t.Year() == u.Year &&
84
                t.Month() == u.Month &&
85
                t.Day() == u.Day &&
86
                t.Hour() == u.Hour &&
87
                t.Minute() == u.Minute &&
88
                t.Second() == u.Second &&
89
                t.Nanosecond() == u.Nanosecond &&
90
                t.Weekday() == u.Weekday
91
}
92
 
93
func TestSecondsToUTC(t *testing.T) {
94
        for _, test := range utctests {
95
                sec := test.seconds
96
                golden := &test.golden
97
                tm := Unix(sec, 0).UTC()
98
                newsec := tm.Unix()
99
                if newsec != sec {
100
                        t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec)
101
                }
102
                if !same(tm, golden) {
103
                        t.Errorf("SecondsToUTC(%d):  // %#v", sec, tm)
104
                        t.Errorf("  want=%+v", *golden)
105
                        t.Errorf("  have=%v", tm.Format(RFC3339+" MST"))
106
                }
107
        }
108
}
109
 
110
func TestNanosecondsToUTC(t *testing.T) {
111
        for _, test := range nanoutctests {
112
                golden := &test.golden
113
                nsec := test.seconds*1e9 + int64(golden.Nanosecond)
114
                tm := Unix(0, nsec).UTC()
115
                newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
116
                if newnsec != nsec {
117
                        t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec, newnsec)
118
                }
119
                if !same(tm, golden) {
120
                        t.Errorf("NanosecondsToUTC(%d):", nsec)
121
                        t.Errorf("  want=%+v", *golden)
122
                        t.Errorf("  have=%+v", tm.Format(RFC3339+" MST"))
123
                }
124
        }
125
}
126
 
127
func TestSecondsToLocalTime(t *testing.T) {
128
        for _, test := range localtests {
129
                sec := test.seconds
130
                golden := &test.golden
131
                tm := Unix(sec, 0)
132
                newsec := tm.Unix()
133
                if newsec != sec {
134
                        t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, newsec)
135
                }
136
                if !same(tm, golden) {
137
                        t.Errorf("SecondsToLocalTime(%d):", sec)
138
                        t.Errorf("  want=%+v", *golden)
139
                        t.Errorf("  have=%+v", tm.Format(RFC3339+" MST"))
140
                }
141
        }
142
}
143
 
144
func TestNanosecondsToLocalTime(t *testing.T) {
145
        for _, test := range nanolocaltests {
146
                golden := &test.golden
147
                nsec := test.seconds*1e9 + int64(golden.Nanosecond)
148
                tm := Unix(0, nsec)
149
                newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
150
                if newnsec != nsec {
151
                        t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", nsec, newnsec)
152
                }
153
                if !same(tm, golden) {
154
                        t.Errorf("NanosecondsToLocalTime(%d):", nsec)
155
                        t.Errorf("  want=%+v", *golden)
156
                        t.Errorf("  have=%+v", tm.Format(RFC3339+" MST"))
157
                }
158
        }
159
}
160
 
161
func TestSecondsToUTCAndBack(t *testing.T) {
162
        f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
163
        f32 := func(sec int32) bool { return f(int64(sec)) }
164
        cfg := &quick.Config{MaxCount: 10000}
165
 
166
        // Try a reasonable date first, then the huge ones.
167
        if err := quick.Check(f32, cfg); err != nil {
168
                t.Fatal(err)
169
        }
170
        if err := quick.Check(f, cfg); err != nil {
171
                t.Fatal(err)
172
        }
173
}
174
 
175
func TestNanosecondsToUTCAndBack(t *testing.T) {
176
        f := func(nsec int64) bool {
177
                t := Unix(0, nsec).UTC()
178
                ns := t.Unix()*1e9 + int64(t.Nanosecond())
179
                return ns == nsec
180
        }
181
        f32 := func(nsec int32) bool { return f(int64(nsec)) }
182
        cfg := &quick.Config{MaxCount: 10000}
183
 
184
        // Try a small date first, then the large ones. (The span is only a few hundred years
185
        // for nanoseconds in an int64.)
186
        if err := quick.Check(f32, cfg); err != nil {
187
                t.Fatal(err)
188
        }
189
        if err := quick.Check(f, cfg); err != nil {
190
                t.Fatal(err)
191
        }
192
}
193
 
194
type TimeFormatTest struct {
195
        time           Time
196
        formattedValue string
197
}
198
 
199
var rfc3339Formats = []TimeFormatTest{
200
        {Date(2008, 9, 17, 20, 4, 26, 0, UTC), "2008-09-17T20:04:26Z"},
201
        {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T20:04:26-05:00"},
202
        {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T01:15:06+04:20"},
203
}
204
 
205
func TestRFC3339Conversion(t *testing.T) {
206
        for _, f := range rfc3339Formats {
207
                if f.time.Format(RFC3339) != f.formattedValue {
208
                        t.Error("RFC3339:")
209
                        t.Errorf("  want=%+v", f.formattedValue)
210
                        t.Errorf("  have=%+v", f.time.Format(RFC3339))
211
                }
212
        }
213
}
214
 
215
type FormatTest struct {
216
        name   string
217
        format string
218
        result string
219
}
220
 
221
var formatTests = []FormatTest{
222
        {"ANSIC", ANSIC, "Wed Feb  4 21:00:57 2009"},
223
        {"UnixDate", UnixDate, "Wed Feb  4 21:00:57 PST 2009"},
224
        {"RubyDate", RubyDate, "Wed Feb 04 21:00:57 -0800 2009"},
225
        {"RFC822", RFC822, "04 Feb 09 2100 PST"},
226
        {"RFC850", RFC850, "Wednesday, 04-Feb-09 21:00:57 PST"},
227
        {"RFC1123", RFC1123, "Wed, 04 Feb 2009 21:00:57 PST"},
228
        {"RFC1123Z", RFC1123Z, "Wed, 04 Feb 2009 21:00:57 -0800"},
229
        {"RFC3339", RFC3339, "2009-02-04T21:00:57-08:00"},
230
        {"Kitchen", Kitchen, "9:00PM"},
231
        {"am/pm", "3pm", "9pm"},
232
        {"AM/PM", "3PM", "9PM"},
233
        {"two-digit year", "06 01 02", "09 02 04"},
234
        // Time stamps, Fractional seconds.
235
        {"Stamp", Stamp, "Feb  4 21:00:57"},
236
        {"StampMilli", StampMilli, "Feb  4 21:00:57.012"},
237
        {"StampMicro", StampMicro, "Feb  4 21:00:57.012345"},
238
        {"StampNano", StampNano, "Feb  4 21:00:57.012345678"},
239
}
240
 
241
func TestFormat(t *testing.T) {
242
        // The numeric time represents Thu Feb  4 21:00:57.012345678 PST 2010
243
        time := Unix(0, 1233810057012345678)
244
        for _, test := range formatTests {
245
                result := time.Format(test.format)
246
                if result != test.result {
247
                        t.Errorf("%s expected %q got %q", test.name, test.result, result)
248
                }
249
        }
250
}
251
 
252
type ParseTest struct {
253
        name       string
254
        format     string
255
        value      string
256
        hasTZ      bool // contains a time zone
257
        hasWD      bool // contains a weekday
258
        yearSign   int  // sign of year
259
        fracDigits int  // number of digits of fractional second
260
}
261
 
262
var parseTests = []ParseTest{
263
        {"ANSIC", ANSIC, "Thu Feb  4 21:00:57 2010", false, true, 1, 0},
264
        {"UnixDate", UnixDate, "Thu Feb  4 21:00:57 PST 2010", true, true, 1, 0},
265
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
266
        {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0},
267
        {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
268
        {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
269
        {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
270
        {"custom: \"2006-01-02 15:04:05-07\"", "2006-01-02 15:04:05-07", "2010-02-04 21:00:57-08", true, false, 1, 0},
271
        // Optional fractional seconds.
272
        {"ANSIC", ANSIC, "Thu Feb  4 21:00:57.0 2010", false, true, 1, 1},
273
        {"UnixDate", UnixDate, "Thu Feb  4 21:00:57.01 PST 2010", true, true, 1, 2},
274
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3},
275
        {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4},
276
        {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5},
277
        {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5},
278
        {"RFC3339", RFC3339, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9},
279
        // Amount of white space should not matter.
280
        {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
281
        {"ANSIC", ANSIC, "Thu      Feb     4     21:00:57     2010", false, true, 1, 0},
282
        // Case should not matter
283
        {"ANSIC", ANSIC, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
284
        {"ANSIC", ANSIC, "thu feb 4 21:00:57 2010", false, true, 1, 0},
285
        // Fractional seconds.
286
        {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 21:00:57.012 2010", false, true, 1, 3},
287
        {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb  4 21:00:57.012345 2010", false, true, 1, 6},
288
        {"nanosecond", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb  4 21:00:57.012345678 2010", false, true, 1, 9},
289
        // Leading zeros in other places should not be taken as fractional seconds.
290
        {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1},
291
        {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2},
292
}
293
 
294
func TestParse(t *testing.T) {
295
        for _, test := range parseTests {
296
                time, err := Parse(test.format, test.value)
297
                if err != nil {
298
                        t.Errorf("%s error: %v", test.name, err)
299
                } else {
300
                        checkTime(time, &test, t)
301
                }
302
        }
303
}
304
 
305
var rubyTests = []ParseTest{
306
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
307
        // Ignore the time zone in the test. If it parses, it'll be OK.
308
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
309
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
310
        {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
311
}
312
 
313
// Problematic time zone format needs special tests.
314
func TestRubyParse(t *testing.T) {
315
        for _, test := range rubyTests {
316
                time, err := Parse(test.format, test.value)
317
                if err != nil {
318
                        t.Errorf("%s error: %v", test.name, err)
319
                } else {
320
                        checkTime(time, &test, t)
321
                }
322
        }
323
}
324
 
325
func checkTime(time Time, test *ParseTest, t *testing.T) {
326
        // The time should be Thu Feb  4 21:00:57 PST 2010
327
        if test.yearSign*time.Year() != 2010 {
328
                t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010)
329
        }
330
        if time.Month() != February {
331
                t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February)
332
        }
333
        if time.Day() != 4 {
334
                t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
335
        }
336
        if time.Hour() != 21 {
337
                t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
338
        }
339
        if time.Minute() != 0 {
340
                t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
341
        }
342
        if time.Second() != 57 {
343
                t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
344
        }
345
        // Nanoseconds must be checked against the precision of the input.
346
        nanosec, err := strconv.ParseUint("012345678"[:test.fracDigits]+"000000000"[:9-test.fracDigits], 10, 0)
347
        if err != nil {
348
                panic(err)
349
        }
350
        if time.Nanosecond() != int(nanosec) {
351
                t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec)
352
        }
353
        name, offset := time.Zone()
354
        if test.hasTZ && offset != -28800 {
355
                t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, offset, -28800)
356
        }
357
        if test.hasWD && time.Weekday() != Thursday {
358
                t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday)
359
        }
360
}
361
 
362
func TestFormatAndParse(t *testing.T) {
363
        const fmt = "Mon MST " + RFC3339 // all fields
364
        f := func(sec int64) bool {
365
                t1 := Unix(sec, 0)
366
                if t1.Year() < 1000 || t1.Year() > 9999 {
367
                        // not required to work
368
                        return true
369
                }
370
                t2, err := Parse(fmt, t1.Format(fmt))
371
                if err != nil {
372
                        t.Errorf("error: %s", err)
373
                        return false
374
                }
375
                if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
376
                        t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix())
377
                        return false
378
                }
379
                return true
380
        }
381
        f32 := func(sec int32) bool { return f(int64(sec)) }
382
        cfg := &quick.Config{MaxCount: 10000}
383
 
384
        // Try a reasonable date first, then the huge ones.
385
        if err := quick.Check(f32, cfg); err != nil {
386
                t.Fatal(err)
387
        }
388
        if err := quick.Check(f, cfg); err != nil {
389
                t.Fatal(err)
390
        }
391
}
392
 
393
type ParseErrorTest struct {
394
        format string
395
        value  string
396
        expect string // must appear within the error
397
}
398
 
399
var parseErrorTests = []ParseErrorTest{
400
        {ANSIC, "Feb  4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
401
        {ANSIC, "Thu Feb  4 21:00:57 @2010", "cannot parse"},
402
        {ANSIC, "Thu Feb  4 21:00:60 2010", "second out of range"},
403
        {ANSIC, "Thu Feb  4 21:61:57 2010", "minute out of range"},
404
        {ANSIC, "Thu Feb  4 24:00:60 2010", "hour out of range"},
405
        {"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59x01 2010", "cannot parse"},
406
        {"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59.xxx 2010", "cannot parse"},
407
        {"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59.-123 2010", "fractional second out of range"},
408
}
409
 
410
func TestParseErrors(t *testing.T) {
411
        for _, test := range parseErrorTests {
412
                _, err := Parse(test.format, test.value)
413
                if err == nil {
414
                        t.Errorf("expected error for %q %q", test.format, test.value)
415
                } else if strings.Index(err.Error(), test.expect) < 0 {
416
                        t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err)
417
                }
418
        }
419
}
420
 
421
func TestNoonIs12PM(t *testing.T) {
422
        noon := Date(0, January, 1, 12, 0, 0, 0, UTC)
423
        const expect = "12:00PM"
424
        got := noon.Format("3:04PM")
425
        if got != expect {
426
                t.Errorf("got %q; expect %q", got, expect)
427
        }
428
        got = noon.Format("03:04PM")
429
        if got != expect {
430
                t.Errorf("got %q; expect %q", got, expect)
431
        }
432
}
433
 
434
func TestMidnightIs12AM(t *testing.T) {
435
        midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
436
        expect := "12:00AM"
437
        got := midnight.Format("3:04PM")
438
        if got != expect {
439
                t.Errorf("got %q; expect %q", got, expect)
440
        }
441
        got = midnight.Format("03:04PM")
442
        if got != expect {
443
                t.Errorf("got %q; expect %q", got, expect)
444
        }
445
}
446
 
447
func Test12PMIsNoon(t *testing.T) {
448
        noon, err := Parse("3:04PM", "12:00PM")
449
        if err != nil {
450
                t.Fatal("error parsing date:", err)
451
        }
452
        if noon.Hour() != 12 {
453
                t.Errorf("got %d; expect 12", noon.Hour())
454
        }
455
        noon, err = Parse("03:04PM", "12:00PM")
456
        if err != nil {
457
                t.Fatal("error parsing date:", err)
458
        }
459
        if noon.Hour() != 12 {
460
                t.Errorf("got %d; expect 12", noon.Hour())
461
        }
462
}
463
 
464
func Test12AMIsMidnight(t *testing.T) {
465
        midnight, err := Parse("3:04PM", "12:00AM")
466
        if err != nil {
467
                t.Fatal("error parsing date:", err)
468
        }
469
        if midnight.Hour() != 0 {
470
                t.Errorf("got %d; expect 0", midnight.Hour())
471
        }
472
        midnight, err = Parse("03:04PM", "12:00AM")
473
        if err != nil {
474
                t.Fatal("error parsing date:", err)
475
        }
476
        if midnight.Hour() != 0 {
477
                t.Errorf("got %d; expect 0", midnight.Hour())
478
        }
479
}
480
 
481
// Check that a time without a Zone still produces a (numeric) time zone
482
// when formatted with MST as a requested zone.
483
func TestMissingZone(t *testing.T) {
484
        time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006")
485
        if err != nil {
486
                t.Fatal("error parsing date:", err)
487
        }
488
        expect := "Thu Feb  2 16:10:03 -0500 2006" // -0500 not EST
489
        str := time.Format(UnixDate)               // uses MST as its time zone
490
        if str != expect {
491
                t.Errorf("got %s; expect %s", str, expect)
492
        }
493
}
494
 
495
func TestMinutesInTimeZone(t *testing.T) {
496
        time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
497
        if err != nil {
498
                t.Fatal("error parsing date:", err)
499
        }
500
        expected := (1*60 + 23) * 60
501
        _, offset := time.Zone()
502
        if offset != expected {
503
                t.Errorf("ZoneOffset = %d, want %d", offset, expected)
504
        }
505
}
506
 
507
type ISOWeekTest struct {
508
        year       int // year
509
        month, day int // month and day
510
        yex        int // expected year
511
        wex        int // expected week
512
}
513
 
514
var isoWeekTests = []ISOWeekTest{
515
        {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
516
        {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
517
        {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
518
        {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
519
        {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
520
        {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
521
        {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
522
        {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
523
        {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
524
        {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
525
        {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
526
        {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
527
        {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
528
        {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
529
        {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
530
        {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
531
        {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
532
        {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
533
        {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
534
        {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
535
        {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
536
        {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
537
        {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
538
        {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
539
        {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
540
        {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
541
        {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
542
        {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
543
        {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
544
        {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
545
        {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
546
        {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
547
        {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
548
}
549
 
550
func TestISOWeek(t *testing.T) {
551
        // Selected dates and corner cases
552
        for _, wt := range isoWeekTests {
553
                dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC)
554
                y, w := dt.ISOWeek()
555
                if w != wt.wex || y != wt.yex {
556
                        t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
557
                                y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day)
558
                }
559
        }
560
 
561
        // The only real invariant: Jan 04 is in week 1
562
        for year := 1950; year < 2100; year++ {
563
                if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 {
564
                        t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, year)
565
                }
566
        }
567
}
568
 
569
var durationTests = []struct {
570
        str string
571
        d   Duration
572
}{
573
        {"0", 0},
574
        {"1ns", 1 * Nanosecond},
575
        {"1.1us", 1100 * Nanosecond},
576
        {"2.2ms", 2200 * Microsecond},
577
        {"3.3s", 3300 * Millisecond},
578
        {"4m5s", 4*Minute + 5*Second},
579
        {"4m5.001s", 4*Minute + 5001*Millisecond},
580
        {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
581
        {"8m0.000000001s", 8*Minute + 1*Nanosecond},
582
        {"2562047h47m16.854775807s", 1<<63 - 1},
583
        {"-2562047h47m16.854775808s", -1 << 63},
584
}
585
 
586
func TestDurationString(t *testing.T) {
587
        for _, tt := range durationTests {
588
                if str := tt.d.String(); str != tt.str {
589
                        t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
590
                }
591
                if tt.d > 0 {
592
                        if str := (-tt.d).String(); str != "-"+tt.str {
593
                                t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
594
                        }
595
                }
596
        }
597
}
598
 
599
var dateTests = []struct {
600
        year, month, day, hour, min, sec, nsec int
601
        z                                      *Location
602
        unix                                   int64
603
}{
604
        {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400},   // 1:00:00 PDT
605
        {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999}, // 1:59:59 PDT
606
        {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600},   // 2:00:00 PST
607
 
608
        {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800},   // 1:00:00 PST
609
        {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399}, // 1:59:59 PST
610
        {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400},   // 3:00:00 PDT
611
        {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600},  // 2:30:00 PDT ≡ 1:30 PST
612
 
613
        // Many names for Fri Nov 18 7:56:35 PST 2011
614
        {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795},                 // Nov 18 7:56:35
615
        {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795},               // Nov 19 -17:56:35
616
        {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795},                // Nov 17 31:56:35
617
        {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795},                // Nov 18 6:116:35
618
        {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795},                 // Oct 49 7:56:35
619
        {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795},                 // Nov 18 7:55:95
620
        {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795},               // Nov 18 7:56:34 + 10⁹ns
621
        {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795},                // Dec -21 7:56:35
622
        {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795},                 // Jan -52 7:56:35 2012
623
        {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795},   // (Jan-2) 18 7:56:35 2012
624
        {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec+11) 18 7:56:35 2010
625
}
626
 
627
func TestDate(t *testing.T) {
628
        for _, tt := range dateTests {
629
                time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
630
                want := Unix(tt.unix, 0)
631
                if !time.Equal(want) {
632
                        t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
633
                                tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z,
634
                                time, want)
635
                }
636
        }
637
}
638
 
639
// Several ways of getting from
640
// Fri Nov 18 7:56:35 PST 2011
641
// to
642
// Thu Mar 19 7:56:35 PST 2016
643
var addDateTests = []struct {
644
        years, months, days int
645
}{
646
        {4, 4, 1},
647
        {3, 16, 1},
648
        {3, 15, 30},
649
        {5, -6, -18 - 30 - 12},
650
}
651
 
652
func TestAddDate(t *testing.T) {
653
        t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC)
654
        t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC)
655
        for _, at := range addDateTests {
656
                time := t0.AddDate(at.years, at.months, at.days)
657
                if !time.Equal(t1) {
658
                        t.Errorf("AddDate(%d, %d, %d) = %v, want %v",
659
                                at.years, at.months, at.days,
660
                                time, t1)
661
                }
662
        }
663
}
664
 
665
var daysInTests = []struct {
666
        year, month, di int
667
}{
668
        {2011, 1, 31},  // January, first month, 31 days
669
        {2011, 2, 28},  // February, non-leap year, 28 days
670
        {2012, 2, 29},  // February, leap year, 29 days
671
        {2011, 6, 30},  // June, 30 days
672
        {2011, 12, 31}, // December, last month, 31 days
673
}
674
 
675
func TestDaysIn(t *testing.T) {
676
        // The daysIn function is not exported.
677
        // Test the daysIn function via the `var DaysIn = daysIn`
678
        // statement in the internal_test.go file.
679
        for _, tt := range daysInTests {
680
                di := DaysIn(Month(tt.month), tt.year)
681
                if di != tt.di {
682
                        t.Errorf("got %d; expected %d for %d-%02d",
683
                                di, tt.di, tt.year, tt.month)
684
                }
685
        }
686
}
687
 
688
func TestAddToExactSecond(t *testing.T) {
689
        // Add an amount to the current time to round it up to the next exact second.
690
        // This test checks that the nsec field still lies within the range [0, 999999999].
691
        t1 := Now()
692
        t2 := t1.Add(Second - Duration(t1.Nanosecond()))
693
        sec := (t1.Second() + 1) % 60
694
        if t2.Second() != sec || t2.Nanosecond() != 0 {
695
                t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
696
        }
697
}
698
 
699
func equalTimeAndZone(a, b Time) bool {
700
        aname, aoffset := a.Zone()
701
        bname, boffset := b.Zone()
702
        return a.Equal(b) && aoffset == boffset && aname == bname
703
}
704
 
705
var gobTests = []Time{
706
        Date(0, 1, 2, 3, 4, 5, 6, UTC),
707
        Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
708
        Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
709
        Time{},                              // nil location
710
        Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
711
        Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
712
}
713
 
714
func TestTimeGob(t *testing.T) {
715
        var b bytes.Buffer
716
        enc := gob.NewEncoder(&b)
717
        dec := gob.NewDecoder(&b)
718
        for _, tt := range gobTests {
719
                var gobtt Time
720
                if err := enc.Encode(&tt); err != nil {
721
                        t.Errorf("%v gob Encode error = %q, want nil", tt, err)
722
                } else if err := dec.Decode(&gobtt); err != nil {
723
                        t.Errorf("%v gob Decode error = %q, want nil", tt, err)
724
                } else if !equalTimeAndZone(gobtt, tt) {
725
                        t.Errorf("Decoded time = %v, want %v", gobtt, tt)
726
                }
727
                b.Reset()
728
        }
729
}
730
 
731
var invalidEncodingTests = []struct {
732
        bytes []byte
733
        want  string
734
}{
735
        {[]byte{}, "Time.GobDecode: no data"},
736
        {[]byte{0, 2, 3}, "Time.GobDecode: unsupported version"},
737
        {[]byte{1, 2, 3}, "Time.GobDecode: invalid length"},
738
}
739
 
740
func TestInvalidTimeGob(t *testing.T) {
741
        for _, tt := range invalidEncodingTests {
742
                var ignored Time
743
                err := ignored.GobDecode(tt.bytes)
744
                if err == nil || err.Error() != tt.want {
745
                        t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want)
746
                }
747
        }
748
}
749
 
750
var notEncodableTimes = []struct {
751
        time Time
752
        want string
753
}{
754
        {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.GobEncode: zone offset has fractional minute"},
755
        {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.GobEncode: unexpected zone offset"},
756
        {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.GobEncode: unexpected zone offset"},
757
        {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.GobEncode: unexpected zone offset"},
758
}
759
 
760
func TestNotGobEncodableTime(t *testing.T) {
761
        for _, tt := range notEncodableTimes {
762
                _, err := tt.time.GobEncode()
763
                if err == nil || err.Error() != tt.want {
764
                        t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want)
765
                }
766
        }
767
}
768
 
769
var jsonTests = []struct {
770
        time Time
771
        json string
772
}{
773
        {Date(9999, 4, 12, 23, 20, 50, .52*1e9, UTC), `"9999-04-12T23:20:50.52Z"`},
774
        {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`},
775
        {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
776
}
777
 
778
func TestTimeJSON(t *testing.T) {
779
        for _, tt := range jsonTests {
780
                var jsonTime Time
781
 
782
                if jsonBytes, err := json.Marshal(tt.time); err != nil {
783
                        t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err)
784
                } else if string(jsonBytes) != tt.json {
785
                        t.Errorf("%v JSON = %q, want %q", tt.time, string(jsonBytes), tt.json)
786
                } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil {
787
                        t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err)
788
                } else if !equalTimeAndZone(jsonTime, tt.time) {
789
                        t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time)
790
                }
791
        }
792
}
793
 
794
func TestInvalidTimeJSON(t *testing.T) {
795
        var tt Time
796
        err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt)
797
        _, isParseErr := err.(*ParseError)
798
        if !isParseErr {
799
                t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err)
800
        }
801
}
802
 
803
var notJSONEncodableTimes = []struct {
804
        time Time
805
        want string
806
}{
807
        {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
808
        {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
809
}
810
 
811
func TestNotJSONEncodableTime(t *testing.T) {
812
        for _, tt := range notJSONEncodableTimes {
813
                _, err := tt.time.MarshalJSON()
814
                if err == nil || err.Error() != tt.want {
815
                        t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want)
816
                }
817
        }
818
}
819
 
820
var parseDurationTests = []struct {
821
        in   string
822
        ok   bool
823
        want Duration
824
}{
825
        // simple
826
        {"0", true, 0},
827
        {"5s", true, 5 * Second},
828
        {"30s", true, 30 * Second},
829
        {"1478s", true, 1478 * Second},
830
        // sign
831
        {"-5s", true, -5 * Second},
832
        {"+5s", true, 5 * Second},
833
        {"-0", true, 0},
834
        {"+0", true, 0},
835
        // decimal
836
        {"5.0s", true, 5 * Second},
837
        {"5.6s", true, 5*Second + 600*Millisecond},
838
        {"5.s", true, 5 * Second},
839
        {".5s", true, 500 * Millisecond},
840
        {"1.0s", true, 1 * Second},
841
        {"1.00s", true, 1 * Second},
842
        {"1.004s", true, 1*Second + 4*Millisecond},
843
        {"1.0040s", true, 1*Second + 4*Millisecond},
844
        {"100.00100s", true, 100*Second + 1*Millisecond},
845
        // different units
846
        {"10ns", true, 10 * Nanosecond},
847
        {"11us", true, 11 * Microsecond},
848
        {"12µs", true, 12 * Microsecond}, // U+00B5
849
        {"12μs", true, 12 * Microsecond}, // U+03BC
850
        {"13ms", true, 13 * Millisecond},
851
        {"14s", true, 14 * Second},
852
        {"15m", true, 15 * Minute},
853
        {"16h", true, 16 * Hour},
854
        // composite durations
855
        {"3h30m", true, 3*Hour + 30*Minute},
856
        {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond},
857
        {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
858
        {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
859
        {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
860
 
861
        // errors
862
        {"", false, 0},
863
        {"3", false, 0},
864
        {"-", false, 0},
865
        {"s", false, 0},
866
        {".", false, 0},
867
        {"-.", false, 0},
868
        {".s", false, 0},
869
        {"+.s", false, 0},
870
}
871
 
872
func TestParseDuration(t *testing.T) {
873
        for _, tc := range parseDurationTests {
874
                d, err := ParseDuration(tc.in)
875
                if tc.ok && (err != nil || d != tc.want) {
876
                        t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
877
                } else if !tc.ok && err == nil {
878
                        t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
879
                }
880
        }
881
}
882
 
883
func TestParseDurationRoundTrip(t *testing.T) {
884
        for i := 0; i < 100; i++ {
885
                // Resolutions finer than milliseconds will result in
886
                // imprecise round-trips.
887
                d0 := Duration(rand.Int31()) * Millisecond
888
                s := d0.String()
889
                d1, err := ParseDuration(s)
890
                if err != nil || d0 != d1 {
891
                        t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
892
                }
893
        }
894
}
895
 
896
func BenchmarkNow(b *testing.B) {
897
        for i := 0; i < b.N; i++ {
898
                Now()
899
        }
900
}
901
 
902
func BenchmarkFormat(b *testing.B) {
903
        time := Unix(1265346057, 0)
904
        for i := 0; i < b.N; i++ {
905
                time.Format("Mon Jan  2 15:04:05 2006")
906
        }
907
}
908
 
909
func BenchmarkParse(b *testing.B) {
910
        for i := 0; i < b.N; i++ {
911
                Parse(ANSIC, "Mon Jan  2 15:04:05 2006")
912
        }
913
}
914
 
915
func BenchmarkHour(b *testing.B) {
916
        t := Now()
917
        for i := 0; i < b.N; i++ {
918
                _ = t.Hour()
919
        }
920
}
921
 
922
func BenchmarkSecond(b *testing.B) {
923
        t := Now()
924
        for i := 0; i < b.N; i++ {
925
                _ = t.Second()
926
        }
927
}
928
 
929
func BenchmarkYear(b *testing.B) {
930
        t := Now()
931
        for i := 0; i < b.N; i++ {
932
                _ = t.Year()
933
        }
934
}
935
 
936
func BenchmarkDay(b *testing.B) {
937
        t := Now()
938
        for i := 0; i < b.N; i++ {
939
                _ = t.Day()
940
        }
941
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.