2023-01-16 13:14:42 +00:00
|
|
|
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"
|
2023-01-16 13:14:42 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSimpleMapStream(t *testing.T) {
|
|
|
|
type args[K int, V int] struct {
|
|
|
|
m map[K]V
|
|
|
|
}
|
|
|
|
type testCase[K int, V int] struct {
|
|
|
|
name string
|
|
|
|
args args[K, V]
|
2023-01-28 14:36:15 +00:00
|
|
|
want MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
}
|
|
|
|
tests := []testCase[int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args[int, int]{make(map[int]int)},
|
2023-01-28 14:36:15 +00:00
|
|
|
want: MapStream[int, int]{make(map[int]int)},
|
2023-01-16 13:14:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := NewSimpleMapStream(tt.args.m); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("NewSimpleMapStream() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 11:31:23 +00:00
|
|
|
var y = number.Range(1, 1000, 1)
|
|
|
|
var w = slice.ToMap(y, func(v int) (int, int) {
|
2023-01-16 13:14:42 +00:00
|
|
|
return v, v
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
func TestSimpleMapFilterAndMapToSlice(t *testing.T) {
|
|
|
|
type args[K int, V int, R int] struct {
|
2023-01-28 14:36:15 +00:00
|
|
|
mm MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
fn func(K, V) (R, bool)
|
|
|
|
c int
|
|
|
|
}
|
|
|
|
type testCase[K int, V int, R int] struct {
|
|
|
|
name string
|
|
|
|
args args[K, V, R]
|
2023-01-28 14:36:15 +00:00
|
|
|
want Stream[R]
|
2023-01-16 13:14:42 +00:00
|
|
|
}
|
|
|
|
tests := []testCase[int, int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args[int, int, int]{
|
|
|
|
mm: NewSimpleMapStream(w),
|
|
|
|
fn: func(k, v int) (int, bool) {
|
|
|
|
if v > 500 {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
},
|
|
|
|
c: 6,
|
|
|
|
},
|
2023-01-28 14:36:15 +00:00
|
|
|
want: NewStream(y[500:]),
|
2023-01-16 13:14:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-04 04:35:58 +00:00
|
|
|
if got := SimpleMapFilterAndMapToSlice(tt.args.mm, tt.args.fn).Sort(func(i, j int) bool {
|
2023-01-16 13:14:42 +00:00
|
|
|
return i < j
|
|
|
|
}); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("SimpleMapFilterAndMapToSlice() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapParallelFilterAndMapToMap(t *testing.T) {
|
|
|
|
type args[KK string, VV string, K int, V int] struct {
|
2023-01-28 14:36:15 +00:00
|
|
|
mm MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
fn func(K, V) (KK, VV, bool)
|
|
|
|
c int
|
|
|
|
}
|
|
|
|
type testCase[KK string, VV string, K int, V int] struct {
|
|
|
|
name string
|
|
|
|
args args[KK, VV, K, V]
|
2023-01-28 14:36:15 +00:00
|
|
|
want MapStream[KK, VV]
|
2023-01-16 13:14:42 +00:00
|
|
|
}
|
|
|
|
tests := []testCase[string, string, int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args[string, string, int, int]{
|
|
|
|
mm: NewSimpleMapStream(w),
|
|
|
|
fn: func(k, v int) (string, string, bool) {
|
|
|
|
if v > 500 {
|
|
|
|
t := strconv.Itoa(v)
|
|
|
|
return t, t, true
|
|
|
|
}
|
|
|
|
return "", "", false
|
|
|
|
},
|
|
|
|
c: 6,
|
|
|
|
},
|
2023-01-21 11:31:23 +00:00
|
|
|
want: NewSimpleMapStream(slice.ToMap(y[500:], func(v int) (K, T string) {
|
2023-01-16 13:14:42 +00:00
|
|
|
t := strconv.Itoa(v)
|
|
|
|
return t, t
|
|
|
|
}, true)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := SimpleMapParallelFilterAndMapToMap(tt.args.mm, tt.args.fn, tt.args.c); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("SimpleMapParallelFilterAndMapToMap() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapStreamFilterAndMapToMap(t *testing.T) {
|
|
|
|
type args[KK string, VV string, K int, V int] struct {
|
2023-01-28 14:36:15 +00:00
|
|
|
a MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
fn func(K, V) (KK, VV, bool)
|
|
|
|
}
|
|
|
|
type testCase[KK string, VV string, K int, V int] struct {
|
|
|
|
name string
|
|
|
|
args args[KK, VV, K, V]
|
2023-01-28 14:36:15 +00:00
|
|
|
wantR MapStream[KK, VV]
|
2023-01-16 13:14:42 +00:00
|
|
|
}
|
|
|
|
tests := []testCase[string, string, int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args[string, string, int, int]{
|
|
|
|
a: NewSimpleMapStream(w),
|
|
|
|
fn: func(k, v int) (string, string, bool) {
|
|
|
|
if v > 500 {
|
|
|
|
t := strconv.Itoa(v)
|
|
|
|
return t, t, true
|
|
|
|
}
|
|
|
|
return "", "", false
|
|
|
|
},
|
|
|
|
},
|
2023-01-21 11:31:23 +00:00
|
|
|
wantR: NewSimpleMapStream(slice.ToMap(y[500:], func(v int) (K, T string) {
|
2023-01-16 13:14:42 +00:00
|
|
|
t := strconv.Itoa(v)
|
|
|
|
return t, t
|
|
|
|
}, true)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if gotR := SimpleMapStreamFilterAndMapToMap(tt.args.a, tt.args.fn); !reflect.DeepEqual(gotR, tt.wantR) {
|
|
|
|
t.Errorf("SimpleMapStreamFilterAndMapToMap() = %v, want %v", gotR, tt.wantR)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapStream_ForEach(t *testing.T) {
|
|
|
|
type args[K int, V int] struct {
|
|
|
|
fn func(K, V)
|
|
|
|
}
|
|
|
|
type testCase[K int, V int] struct {
|
|
|
|
name string
|
2023-01-28 14:36:15 +00:00
|
|
|
r MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
args args[K, V]
|
|
|
|
}
|
|
|
|
tests := []testCase[int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
2023-01-21 11:31:23 +00:00
|
|
|
r: NewSimpleMapStream(slice.ToMap(y[0:10], func(v int) (int, int) {
|
2023-01-16 13:14:42 +00:00
|
|
|
return v, v
|
|
|
|
}, true)),
|
|
|
|
args: args[int, int]{
|
|
|
|
fn: func(k, v int) {
|
|
|
|
fmt.Println(k, v)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.r.ForEach(tt.args.fn)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapStream_Len(t *testing.T) {
|
|
|
|
type testCase[K int, V int] struct {
|
|
|
|
name string
|
2023-01-28 14:36:15 +00:00
|
|
|
r MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
want int
|
|
|
|
}
|
|
|
|
tests := []testCase[int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
r: NewSimpleMapStream(w),
|
|
|
|
want: len(w),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapStream_ParallelForEach(t *testing.T) {
|
|
|
|
type args[K int, V int] struct {
|
|
|
|
fn func(K, V)
|
|
|
|
c int
|
|
|
|
}
|
|
|
|
type testCase[K int, V int] struct {
|
|
|
|
name string
|
2023-01-28 14:36:15 +00:00
|
|
|
r MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
args args[K, V]
|
|
|
|
}
|
|
|
|
tests := []testCase[int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
r: NewSimpleMapStream(w),
|
|
|
|
args: args[int, int]{
|
|
|
|
func(k, v int) {
|
|
|
|
fmt.Println(k, v)
|
|
|
|
},
|
|
|
|
6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.r.ParallelForEach(tt.args.fn, tt.args.c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMapStream_Result(t *testing.T) {
|
|
|
|
type testCase[K int, V int] struct {
|
|
|
|
name string
|
2023-01-28 14:36:15 +00:00
|
|
|
r MapStream[K, V]
|
2023-01-16 13:14:42 +00:00
|
|
|
want map[K]V
|
|
|
|
}
|
|
|
|
tests := []testCase[int, int]{
|
|
|
|
{
|
|
|
|
name: "t1",
|
2023-01-21 11:31:23 +00:00
|
|
|
r: NewSimpleMapStream(slice.ToMap(y, func(v int) (int, int) {
|
2023-01-16 13:14:42 +00:00
|
|
|
return v, v
|
|
|
|
}, true)),
|
2023-01-21 11:31:23 +00:00
|
|
|
want: slice.ToMap(y, func(v int) (int, int) {
|
2023-01-16 13:14:42 +00:00
|
|
|
return v, v
|
|
|
|
}, true),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|