From 82b87d4feda73bf1f5310df92670309538df43f2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Sat, 11 Sep 2021 16:37:44 -0600 Subject: Complete 2.1-3 --- exercises/2.1-3/search_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 exercises/2.1-3/search_test.go (limited to 'exercises') diff --git a/exercises/2.1-3/search_test.go b/exercises/2.1-3/search_test.go new file mode 100644 index 0000000..c4ed033 --- /dev/null +++ b/exercises/2.1-3/search_test.go @@ -0,0 +1,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) + } + }) +} -- cgit v1.2.3