Skip to content

Commit c0d2829

Browse files
drakkanimmanuwell
andcommitted
fix: correctly compare unordered slices
Fixes #2231 Co-authored-by: immanuwell <pchpr.00@list.ru> Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
1 parent 45765cf commit c0d2829

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

internal/httpd/internal_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4317,3 +4317,57 @@ func isSharedProviderSupported() bool {
43174317
return false
43184318
}
43194319
}
4320+
4321+
func TestSlicesEqual(t *testing.T) {
4322+
tests := []struct {
4323+
name string
4324+
s1 []string
4325+
s2 []string
4326+
want bool
4327+
}{
4328+
{
4329+
name: "same order",
4330+
s1: []string{"read", "write"},
4331+
s2: []string{"read", "write"},
4332+
want: true,
4333+
},
4334+
{
4335+
name: "different order",
4336+
s1: []string{"read", "write"},
4337+
s2: []string{"write", "read"},
4338+
want: true,
4339+
},
4340+
{
4341+
name: "different lengths",
4342+
s1: []string{"read"},
4343+
s2: []string{"read", "write"},
4344+
want: false,
4345+
},
4346+
{
4347+
name: "same length different values",
4348+
s1: []string{"read", "write"},
4349+
s2: []string{"read", "delete"},
4350+
want: false,
4351+
},
4352+
{
4353+
name: "same length different multiplicity",
4354+
s1: []string{"read", "write"},
4355+
s2: []string{"read", "read"},
4356+
want: false,
4357+
},
4358+
{
4359+
name: "same multiplicity",
4360+
s1: []string{"read", "read", "write"},
4361+
s2: []string{"write", "read", "read"},
4362+
want: true,
4363+
},
4364+
}
4365+
4366+
for _, tt := range tests {
4367+
t.Run(tt.name, func(t *testing.T) {
4368+
if got := util.SlicesEqual(tt.s1, tt.s2); got != tt.want {
4369+
t.Fatalf("SlicesEqual(%v, %v) = %v, want %v", tt.s1, tt.s2, got, tt.want)
4370+
}
4371+
})
4372+
}
4373+
}

internal/util/util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444
"path/filepath"
4545
"regexp"
4646
"runtime"
47-
"slices"
4847
"strconv"
4948
"strings"
5049
"time"
@@ -960,8 +959,15 @@ func SlicesEqual(s1, s2 []string) bool {
960959
if len(s1) != len(s2) {
961960
return false
962961
}
962+
963+
counts := make(map[string]int)
963964
for _, v := range s1 {
964-
if !slices.Contains(s2, v) {
965+
counts[v]++
966+
}
967+
968+
for _, v := range s2 {
969+
counts[v]--
970+
if counts[v] < 0 {
965971
return false
966972
}
967973
}

0 commit comments

Comments
 (0)