Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rational number fuzzer functions and corresponding tests #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiken.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions lib/aiken/fuzz.ak
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use aiken/builtin
use aiken/collection/list
use aiken/math
use aiken/option
use aiken/math/rational.{Rational, new, reduce}

// ## Constructing
// ### Primitives
Expand Down Expand Up @@ -199,6 +200,99 @@ pub fn int_at_most(max: Int) -> Fuzzer<Int> {
}
}

/// Generates a random rational value within the range `[-255, 16383]`,
/// following the specified distribution:
/// [-255,-101] 0.1%
/// [-100,-1] 23.3%
/// [0, 100] 68.8%
/// [101,255] 4.9%
/// >255 3.0%
pub fn rational() -> Fuzzer<Rational> {
map(both(int(), int_at_least(1)),
fn((numerator, denominator)) {
expect Some(fraction) = new(numerator, denominator)
fraction
}
)
}

/// Generates rational values between a lower and upper bound (both inclusive), with the following distribution:
/// [-255, -101] 31.1%
/// [-100, -1] 19.2%
/// [0, 100] 19.6%
/// [101, 255] 30.1%
///
/// The upper and lower bounds must be between -255 and 255.
pub fn rational_between(optional_min: Option<Rational>, optional_max: Option<Rational>) -> Fuzzer<Rational> {
expect Some(lower_bound) = optional_min
expect Some(upper_bound) = optional_max
expect True = correct_bounds(lower_bound, upper_bound)

if lower_bound == upper_bound {
expect Some(fraction) = new(rational.numerator(lower_bound), rational.denominator(lower_bound))
constant(fraction)
} else if rational.compare(lower_bound, upper_bound) == Greater {
rational_between(Some(upper_bound), Some(lower_bound))
} else {
let denominator <- and_then(int_at_least(math.max(rational.denominator(lower_bound), rational.denominator(upper_bound)) + 1))
let min_numerator = binary_search_min_numerator(lower_bound, denominator, -255 * denominator, 255 * denominator)
let max_numerator = binary_search_max_numerator(upper_bound, denominator, -255 * denominator, 255 * denominator)
map(int_between(min_numerator, max_numerator), fn(numerator) {
expect Some(fraction) = new(numerator, denominator)
reduce(fraction)
})
}
}

/// Generates a random rational value that is at least `optional_min`.
/// The lower bound must be between -255 and 255.
pub fn rational_at_least(optional_min: Option<Rational>) -> Fuzzer<Rational> {
rational_between(optional_min, new(255, 1))
}

/// Generates a random rational value that is at most `opt_max`.
/// The upper bound must be between -255 and 255.
pub fn rational_at_most(optional_max: Option<Rational>) {
rational_between(optional_max, new(-255, 1))
}


fn correct_bounds(min_fraction: Rational, max_fraction: Rational) {
expect Some(lower_bound) = new(-256, 1)
expect Some(upper_bound) = new(256, 1)

(rational.compare(min_fraction, lower_bound) == Greater
&& rational.compare(min_fraction, upper_bound) == Less
&& rational.compare(max_fraction, lower_bound) == Greater
&& rational.compare(max_fraction, upper_bound) == Less)
}


fn binary_search_min_numerator(min_fraction: Rational, denominator: Int, low: Int, high: Int) -> Int {
let mid_point = (low + high) / 2
expect Some(mid_fraction) = new(mid_point, denominator)

if low > high {
low
} else if rational.compare(mid_fraction, min_fraction) == Less {
binary_search_min_numerator(min_fraction, denominator, mid_point + 1, high)
} else {
binary_search_min_numerator(min_fraction, denominator, low, mid_point - 1)
}
}

fn binary_search_max_numerator(max_fraction: Rational, denominator: Int, low: Int, high: Int) -> Int {
let mid_point = (low + high) / 2
expect Some(mid_fraction) = new(mid_point, denominator)

if low > high {
high
} else if rational.compare(mid_fraction, max_fraction) == Greater {
binary_search_max_numerator(max_fraction, denominator, low, mid_point - 1)
} else {
binary_search_max_numerator(max_fraction, denominator, mid_point + 1, high)
}
}
// ### Data-structures

/// Generate a random list of elements from a given fuzzer. The list contains
Expand Down
56 changes: 54 additions & 2 deletions lib/aiken/fuzz.test.ak
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use aiken/collection/list
use aiken/fuzz.{
and_then, bool, int, int_between, label, list_between, list_with_elem, map,
one_of, set, set_between, sublist, such_that,
one_of, rational_at_least, rational_at_most, rational_between, rational, set, set_between, sublist, such_that,
}
use aiken/math
use aiken/primitive/bytearray
use aiken/primitive/string
use aiken/math/rational.{ Rational, new}

test prop_int_distribution(n via int()) {
label(
Expand All @@ -17,7 +18,7 @@ test prop_int_distribution(n via int()) {
@"0"
} else if n < 256 {
@"]0; 255]"
} else if n < 16383 {
} else if n <= 16383 {
@"[256; 16383]"
} else {
fail @"n > 16383"
Expand Down Expand Up @@ -200,6 +201,57 @@ test prop_set_between_distribution(n via set_between(int_between(0, 50), 3, 13))
True
}

test rational_distribution(fraction via rational()) {
fraction_distribution(fraction)
}

test prop_fraction_between_bounds(fraction via rational_between(new(-255, 1), new(255, 1))) {
fraction_distribution(fraction)
expect Some(lower_bound) = new(-255, 1)
expect Some(upper_bound) = new(255, 1)
(rational.compare(fraction, lower_bound) == Greater || rational.compare(fraction, lower_bound) == Equal) &&
(rational.compare(fraction, upper_bound) == Less || rational.compare(fraction, upper_bound) == Equal)
}

test prop_at_least_for_positive_fractions(fraction via rational_at_least(new(1, 1))) {
fraction_distribution(fraction)
expect Some(lower_bound) = new(1, 1)
expect Some(upper_bound) = new(255, 1)
(rational.compare(fraction, lower_bound) == Greater || rational.compare(fraction, lower_bound) == Equal) &&
(rational.compare(fraction, upper_bound) == Less || rational.compare(fraction, upper_bound) == Equal)
}

test prop_at_most_for_negative_fraction(fraction via rational_at_most(new(0, 1))) {
fraction_distribution(fraction)
expect Some(lower_bound) = new(-255, 1)
expect Some(upper_bound) = new(0, 1)
(rational.compare(fraction, lower_bound) == Greater || rational.compare(fraction, lower_bound) == Equal) &&
(rational.compare(fraction, upper_bound) == Less || rational.compare(fraction, upper_bound) == Equal)
}

fn fraction_distribution(fraction: Rational) {
expect Some(bound_1) = new(-255, 1)
expect Some(bound_2) = new(-100, 1)
expect Some(bound_3) = new(0, 1)
expect Some(bound_4) = new(100, 1)
expect Some(bound_5) = new(256, 1)

label(
if rational.compare(fraction, bound_1) == Less {
fail
} else if rational.compare(fraction, bound_2) == Less {
@"[-255,-101]"
} else if rational.compare(fraction, bound_3) == Less {
@"[-100,-1]"
} else if rational.compare(fraction, bound_4) == Less {
@"[0, 100]"
} else if rational.compare(fraction, bound_5) == Less {
@"[101,255]"
} else {
@">255"
}
)
}
// This property simply illustrate a case where the `set`
// fuzzer would fail and not loop forever after not being
// able to satisfy the demand (not enough entropy in the
Expand Down