mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
SortByDistance copy fix
This commit is contained in:

committed by
Brian Tiger Chow

parent
b838cc0619
commit
ab04137853
@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user