-
Notifications
You must be signed in to change notification settings - Fork 7
/
search_test.go
132 lines (114 loc) · 2.38 KB
/
search_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gostrutils
import "testing"
func TestGetStringIndexInSliceFound(t *testing.T) {
list := genList()
idx := GetStringIndexInSlice(list, "hello")
if idx == -1 {
t.Error("Unable to find 'hello' in slice")
}
}
func TestGetStringIndexInSliceNotFound(t *testing.T) {
list := genList()
idx := GetStringIndexInSlice(list, "planet")
if idx != -1 {
t.Error("Unable to find 'hello' on the slice")
}
}
func TestSearchFound(t *testing.T) {
list := genList()
found := IsStringInSlice(list, "hello")
if !found {
t.Error("Unable to find 'hello' on the slice")
}
}
func TestSearchNotFound(t *testing.T) {
list := genList()
found := IsStringInSlice(list, "planet")
if found {
t.Error("Able to find 'planet' at the list")
}
}
func genList() []string {
list := make([]string, 2)
list[0] = "hello"
list[1] = "world"
return list
}
func TestGetBytesRuneIndexInSlice(t *testing.T) {
type checkList struct {
str []byte
needle rune
expected int
}
validList := []checkList{
{
str: []byte("hello"),
needle: 'o',
expected: 4,
},
{
str: []byte("שלום עולם"),
needle: 'ע',
expected: 5,
},
{
str: []byte("hello"),
needle: 'w',
expected: -1,
},
{
str: []byte("שלום עולם"),
needle: 'ז',
expected: -1,
},
}
t.Run("validList", func(t2 *testing.T) {
for _, item := range validList {
result := GetBytesRuneIndexInSlice(item.str, item.needle)
if result != item.expected {
t2.Errorf("'%s'['%U'] expected to be at %d but it was located at %d",
item.str, item.needle, item.expected, result,
)
}
}
})
}
func TestIsRuneInByteSlice(t *testing.T) {
type checkList struct {
str []byte
needle rune
expected bool
}
validList := []checkList{
{
str: []byte("hello"),
needle: 'o',
expected: true,
},
{
str: []byte("שלום עולם"),
needle: 'ע',
expected: true,
},
{
str: []byte("hello"),
needle: 'w',
expected: false,
},
{
str: []byte("שלום עולם"),
needle: 'ז',
expected: false,
},
}
t.Run("validList", func(t2 *testing.T) {
for _, item := range validList {
result := IsRuneInByteSlice(item.str, item.needle)
if result != item.expected {
t2.Errorf("'%s'['%U'] expected to be at %T but %T returned",
item.str, item.needle, item.expected, result,
)
}
}
})
}