Commit f1ba975b authored by Juan Batiz-Benet's avatar Juan Batiz-Benet Committed by Brian Tiger Chow

SortByDistance copy fix

parent 681a47eb
......@@ -86,9 +86,11 @@ func (s byDistanceToCenter) Less(i, j int) bool {
// It returns a new list, where the Keys toSort have been sorted by their
// distance to the center Key.
func SortByDistance(sp KeySpace, center Key, toSort []Key) []Key {
toSortCopy := make([]Key, len(toSort))
copy(toSortCopy, toSort)
bdtc := &byDistanceToCenter{
Center: center,
Keys: toSort[:], // copy
Keys: toSortCopy, // copy
}
sort.Sort(bdtc)
return bdtc.Keys
......
......@@ -97,7 +97,7 @@ func TestXorKeySpace(t *testing.T) {
}
}
func TestCenterSorting(t *testing.T) {
func TestDistancesAndCenterSorting(t *testing.T) {
adjs := [][]byte{
[]byte{173, 149, 19, 27, 192, 183, 153, 192, 177, 175, 71, 127, 177, 79, 207, 38, 166, 169, 247, 96, 121, 228, 139, 240, 144, 172, 183, 232, 54, 123, 253, 14},
......@@ -136,4 +136,12 @@ func TestCenterSorting(t *testing.T) {
t.Errorf("2<<32 should be smaller")
}
keys2 := SortByDistance(XORKeySpace, keys[2], keys)
order := []int{2, 3, 4, 5, 1, 0}
for i, o := range order {
if !bytes.Equal(keys[o].Adjusted, keys2[i].Adjusted) {
t.Errorf("order is wrong. %d?? %v == %v", o, keys[o], keys2[i])
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment