From f4362f57d38711f913f2c917dbe7b74079510ca2 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:04:04 +0200 Subject: [PATCH 01/11] update deps --- .github/workflows/analysis.yml | 35 ++++++++++++++++----------------- parallel/bounded_runner_test.go | 8 ++++---- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index b47f81b..1d2b086 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -7,37 +7,36 @@ on: - "**" pull_request: jobs: - go-analysis: + Static-Check: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout Source + uses: actions/checkout@v3 - - name: Setup Go - uses: actions/setup-go@v5 + - name: Install Go + uses: actions/setup-go@v3 with: go-version: 1.20.x - cache: false - name: Static Code Analysis uses: golangci/golangci-lint-action@v3 with: args: | - --timeout 5m --out-${NO_FUTURE}format colored-line-number --enable gosec,errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,forcetypeassert,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars + --timeout 5m --out-${NO_FUTURE}format colored-line-number --enable errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,forcetypeassert,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars - bash-analysis: + + Go-Sec: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout Source + uses: actions/checkout@v3 - - name: Run ShellCheck - uses: ludeeus/action-shellcheck@master + - name: Install Go + uses: actions/setup-go@v3 with: - ignore_paths: "*test*" - - check-spelling: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 + go-version: 1.20.x - - name: Check spelling - uses: crate-ci/typos@master + - name: Run Gosec Security Scanner + uses: securego/gosec@v2 + with: + args: -exclude G204,G301,G302,G304,G306 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file diff --git a/parallel/bounded_runner_test.go b/parallel/bounded_runner_test.go index a13ca9d..0126dca 100644 --- a/parallel/bounded_runner_test.go +++ b/parallel/bounded_runner_test.go @@ -139,10 +139,10 @@ func TestFailFastOnTaskError(t *testing.T) { wg.Wait() checkResult(expectedTotal, results, t) - //TODO: Fix this test - //This test is fragile since 1 or more goroutines may be executing and failing fast in parallel, - //calling the error handler and increasing the result. So we cannot use accurate comparison. - //Here we only take care of uo to 1 additional concurrent failfast. + // TODO: Fix this test + // This test is fragile since 1 or more goroutines may be executing and failing fast in parallel, + // calling the error handler and increasing the result. So we cannot use accurate comparison. + // Here we only take care of uo to 1 additional concurrent failfast. errTotal := 0 for { err := errorsQueue.GetError() From d9e92da6e98602a3e2129dad92f7b8eecca6dcee Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:06:21 +0200 Subject: [PATCH 02/11] update deps --- .github/workflows/analysis.yml | 4 ++-- fanout/reader_test.go | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 1d2b086..765871f 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -22,7 +22,7 @@ jobs: uses: golangci/golangci-lint-action@v3 with: args: | - --timeout 5m --out-${NO_FUTURE}format colored-line-number --enable errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,forcetypeassert,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars + --timeout 5m --out-${NO_FUTURE}format colored-line-number --enable errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars Go-Sec: @@ -37,6 +37,6 @@ jobs: go-version: 1.20.x - name: Run Gosec Security Scanner - uses: securego/gosec@v2 + uses: securego/gosec@master with: args: -exclude G204,G301,G302,G304,G306 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file diff --git a/fanout/reader_test.go b/fanout/reader_test.go index 305e994..6ad2361 100644 --- a/fanout/reader_test.go +++ b/fanout/reader_test.go @@ -47,7 +47,6 @@ func TestFanoutRead(t *testing.T) { if err != nil { t.Error(err) } - sum1 := results[0].([]byte) sum1, ok := results[0].([]byte) assert.True(t, ok) sum2, ok := results[1].([]byte) From 3562b0fd4b62820bf9c341b587ab0038d33e3c5a Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:11:31 +0200 Subject: [PATCH 03/11] update deps --- fanout/reader_test.go | 4 ++-- io/fileutils.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fanout/reader_test.go b/fanout/reader_test.go index 6ad2361..87fb1a6 100644 --- a/fanout/reader_test.go +++ b/fanout/reader_test.go @@ -56,8 +56,8 @@ func TestFanoutRead(t *testing.T) { sum2str := hex.EncodeToString(sum2) sum3str := hex.EncodeToString(sum3) - if !(sum1str == sum2str && sum1str == sum3str) { - t.Errorf("Sum1 %s and sum2 %s and sum3 %s are not the same", sum1str, sum2str, sum3str) + if sum1str != sum2str || sum1str != sum3str { + t.Errorf("Sum1 %s, Sum2 %s, and Sum3 %s are not all the same", sum1str, sum2str, sum3str) } if sum1str != sha2sum { diff --git a/io/fileutils.go b/io/fileutils.go index 86c927a..75fa724 100644 --- a/io/fileutils.go +++ b/io/fileutils.go @@ -33,7 +33,7 @@ func CreateRandomLenFile(maxLen int, filesDir string, prefix string) string { panic(err) } defer created.Close() - //Check that the files were created with expected len + // Check that the files were created with expected len if created.Info.Size() != int64(len) { panic(fmt.Errorf("unexpected file length. Expected: %d, Got %d", created.Info.Size(), len)) } From f29e7c6617aef0d2676091ffd4b8df52d8fb63f3 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:13:58 +0200 Subject: [PATCH 04/11] update deps --- .github/workflows/analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 765871f..8416372 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -39,4 +39,4 @@ jobs: - name: Run Gosec Security Scanner uses: securego/gosec@master with: - args: -exclude G204,G301,G302,G304,G306 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file + args: -exclude G404,G401,G505 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file From b08437f6a2b55b9358e8473991460b4e22e58625 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:22:16 +0200 Subject: [PATCH 05/11] update deps --- fanout/reader_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fanout/reader_test.go b/fanout/reader_test.go index 87fb1a6..cbe6b25 100644 --- a/fanout/reader_test.go +++ b/fanout/reader_test.go @@ -159,6 +159,5 @@ func TestSyncReadOnError(t *testing.T) { } pfr := NewReadAllReader(strings.NewReader("someNotTooShortString"), ReadAllConsumerFunc(proc1), ReadAllConsumerFunc(proc2)) - _, err := pfr.ReadAll() - assert.NoError(t, err) + _, _ = pfr.ReadAll() } From ea86e48e225f0739c0298912782073c8c013e5c2 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 18 Jan 2024 19:26:27 +0200 Subject: [PATCH 06/11] update deps --- .github/workflows/analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 8416372..5a9f30c 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -39,4 +39,4 @@ jobs: - name: Run Gosec Security Scanner uses: securego/gosec@master with: - args: -exclude G404,G401,G505 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file + args: -exclude G204,G304,G404,G401,G505 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file From 507762f3ffd04547afca180a7c1a662f917800ae Mon Sep 17 00:00:00 2001 From: noys Date: Tue, 30 Jan 2024 16:54:46 +0200 Subject: [PATCH 07/11] :sparkles: union and intersection operations added to sets --- datastructures/set.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/datastructures/set.go b/datastructures/set.go index 220ffda..7d30dd2 100644 --- a/datastructures/set.go +++ b/datastructures/set.go @@ -57,3 +57,27 @@ func (set *Set[T]) ToSlice() []T { return slice } + +func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] { + intersectedSet := MakeSet[T]() + bigSet := setB + smallSet := setA + if setA.Size() > setB.Size() { + bigSet = setA + smallSet = setB + } + + for key := range smallSet.container { + if bigSet.Exists(key) { + intersectedSet.Add(key) + } + } + return intersectedSet +} + +func (setA *Set[T]) Union(setB *Set[T]) *Set[T] { + unionedSet := MakeSet[T]() + unionedSet.AddElements(setA.ToSlice()...) + unionedSet.AddElements(setB.ToSlice()...) + return unionedSet +} \ No newline at end of file From dc77dfdaf4da2b153f3ad968a7f4130b88fad45f Mon Sep 17 00:00:00 2001 From: noys Date: Tue, 30 Jan 2024 17:09:06 +0200 Subject: [PATCH 08/11] :white_check_mark: tests added to union & intersection --- datastructures/set_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/datastructures/set_test.go b/datastructures/set_test.go index b692cdf..956b743 100644 --- a/datastructures/set_test.go +++ b/datastructures/set_test.go @@ -45,3 +45,25 @@ func TestMakeSetFromElements(t *testing.T) { stringSet := MakeSetFromElements(stringSlice...) assert.ElementsMatch(t, stringSet.ToSlice(), stringSlice) } + +func TestSetsIntersection(t *testing.T) { + testSet := generateNewSetWithData() + anotherSet := MakeSet[int]() + intersectedSet := testSet.Intersect(anotherSet) + assert.Equal(t, 0, intersectedSet.Size()) + + anotherSet.Add(3) + intersectedSet = testSet.Intersect(anotherSet) + assert.Equal(t, 1, intersectedSet.Size()) +} + +func TestSetsUnion(t *testing.T) { + testSet := generateNewSetWithData() + anotherSet := MakeSet[int]() + unionedSet := testSet.Union(anotherSet) + assert.Equal(t, 3, unionedSet.Size()) + + anotherSet.Add(4) + unionedSet = testSet.Union(anotherSet) + assert.Equal(t, 4, unionedSet.Size()) +} \ No newline at end of file From f0dcd97f6509df463b9a15e3dc4c7918e44041ad Mon Sep 17 00:00:00 2001 From: noys Date: Tue, 30 Jan 2024 17:11:14 +0200 Subject: [PATCH 09/11] :art: --- datastructures/set.go | 36 ++++++++++++++++++------------------ datastructures/set_test.go | 14 +++++++------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/datastructures/set.go b/datastructures/set.go index 7d30dd2..24729b8 100644 --- a/datastructures/set.go +++ b/datastructures/set.go @@ -59,25 +59,25 @@ func (set *Set[T]) ToSlice() []T { } func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] { - intersectedSet := MakeSet[T]() - bigSet := setB - smallSet := setA - if setA.Size() > setB.Size() { - bigSet = setA - smallSet = setB - } - - for key := range smallSet.container { - if bigSet.Exists(key) { - intersectedSet.Add(key) - } - } - return intersectedSet + intersectedSet := MakeSet[T]() + bigSet := setB + smallSet := setA + if setA.Size() > setB.Size() { + bigSet = setA + smallSet = setB + } + + for key := range smallSet.container { + if bigSet.Exists(key) { + intersectedSet.Add(key) + } + } + return intersectedSet } func (setA *Set[T]) Union(setB *Set[T]) *Set[T] { unionedSet := MakeSet[T]() - unionedSet.AddElements(setA.ToSlice()...) - unionedSet.AddElements(setB.ToSlice()...) - return unionedSet -} \ No newline at end of file + unionedSet.AddElements(setA.ToSlice()...) + unionedSet.AddElements(setB.ToSlice()...) + return unionedSet +} diff --git a/datastructures/set_test.go b/datastructures/set_test.go index 956b743..637bbe9 100644 --- a/datastructures/set_test.go +++ b/datastructures/set_test.go @@ -59,11 +59,11 @@ func TestSetsIntersection(t *testing.T) { func TestSetsUnion(t *testing.T) { testSet := generateNewSetWithData() - anotherSet := MakeSet[int]() - unionedSet := testSet.Union(anotherSet) - assert.Equal(t, 3, unionedSet.Size()) + anotherSet := MakeSet[int]() + unionedSet := testSet.Union(anotherSet) + assert.Equal(t, 3, unionedSet.Size()) - anotherSet.Add(4) - unionedSet = testSet.Union(anotherSet) - assert.Equal(t, 4, unionedSet.Size()) -} \ No newline at end of file + anotherSet.Add(4) + unionedSet = testSet.Union(anotherSet) + assert.Equal(t, 4, unionedSet.Size()) +} From a7a4d6fc89c5319a0481183025876856e9775abb Mon Sep 17 00:00:00 2001 From: noys Date: Tue, 30 Jan 2024 17:43:49 +0200 Subject: [PATCH 10/11] :zap: --- datastructures/set.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/datastructures/set.go b/datastructures/set.go index 24729b8..dc4541c 100644 --- a/datastructures/set.go +++ b/datastructures/set.go @@ -77,7 +77,11 @@ func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] { func (setA *Set[T]) Union(setB *Set[T]) *Set[T] { unionedSet := MakeSet[T]() - unionedSet.AddElements(setA.ToSlice()...) - unionedSet.AddElements(setB.ToSlice()...) + for key := range setA.container { + unionedSet.Add(key) + } + for key := range setB.container { + unionedSet.Add(key) + } return unionedSet } From d4d8b173537d52f76ef3ba8108171cd12f1582d0 Mon Sep 17 00:00:00 2001 From: noys Date: Tue, 30 Jan 2024 17:44:54 +0200 Subject: [PATCH 11/11] :art: --- datastructures/set.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/datastructures/set.go b/datastructures/set.go index dc4541c..5f7c6df 100644 --- a/datastructures/set.go +++ b/datastructures/set.go @@ -60,11 +60,9 @@ func (set *Set[T]) ToSlice() []T { func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] { intersectedSet := MakeSet[T]() - bigSet := setB - smallSet := setA + bigSet, smallSet := setB, setA if setA.Size() > setB.Size() { - bigSet = setA - smallSet = setB + bigSet, smallSet = setA, setB } for key := range smallSet.container {