-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgigasecond_test.go
58 lines (52 loc) · 1.33 KB
/
gigasecond_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
package gigasecond
// Write a function AddGigasecond that works with time.Time.
import (
"os"
"testing"
"time"
)
// date formats used in test data
const (
fmtD = "2006-01-02"
fmtDT = "2006-01-02T15:04:05"
)
func TestAddGigasecond(t *testing.T) {
for _, tc := range addCases {
in := parse(tc.in, t)
want := parse(tc.want, t)
got := AddGigasecond(in)
if !got.Equal(want) {
t.Fatalf(`FAIL: %s
AddGigasecond(%s)
= %s
want %s`, tc.description, in, got, want)
}
t.Log("PASS:", tc.description)
}
t.Log("Tested", len(addCases), "cases.")
}
func parse(s string, t *testing.T) time.Time {
tt, err := time.Parse(fmtDT, s) // try full date time format first
if err != nil {
tt, err = time.Parse(fmtD, s) // also allow just date
}
if err != nil {
// can't run tests if input won't parse. if this seems to be a
// development or ci environment, raise an error. if this condition
// makes it to the solver though, ask for a bug report.
_, statErr := os.Stat("example_gen.go")
if statErr == nil || os.Getenv("TRAVIS_GO_VERSION") > "" {
t.Fatal(err)
} else {
t.Log(err)
t.Skip("(This is not your fault, and is unexpected. " +
"Please file an issue at https://github.com/exercism/go.)")
}
}
return tt
}
func BenchmarkAddGigasecond(b *testing.B) {
for i := 0; i < b.N; i++ {
AddGigasecond(time.Time{})
}
}