From 7c251ab87ac7e45bdd13891184f68d4c128d4f09 Mon Sep 17 00:00:00 2001 From: Dima Kossovich Date: Mon, 6 Nov 2023 17:20:54 +0200 Subject: [PATCH] add tests --- internal/algo/parallel/qsort/qsort_test.go | 16 +++++- internal/internalpipe/mapfilterer_test.go | 61 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/internal/algo/parallel/qsort/qsort_test.go b/internal/algo/parallel/qsort/qsort_test.go index af5ecd6..09abceb 100644 --- a/internal/algo/parallel/qsort/qsort_test.go +++ b/internal/algo/parallel/qsort/qsort_test.go @@ -9,12 +9,26 @@ import ( "github.com/stretchr/testify/require" ) +// saving random array to make tests faster +var ( + rndAr []int + rndMx sync.Mutex +) + func rnd(n int) []int { + rndMx.Lock() + defer rndMx.Unlock() + + if len(rndAr) >= n { + return rndAr[:n] + } + res := make([]int, n) r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < n; i++ { res[i] = r.Intn(n) } + rndAr = res return res } @@ -111,7 +125,7 @@ func Test_sort_big(t *testing.T) { } func Test_sort_huge(t *testing.T) { - a := rnd(1_000_000) + a := rnd(500_000) tickets := genTickets(12) var wg sync.WaitGroup wg.Add(1) diff --git a/internal/internalpipe/mapfilterer_test.go b/internal/internalpipe/mapfilterer_test.go index 6d7b968..685171b 100644 --- a/internal/internalpipe/mapfilterer_test.go +++ b/internal/internalpipe/mapfilterer_test.go @@ -106,4 +106,65 @@ func Test_MapFilter(t *testing.T) { require.Equal(t, exp[i], r) } }) + + t.Run("single thread lim set empty", 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, false }). + Do() + + require.Equal(t, 0, len(res)) + }) + + 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, false }). + Parallel(7).Do() + + require.Equal(t, 0, len(res)) + }) + + 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, false }). + Do() + + require.Equal(t, 0, len(res)) + }) + + 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, false }). + Do() + + require.Equal(t, 0, len(res)) + }) }