github.com/r8d8/go-ethereum@v5.5.2+incompatible/p2p/discover/distance_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package discover
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  	"testing/quick"
    24  
    25  	"github.com/ethereumproject/go-ethereum/common"
    26  	"github.com/ethereumproject/go-ethereum/crypto"
    27  )
    28  
    29  var goldenClosest = [][]NodeID{
    30  	{
    31  		MustHexID("0x84d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    32  		MustHexID("0x57d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    33  	},
    34  	{
    35  		MustHexID("0x22d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    36  		MustHexID("0x44d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    37  		MustHexID("0xe2d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    38  	},
    39  }
    40  
    41  func TestClosest(t *testing.T) {
    42  	for _, gold := range goldenClosest {
    43  		want := make([]*Node, len(gold))
    44  		for i, id := range gold {
    45  			want[i] = &Node{
    46  				ID:  id,
    47  				sha: crypto.Keccak256Hash(id[:]),
    48  			}
    49  		}
    50  
    51  		c := &closest{}
    52  
    53  		// insert in reverse order
    54  		for i := len(want) - 1; i >= 0; i-- {
    55  			c.Add(want[i])
    56  		}
    57  		if got := c.Slice(); !reflect.DeepEqual(got, want) {
    58  			t.Errorf("got %+v, want %+v", got, want)
    59  		}
    60  
    61  		// insert again (duplicate)
    62  		for _, n := range want {
    63  			c.Add(n)
    64  		}
    65  		if got := c.Slice(); !reflect.DeepEqual(got, want) {
    66  			t.Errorf("after reinsert got %+v, want %+v", got, gold)
    67  		}
    68  	}
    69  }
    70  
    71  func TestDistcmp(t *testing.T) {
    72  	distcmpBig := func(target, a, b common.Hash) int {
    73  		tbig := new(big.Int).SetBytes(target[:])
    74  		abig := new(big.Int).SetBytes(a[:])
    75  		bbig := new(big.Int).SetBytes(b[:])
    76  		return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig))
    77  	}
    78  	if err := quick.CheckEqual(distcmp, distcmpBig, quickcfg()); err != nil {
    79  		t.Error(err)
    80  	}
    81  }
    82  
    83  // the random tests is likely to miss the case where they're equal.
    84  func TestDistcmpEqual(t *testing.T) {
    85  	base := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    86  	x := common.Hash{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
    87  	if distcmp(base, x, x) != 0 {
    88  		t.Errorf("distcmp(base, x, x) != 0")
    89  	}
    90  }
    91  
    92  func TestLogdist(t *testing.T) {
    93  	logdistBig := func(a, b common.Hash) int {
    94  		abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
    95  		return new(big.Int).Xor(abig, bbig).BitLen()
    96  	}
    97  	if err := quick.CheckEqual(logdist, logdistBig, quickcfg()); err != nil {
    98  		t.Error(err)
    99  	}
   100  }
   101  
   102  // the random tests is likely to miss the case where they're equal.
   103  func TestLogdistEqual(t *testing.T) {
   104  	x := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   105  	if logdist(x, x) != 0 {
   106  		t.Errorf("logdist(x, x) != 0")
   107  	}
   108  }