From 509c5063d66e8bbef4ec1def1c99c318be51aceb Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 25 Aug 2016 03:13:39 +0200 Subject: Initial import --- go/triangle/.triangle.go.swp | Bin 0 -> 12288 bytes go/triangle/README.md | 29 +++++++++++++ go/triangle/triangle.go | 17 ++++++++ go/triangle/triangle_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 go/triangle/.triangle.go.swp create mode 100644 go/triangle/README.md create mode 100644 go/triangle/triangle.go create mode 100644 go/triangle/triangle_test.go (limited to 'go/triangle') diff --git a/go/triangle/.triangle.go.swp b/go/triangle/.triangle.go.swp new file mode 100644 index 0000000..d58a2a8 Binary files /dev/null and b/go/triangle/.triangle.go.swp differ diff --git a/go/triangle/README.md b/go/triangle/README.md new file mode 100644 index 0000000..8f1485d --- /dev/null +++ b/go/triangle/README.md @@ -0,0 +1,29 @@ +# Triangle + +Write a program that can tell you if a triangle is equilateral, isosceles, or scalene. + +The program should raise an error if the triangle cannot exist. + +## Hint + +The sum of the lengths of any two sides of a triangle always exceeds or +is equal to the length of the third side, a principle known as the _triangle +inequality_. + +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 + +The Ruby Koans triangle project, parts 1 & 2 [http://rubykoans.com](http://rubykoans.com) + +## 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/triangle/triangle.go b/go/triangle/triangle.go new file mode 100644 index 0000000..7243375 --- /dev/null +++ b/go/triangle/triangle.go @@ -0,0 +1,17 @@ +package triangle + +const testVersion = 2 + +// Code this function. +func KindFromSides(a, b, c float64) Kind + +// Notice it returns this type. Pick something suitable. +type Kind + +// Pick values for the following identifiers used by the test program. +NaT // not a triangle +Equ // equilateral +Iso // isosceles +Sca // scalene + +// Organize your code for readability. diff --git a/go/triangle/triangle_test.go b/go/triangle/triangle_test.go new file mode 100644 index 0000000..4532389 --- /dev/null +++ b/go/triangle/triangle_test.go @@ -0,0 +1,99 @@ +package triangle + +import ( + "math" + "testing" +) + +const targetTestVersion = 2 + +type testCase struct { + want Kind + a, b, c float64 +} + +// basic test cases +var testData = []testCase{ + {Equ, 2, 2, 2}, // same length + {Equ, 10, 10, 10}, // a little bigger + {Iso, 3, 4, 4}, // last two sides equal + {Iso, 4, 3, 4}, // first and last sides equal + {Iso, 4, 4, 3}, // first two sides equal + {Iso, 10, 10, 2}, // again + {Iso, 2, 4, 2}, // a "triangle" that is just a line is still OK + {Sca, 3, 4, 5}, // no sides equal + {Sca, 10, 11, 12}, // again + {Sca, 5, 4, 2}, // descending order + {Sca, .4, .6, .3}, // small sides + {Sca, 1, 4, 3}, // a "triangle" that is just a line is still OK + {NaT, 0, 0, 0}, // zero length + {NaT, 3, 4, -5}, // negative length + {NaT, 1, 1, 3}, // fails triangle inequality + {NaT, 2, 5, 2}, // another + {NaT, 7, 3, 2}, // another +} + +// generate cases with NaN and Infs, append to basic cases +func init() { + nan := math.NaN() + pinf := math.Inf(1) + ninf := math.Inf(-1) + nf := make([]testCase, 4*4*4) + i := 0 + for _, a := range []float64{3, nan, pinf, ninf} { + for _, b := range []float64{4, nan, pinf, ninf} { + for _, c := range []float64{5, nan, pinf, ninf} { + nf[i] = testCase{NaT, a, b, c} + i++ + } + } + } + testData = append(testData, nf[1:]...) +} + +// Test that the kinds are not equal to each other. +// If they are equal, then TestKind will return false positives. +func TestKindsNotEqual(t *testing.T) { + kindsAndNames := []struct { + kind Kind + name string + }{ + {Equ, "Equ"}, + {Iso, "Iso"}, + {Sca, "Sca"}, + {NaT, "NaT"}, + } + + for i, pair1 := range kindsAndNames { + for j := i + 1; j < len(kindsAndNames); j++ { + pair2 := kindsAndNames[j] + if pair1.kind == pair2.kind { + t.Fatalf("%s should not be equal to %s", pair1.name, pair2.name) + } + } + } +} + +func TestKind(t *testing.T) { + for _, test := range testData { + got := KindFromSides(test.a, test.b, test.c) + if got != test.want { + t.Fatalf("Triangle with sides, %g, %g, %g = %v, want %v", + test.a, test.b, test.c, got, test.want) + } + } +} + +func TestTestVersion(t *testing.T) { + if testVersion != targetTestVersion { + t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion) + } +} + +func BenchmarkKind(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, test := range testData { + KindFromSides(test.a, test.b, test.c) + } + } +} -- cgit v1.2.3