github.com/xxRanger/go-ethereum@v1.8.23/swarm/storage/types_test.go (about)

     1  // Copyright 2018 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 storage
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  )
    23  
    24  // TestProximity validates Proximity function with explicit
    25  // values in a table-driven test. It is highly dependant on
    26  // MaxPO constant and it validates cases up to MaxPO=32.
    27  func TestProximity(t *testing.T) {
    28  	// integer from base2 encoded string
    29  	bx := func(s string) uint8 {
    30  		i, err := strconv.ParseUint(s, 2, 8)
    31  		if err != nil {
    32  			t.Fatal(err)
    33  		}
    34  		return uint8(i)
    35  	}
    36  	// adjust expected bins in respect to MaxPO
    37  	limitPO := func(po uint8) uint8 {
    38  		if po > MaxPO {
    39  			return MaxPO
    40  		}
    41  		return po
    42  	}
    43  	base := []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00000000")}
    44  	for _, tc := range []struct {
    45  		addr []byte
    46  		po   uint8
    47  	}{
    48  		{
    49  			addr: base,
    50  			po:   MaxPO,
    51  		},
    52  		{
    53  			addr: []byte{bx("10000000"), bx("00000000"), bx("00000000"), bx("00000000")},
    54  			po:   limitPO(0),
    55  		},
    56  		{
    57  			addr: []byte{bx("01000000"), bx("00000000"), bx("00000000"), bx("00000000")},
    58  			po:   limitPO(1),
    59  		},
    60  		{
    61  			addr: []byte{bx("00100000"), bx("00000000"), bx("00000000"), bx("00000000")},
    62  			po:   limitPO(2),
    63  		},
    64  		{
    65  			addr: []byte{bx("00010000"), bx("00000000"), bx("00000000"), bx("00000000")},
    66  			po:   limitPO(3),
    67  		},
    68  		{
    69  			addr: []byte{bx("00001000"), bx("00000000"), bx("00000000"), bx("00000000")},
    70  			po:   limitPO(4),
    71  		},
    72  		{
    73  			addr: []byte{bx("00000100"), bx("00000000"), bx("00000000"), bx("00000000")},
    74  			po:   limitPO(5),
    75  		},
    76  		{
    77  			addr: []byte{bx("00000010"), bx("00000000"), bx("00000000"), bx("00000000")},
    78  			po:   limitPO(6),
    79  		},
    80  		{
    81  			addr: []byte{bx("00000001"), bx("00000000"), bx("00000000"), bx("00000000")},
    82  			po:   limitPO(7),
    83  		},
    84  		{
    85  			addr: []byte{bx("00000000"), bx("10000000"), bx("00000000"), bx("00000000")},
    86  			po:   limitPO(8),
    87  		},
    88  		{
    89  			addr: []byte{bx("00000000"), bx("01000000"), bx("00000000"), bx("00000000")},
    90  			po:   limitPO(9),
    91  		},
    92  		{
    93  			addr: []byte{bx("00000000"), bx("00100000"), bx("00000000"), bx("00000000")},
    94  			po:   limitPO(10),
    95  		},
    96  		{
    97  			addr: []byte{bx("00000000"), bx("00010000"), bx("00000000"), bx("00000000")},
    98  			po:   limitPO(11),
    99  		},
   100  		{
   101  			addr: []byte{bx("00000000"), bx("00001000"), bx("00000000"), bx("00000000")},
   102  			po:   limitPO(12),
   103  		},
   104  		{
   105  			addr: []byte{bx("00000000"), bx("00000100"), bx("00000000"), bx("00000000")},
   106  			po:   limitPO(13),
   107  		},
   108  		{
   109  			addr: []byte{bx("00000000"), bx("00000010"), bx("00000000"), bx("00000000")},
   110  			po:   limitPO(14),
   111  		},
   112  		{
   113  			addr: []byte{bx("00000000"), bx("00000001"), bx("00000000"), bx("00000000")},
   114  			po:   limitPO(15),
   115  		},
   116  		{
   117  			addr: []byte{bx("00000000"), bx("00000000"), bx("10000000"), bx("00000000")},
   118  			po:   limitPO(16),
   119  		},
   120  		{
   121  			addr: []byte{bx("00000000"), bx("00000000"), bx("01000000"), bx("00000000")},
   122  			po:   limitPO(17),
   123  		},
   124  		{
   125  			addr: []byte{bx("00000000"), bx("00000000"), bx("00100000"), bx("00000000")},
   126  			po:   limitPO(18),
   127  		},
   128  		{
   129  			addr: []byte{bx("00000000"), bx("00000000"), bx("00010000"), bx("00000000")},
   130  			po:   limitPO(19),
   131  		},
   132  		{
   133  			addr: []byte{bx("00000000"), bx("00000000"), bx("00001000"), bx("00000000")},
   134  			po:   limitPO(20),
   135  		},
   136  		{
   137  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000100"), bx("00000000")},
   138  			po:   limitPO(21),
   139  		},
   140  		{
   141  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000010"), bx("00000000")},
   142  			po:   limitPO(22),
   143  		},
   144  		{
   145  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000001"), bx("00000000")},
   146  			po:   limitPO(23),
   147  		},
   148  		{
   149  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("10000000")},
   150  			po:   limitPO(24),
   151  		},
   152  		{
   153  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("01000000")},
   154  			po:   limitPO(25),
   155  		},
   156  		{
   157  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00100000")},
   158  			po:   limitPO(26),
   159  		},
   160  		{
   161  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00010000")},
   162  			po:   limitPO(27),
   163  		},
   164  		{
   165  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00001000")},
   166  			po:   limitPO(28),
   167  		},
   168  		{
   169  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00000100")},
   170  			po:   limitPO(29),
   171  		},
   172  		{
   173  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00000010")},
   174  			po:   limitPO(30),
   175  		},
   176  		{
   177  			addr: []byte{bx("00000000"), bx("00000000"), bx("00000000"), bx("00000001")},
   178  			po:   limitPO(31),
   179  		},
   180  	} {
   181  		got := uint8(Proximity(base, tc.addr))
   182  		if got != tc.po {
   183  			t.Errorf("got %v bin, want %v", got, tc.po)
   184  		}
   185  	}
   186  }