-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarray_test.go
238 lines (202 loc) · 4.34 KB
/
garray_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package garray
import (
"fmt"
"log"
"strings"
"testing"
)
func TestAverage(t *testing.T) {
if Average([]int{2, 4}) != 3 {
t.Fail()
}
}
func TestConcat(t *testing.T) {
a := []int{1, 2, 3}
b := []int{4, 5, 6}
c := Concat(a, b)
if len(c) != 6 || c[0] != 1 || c[1] != 2 || c[2] != 3 || c[3] != 4 || c[4] != 5 || c[5] != 6 {
t.Fail()
}
}
func TestMap(t *testing.T) {
l := []int{1, 2, 3, 4, 5}
result := Map(l, func(i int) string {
return fmt.Sprintf(`%d`, i)
})
if result[0] != `1` ||
result[1] != `2` ||
result[2] != `3` ||
result[3] != `4` ||
result[4] != `5` {
t.Fail()
}
}
func ExampleMapIE() {
type student struct {
name string
age int
}
students := []student{{`Jack`, 10}, {`Mike`, 20}, {`Rose`, 15}}
studentAges, err := ToMapIE(students,
func(i int) (string, error) { return students[i].name, nil },
func(i int) (int, error) { return students[i].age, nil })
if err != nil {
fmt.Print(err)
} else {
fmt.Print(studentAges)
}
// output: map[Jack:10 Mike:20 Rose:15]
}
func TestShallowCopy(t *testing.T) {
l := []int{1, 2, 3, 4, 5}
result := ShallowCopy(l)
if result[0] != 1 ||
result[1] != 2 ||
result[2] != 3 ||
result[3] != 4 ||
result[4] != 5 {
t.Fail()
}
if &l[0] == &result[0] {
t.Fail()
}
}
func ExampleFindIf() {
list := []int{1, 2, 3, 4, 5}
pos := FindIf(list, func(i int) bool {
return list[i] == 3
})
fmt.Println(pos)
// output: 2
}
func ExampleRemoveIf() {
list := []int{1, 2, 3, 1, 2, 3, 1, 2, 3}
result, deleted := RemoveIf(list, func(i int) bool {
return list[i] == 2
})
fmt.Println(result, deleted)
// output: [1 3 1 3 1 3] 3
}
func ExampleCount() {
l := []int{1, 0, 1, 0, 1}
fmt.Println(Count(l, 1))
// output: 3
}
func ExampleCountIf() {
l := []int{1, 2, 3, 4, 5}
fmt.Println(CountIf(l, func(i int) bool { return i%2 == 0 }))
// output: 2
}
func ExampleRepeat() {
fmt.Println(Repeat(1, 6))
// output: [1 1 1 1 1 1]
}
func ExampleReverse() {
l := []int{1, 2, 3, 4}
Reverse(l)
fmt.Println(l)
l = []int{1, 2, 3}
Reverse(l)
fmt.Println(l)
// output: [4 3 2 1]
// [3 2 1]
}
func ExampleFilter() {
lst := []int{1, 2, 3, 4, 5, 6}
isEven := func(n int) bool { return n%2 == 0 } // expected filtered array of even numbers
evens := Filter(lst, isEven)
fmt.Println(evens)
// Output: [2 4 6]
}
func ExampleFilterI() {
lst := []string{"apple", "banana", "cherry", "grape"}
firstTwoItems := func(i int) bool {
return i < 2 // expected Filtered array of first two items from the list
}
result := FilterI(lst, firstTwoItems)
fmt.Println(result)
// Output: [apple banana]
}
func ExampleFilterIE() {
nums := []int{1, 2, 3, 4, 5}
evenNums := func(i int) (bool, error) {
return nums[i]%2 == 0, nil // expected filterd array with only even numbers
}
filteredNums, _ := FilterIE(nums, evenNums)
fmt.Println(filteredNums)
// Output: [2 4]
}
func ExampleApply() {
numList := []int{1, 2, 3, 4, 5, 6}
double := func(n int) error {
fmt.Print(n*2, " ")
return nil
}
err := Apply(numList, double)
if err != nil {
fmt.Println("Error Occured")
}
// expected output to be array elements multiplied by 2
// Output:2 4 6 8 10 12
}
func ExampleApplyI() {
words := []string{"bar", "foo"}
var capitalize = func(i int) error {
words[i] = strings.ToUpper(words[i])
return nil
}
err := ApplyI(words, capitalize) // all elements capitalized
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", words)
// Output:[BAR FOO]
}
func ExampleSumBy() {
type student struct {
name string
age int
}
students := []student{{`Jack`, 10}, {`Mike`, 20}, {`Rose`, 15}}
totalAge := SumBy(students, func(s student) int {
return s.age
})
fmt.Println(totalAge)
// Output: 45
}
func ExampleSumByP() {
type student struct {
name string
age int
}
students := []student{{`Jack`, 10}, {`Mike`, 20}, {`Rose`, 15}}
totalAge := SumByP(students, func(s *student) int {
return s.age
})
fmt.Println(totalAge)
// Output: 45
}
func ExampleSumByI() {
type student struct {
name string
age int
}
students := []student{{`Jack`, 10}, {`Mike`, 20}, {`Rose`, 15}}
totalAge := SumByI(students, func(i int) int {
return students[i].age
})
fmt.Println(totalAge)
// Output: 45
}
func ExamplePaginate() {
l := []int{1, 2, 3, 4, 5, 6, 7, 8}
i := 0
for p := Paginate(l, 3, i); len(p) > 0; {
fmt.Println(p)
i++
p = Paginate(l, 3, i)
}
// Output: [1 2 3]
// [4 5 6]
// [7 8]
}