util_test.go 687 Bytes
Newer Older
Aarsh Shah's avatar
Aarsh Shah committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
package kbucket

import (
	"github.com/libp2p/go-libp2p-core/test"
	"github.com/stretchr/testify/require"
	"testing"
)

func TestCloser(t *testing.T) {
	Pa := test.RandPeerIDFatal(t)
	Pb := test.RandPeerIDFatal(t)
	var X string

	// returns true if d(Pa, X) < d(Pb, X)
	for {
		X = string(test.RandPeerIDFatal(t))
		if xor(ConvertPeerID(Pa), ConvertKey(X)).less(xor(ConvertPeerID(Pb), ConvertKey(X))) {
			break
		}
	}

	require.True(t, Closer(Pa, Pb, X))

	// returns false if d(Pa,X) > d(Pb, X)
	for {
		X = string(test.RandPeerIDFatal(t))
		if xor(ConvertPeerID(Pb), ConvertKey(X)).less(xor(ConvertPeerID(Pa), ConvertKey(X))) {
			break
		}

	}
	require.False(t, Closer(Pa, Pb, X))
Aarsh Shah's avatar
go fmt  
Aarsh Shah committed
33
}