proximity_test.go 4.8 KB
Newer Older
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 33
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package swarm

import (
	"testing"
)

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