From 8b2aa597999daf3efb5e842717fc2d373372ae4f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 28 Aug 2016 01:56:09 +0200 Subject: Solve binary --- go/binary/binary_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 go/binary/binary_test.go (limited to 'go/binary/binary_test.go') 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) + } + } +} -- cgit v1.2.3