-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dima Kossovich
committed
Nov 6, 2023
1 parent
c1874a5
commit ef97d6e
Showing
5 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package internalpipe | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_MapFilter(t *testing.T) { | ||
t.Parallel() | ||
|
||
exp := make([]int, 0, 100_000) | ||
for i := 0; i < 100_000; i++ { | ||
if i%2 == 0 { | ||
exp = append(exp, i+1) | ||
} | ||
} | ||
|
||
t.Run("single thread lim set", func(t *testing.T) { | ||
p := Pipe[int]{ | ||
Fn: func(i int) (*int, bool) { | ||
return &i, false | ||
}, | ||
Len: 100_000, | ||
ValLim: -1, | ||
GoroutinesCnt: 1, | ||
} | ||
res := p.MapFilter(func(x int) (int, bool) { return x + 1, x%2 == 0 }). | ||
Do() | ||
|
||
require.Equal(t, len(exp), len(res)) | ||
for i, r := range res { | ||
require.Equal(t, exp[i], r) | ||
} | ||
}) | ||
|
||
t.Run("seven thread lim set", func(t *testing.T) { | ||
p := Pipe[int]{ | ||
Fn: func(i int) (*int, bool) { | ||
return &i, false | ||
}, | ||
Len: 100_000, | ||
ValLim: -1, | ||
GoroutinesCnt: 7, | ||
} | ||
|
||
res := p.MapFilter(func(x int) (int, bool) { return x + 1, x%2 == 0 }). | ||
Parallel(7).Do() | ||
|
||
for i, r := range res { | ||
require.Equal(t, exp[i], r) | ||
} | ||
}) | ||
|
||
t.Run("single thread ValLim set", func(t *testing.T) { | ||
p := Pipe[int]{ | ||
Fn: func(i int) (*int, bool) { | ||
return &i, false | ||
}, | ||
Len: -1, | ||
ValLim: len(exp), | ||
GoroutinesCnt: 1, | ||
} | ||
res := p.MapFilter(func(x int) (int, bool) { return x + 1, x%2 == 0 }). | ||
Do() | ||
|
||
require.Equal(t, len(exp), len(res)) | ||
for i, r := range res { | ||
require.Equal(t, exp[i], r) | ||
} | ||
}) | ||
|
||
t.Run("seven thread ValLim set", func(t *testing.T) { | ||
p := Pipe[int]{ | ||
Fn: func(i int) (*int, bool) { | ||
return &i, false | ||
}, | ||
Len: -1, | ||
ValLim: len(exp), | ||
GoroutinesCnt: 7, | ||
} | ||
res := p.MapFilter(func(x int) (int, bool) { return x + 1, x%2 == 0 }). | ||
Do() | ||
|
||
require.Equal(t, len(exp), len(res)) | ||
for i, r := range res { | ||
require.Equal(t, exp[i], r) | ||
} | ||
}) | ||
|
||
t.Run("seven thread ValLim multiple calls", func(t *testing.T) { | ||
p := Pipe[int]{ | ||
Fn: func(i int) (*int, bool) { | ||
return &i, false | ||
}, | ||
Len: -1, | ||
ValLim: len(exp), | ||
GoroutinesCnt: 7, | ||
} | ||
res := p.MapFilter(func(x int) (int, bool) { return x + 1, x%2 == 0 }). | ||
MapFilter(func(x int) (int, bool) { return x, true }). | ||
Do() | ||
|
||
require.Equal(t, len(exp), len(res)) | ||
for i, r := range res { | ||
require.Equal(t, exp[i], r) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters