summaryrefslogtreecommitdiff
path: root/go/gigasecond
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-25 03:13:39 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-25 03:13:39 +0200
commit509c5063d66e8bbef4ec1def1c99c318be51aceb (patch)
treeafc811c4781a4e317043e2a0237499defc168044 /go/gigasecond
Initial import
Diffstat (limited to 'go/gigasecond')
-rw-r--r--go/gigasecond/README.md23
-rw-r--r--go/gigasecond/cases_test.go31
-rw-r--r--go/gigasecond/gigasecond.go9
-rw-r--r--go/gigasecond/gigasecond_test.go63
4 files changed, 126 insertions, 0 deletions
diff --git a/go/gigasecond/README.md b/go/gigasecond/README.md
new file mode 100644
index 0000000..788f4c0
--- /dev/null
+++ b/go/gigasecond/README.md
@@ -0,0 +1,23 @@
+# Gigasecond
+
+Write a program that calculates the moment when someone has lived for 10^9 seconds.
+
+A gigasecond is 10^9 (1,000,000,000) 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://exercism.io/languages/go).
+
+## Source
+
+Chapter 9 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=09](http://pine.fm/LearnToProgram/?Chapter=09)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/gigasecond/cases_test.go b/go/gigasecond/cases_test.go
new file mode 100644
index 0000000..520101b
--- /dev/null
+++ b/go/gigasecond/cases_test.go
@@ -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",
+ },
+}
diff --git a/go/gigasecond/gigasecond.go b/go/gigasecond/gigasecond.go
new file mode 100644
index 0000000..dd3f310
--- /dev/null
+++ b/go/gigasecond/gigasecond.go
@@ -0,0 +1,9 @@
+package gigasecond
+
+import "time"
+
+const testVersion = 4
+
+func AddGigasecond(t time.Time) time.Time {
+ return t.Add(1e9 * time.Second)
+}
diff --git a/go/gigasecond/gigasecond_test.go b/go/gigasecond/gigasecond_test.go
new file mode 100644
index 0000000..96642cf
--- /dev/null
+++ b/go/gigasecond/gigasecond_test.go
@@ -0,0 +1,63 @@
+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 targetTestVersion = 4
+
+// date formats used in test data
+const (
+ fmtD = "2006-01-02"
+ fmtDT = "2006-01-02T15:04:05"
+)
+
+func TestAddGigasecond(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
+ }
+ 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 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{})
+ }
+}