-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfind_test.go
68 lines (60 loc) · 2.13 KB
/
find_test.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package goterators
import (
"fmt"
"testing"
)
func TestFindInt(t *testing.T) {
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
expectedItem1 := 18
expectedIndex1 := 17
matchedItem, index, err := Find(testSource, func(item int) bool {
return item == expectedItem1
})
if err != nil {
t.Errorf("Find item1 = %v, expected no error, got = %v", expectedItem1, err)
}
if matchedItem != expectedItem1 {
t.Errorf("Find item1 = %v, Expected = %v , got = %v", expectedItem1, expectedItem1, matchedItem)
}
if index != expectedIndex1 {
t.Errorf("Find item1 = %v, Expected = %v , got = %v", expectedItem1, expectedIndex1, index)
}
expectedItem2 := 50
matchedItem, index, err = Find(testSource, func(item int) bool {
return item == expectedItem2
})
if err == nil {
t.Errorf("Find item2 = %v, expected <not_found>, got = no-error", expectedItem1)
}
}
func TestFindString(t *testing.T) {
testSource := []string{"basis", "pray", "platform", "top", "border", "pole", "kidnap", "eaux", "stream", "lemon", "helpless", "indulge", "highlight", "city", "miner", "photography", "squeeze", "king", "offender", "rugby"}
expectedItem1 := "king"
expectedIndex1 := 17
matchedItem, index, err := Find(testSource, func(item string) bool {
return item == expectedItem1
})
if err != nil {
t.Errorf("Find item1 = %v, expected no error, got = %v", expectedItem1, err)
}
if matchedItem != expectedItem1 {
t.Errorf("Find item1 = %v, Expected = %v , got = %v", expectedItem1, expectedItem1, matchedItem)
}
if index != expectedIndex1 {
t.Errorf("Find item1 = %v, Expected = %v , got = %v", expectedItem1, expectedIndex1, index)
}
expectedItem2 := "webuild"
matchedItem, index, err = Find(testSource, func(item string) bool {
return item == expectedItem2
})
if err == nil {
t.Errorf("Find item2 = %v, expected <not_found>, got = no-error", expectedItem1)
}
}
func ExampleFind() {
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
firstFoundItem, index, _ := Find(testSource, func(item int) bool {
return item%10 == 0
})
fmt.Println("Find: ", firstFoundItem, index)
}