summaryrefslogtreecommitdiff
path: root/go/binary/binary_test.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 01:56:09 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 01:56:09 +0200
commit8b2aa597999daf3efb5e842717fc2d373372ae4f (patch)
treed893416c2b2f0f0ed0fbe7a17e5beee096a220b0 /go/binary/binary_test.go
parentd40949d4cfc17d784980f3bbc5da71b339539f48 (diff)
Solve binary
Diffstat (limited to 'go/binary/binary_test.go')
-rw-r--r--go/binary/binary_test.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/go/binary/binary_test.go b/go/binary/binary_test.go
new file mode 100644
index 0000000..cd5af58
--- /dev/null
+++ b/go/binary/binary_test.go
@@ -0,0 +1,67 @@
+package binary
+
+import (
+ "testing"
+)
+
+// You must implement the function,
+//
+// func ParseBinary(string) (int, error)
+//
+// It is standard for Go functions to return error values to report error conditions.
+// The test cases have some inputs that are invalid.
+// For invalid inputs, return an error that signals to the user why the error happened.
+// The test cases can only check that you return *some* error,
+// but it's still good practice to return useful errors.
+//
+// Also define a testVersion with a value that matches
+// the targetTestVersion here.
+
+const targetTestVersion = 1
+
+var testCases = []struct {
+ binary string
+ expected int
+ ok bool
+}{
+ {"1", 1, true},
+ {"10", 2, true},
+ {"11", 3, true},
+ {"100", 4, true},
+ {"1001", 9, true},
+ {"11010", 26, true},
+ {"10001101000", 1128, true},
+ {"0", 0, true},
+ {"foo101", 0, false},
+ {"101bar", 0, false},
+ {"101baz010", 0, false},
+ {"22", 0, false},
+}
+
+func TestParseBinary(t *testing.T) {
+ for _, tt := range testCases {
+ actual, err := ParseBinary(tt.binary)
+ if tt.ok {
+ if err != nil {
+ t.Fatalf("ParseBinary(%v) returned error %q. Error not expected.",
+ tt.binary, err)
+ }
+ if actual != tt.expected {
+ t.Fatalf("ParseBinary(%v): actual %d, expected %v",
+ tt.binary, actual, tt.expected)
+ }
+ } else if err == nil {
+ t.Fatalf("ParseBinary(%v) returned %d and no error. Expected an error.",
+ tt.binary, actual)
+ }
+ }
+}
+
+// Benchmark combined time for all tests
+func BenchmarkBinary(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, tt := range testCases {
+ ParseBinary(tt.binary)
+ }
+ }
+}