~telemachus/algorithms

algorithms/binarysearch_test.go -rw-r--r-- 819 bytes
c4396785Peter Aronoff Update for golangci-lint 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package algorithms_test

import (
	"testing"

	"git.sr.ht/~telemachus/algorithms"
)

func TestBinarySearch(t *testing.T) {
	tests := map[string]struct {
		slice    []int
		wanted   int
		expected int
	}{
		"empty slice":                 {[]int{}, 3, -1},
		"wanted not in slice":         {[]int{1, 2, 3}, 4, -1},
		"wanted is a negative number": {[]int{1, 2, 3}, -4, -1},
		"wanted at start of slice":    {[]int{1, 2, 3}, 1, 0},
		"wanted at end of slice":      {[]int{1, 2, 3, 4, 5, 6, 7}, 7, 6},
		"wanted in middle of slice":   {[]int{1, 2, 3, 4, 5, 6, 7}, 4, 3},
	}

	for msg, tc := range tests {
		t.Run(msg, func(t *testing.T) {
			actual := algorithms.BinarySearch(tc.slice, tc.wanted)
			if tc.expected != actual {
				t.Errorf("expected %d; actual %d; given %#v", tc.expected, actual, tc.slice)
			}
		})
	}
}