-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_test.go
197 lines (181 loc) · 4.53 KB
/
slice_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
package goramda
import (
"fmt"
"reflect"
"math/rand"
"testing"
)
func TestHead(t *testing.T) {
data := map[string]interface{}{
"Sting Slice": []string{"A", "V", "C"},
"Integer Slice": []int{5, 50, 500},
"Float Slice": []float64{0.11, 0.22, 0.33},
}
for key, v := range data {
out := Head(v)
if out == nil {
t.Fatalf("\n Unexpected error %s Tail value :[%v] and type: %T", key, out, out)
}
}
}
func TestTail(t *testing.T) {
data := map[string]struct {
data interface{}
expectedVal interface{}
}{
"Sting Slice": {
data: []string{"A", "V", "C"},
expectedVal: []string{"A", "V", "C"},
},
"Int Slice": {
data: []int{5, 50, 500, 5000},
expectedVal: []int{5, 50, 500, 5000},
},
"Float Slice": {
data: []float64{0.11, 0.22, 0.23, 0.24, 0.33},
expectedVal: []float64{0.11, 0.22, 0.23, 0.24, 0.33},
},
"Empty Slice": {
data: []int{},
expectedVal: []int{},
},
"Sting": {
data: "2547893",
expectedVal: "547893",
},
"EmptySting": {
data: "",
expectedVal: "",
},
}
for key, v := range data {
out := Tail(v.data)
if NotEquals(out, v.expectedVal) {
t.Fatalf("\n Unexpected error %s Tail value :[%v] and type: %T", key, out, out)
}
}
}
func TestLast(t *testing.T) {
data := map[string]struct {
data interface{}
expectedVal interface{}
}{
"Sting Slice": {
data: []string{"A", "V", "C"},
expectedVal: "C",
},
"Int Slice": {
data: []int{5, 50, 500, 5000},
expectedVal: 5000,
},
"Float Slice": {
data: []float64{0.11, 0.22, 0.23, 0.24, 0.33},
expectedVal: 0.33,
},
"Empty Slice": {
data: []int{},
expectedVal: nil,
},
"Sting": {
data: "2547893",
expectedVal: uint8('3'),
},
"EmptySting": {
data: "",
expectedVal: nil,
},
}
for key, v := range data {
out := Last(v.data)
if NotEquals(out, v.expectedVal) {
t.Fatalf("\n Unexpected error on (%s) Tail value :[%v] and type: %T", key, out, out)
}
}
}
func TestDropTypeAssert(t *testing.T) {
data := map[string]struct {
data interface{}
expectedLen int
}{
"Sting Slice": {
data: []string{"A", "V", "C"},
expectedLen: 1,
},
"Sting Slice 4": {
data: []string{"Aasd", "ddd", "Cccc", "dddd"},
expectedLen: 2,
},
}
for _, v := range data {
out := Drop(2, v.data)
outLen := reflect.ValueOf(out).Len()
_, ok := out.([]string)
if !ok || outLen != v.expectedLen {
t.Fatalf("unexported result type assert (%v) length of array (%d) but expected: %d", ok, outLen, 1)
}
}
}
func TestDrop(t *testing.T) {
data := map[string]interface{}{
"Sting Slice": []string{"A", "V", "C"},
"Integer Slice": []int{5, 50, 500},
"Float Slice": []float64{0.11, 0.22, 0.33},
}
for _, v := range data {
out := Drop(2, v)
outLen := reflect.ValueOf(out).Len()
if outLen != 1 {
t.Fatalf("unexported length of array (%d) but expected: %d", outLen, 1)
}
}
}
func TestNth(t *testing.T) {
data := map[string]interface{}{
"Sting Slice": []string{"A", "V", "C"},
"Integer Slice": []int{5, 50, 500},
"Float Slice": []float64{0.11, 0.22, 0.33},
}
for key, v := range data {
index := rand.Intn(len(data))
out := Nth(index, v)
fmt.Printf("\n %s IndexOf[%d] value :[%v] and type: %T", key, index, out, out)
}
}
func TestFind(t *testing.T) {
data := map[string]interface{}{
"Sting Slice": []string{"V", "I", "J", "A", "Y", "A", "K", "N", "T", "H"},
"Integer Slice": []int{5, 50, 500, 2, 3, 5, 85, 96},
"Float Slice": []float64{0.11, 0.22, 0.33, 0.32, 2.2, 3.3, 5.5},
}
for key, v := range data {
arrV := reflect.ValueOf(v)
index := rand.Intn(arrV.Len() - 1)
v1 := arrV.Index(index).Interface()
findFunc := Find(func(d interface{}) bool {
return Equals(d, v1)
})
out := findFunc(v)
if v1 != out {
t.Fatalf("Find value of (%v) unexpected result expected value [%v] got: %v", key, v1, out)
}
}
}
func TestFindIndex(t *testing.T) {
data := map[string]interface{}{
"Sting Slice": []string{"V", "I", "J", "A", "Y", "A", "K", "N", "T", "H"},
"Integer Slice": []int{5, 50, 500, 2, 3, 51, 85, 96},
"Float Slice": []float64{0.11, 0.22, 0.33, 0.32, 2.2, 3.3, 5.5},
}
for key, v := range data {
arrV := reflect.ValueOf(v)
index := rand.Intn(arrV.Len() - 1)
v1 := arrV.Index(index).Interface()
findFunc := FindIndex(func(d interface{}) bool {
return Equals(d, v1)
})
out := findFunc(v)
if out != index {
t.Fatalf("findIndex of (%v) unexpected result expected index [%v] got: %v", key, index, out)
}
}
}