commit 9bbd3ed136e79e79f64ab5ebe8c8481641c5a8f0
parent 995f4b4856a459248b5c7707efddff7049c52521
Author: dwrz <dwrz@dwrz.net>
Date: Sun, 3 Mar 2019 02:36:28 +0000
Add go/gigasecond
Diffstat:
4 files changed, 135 insertions(+), 0 deletions(-)
diff --git a/go/gigasecond/README.md b/go/gigasecond/README.md
@@ -0,0 +1,29 @@
+# Gigasecond
+
+Calculate the moment when someone has lived for 10^9 seconds.
+
+A gigasecond is 10^9 (1,000,000,000) seconds.
+
+## Running the tests
+
+To run the tests run the command `go test` from within the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
+flags:
+
+ go test -v --bench . --benchmem
+
+Keep in mind that each reviewer will run benchmarks on a different machine, with
+different specs, so the results from these benchmark tests may vary.
+
+## Further information
+
+For more detailed information about the Go track, including how to get help if
+you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources).
+
+## 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 Solutions
+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
@@ -0,0 +1,38 @@
+package gigasecond
+
+// Source: exercism/problem-specifications
+// Commit: 5506bac gigasecond: Apply new "input" policy
+// Problem Specifications Version: 1.1.0
+
+// Add one gigasecond to the input.
+var addCases = []struct {
+ description string
+ in string
+ want string
+}{
+ {
+ "date only specification of time",
+ "2011-04-25",
+ "2043-01-01T01:46:40",
+ },
+ {
+ "second test for date only specification of time",
+ "1977-06-13",
+ "2009-02-19T01:46:40",
+ },
+ {
+ "third test for date only specification of time",
+ "1959-07-19",
+ "1991-03-27T01:46:40",
+ },
+ {
+ "full time specified",
+ "2015-01-24T22:00:00",
+ "2046-10-02T23:46:40",
+ },
+ {
+ "full time with day roll-over",
+ "2015-01-24T23:59:59",
+ "2046-10-03T01:46:39",
+ },
+}
diff --git a/go/gigasecond/gigasecond.go b/go/gigasecond/gigasecond.go
@@ -0,0 +1,10 @@
+// Package gigasecond provides functions that modify time by gigasecond values.
+package gigasecond
+
+import "time"
+
+// AddGigasecond returns a time that is one gigasecond into the future from the
+// input time, t.
+func AddGigasecond(t time.Time) time.Time {
+ return t.Add(1000000000 * time.Second)
+}
diff --git a/go/gigasecond/gigasecond_test.go b/go/gigasecond/gigasecond_test.go
@@ -0,0 +1,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{})
+ }
+}