Skip to content

Commit a60edf6

Browse files
committed
test everything!
1 parent db3c7e5 commit a60edf6

File tree

13 files changed

+989
-359
lines changed

13 files changed

+989
-359
lines changed

docs/slice/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Slice struct {
5555
| [Equal](./equal.md) | Equal returns true if slices are equal |
5656
| [Filter](./filter.md) | Filter returns slice of T for which F returned true |
5757
| [Find](./find.md) | Find returns the first element for which f returns true |
58-
| [FindIndex](./findindex.md) | FindIndex is like Find, but return element index instead of element itself |
58+
| [FindIndex](./findindex.md) | FindIndex is like Find, but return element index instead of element itself. Returns -1 if element not found |
5959
| [Join](./join.md) | Join concatenates elements of the slice to create a single string. |
6060
| [GroupBy](./groupby.md) | GroupBy groups element from array by value returned by f |
6161
| [InsertAt](./insertat.md) | InsertAt returns the slice with element inserted at given index. |

docs/slice/choice.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ func (s Slice) Choice(seed int64) (T, error) {
5555
}
5656
```
5757

58+
## Tests
59+
60+
```go
61+
func TestSliceChoice(t *testing.T) {
62+
f := func(given []T, seed int64, expected T) {
63+
actual, _ := Slice{given}.Choice(seed)
64+
assert.Equal(t, expected, actual, "they should be equal")
65+
}
66+
f([]T{1}, 0, 1)
67+
f([]T{1, 2, 3}, 1, 3)
68+
f([]T{1, 2, 3}, 2, 2)
69+
}
70+
```

docs/slice/findindex.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Slice.FindIndex
22

33
```go
4-
func (s Slice) FindIndex(f func(el T) bool) (int, error)
4+
func (s Slice) FindIndex(f func(el T) bool) int
55
```
66

7-
FindIndex is like Find, but return element index instead of element itself
7+
FindIndex is like Find, but return element index instead of element itself. Returns -1 if element not found
88

99
Generic types: T.
1010

@@ -29,43 +29,37 @@ Generic types: T.
2929
| SliceUint64 | uint64 |
3030
| SliceInterface | interface{} |
3131

32-
## Errors
33-
34-
| Error | Message |
35-
| -------- | ------ |
36-
| ErrNotFound | given element is not found |
37-
3832
## Source
3933

4034
```go
41-
// FindIndex is like Find, but return element index instead of element itself
42-
func (s Slice) FindIndex(f func(el T) bool) (int, error) {
35+
// FindIndex is like Find, but return element index instead of element itself.
36+
// Returns -1 if element not found
37+
func (s Slice) FindIndex(f func(el T) bool) int {
4338
for i, el := range s.Data {
4439
if f(el) {
45-
return i, nil
40+
return i
4641
}
4742
}
48-
return 0, ErrNotFound
43+
return -1
4944
}
5045
```
5146

5247
## Tests
5348

5449
```go
5550
func TestSliceFindIndex(t *testing.T) {
56-
f := func(given []T, expectedInd int, expectedErr error) {
51+
f := func(given []T, expectedInd int) {
5752
even := func(t T) bool { return (t % 2) == 0 }
58-
index, err := Slice{given}.FindIndex(even)
53+
index := Slice{given}.FindIndex(even)
5954
assert.Equal(t, expectedInd, index, "they should be equal")
60-
assert.Equal(t, expectedErr, err, "they should be equal")
6155
}
62-
f([]T{}, 0, ErrNotFound)
63-
f([]T{1}, 0, ErrNotFound)
64-
f([]T{1}, 0, ErrNotFound)
65-
f([]T{2}, 0, nil)
66-
f([]T{1, 2}, 1, nil)
67-
f([]T{1, 2, 3}, 1, nil)
68-
f([]T{1, 3, 5, 7, 9, 2}, 5, nil)
69-
f([]T{1, 3, 5}, 0, ErrNotFound)
56+
f([]T{}, -1)
57+
f([]T{1}, -1)
58+
f([]T{1}, -1)
59+
f([]T{2}, 0)
60+
f([]T{1, 2}, 1)
61+
f([]T{1, 2, 3}, 1)
62+
f([]T{1, 3, 5, 7, 9, 2}, 5)
63+
f([]T{1, 3, 5}, -1)
7064
}
7165
```

docs/slice/without.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,30 @@ func (s Slice) Without(elements ...T) []T {
5050
}
5151
```
5252

53+
## Tests
54+
55+
```go
56+
func TestSliceWithout(t *testing.T) {
57+
f := func(given []T, items []T, expected []T) {
58+
actual := Slice{given}.Without(items...)
59+
assert.Equal(t, expected, actual, "they should be equal")
60+
}
61+
f([]T{}, []T{}, []T{})
62+
f([]T{}, []T{1, 2}, []T{})
63+
64+
f([]T{1}, []T{1}, []T{})
65+
f([]T{1}, []T{1, 2}, []T{})
66+
f([]T{1}, []T{2}, []T{1})
67+
68+
f([]T{1, 2, 3, 4}, []T{1}, []T{2, 3, 4})
69+
f([]T{1, 2, 3, 4}, []T{2}, []T{1, 3, 4})
70+
f([]T{1, 2, 3, 4}, []T{4}, []T{1, 2, 3})
71+
72+
f([]T{1, 2, 3, 4}, []T{1, 2}, []T{3, 4})
73+
f([]T{1, 2, 3, 4}, []T{1, 2, 4}, []T{3})
74+
f([]T{1, 2, 3, 4}, []T{1, 2, 3, 4}, []T{})
75+
f([]T{1, 2, 3, 4}, []T{2, 4}, []T{1, 3})
76+
77+
f([]T{1, 1, 2, 3, 1, 4, 1}, []T{1}, []T{2, 3, 4})
78+
}
79+
```

docs/slices/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ type Slices struct {
3636
| -------- | ----------- |
3737
| [Concat](./concat.md) | Concat concatenates given slices into a single slice. |
3838
| [Product](./product.md) | Product returns cortesian product of elements {{1, 2}, {3, 4}} -> {1, 3}, {1, 4}, {2, 3}, {2, 4} |
39-
| [Zip](./zip.md) | Zip returns array of arrays of elements from given arrs on the same position |
39+
| [Zip](./zip.md) | Zip returns chan of arrays of elements from given arrs on the same position. |

docs/slices/concat.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,19 @@ func (s Slices) Concat() []T {
4242
}
4343
```
4444

45+
## Tests
46+
47+
```go
48+
func TestSlicesConcat(t *testing.T) {
49+
f := func(given [][]T, expected []T) {
50+
actual := Slices{given}.Concat()
51+
assert.Equal(t, expected, actual, "they should be equal")
52+
}
53+
f([][]T{}, []T{})
54+
f([][]T{{}}, []T{})
55+
f([][]T{{1}}, []T{1})
56+
f([][]T{{1}, {}}, []T{1})
57+
f([][]T{{}, {1}}, []T{1})
58+
f([][]T{{1, 2}, {3, 4, 5}}, []T{1, 2, 3, 4, 5})
59+
}
60+
```

docs/slices/zip.md

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Slices.Zip
22

33
```go
4-
func (s Slices) Zip() [][]T
4+
func (s Slices) Zip() chan []T
55
```
66

7-
Zip returns array of arrays of elements from given arrs on the same position
7+
Zip returns chan of arrays of elements from given arrs on the same position.
88

99
Generic types: T.
1010

@@ -32,25 +32,56 @@ Generic types: T.
3232
## Source
3333

3434
```go
35-
// Zip returns array of arrays of elements from given arrs
36-
// on the same position
37-
func (s Slices) Zip() [][]T {
35+
// Zip returns chan of arrays of elements from given arrs on the same position.
36+
func (s Slices) Zip() chan []T {
37+
if len(s.Data) == 0 {
38+
result := make(chan []T)
39+
close(result)
40+
return result
41+
}
42+
3843
size := len(s.Data[0])
3944
for _, arr := range s.Data[1:] {
40-
if len(arr) > size {
45+
if len(arr) < size {
4146
size = len(arr)
4247
}
4348
}
4449
45-
result := make([][]T, 0, size)
46-
for i := 0; i <= size; i++ {
47-
chunk := make([]T, 0, len(s.Data))
48-
for _, arr := range s.Data {
49-
chunk = append(chunk, arr[i])
50+
result := make(chan []T, 1)
51+
go func() {
52+
for i := 0; i < size; i++ {
53+
chunk := make([]T, 0, len(s.Data))
54+
for _, arr := range s.Data {
55+
chunk = append(chunk, arr[i])
56+
}
57+
result <- chunk
5058
}
51-
result = append(result, chunk)
52-
}
59+
close(result)
60+
}()
5361
return result
5462
}
5563
```
5664

65+
## Tests
66+
67+
```go
68+
func TestSlicesZip(t *testing.T) {
69+
f := func(given [][]T, expected [][]T) {
70+
actual := make([][]T, 0)
71+
i := 0
72+
s := Slices{given}
73+
for el := range s.Zip() {
74+
actual = append(actual, el)
75+
i++
76+
if i > 50 {
77+
t.Fatal("infinite loop")
78+
}
79+
}
80+
assert.Equal(t, expected, actual, "they should be equal")
81+
}
82+
f([][]T{}, [][]T{})
83+
f([][]T{{1}, {2}}, [][]T{{1, 2}})
84+
f([][]T{{1, 2}, {3, 4}}, [][]T{{1, 3}, {2, 4}})
85+
f([][]T{{1, 2, 3}, {4, 5}}, [][]T{{1, 4}, {2, 5}})
86+
}
87+
```

0 commit comments

Comments
 (0)