summaryrefslogtreecommitdiff
path: root/go/grains/grains_test.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-26 09:59:34 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-26 09:59:34 +0200
commita3817ba908b3705f96e572c48d5a9d96aae40118 (patch)
tree505b7eabcb376aac18837d197ca2f39722a0f1e3 /go/grains/grains_test.go
parentcf38d5bfd8567e7b35a3e8e04998b87e38e7af94 (diff)
Solve grains
Diffstat (limited to 'go/grains/grains_test.go')
-rw-r--r--go/grains/grains_test.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/go/grains/grains_test.go b/go/grains/grains_test.go
new file mode 100644
index 0000000..e37cdab
--- /dev/null
+++ b/go/grains/grains_test.go
@@ -0,0 +1,61 @@
+package grains
+
+import (
+ "testing"
+)
+
+var squareTests = []struct {
+ input int
+ expectedVal uint64
+ expectError bool
+}{
+ {1, 1, false},
+ {2, 2, false},
+ {3, 4, false},
+ {4, 8, false},
+ {16, 32768, false},
+ {32, 2147483648, false},
+ {64, 9223372036854775808, false},
+ {65, 0, true},
+ {0, 0, true},
+ {-1, 0, true},
+}
+
+func TestSquare(t *testing.T) {
+ for _, test := range squareTests {
+ actualVal, actualErr := Square(test.input)
+ if actualVal != test.expectedVal {
+ t.Errorf("Square(%d) expected %d, Actual %d", test.input, test.expectedVal, actualVal)
+ }
+
+ // if we expect an error and there isn't one
+ if test.expectError && actualErr == nil {
+ t.Errorf("Square(%d) expected an error, but error is nil", test.input)
+ }
+ // if we don't expect an error and there is one
+ if !test.expectError && actualErr != nil {
+ t.Errorf("Square(%d) expected no error, but error is: %s", test.input, actualErr)
+ }
+ }
+}
+
+func TestTotal(t *testing.T) {
+ var expected uint64 = 18446744073709551615
+ if actual := Total(); actual != expected {
+ t.Errorf("Total() expected %d, Actual %d", expected, actual)
+ }
+}
+
+func BenchmarkSquare(b *testing.B) {
+ b.StopTimer()
+
+ for _, test := range squareTests {
+ b.StartTimer()
+
+ for i := 0; i < b.N; i++ {
+ Square(test.input)
+ }
+
+ b.StopTimer()
+ }
+}