Fix bug in EqualUnsorted when comparing lists of the same length

This commit is contained in:
James Munnelly 2018-04-04 23:40:08 +01:00
parent 8d3c2f2b25
commit b866b8cdf4
2 changed files with 10 additions and 2 deletions

View File

@ -24,8 +24,10 @@ func EqualUnsorted(s1 []string, s2 []string) bool {
return false
}
s1_2, s2_2 := make([]string, len(s1)), make([]string, len(s2))
sort.Strings(s1)
sort.Strings(s2)
copy(s1_2, s1)
copy(s2_2, s2)
sort.Strings(s1_2)
sort.Strings(s2_2)
for i, s := range s1_2 {
if s != s2_2[i] {
return false

View File

@ -36,6 +36,12 @@ func TestEqualUnsorted(t *testing.T) {
s2: []string{"a", "b", "c"},
equal: true,
},
{
desc: "unequal lists of the same length are not equal",
s1: []string{"example.com"},
s2: []string{"notexample.com"},
equal: false,
},
}
for _, test := range tests {
t.Run(test.desc, func(test testT) func(*testing.T) {