Skip to content

chore: implement generalized symmetric difference for set comparison #14407

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

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
Emyrk committed Aug 22, 2024
commit ae75e5dcb37fc4e680708dd64c25ca2c8b0a479a
2 changes: 1 addition & 1 deletion coderd/util/slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Difference[T comparable](a []T, b []T) []T {
}

func SymmetricDifferenceFunc[T any](a, b []T, equal func(a, b T) bool) (add []T, remove []T) {
return DifferenceFunc(a, b, equal), DifferenceFunc(b, a, equal)
return DifferenceFunc(b, a, equal), DifferenceFunc(a, b, equal)
}

func DifferenceFunc[T any](a []T, b []T, equal func(a, b T) bool) []T {
Expand Down
44 changes: 44 additions & 0 deletions coderd/util/slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,48 @@ func TestSymmetricDifference(t *testing.T) {
require.ElementsMatch(t, []int{}, add)
require.ElementsMatch(t, []int{}, remove)
})

t.Run("ToEmpty", func(t *testing.T) {
t.Parallel()

add, remove := slice.SymmetricDifference(
[]int{1, 2, 3},
[]int{},
)
require.ElementsMatch(t, []int{}, add)
require.ElementsMatch(t, []int{1, 2, 3}, remove)
})

t.Run("ToNil", func(t *testing.T) {
t.Parallel()

add, remove := slice.SymmetricDifference(
[]int{1, 2, 3},
nil,
)
require.ElementsMatch(t, []int{}, add)
require.ElementsMatch(t, []int{1, 2, 3}, remove)
})

t.Run("FromEmpty", func(t *testing.T) {
t.Parallel()

add, remove := slice.SymmetricDifference(
[]int{},
[]int{1, 2, 3},
)
require.ElementsMatch(t, []int{1, 2, 3}, add)
require.ElementsMatch(t, []int{}, remove)
})

t.Run("FromNil", func(t *testing.T) {
t.Parallel()

add, remove := slice.SymmetricDifference(
nil,
[]int{1, 2, 3},
)
require.ElementsMatch(t, []int{1, 2, 3}, add)
require.ElementsMatch(t, []int{}, remove)
})
}
Loading