github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/network/kademlia/address_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package kademlia 13 14 import ( 15 "math/rand" 16 "reflect" 17 "testing" 18 19 "github.com/Sberex/go-sberex/common" 20 ) 21 22 func (Address) Generate(rand *rand.Rand, size int) reflect.Value { 23 var id Address 24 for i := 0; i < len(id); i++ { 25 id[i] = byte(uint8(rand.Intn(255))) 26 } 27 return reflect.ValueOf(id) 28 } 29 30 func TestCommonBitsAddrF(t *testing.T) { 31 a := Address(common.HexToHash("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 32 b := Address(common.HexToHash("0x8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 33 c := Address(common.HexToHash("0x4123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 34 d := Address(common.HexToHash("0x0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 35 e := Address(common.HexToHash("0x01A3456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 36 ab := CommonBitsAddrF(a, b, func() byte { return byte(0x00) }, 10) 37 expab := Address(common.HexToHash("0x8000000000000000000000000000000000000000000000000000000000000000")) 38 39 if ab != expab { 40 t.Fatalf("%v != %v", ab, expab) 41 } 42 ac := CommonBitsAddrF(a, c, func() byte { return byte(0x00) }, 10) 43 expac := Address(common.HexToHash("0x4000000000000000000000000000000000000000000000000000000000000000")) 44 45 if ac != expac { 46 t.Fatalf("%v != %v", ac, expac) 47 } 48 ad := CommonBitsAddrF(a, d, func() byte { return byte(0x00) }, 10) 49 expad := Address(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")) 50 51 if ad != expad { 52 t.Fatalf("%v != %v", ad, expad) 53 } 54 ae := CommonBitsAddrF(a, e, func() byte { return byte(0x00) }, 10) 55 expae := Address(common.HexToHash("0x0180000000000000000000000000000000000000000000000000000000000000")) 56 57 if ae != expae { 58 t.Fatalf("%v != %v", ae, expae) 59 } 60 acf := CommonBitsAddrF(a, c, func() byte { return byte(0xff) }, 10) 61 expacf := Address(common.HexToHash("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) 62 63 if acf != expacf { 64 t.Fatalf("%v != %v", acf, expacf) 65 } 66 aeo := CommonBitsAddrF(a, e, func() byte { return byte(0x00) }, 2) 67 expaeo := Address(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")) 68 69 if aeo != expaeo { 70 t.Fatalf("%v != %v", aeo, expaeo) 71 } 72 aep := CommonBitsAddrF(a, e, func() byte { return byte(0xff) }, 2) 73 expaep := Address(common.HexToHash("0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) 74 75 if aep != expaep { 76 t.Fatalf("%v != %v", aep, expaep) 77 } 78 79 } 80 81 func TestRandomAddressAt(t *testing.T) { 82 var a Address 83 for i := 0; i < 100; i++ { 84 a = RandomAddress() 85 prox := rand.Intn(255) 86 b := RandomAddressAt(a, prox) 87 if proximity(a, b) != prox { 88 t.Fatalf("incorrect address prox(%v, %v) == %v (expected %v)", a, b, proximity(a, b), prox) 89 } 90 } 91 }