mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-22 14:21:55 -06:00
Refactor string slice helper to common/test (#562)
* Refactor string slice helper to common/test Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com> * Do not modify the given slice * Add Unsorted in the function name
This commit is contained in:
parent
53a726fab1
commit
2a2f42990a
34
src/github.com/matrix-org/dendrite/common/test/slice.go
Normal file
34
src/github.com/matrix-org/dendrite/common/test/slice.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package test
|
||||
|
||||
import "sort"
|
||||
|
||||
// UnsortedStringSliceEqual returns true if the slices have same length & elements.
|
||||
// Does not modify the given slice.
|
||||
func UnsortedStringSliceEqual(first, second []string) bool {
|
||||
if len(first) != len(second) {
|
||||
return false
|
||||
}
|
||||
|
||||
a, b := first[:], second[:]
|
||||
sort.Strings(a)
|
||||
sort.Strings(b)
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -19,8 +19,7 @@ import (
|
|||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"sort"
|
||||
|
||||
"github.com/matrix-org/dendrite/common/test"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
@ -90,24 +89,6 @@ func (db *getEventDB) EventsFromIDs(ctx context.Context, eventIDs []string) (res
|
|||
return
|
||||
}
|
||||
|
||||
// Returns if the slices are equal after sorting them.
|
||||
func compareUnsortedStringSlices(a []string, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
sort.Strings(a)
|
||||
sort.Strings(b)
|
||||
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func TestGetAuthChainSingle(t *testing.T) {
|
||||
db := createEventDB()
|
||||
|
||||
|
@ -135,7 +116,7 @@ func TestGetAuthChainSingle(t *testing.T) {
|
|||
|
||||
expectedIDs := []string{"a", "b", "c", "d", "e"}
|
||||
|
||||
if !compareUnsortedStringSlices(expectedIDs, returnedIDs) {
|
||||
if !test.UnsortedStringSliceEqual(expectedIDs, returnedIDs) {
|
||||
t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs)
|
||||
}
|
||||
}
|
||||
|
@ -168,7 +149,7 @@ func TestGetAuthChainMultiple(t *testing.T) {
|
|||
|
||||
expectedIDs := []string{"a", "b", "c", "d", "e", "f"}
|
||||
|
||||
if !compareUnsortedStringSlices(expectedIDs, returnedIDs) {
|
||||
if !test.UnsortedStringSliceEqual(expectedIDs, returnedIDs) {
|
||||
t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue