Sha256: 5c2eccb3b28a6d40c29f17a55b9b987df0d25f26ba6bcb81f4742ba3bbd2c9c3

Contents?: true

Size: 1.12 KB

Versions: 107

Compression:

Stored size: 1.12 KB

Contents

package binarysearch

import (
	"fmt"
	"math/rand"
	"testing"
)

func TestSearchInts(t *testing.T) {
	for _, test := range testCases {
		if x := SearchInts(test.slice, test.key); x != test.x {
			t.Fatalf("FAIL: %s\nSearchInts(%#v, %d) = %d, want %d",
				test.description, test.slice, test.key, x, test.x)
		}
		t.Logf("SUCCESS: %s", test.description)
	}
}

// Benchmarks also test searching larger random slices

type query struct {
	slice []int
	x     int
}

func newQuery(n int) (query, error) {
	q := query{slice: make([]int, n)}
	for i := 0; i < n; i++ {
		q.slice[i] = i
	}
	q.x = rand.Intn(n)
	if res := SearchInts(q.slice, q.x); res != q.x {
		return q, fmt.Errorf("Search of %d values gave different answer", n)
	}
	return q, nil
}

func runBenchmark(n int, b *testing.B) {
	q, err := newQuery(n)
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		SearchInts(q.slice, q.x)
	}
}

func Benchmark1e2(b *testing.B) { runBenchmark(1e2, b) }
func Benchmark1e4(b *testing.B) { runBenchmark(1e4, b) }
func Benchmark1e6(b *testing.B) { runBenchmark(1e6, b) }
func Benchmark1e8(b *testing.B) { runBenchmark(1e8, b) }

Version data entries

107 entries across 107 versions & 1 rubygems

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