From 2a2f42990aadd102edf3f071a70324cbb3b886d6 Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Thu, 26 Jul 2018 22:04:39 +0530 Subject: [PATCH] Refactor string slice helper to common/test (#562) * Refactor string slice helper to common/test Signed-off-by: Anant Prakash * Do not modify the given slice * Add Unsorted in the function name --- .../matrix-org/dendrite/common/test/slice.go | 34 +++++++++++++++++++ .../dendrite/roomserver/query/query_test.go | 25 ++------------ 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/common/test/slice.go diff --git a/src/github.com/matrix-org/dendrite/common/test/slice.go b/src/github.com/matrix-org/dendrite/common/test/slice.go new file mode 100644 index 00000000..00c740db --- /dev/null +++ b/src/github.com/matrix-org/dendrite/common/test/slice.go @@ -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 +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go b/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go index 45e46f67..76c2e158 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go @@ -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) } }