From a2862b4a2aa7f06b9adfe6bf05411416dc66cae3 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Mon, 22 Jan 2024 16:39:37 +0200 Subject: [PATCH 1/2] 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 305e994..74c45b8 100644 --- a/fanout/reader_test.go +++ b/fanout/reader_test.go @@ -160,6 +160,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 6fbd5ecad6a9316c939f6982eb294b219e4d80a9 Mon Sep 17 00:00:00 2001 From: Noy Shabtay <70848358+noyshabtay@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:50:32 +0200 Subject: [PATCH 2/2] Add Sets Intersection and Union Operations (#48) * update deps * union and intersection operations added to sets * tests were added --------- Co-authored-by: Michael Sverdlov Co-authored-by: Michael Sverdlov --- .github/workflows/analysis.yml | 35 ++++++++++++++++----------------- datastructures/set.go | 26 ++++++++++++++++++++++++ datastructures/set_test.go | 22 +++++++++++++++++++++ fanout/reader_test.go | 5 ++--- io/fileutils.go | 2 +- parallel/bounded_runner_test.go | 8 ++++---- 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index b47f81b..5a9f30c 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,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@master + with: + args: -exclude G204,G304,G404,G401,G505 -tests -exclude-dir \.*test\.* ./... \ No newline at end of file diff --git a/datastructures/set.go b/datastructures/set.go index 220ffda..5f7c6df 100644 --- a/datastructures/set.go +++ b/datastructures/set.go @@ -57,3 +57,29 @@ func (set *Set[T]) ToSlice() []T { return slice } + +func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] { + intersectedSet := MakeSet[T]() + bigSet, smallSet := setB, setA + if setA.Size() > setB.Size() { + bigSet, smallSet = setA, 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]() + for key := range setA.container { + unionedSet.Add(key) + } + for key := range setB.container { + unionedSet.Add(key) + } + return unionedSet +} diff --git a/datastructures/set_test.go b/datastructures/set_test.go index b692cdf..637bbe9 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()) +} diff --git a/fanout/reader_test.go b/fanout/reader_test.go index 74c45b8..cbe6b25 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) @@ -57,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)) } 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()