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