Skip to content

Commit

Permalink
Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
waalge authored and KtorZ committed Jun 7, 2024
1 parent 11431ce commit 331e104
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 19 additions & 7 deletions lib/aiken/fuzz.ak
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ pub fn bytearray() -> Fuzzer<ByteArray> {
|> builtin.blake2b_256
}

/// Generate a random [`ByteArray`](https://aiken-lang.github.io/prelude/aiken.html#ByteArray) of `len` bytes.
pub fn bytearray_fixed(len: Int) -> Fuzzer<ByteArray> {
expect len >= 0
bytearray_fixed_inner(len)
}

fn bytearray_fixed_inner(len: Int) -> Fuzzer<ByteArray> {
if len > 32 {
let head <- and_then(bytearray())
let tail <- map(bytearray_fixed(len - 32))
builtin.append_bytearray(head, tail)
} else if len < 32 {
map(bytearray(), builtin.slice_bytearray(0, len, _))
} else {
bytearray()
}
}

/// Generate a random [`ByteArray`](https://aiken-lang.github.io/prelude/aiken.html#ByteArray) of
/// size within a lower and upper bounds.
pub fn bytearray_between(min: Int, max: Int) -> Fuzzer<ByteArray> {
Expand All @@ -42,13 +60,7 @@ pub fn bytearray_between(min: Int, max: Int) -> Fuzzer<ByteArray> {
} else {
expect min >= 0
let size <- and_then(int_between(min, max))
if size <= 32 {
map(bytearray(), builtin.slice_bytearray(0, size, _))
} else {
let head <- and_then(bytearray())
let tail <- map(bytearray_between(0, size - 32))
builtin.append_bytearray(head, tail)
}
bytearray_fixed_inner(size)
}
}

Expand Down
12 changes: 9 additions & 3 deletions lib/aiken/fuzz.test.ak
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ test prop_bytearray_between(params via bytearray_between()) {

let len = bytearray.length(bytes)

let mid = ( min + max ) / 2

label(
if len > 32 {
@"> 32"
if len < min {
@"< min (impossible)"
} else if len <= mid {
@"<= mid (~50%)"
} else if len <= max {
@"<= max (~50%)"
} else {
@"<= 32"
@"> max (impossible)"
},
)

Expand Down

0 comments on commit 331e104

Please sign in to comment.