Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Kossovich committed Nov 6, 2023
1 parent ef97d6e commit 7c251ab
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/algo/parallel/qsort/qsort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions internal/internalpipe/mapfilterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

0 comments on commit 7c251ab

Please sign in to comment.