summaryrefslogtreecommitdiff
path: root/exercises/2.1-3/search_test.go
blob: c4ed033f202a5cb620d37a49259efccd22de31d7 (plain)
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
32
33
34
35
36
37
38
39
package main

import "testing"

func linearSearch(A []string, v string) int {
	i := 0
	length := len(A)

	for i < length && v != A[i] {
		i = i + 1
	}
	if i == length {
		return -1
	}

	return i
}

func TestSearch(t *testing.T) {
	t.Run("Found", func(t *testing.T) {
		items := []string{"apples", "bananas", "shrimp", "tuna"}

		result := linearSearch(items, "shrimp")

		if result != 2 {
			t.Fatalf("Expected 2, Got: %v", result)
		}
	})

	t.Run("Not Found", func(t *testing.T) {
		items := []string{"apples", "bananas", "shrimp", "tuna"}

		result := linearSearch(items, "chips")

		if result != -1 {
			t.Fatalf("Expected -1, Got: %v", result)
		}
	})
}