wp-go/stream/stream_test.go

601 lines
12 KiB
Go
Raw Permalink Normal View History

package stream
import (
"fmt"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice"
"reflect"
2023-01-16 09:59:42 +00:00
"strconv"
"testing"
)
2023-01-28 14:36:15 +00:00
var s = NewStream(number.Range(1, 10, 1))
func TestSimpleSliceStream_Filter(t *testing.T) {
type args[T int] struct {
fn func(T) bool
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
func(t int) (r bool) {
if t > 5 {
r = true
}
return
},
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(6, 10, 1)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Filter(tt.args.fn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Filter() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_ForEach(t *testing.T) {
type args[T int] struct {
fn func(T)
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
func(t int) {
fmt.Println(t)
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.r.ForEach(tt.args.fn)
})
}
}
func TestSimpleSliceStream_Limit(t *testing.T) {
type args struct {
limit int
offset int
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args{
limit: 3,
offset: 5,
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(6, 8, 1)},
},
2023-01-16 10:28:58 +00:00
{
name: "t2",
r: s,
args: args{
limit: 3,
offset: 9,
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(10, 10, 1)},
2023-01-16 10:28:58 +00:00
},
{
name: "t3",
r: s,
args: args{
limit: 3,
offset: 11,
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{},
2023-01-16 10:28:58 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Limit(tt.args.limit, tt.args.offset); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Limit() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_Map(t *testing.T) {
type args[T int] struct {
fn func(T) T
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
func(t int) (r int) {
return t * 2
},
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(2, 20, 2)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Map(tt.args.fn); !reflect.DeepEqual(got, tt.want) {
2023-01-28 14:36:15 +00:00
t.Errorf("MapNewStream() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_Result(t *testing.T) {
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
want []T
}
tests := []testCase[int]{
{
name: "t1",
r: s,
2023-01-21 11:31:23 +00:00
want: number.Range(1, 10, 1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Result(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Result() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_Sort(t *testing.T) {
type args[T int] struct {
fn func(i, j T) bool
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
fn: func(i, j int) bool {
return i > j
},
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(10, 1, -1)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Sort(tt.args.fn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Sort() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_parallelForEach(t *testing.T) {
type args[T int] struct {
fn func(T)
c int
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
fn: func(t int) {
fmt.Println(t)
},
c: 3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.r.ParallelForEach(tt.args.fn, tt.args.c)
})
}
}
func TestSimpleSliceStream_ParallelFilter(t *testing.T) {
type args[T int] struct {
fn func(T) bool
c int
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
fn: func(t int) bool {
return t > 3
},
c: 6,
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(4, 10, 1)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.ParallelFilter(tt.args.fn, tt.args.c).Sort(func(i, j int) bool {
return i < j
}); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParallelFilter() = %v, want %v", got, tt.want)
}
})
}
}
func TestSimpleSliceStream_ParallelMap(t *testing.T) {
type args[T int] struct {
fn func(T) T
c int
}
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
args args[T]
2023-01-28 14:36:15 +00:00
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
r: s,
args: args[int]{
fn: func(t int) int {
return t * 2
},
c: 6,
},
2023-01-28 14:36:15 +00:00
want: Stream[int]{number.Range(2, 20, 2)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.ParallelMap(tt.args.fn, tt.args.c).Sort(func(i, j int) bool {
return i < j
}); !reflect.DeepEqual(got, tt.want) {
2023-01-28 14:36:15 +00:00
t.Errorf("ParallelMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestReduce(t *testing.T) {
type args[S, T int] struct {
2023-01-28 14:36:15 +00:00
s Stream[S]
2023-01-16 06:42:13 +00:00
fn func(S, T) T
init T
}
type testCase[S, T int] struct {
name string
args args[S, T]
wantR T
}
tests := []testCase[int, int]{
{
name: "t1",
args: args[int, int]{
s, func(i, r int) int {
return i + r
},
2023-01-16 06:42:13 +00:00
0,
},
2023-01-21 11:31:23 +00:00
wantR: number.Sum(s.Result()...),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-16 06:42:13 +00:00
if gotR := Reduce(tt.args.s, tt.args.fn, tt.args.init); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("Reduce() = %v, want %v", gotR, tt.wantR)
}
})
}
}
func TestSimpleSliceStream_Reverse(t *testing.T) {
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
want Stream[T]
}
tests := []testCase[int]{
{
name: "t1",
2023-01-28 14:36:15 +00:00
r: NewStream(number.Range(1, 10, 1)),
want: Stream[int]{number.Range(10, 1, -1)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Reverse(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Reverse() = %v, want %v", got, tt.want)
}
})
}
}
2023-01-16 09:59:42 +00:00
2023-01-21 11:31:23 +00:00
var x = number.Range(1, 100000, 1)
2023-01-16 09:59:42 +00:00
func TestSimpleStreamMap(t *testing.T) {
type args[T int, R string] struct {
2023-01-28 14:36:15 +00:00
a Stream[T]
2023-01-16 09:59:42 +00:00
fn func(T) R
}
type testCase[T int, R string] struct {
name string
args args[T, R]
2023-01-28 14:36:15 +00:00
want Stream[R]
2023-01-16 09:59:42 +00:00
}
tests := []testCase[int, string]{
{
name: "t1",
args: args[int, string]{
2023-01-28 14:36:15 +00:00
a: NewStream(x),
2023-01-16 09:59:42 +00:00
fn: strconv.Itoa,
},
2023-01-28 14:36:15 +00:00
want: Stream[string]{
2023-01-21 11:31:23 +00:00
slice.Map(x, strconv.Itoa),
2023-01-16 09:59:42 +00:00
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if got := MapNewStream(tt.args.a, tt.args.fn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapNewStream() = %v, want %v", got, tt.want)
2023-01-16 09:59:42 +00:00
}
})
}
}
func TestSimpleParallelMap(t *testing.T) {
type args[T string, R int] struct {
2023-01-28 14:36:15 +00:00
a Stream[string]
2023-01-16 09:59:42 +00:00
fn func(T) R
c int
}
type testCase[T string, R int] struct {
name string
args args[T, R]
2023-01-28 14:36:15 +00:00
want Stream[R]
2023-01-16 09:59:42 +00:00
}
tests := []testCase[string, int]{
{
name: "t1",
args: args[string, int]{
2023-01-28 14:36:15 +00:00
a: NewStream(slice.Map(x, strconv.Itoa)),
2023-01-16 09:59:42 +00:00
fn: func(s string) int {
i, _ := strconv.Atoi(s)
return i
},
c: 6,
},
2023-01-28 14:36:15 +00:00
want: NewStream(x),
2023-01-16 09:59:42 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if got := ParallelMap(tt.args.a, tt.args.fn, tt.args.c).Sort(func(i, j int) bool {
2023-01-16 09:59:42 +00:00
return i < j
}); !reflect.DeepEqual(got, tt.want) {
2023-01-28 14:36:15 +00:00
t.Errorf("ParallelMap() = %v, want %v", got, tt.want)
2023-01-16 09:59:42 +00:00
}
})
}
}
func TestSimpleParallelFilterAndMap(t *testing.T) {
type args[T string, R int] struct {
2023-01-28 14:36:15 +00:00
a Stream[string]
2023-01-16 09:59:42 +00:00
fn func(T) (R, bool)
c int
}
type testCase[T string, R int] struct {
name string
args args[T, R]
2023-01-28 14:36:15 +00:00
want Stream[R]
2023-01-16 09:59:42 +00:00
}
tests := []testCase[string, int]{
{
name: "t1",
args: args[string, int]{
2023-01-28 14:36:15 +00:00
a: NewStream(slice.Map(x, strconv.Itoa)),
2023-01-16 09:59:42 +00:00
fn: func(s string) (int, bool) {
i, _ := strconv.Atoi(s)
if i > 50000 {
return i, true
}
return 0, false
},
c: 6,
},
2023-01-28 14:36:15 +00:00
want: NewStream(x[50000:]),
2023-01-16 09:59:42 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if got := ParallelFilterAndMap(tt.args.a, tt.args.fn, tt.args.c).Sort(func(i, j int) bool {
2023-01-16 09:59:42 +00:00
return i < j
}); !reflect.DeepEqual(got, tt.want) {
2023-01-28 14:36:15 +00:00
t.Errorf("ParallelFilterAndMap() = %v, want %v", got, tt.want)
2023-01-16 09:59:42 +00:00
}
})
}
}
func TestSimpleStreamFilterAndMap(t *testing.T) {
type args[T string, R int] struct {
2023-01-28 14:36:15 +00:00
a Stream[T]
2023-01-16 09:59:42 +00:00
fn func(T) (R, bool)
}
type testCase[T any, R any] struct {
name string
args args[string, int]
2023-01-28 14:36:15 +00:00
want Stream[R]
2023-01-16 09:59:42 +00:00
}
tests := []testCase[string, int]{
{
name: "t1",
args: args[string, int]{
2023-01-28 14:36:15 +00:00
a: NewStream(slice.Map(x, strconv.Itoa)),
2023-01-16 09:59:42 +00:00
fn: func(s string) (int, bool) {
i, _ := strconv.Atoi(s)
if i > 50000 {
return i, true
}
return 0, false
},
},
2023-01-28 14:36:15 +00:00
want: NewStream(x[50000:]),
2023-01-16 09:59:42 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if got := FilterAndMapNewStream(tt.args.a, tt.args.fn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FilterAndMapNewStream() = %v, want %v", got, tt.want)
2023-01-16 09:59:42 +00:00
}
})
}
}
2023-01-16 10:05:31 +00:00
func TestSimpleSliceStream_Len(t *testing.T) {
type testCase[T int] struct {
name string
2023-01-28 14:36:15 +00:00
r Stream[T]
2023-01-16 10:05:31 +00:00
want int
}
tests := []testCase[int]{
{
name: "t1",
r: s,
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Len(); got != tt.want {
t.Errorf("Len() = %v, want %v", got, tt.want)
}
})
}
}
2023-01-16 13:14:42 +00:00
func TestSimpleParallelFilterAndMapToMap(t *testing.T) {
type args[T int, K int, V int] struct {
2023-01-28 14:36:15 +00:00
a Stream[V]
2023-01-16 13:14:42 +00:00
fn func(t T) (K, V, bool)
c int
}
type testCase[T int, K int, V int] struct {
name string
args args[T, K, V]
2023-01-28 14:36:15 +00:00
wantR MapStream[K, V]
2023-01-16 13:14:42 +00:00
}
tests := []testCase[int, int, int]{
{
name: "t1",
args: args[int, int, int]{
2023-01-28 14:36:15 +00:00
a: NewStream(x),
2023-01-16 13:14:42 +00:00
fn: func(v int) (int, int, bool) {
if v >= 50000 {
return v, v, true
}
return 0, 0, false
},
c: 6,
},
2023-01-21 11:31:23 +00:00
wantR: NewSimpleMapStream(slice.ToMap(x[50000:], func(t int) (int, int) {
2023-01-16 13:14:42 +00:00
return t, t
}, true)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if gotR := ParallelFilterAndMapToMapStream(tt.args.a, tt.args.fn, tt.args.c); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("ParallelFilterAndMapToMapStream() = %v, want %v", gotR, tt.wantR)
2023-01-16 13:14:42 +00:00
}
})
}
}
2023-01-17 05:23:42 +00:00
func TestSimpleSliceFilterAndMapToMap(t *testing.T) {
type args[T int, K int, V int] struct {
2023-01-28 14:36:15 +00:00
a Stream[T]
2023-01-17 05:23:42 +00:00
fn func(t T) (K, V, bool)
isCoverPrev bool
}
type testCase[T int, K int, V int] struct {
name string
args args[T, K, V]
2023-01-28 14:36:15 +00:00
wantR MapStream[K, V]
2023-01-17 05:23:42 +00:00
}
tests := []testCase[int, int, int]{
{
name: "t1",
args: args[int, int, int]{
2023-01-28 14:36:15 +00:00
a: NewStream(number.Range(1, 10, 1)),
2023-01-17 05:23:42 +00:00
fn: func(i int) (int, int, bool) {
if i > 6 {
return i, i, true
}
return 0, 0, false
},
},
2023-01-21 11:31:23 +00:00
wantR: NewSimpleMapStream(slice.ToMap(number.Range(7, 10, 1), func(t int) (int, int) {
2023-01-17 05:23:42 +00:00
return t, t
}, true)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-28 14:36:15 +00:00
if gotR := SliceFilterAndMapToMapStream(tt.args.a, tt.args.fn, tt.args.isCoverPrev); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("SliceFilterAndMapToMapStream() = %v, want %v", gotR, tt.wantR)
2023-01-17 05:23:42 +00:00
}
})
}
}