aboutsummaryrefslogtreecommitdiff
path: root/filter_test.go
blob: a19d3618f6ae3639811f7e139c8cf0379bb32671 (plain) (blame)
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
package collection

import (
	"reflect"
	"testing"
)

func TestFilterSync(t *testing.T) {
	type args struct {
		collection []int
		filter     func(item int, idx int) bool
	}
	tests := []struct {
		name string
		args args
		want []int
	}{
		{
			name: "odds",
			args: args{
				collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
				filter: func(item int, idx int) bool {
					return item%2 == 0
				},
			},
			want: []int{2, 4, 6, 8, 10},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := FilterSync(tt.args.collection, tt.args.filter); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("Filter() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestFilter(t *testing.T) {
	type args struct {
		collection []int
		filter     func(item int, idx int) bool
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{
			name: "odds count",
			args: args{
				collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
				filter: func(item int, idx int) bool {
					return item%2 == 0
				},
			},
			want: 5,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Filter(tt.args.collection, tt.args.filter); len(got) != tt.want {
				t.Errorf("FilterParallel() returned %v elements, want %v", len(got), tt.want)
			}
		})
	}
}