nim-dagger/tests/codex/helpers/randomchunker.nim

48 lines
796 B
Nim

import std/sequtils
import pkg/chronos
import pkg/codex/chunker
import pkg/codex/rng
export chunker
type
RandomChunker* = Chunker
proc new*(
T: type RandomChunker,
rng: Rng,
chunkSize = DefaultChunkSize,
size: int,
pad = false): T =
## create a chunker that produces
## random data
##
var consumed = 0
proc reader(data: ChunkBuffer, len: int): Future[int]
{.async, gcsafe, raises: [Defect].} =
var alpha = toSeq(byte('A')..byte('z'))
if consumed >= size:
return 0
var read = 0
while read < len:
rng.shuffle(alpha)
for a in alpha:
if read >= len:
break
data[read] = a
read.inc
consumed += read
return read
Chunker.new(
reader = reader,
pad = pad,
chunkSize = chunkSize)