diff --git a/lib/aiken/fuzz.ak b/lib/aiken/fuzz.ak index 5513977..375b2c1 100644 --- a/lib/aiken/fuzz.ak +++ b/lib/aiken/fuzz.ak @@ -32,6 +32,24 @@ pub fn bytearray() -> Fuzzer { |> 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 { + expect len >= 0 + bytearray_fixed_inner(len) +} + +fn bytearray_fixed_inner(len: Int) -> Fuzzer { + 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 { @@ -42,13 +60,7 @@ pub fn bytearray_between(min: Int, max: Int) -> Fuzzer { } 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) } } diff --git a/lib/aiken/fuzz.test.ak b/lib/aiken/fuzz.test.ak index ff4605a..b9dc7de 100644 --- a/lib/aiken/fuzz.test.ak +++ b/lib/aiken/fuzz.test.ak @@ -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)" }, )