-
Notifications
You must be signed in to change notification settings - Fork 337
/
test_helpers.go
69 lines (55 loc) · 1.53 KB
/
test_helpers.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2023 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swarm
import (
"math/rand"
"testing"
"github.com/ethersphere/bee/pkg/util/testutil"
)
// RandAddress generates a random address.
func RandAddress(tb testing.TB) Address {
tb.Helper()
return NewAddress(testutil.RandBytes(tb, HashSize))
}
// RandAddressAt generates a random address at proximity order prox relative to address.
func RandAddressAt(tb testing.TB, self Address, prox int) Address {
tb.Helper()
addr := make([]byte, len(self.Bytes()))
copy(addr, self.Bytes())
pos := -1
if prox >= 0 {
pos = prox / 8
trans := prox % 8
transbytea := byte(0)
for j := 0; j <= trans; j++ {
transbytea |= 1 << uint8(7-j)
}
flipbyte := byte(1 << uint8(7-trans))
transbyteb := transbytea ^ byte(255)
randbyte := byte(rand.Intn(255))
addr[pos] = ((addr[pos] & transbytea) ^ flipbyte) | randbyte&transbyteb
}
for i := pos + 1; i < len(addr); i++ {
addr[i] = byte(rand.Intn(255))
}
a := NewAddress(addr)
if a.Equal(self) {
tb.Fatalf("generated same address")
}
return a
}
// RandAddresses generates slice with a random address.
func RandAddresses(tb testing.TB, count int) []Address {
tb.Helper()
result := make([]Address, count)
for i := 0; i < count; i++ {
result[i] = RandAddress(tb)
}
return result
}
// RandBatchID generates a random BatchID.
func RandBatchID(tb testing.TB) []byte {
tb.Helper()
return testutil.RandBytes(tb, HashSize)
}