Initial Commit
This commit is contained in:
19
go/gigasecond/README.md
Normal file
19
go/gigasecond/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Gigasecond
|
||||
|
||||
Write a program that will calculate the date that someone turned or will celebrate their 1 Gs anniversary.
|
||||
|
||||
A gigasecond is one billion (10**9) seconds.
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
Chapter 9 in Chris Pine's online Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=09)
|
31
go/gigasecond/cases_test.go
Normal file
31
go/gigasecond/cases_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package gigasecond
|
||||
|
||||
// Source: exercism/x-common
|
||||
// Commit: 1e9e232 Merge pull request #45 from soniakeys/gigasecond-tests
|
||||
|
||||
// Add one gigasecond to the input.
|
||||
var addCases = []struct {
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"2011-04-25",
|
||||
"2043-01-01T01:46:40",
|
||||
},
|
||||
{
|
||||
"1977-06-13",
|
||||
"2009-02-19T01:46:40",
|
||||
},
|
||||
{
|
||||
"1959-07-19",
|
||||
"1991-03-27T01:46:40",
|
||||
},
|
||||
{
|
||||
"2015-01-24T22:00:00",
|
||||
"2046-10-02T23:46:40",
|
||||
},
|
||||
{
|
||||
"2015-01-24T23:59:59",
|
||||
"2046-10-03T01:46:39",
|
||||
},
|
||||
}
|
14
go/gigasecond/gigasecond.go
Normal file
14
go/gigasecond/gigasecond.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package gigasecond
|
||||
|
||||
import "time"
|
||||
|
||||
// TestVersion
|
||||
const TestVersion = 2 // find the value in gigasecond_test.go
|
||||
|
||||
// AddGigasecond Adds a gigasecond to the time t
|
||||
func AddGigasecond(t time.Time) time.Time {
|
||||
return t.Add(time.Second * 1000000000)
|
||||
}
|
||||
|
||||
// Birthday is my fake birthday
|
||||
var Birthday = time.Date(1979, 4, 23, 10, 2, 0, 0, time.UTC)
|
73
go/gigasecond/gigasecond_test.go
Normal file
73
go/gigasecond/gigasecond_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package gigasecond
|
||||
|
||||
// Write a function AddGigasecond that works with time.Time.
|
||||
// Also define a variable Birthday set to your (or someone else's) birthday.
|
||||
// Run go test -v to see your gigasecond anniversary.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testVersion = 2
|
||||
|
||||
// Retired testVersions
|
||||
// (none) 98807b314216ff27492378a00df60410cc971d32
|
||||
// 1 ed0594b6fd6664928d17bbc70b543a56da05a5b8
|
||||
|
||||
// date formats used in test data
|
||||
const (
|
||||
fmtD = "2006-01-02"
|
||||
fmtDT = "2006-01-02T15:04:05"
|
||||
)
|
||||
|
||||
func TestAddGigasecond(t *testing.T) {
|
||||
if TestVersion != testVersion {
|
||||
t.Fatalf("Found TestVersion = %v, want %v.", TestVersion, testVersion)
|
||||
}
|
||||
for _, tc := range addCases {
|
||||
in := parse(tc.in, t)
|
||||
want := parse(tc.want, t)
|
||||
got := AddGigasecond(in)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf(`AddGigasecond(%s)
|
||||
= %s
|
||||
want %s`, in, got, want)
|
||||
}
|
||||
}
|
||||
t.Log("Tested", len(addCases), "cases.")
|
||||
}
|
||||
|
||||
func TestYourAnniversary(t *testing.T) {
|
||||
t.Logf(`
|
||||
Your birthday: %s
|
||||
Your gigasecond anniversary: %s`, Birthday, AddGigasecond(Birthday))
|
||||
}
|
||||
|
||||
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("(Not your problem. " +
|
||||
"please file issue at https://github.com/exercism/xgo.)")
|
||||
}
|
||||
}
|
||||
return tt
|
||||
}
|
||||
|
||||
func BenchmarkAddGigasecond(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
AddGigasecond(time.Time{})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user