Skip to content

Commit

Permalink
[TASK] TRK-801 Implement slices merge function (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skandalik authored Nov 23, 2022
1 parent 3fd6536 commit 7234908
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 21 deletions.
56 changes: 42 additions & 14 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,52 @@ name: Test

on: push

env:
GO111MODULE: on

jobs:
test:
build:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.18', '1.19.x' ]

steps:
- name: Checkout the repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Checkout actions repository
uses: actions/checkout@v2
with:
repository: msales/github-actions
ref: master
token: ${{ secrets.GH_TOKEN }}
path: .github/actions/external

- name: Run the tests
id: test
uses: ./.github/actions/external/go-test
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
org_token: ${{ secrets.GH_TOKEN }}
go-version: ${{ matrix.go-version }}

- name: Display Go version
run: go version

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Install cover
run: go get -u golang.org/x/tools/cmd/cover

- name: Install vet
run: go get -u github.com/mattn/goveralls

- name: Install goveralls
run: go get -u github.com/mattn/goveralls

- name: Staticcheck
run: staticcheck -checks="all,-ST1000" ./...

- name: Vet
run: go vet ./...

- name: Test
run: go test -bench=. -covermode=count -coverprofile=profile_full.cov -coverpkg=./... ./...

- name: Goveralls
run: |
cat profile_full.cov | grep -v .pb.go | grep -v mock | grep -v test > profile.cov;
goveralls -coverprofile=profile.cov -service=github || true;
3 changes: 2 additions & 1 deletion gofp/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ func BenchmarkAny_Struct(b *testing.B) {
{"4", 4},
{"6", 5},
}
b.ResetTimer()

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Any(haystack, func(ts testStruct) bool {
Expand Down
8 changes: 8 additions & 0 deletions gofp/contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func TestContainsAll(t *testing.T) {

func BenchmarkContainsAll_Single(b *testing.B) {
haystack := []int64{1, 2, 3, 4, 5, 6}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -83,6 +85,8 @@ func BenchmarkContainsAll_Single(b *testing.B) {

func BenchmarkContainsAll_Multiple(b *testing.B) {
haystack := []int64{1, 2, 3, 4, 5, 6}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -140,6 +144,8 @@ func TestContainsAtLeastOne(t *testing.T) {

func BenchmarkContainsAtLeastOne_Single(b *testing.B) {
haystack := []int64{1, 2, 3, 4, 5, 6}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -149,6 +155,8 @@ func BenchmarkContainsAtLeastOne_Single(b *testing.B) {

func BenchmarkContainsAtLeastOne_Multiple(b *testing.B) {
haystack := []int64{1, 2, 3, 4, 5, 6}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
16 changes: 16 additions & 0 deletions gofp/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func TestSymDiff(t *testing.T) {
func BenchmarkSymDiff_Both(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5, 6}
slice2 := []int{5, 6, 7, 8, 9, 10}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -84,6 +86,8 @@ func BenchmarkSymDiff_Both(b *testing.B) {
func BenchmarkSymDiff_Both_Pessimistic(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
slice2 := []int{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -102,6 +106,8 @@ func BenchmarkSymDiff_Both_MoreValues_Random(b *testing.B) {
}
rand.Shuffle(len(slice1), func(i, j int) { slice1[i], slice1[j] = slice1[j], slice1[i] })
rand.Shuffle(len(slice2), func(i, j int) { slice2[i], slice2[j] = slice2[j], slice2[i] })

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -112,6 +118,8 @@ func BenchmarkSymDiff_Both_MoreValues_Random(b *testing.B) {
func BenchmarkSymDiff_Both_Pessimistic_RightShorter(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
slice2 := []int{17, 18, 19, 20}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -188,6 +196,8 @@ func TestDiffLeft(t *testing.T) {
func BenchmarkDiffLeft(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5, 6}
slice2 := []int{5, 6, 7, 8}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -206,6 +216,8 @@ func BenchmarkDiffLeft_MoreValues_Random(b *testing.B) {
}
rand.Shuffle(len(slice1), func(i, j int) { slice1[i], slice1[j] = slice1[j], slice1[i] })
rand.Shuffle(len(slice2), func(i, j int) { slice2[i], slice2[j] = slice2[j], slice2[i] })

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -282,6 +294,8 @@ func TestDiffRight(t *testing.T) {
func BenchmarkDiffRight(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5, 6}
slice2 := []int{5, 6, 7, 8}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -300,6 +314,8 @@ func BenchmarkDiffRight_MoreValues_Random(b *testing.B) {
}
rand.Shuffle(len(slice1), func(i, j int) { slice1[i], slice1[j] = slice1[j], slice1[i] })
rand.Shuffle(len(slice2), func(i, j int) { slice2[i], slice2[j] = slice2[j], slice2[i] })

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
2 changes: 2 additions & 0 deletions gofp/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestFilter_Int64(t *testing.T) {

func BenchmarkFilter_Int64(b *testing.B) {
original := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
6 changes: 3 additions & 3 deletions gofp/intersection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"testing"

"github.com/msales/gox/gofp"
"github.com/stretchr/testify/assert"

"github.com/msales/gox/gofp"
)

func TestIntersection_Int(t *testing.T) {
Expand Down Expand Up @@ -83,11 +84,10 @@ func BenchmarkIntersection_TwoSlices(b *testing.B) {
return elementOne == elementTwo
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
gofp.Intersection(slice1, slice2, predicate)
}

b.ReportAllocs()
}
4 changes: 4 additions & 0 deletions gofp/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestMap_StringToStruct(t *testing.T) {

func BenchmarkMap_Int64ToIn32(b *testing.B) {
original := []int64{1, 2, 3, 4, 5, 6}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -63,6 +64,7 @@ func BenchmarkMap_Int64ToIn32(b *testing.B) {

func BenchmarkMap_StringToStruct(b *testing.B) {
original := []string{"1", "2", "3"}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -127,6 +129,7 @@ func TestMapMultiple_StringToStruct(t *testing.T) {

func BenchmarkMapMultiple_Int64ToIn32(b *testing.B) {
original := []int64{1, 2, 3, 4, 5, 6}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -141,6 +144,7 @@ func BenchmarkMapMultiple_Int64ToIn32(b *testing.B) {

func BenchmarkMapMultiple_StringToStruct(b *testing.B) {
original := []string{"1", "2", "3"}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
10 changes: 10 additions & 0 deletions gofp/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gofp

// Merge merges passed slices
func Merge[T any](lists ...[]T) (res []T) {
for _, list := range lists {
res = append(res, list...)
}

return
}
67 changes: 67 additions & 0 deletions gofp/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gofp_test

import (
"testing"

. "github.com/msales/gox/gofp"
)

func TestMerge_Int64(t *testing.T) {
got := Merge([]int64{1, 2, 3}, []int64{4, 5, 6})

want := []int64{1, 2, 3, 4, 5, 6}

if len(want) != len(got) {
t.Errorf("Got %+v, want %+v", got, want)
return
}

for k, v := range want {
if v != got[k] {
t.Errorf("Got %+v, want %+v", got, want)
}
}
}

func TestMerge_Struct(t *testing.T) {
got := Merge([]testID{{ID: 1}, {ID: 2}}, []testID{{ID: 3}})

want := []testID{
{ID: 1},
{ID: 2},
{ID: 3},
}

if len(want) != len(got) {
t.Errorf("Got %+v, want %+v", got, want)
return
}

for k, v := range want {
if v != got[k] {
t.Errorf("Got %+v, want %+v", got, want)
}
}
}

func BenchmarkMerge_Int64(b *testing.B) {
slice1 := []int64{1, 2, 3}
slice2 := []int64{4, 5, 6}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Merge(slice1, slice2)
}
}

func BenchmarkMerge_Struct(b *testing.B) {
slice1 := []testID{{ID: 1}, {ID: 2}, {ID: 3}}
slice2 := []testID{{ID: 4}, {ID: 5}, {ID: 6}}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Merge(slice1, slice2)
}
}
2 changes: 2 additions & 0 deletions gofp/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestReduce(t *testing.T) {

func BenchmarkReduce(b *testing.B) {
original := []int64{1, 2, 3, 4, 5, 6}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
7 changes: 4 additions & 3 deletions gofp/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"sort"
"testing"

"github.com/msales/gox/gofp"
"github.com/stretchr/testify/assert"

"github.com/msales/gox/gofp"
)

func TestUnion_Int(t *testing.T) {
Expand Down Expand Up @@ -100,11 +101,11 @@ func BenchmarkUnion_TwoSlices(b *testing.B) {
predicate := func(elementOne, elementTwo int) bool {
return elementOne == elementTwo
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
gofp.Union(slice1, slice2, predicate)
}

b.ReportAllocs()
}

0 comments on commit 7234908

Please sign in to comment.