-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper test functions in
dot/state
- Loading branch information
Showing
2 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package state | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// newGenerator creates a new PRNG seeded with the | ||
// unix nanoseconds value of the current time. | ||
func newGenerator() (prng *rand.Rand) { | ||
seed := time.Now().UnixNano() | ||
source := rand.NewSource(seed) | ||
return rand.New(source) | ||
} | ||
|
||
func generateKeyValues(tb testing.TB, generator *rand.Rand, size int) (kv map[string][]byte) { | ||
tb.Helper() | ||
|
||
kv = make(map[string][]byte, size) | ||
|
||
const maxKeySize, maxValueSize = 510, 128 | ||
for i := 0; i < size; i++ { | ||
populateKeyValueMap(tb, kv, generator, maxKeySize, maxValueSize) | ||
} | ||
|
||
return kv | ||
} | ||
|
||
func populateKeyValueMap(tb testing.TB, kv map[string][]byte, | ||
generator *rand.Rand, maxKeySize, maxValueSize int) { | ||
tb.Helper() | ||
|
||
for { | ||
const minKeySize = 2 | ||
key := generateRandBytesMinMax(tb, minKeySize, maxKeySize, generator) | ||
|
||
keyString := string(key) | ||
|
||
_, keyExists := kv[keyString] | ||
|
||
if keyExists && key[1] != byte(0) { | ||
continue | ||
} | ||
|
||
const minValueSize = 2 | ||
value := generateRandBytesMinMax(tb, minValueSize, maxValueSize, generator) | ||
|
||
kv[keyString] = value | ||
|
||
break | ||
} | ||
} | ||
|
||
func generateRandBytesMinMax(tb testing.TB, minSize, maxSize int, | ||
generator *rand.Rand) (b []byte) { | ||
tb.Helper() | ||
size := minSize + | ||
generator.Intn(maxSize-minSize) | ||
return generateRandBytes(tb, size, generator) | ||
} | ||
|
||
func generateRandBytes(tb testing.TB, size int, | ||
generator *rand.Rand) (b []byte) { | ||
tb.Helper() | ||
b = make([]byte, size) | ||
_, err := generator.Read(b) | ||
require.NoError(tb, err) | ||
return b | ||
} |