-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_test.go
178 lines (165 loc) · 3.3 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package gotil_test
import (
"fmt"
"reflect"
"testing"
"github.com/gotilty/gotil"
)
func TestFindBy(t *testing.T) {
input := []int64{-100, -5, 30, 100}
expected := int64(30)
result := gotil.FindBy(input, func(val int64) bool {
return val == 30
})
if !reflect.DeepEqual(expected, result) {
t.Errorf("F does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result)
}
input2 := []user{
{
name: "Micheal",
age: 27,
},
{
name: "Joe",
age: 30,
},
{
name: "Olivia",
age: 42,
},
}
result2 := gotil.FindBy(input2, func(val user) bool {
return val.name == "Olivia"
})
expected2 := user{
name: "Olivia",
age: 42,
}
if !reflect.DeepEqual(expected2, result2) {
t.Errorf("FindBy does not works expected\ncase: %v\nexpected: %v taken: %v", input2, expected2, result2)
}
}
func BenchmarkFindByIntegerSlice(b *testing.B) {
for n := 0; n < b.N; n++ {
input := []int64{-100, -5, 30, 100}
gotil.FindBy(input, func(val int64) bool {
return val == 30
})
}
}
func BenchmarkFindByStructSlice(b *testing.B) {
input2 := []user{
{
name: "Micheal",
age: 27,
},
{
name: "Joe",
age: 30,
},
{
name: "Olivia",
age: 42,
},
}
for n := 0; n < b.N; n++ {
gotil.FindBy(input2, func(val user) bool {
return val.name == "Olivia"
})
}
}
func ExampleFindBy() {
data := []int64{-100, -5, 30, 100}
// Input: [-100 -5 30 100]
newData := gotil.FindBy(data, func(val int64) bool {
return val == 30
})
fmt.Println(newData)
// Output: 30
}
func TestFindLastBy(t *testing.T) {
data := []int64{-100, -5, 30, 100}
// Input: [-100 -5 30 100]
result := gotil.FindByFromIndex(data, func(val int64) bool {
return val > 0
}, 3)
fmt.Println(result)
//input := []int64{10, 15, 90, 100, 30, 50}
//expected := int64(100)
//result := gotil.FindLastBy(input, func(val int64) bool {
// return val > 80
//})
//if !reflect.DeepEqual(expected, result) {
// t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result)
//}
//input2 := []user{
// {
// name: "Micheal",
// age: 27,
// },
// {
// name: "Joe",
// age: 30,
// },
// {
// name: "Olivia",
// age: 42,
// },
// {
// name: "Mike",
// age: 43,
// },
//}
//result2 := gotil.FindBy(input2, func(val user) bool {
// return val.age == 42
//})
//expected2 := user{
// name: "Olivia",
// age: 42,
//}
//if !reflect.DeepEqual(expected, result) {
// t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input2, expected2, result2)
// }
}
func BenchmarkFindLastByIntegerSlice(b *testing.B) {
input := []int64{10, 15, 90, 100, 30, 50}
for n := 0; n < b.N; n++ {
_ = gotil.FindLastBy(input, func(val int64) bool {
return val > 80
})
}
}
func BenchmarkFindLastByStructSlice(b *testing.B) {
input := []user{
{
name: "Micheal",
age: 27,
},
{
name: "Joe",
age: 30,
},
{
name: "Olivia",
age: 42,
},
{
name: "Mike",
age: 43,
},
}
for n := 0; n < b.N; n++ {
_ = gotil.FindLastBy(input, func(val user) bool {
return val.age == 42
})
}
}
func ExampleFindLastBy() {
data := []int64{-100, -5, 30, 100}
// Input: [-100 -5 30 100]
newData := gotil.FindLastBy(data, func(val int64) bool {
return val == 30
})
fmt.Println(newData)
// Output: 30
}