Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sets: add Union() #158

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ See also the next section.

These suggestions apply to the development of any Concourse resource.

### Building a new image and ensuring that the pipeline picks it up
### Building a new image and ensuring that the pipeline picks it up

After the local tests are passing, the quickest way to test in a pipeline the freshly pushed version of the Docker image used to be the `fly check-resource-type` command. Unfortunately somewhere in the Concourse 7.6.x series this broke (details: [registry-image-resource #316](https://github.com/concourse/registry-image-resource/issues/316)).

Expand All @@ -248,7 +248,7 @@ You can follow two steps:
1. `fly set-pipeline` with a check_interval for the resource type of 1m instead of the recommended 24h.
2. `fly clear-version`.

For example, assuming that the test pipeline is called `cogito-test`, that the resource in the pipeline is called `cogito` and that there is a job called `motormouse` (all this is true by using the sample pipeline [pipelines/cogito.yml](./pipelines/cogito.yml)),
For example, assuming that the test pipeline is called `cogito-test`, that the resource in the pipeline is called `cogito` and that there is a job called `motormouse` (all this is true by using the sample pipeline [pipelines/cogito.yml](./pipelines/cogito.yml)).

Step 1:

Expand All @@ -268,7 +268,7 @@ Step 2 (the sleep is fundamental to let check_every expire):
```
$ task test:all docker:build docker:smoke docker:push &&
fly -t cogito clear-versions --resource-type=cogito-test/cogito &&
echo "sleeping and hoping :-(" &&
echo "sleeping and hoping :-(" &&
sleep 70 &&
fly -t cogito trigger-job -j cogito-test/motormouse -w
```
Expand Down Expand Up @@ -336,7 +336,7 @@ Update the expired secrets as follows.
## Regenerate your GitHub personal access token (PAT)

1. Go to [Settings | Tokens](https://github.com/settings/tokens) for your account.
2. Create or regenerate a token with name `test-cogito`, with scope "repo:status". Set an expiration of 90 days.
2. Create or regenerate a token with name `test-cogito`, with scope `repo:status`. Set an expiration of 90 days.
3. Copy the token.

## Regenerate a Google Chat hook
Expand Down
12 changes: 12 additions & 0 deletions sets/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ func (s *Set[T]) Intersection(x *Set[T]) *Set[T] {
}
return result
}

// Union returns a set containing all the elements of s and x.
func (s *Set[T]) Union(x *Set[T]) *Set[T] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you also need the set difference as you wrote in chat? or we already have that...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah we have it...
func (s *Set[T]) Difference(x *Set[T]) *Set[T]

result := New[T](max(s.Size(), x.Size()))
for item := range s.items {
result.items[item] = struct{}{}
}
for item := range x.items {
result.items[item] = struct{}{}
}
return result
}
59 changes: 59 additions & 0 deletions sets/sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,65 @@ func TestIntersection(t *testing.T) {
}
}

func TestUnion(t *testing.T) {
type testCase struct {
name string
s *sets.Set[int]
x *sets.Set[int]
wantList []int
}

test := func(t *testing.T, tc testCase) {
result := tc.s.Union(tc.x)
sorted := result.OrderedList()

qt.Assert(t, qt.DeepEquals(sorted, tc.wantList))
}

testCases := []testCase{
{
name: "both empty",
s: sets.From[int](),
x: sets.From[int](),
wantList: []int{},
},
{
name: "empty x",
s: sets.From(1, 2),
x: sets.From[int](),
wantList: []int{1, 2},
},
{
name: "empty s",
s: sets.From[int](),
x: sets.From(1, 2),
wantList: []int{1, 2},
},
{
name: "identical",
s: sets.From(1, 2),
x: sets.From(1, 2),
wantList: []int{1, 2},
},
{
name: "all different",
s: sets.From(1, 3),
x: sets.From(2, 4),
wantList: []int{1, 2, 3, 4},
},
{
name: "partial overlap",
s: sets.From(1, 3),
x: sets.From(3, 5),
wantList: []int{1, 3, 5},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { test(t, tc) })
}
}

func TestRemoveFound(t *testing.T) {
type testCase struct {
name string
Expand Down
Loading