summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2021-09-11 16:37:44 -0600
committermo khan <mo@mokhan.ca>2021-09-11 16:37:44 -0600
commit82b87d4feda73bf1f5310df92670309538df43f2 (patch)
treeef3a1fbba29de51a694330767ef53ecac9a9f4e2 /exercises
parent3bdd90386323e1dffd563da7e69803d6d9732ddb (diff)
Complete 2.1-3
Diffstat (limited to 'exercises')
-rw-r--r--exercises/2.1-3/search_test.go39
1 files changed, 39 insertions, 0 deletions
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)
+ }
+ })
+}