Sha256: d04ed03f08d66b1d405f753ca60ffb1b158ed76bddc47ec74100a47557c779de

Contents?: true

Size: 1.47 KB

Versions: 122

Compression:

Stored size: 1.47 KB

Contents

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.

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 {
				var _ error = err
				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)
		}
	}
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.179 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.178 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.177 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.176 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.175 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.174 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.173 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.172 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.171 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.170 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.169 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.167 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.166 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.165 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.164 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.163 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.162 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.161 tracks/go/exercises/binary/binary_test.go
trackler-2.2.1.160 tracks/go/exercises/binary/binary_test.go