diff --git a/pot/address_test.go b/pot/address_test.go index f7146a68db..3c914fe84c 100644 --- a/pot/address_test.go +++ b/pot/address_test.go @@ -2,65 +2,79 @@ package pot import ( "testing" -) -type testAddress []byte + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethersphere/swarm/log" +) -func newTestAddress(a []byte) testAddress { - if a != nil { - return testAddress(a) - } - return testAddress(make([]byte, 32)) +type distanceTest struct { + x []byte + y []byte + result string } -func (a testAddress) Address() []byte { - return []byte(a) +type distanceCmpTest struct { + x []byte + y []byte + z []byte + result int } -// TestDistance tests the correctness of the distance calculation -// as well as the comparison method -func TestDistance(t *testing.T) { - a := [32]byte{} - b := [32]byte{} - a[0] = 0x91 - b[0] = 0x82 - aa := newTestAddress(a[:]) //NewAddressFromBytes(a[:]) - ab := newTestAddress(b[:]) //NewAddressFromBytes(b[:]) - distance, err := Distance(aa, ab) - if err != nil { - t.Fatal(err) - } - correctDistance := "8593944123082061379093159043613555660984881674403010612303492563087302590464" - if distance.String() != correctDistance { - t.Fatalf("Distance calculation mismatch, got %s, expected %s", distance.String(), correctDistance) - } - - c := [32]byte{} - c[0] = 0x12 - ac := newTestAddress(c[:]) - _, err = DistanceCmp(aa, ab, ac[:31]) - if err == nil { - t.Fatal("Expected length mismatch on address to fail") +var ( + distanceTests = []distanceTest{ + { + x: hexutil.MustDecode("0x9100000000000000000000000000000000000000000000000000000000000000"), + y: hexutil.MustDecode("0x8200000000000000000000000000000000000000000000000000000000000000"), + result: "8593944123082061379093159043613555660984881674403010612303492563087302590464", + }, } - cmp, err := DistanceCmp(aa, ab, ac) - if err != nil { - t.Fatal(err) - } else if cmp != -1 { - t.Fatalf("aaab < aaac, expected -1, got %d", cmp) + distanceCmpTests = []distanceCmpTest{ + { + x: hexutil.MustDecode("0x9100000000000000000000000000000000000000000000000000000000000000"), + y: hexutil.MustDecode("0x8200000000000000000000000000000000000000000000000000000000000000"), + z: hexutil.MustDecode("0x1200000000000000000000000000000000000000000000000000000000000000"), + result: -1, + }, + { + x: hexutil.MustDecode("0x9100000000000000000000000000000000000000000000000000000000000000"), + y: hexutil.MustDecode("0x1200000000000000000000000000000000000000000000000000000000000000"), + z: hexutil.MustDecode("0x8200000000000000000000000000000000000000000000000000000000000000"), + result: 1, + }, + { + x: hexutil.MustDecode("0x9100000000000000000000000000000000000000000000000000000000000000"), + y: hexutil.MustDecode("0x1200000000000000000000000000000000000000000000000000000000000000"), + z: hexutil.MustDecode("0x1200000000000000000000000000000000000000000000000000000000000000"), + result: 0, + }, } +) - cmp, err = DistanceCmp(aa, ac, ab) - if err != nil { - t.Fatal(err) - } else if cmp != 1 { - t.Fatalf("aaab > aaac, expected 1, got %d", cmp) +// TestDistance tests the correctness of the distance calculation +func TestDistance(t *testing.T) { + for i, dt := range distanceTests { + log.Debug("Distance test", "i", i, "dt", dt) + distance, err := Distance(dt.x, dt.y) + if err != nil { + t.Fatal(err) + } + if distance.String() != dt.result { + t.Fatalf("incorrect distance, expected %s, got %s (x: %x, y: %x)", dt.result, distance.String(), dt.x, dt.y) + } } +} - cmp, err = DistanceCmp(aa, ab, ab) - if err != nil { - t.Fatal(err) - } else if cmp != 0 { - t.Fatalf("aaab == aaab, expected 0, got %d", cmp) +// TestDistanceCmp tests the distance comparison method +func TestDistanceCmp(t *testing.T) { + for i, dt := range distanceCmpTests { + log.Debug("DistanceCmp test", "i", i, "dt", dt) + direction, err := DistanceCmp(dt.x, dt.y, dt.z) + if err != nil { + t.Fatal(err) + } + if direction != dt.result { + t.Fatalf("incorrect distance compare, expected %d, got %d (x: %x, y: %x, z: %x)", dt.result, direction, dt.x, dt.y, dt.z) + } } }