Skip to content

Commit

Permalink
refactor(test): use crate test-strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Nov 1, 2023
1 parent 81248b9 commit 01e5e22
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 581 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ serde = { version = "1", features = ["derive"] }
serde_derive = "1"
strum = { version = "0.25", features = ["derive"] }
syn = "2.0"
test-strategy = "0.3.1"
twenty-first = "0.34"
unicode-width = "0.1"

Expand Down
1 change: 1 addition & 0 deletions triton-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ unicode-width.workspace = true
cargo-husky.workspace = true
proptest.workspace = true
proptest-arbitrary-interop.workspace = true
test-strategy.workspace = true

[[bench]]
name = "prove_halt"
Expand Down
93 changes: 44 additions & 49 deletions triton-vm/src/arithmetic_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ impl ArithmeticDomain {
#[cfg(test)]
mod tests {
use itertools::Itertools;
use proptest::prelude::*;
use proptest_arbitrary_interop::arb;
use test_strategy::proptest;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::traits::PrimitiveRootOfUnity;

use super::*;
use twenty_first::shared_math::x_field_element::XFieldElement;

use crate::shared_tests::*;
use proptest::prelude::*;

use super::*;

prop_compose! {
fn arbitrary_domain()(
Expand All @@ -141,50 +144,42 @@ mod tests {

prop_compose! {
fn arbitrary_domain_of_length(length: usize)(
offset in arbitrary_bfield_element(),
offset in arb(),
) -> ArithmeticDomain {
ArithmeticDomain::of_length(length).with_offset(offset)
}
}

proptest! {
#[test]
fn evaluate_empty_polynomial(
domain in arbitrary_domain(),
polynomial in arbitrary_polynomial_of_degree(-1),
) {
domain.evaluate(&polynomial);
}
#[proptest]
fn evaluate_empty_polynomial(
#[strategy(arbitrary_domain())] domain: ArithmeticDomain,
#[strategy(arbitrary_polynomial_of_degree(-1))] polynomial: Polynomial<XFieldElement>,
) {
domain.evaluate(&polynomial);
}

proptest! {
#[test]
fn evaluate_constant_polynomial(
domain in arbitrary_domain(),
polynomial in arbitrary_polynomial_of_degree(0),
) {
domain.evaluate(&polynomial);
}
#[proptest]
fn evaluate_constant_polynomial(
#[strategy(arbitrary_domain())] domain: ArithmeticDomain,
#[strategy(arbitrary_polynomial_of_degree(0))] polynomial: Polynomial<XFieldElement>,
) {
domain.evaluate(&polynomial);
}

proptest! {
#[test]
fn evaluate_linear_polynomial(
domain in arbitrary_domain(),
polynomial in arbitrary_polynomial_of_degree(1),
) {
domain.evaluate(&polynomial);
}
#[proptest]
fn evaluate_linear_polynomial(
#[strategy(arbitrary_domain())] domain: ArithmeticDomain,
#[strategy(arbitrary_polynomial_of_degree(1))] polynomial: Polynomial<XFieldElement>,
) {
domain.evaluate(&polynomial);
}

proptest! {
#[test]
fn evaluate_polynomial(
domain in arbitrary_domain(),
polynomial in arbitrary_polynomial(),
) {
domain.evaluate(&polynomial);
}
#[proptest]
fn evaluate_polynomial(
#[strategy(arbitrary_domain())] domain: ArithmeticDomain,
#[strategy(arbitrary_polynomial())] polynomial: Polynomial<XFieldElement>,
) {
domain.evaluate(&polynomial);
}

#[test]
Expand Down Expand Up @@ -251,21 +246,21 @@ mod tests {
assert_eq!(short_codeword, long_codeword_sub_view);
}

proptest! {
#[test]
fn halving_domain_squares_all_points(domain in arbitrary_halveable_domain()) {
let half_domain = domain.halve();
prop_assert_eq!(domain.length / 2, half_domain.length);
#[proptest]
fn halving_domain_squares_all_points(
#[strategy(arbitrary_halveable_domain())] domain: ArithmeticDomain,
) {
let half_domain = domain.halve();
prop_assert_eq!(domain.length / 2, half_domain.length);

let domain_points = domain.domain_values();
let half_domain_points = half_domain.domain_values();
let domain_points = domain.domain_values();
let half_domain_points = half_domain.domain_values();

for (domain_point, halved_domain_point) in domain_points
.into_iter()
.zip(half_domain_points.into_iter())
{
prop_assert_eq!(domain_point.square(), halved_domain_point);
}
for (domain_point, halved_domain_point) in domain_points
.into_iter()
.zip(half_domain_points.into_iter())
{
prop_assert_eq!(domain_point.square(), halved_domain_point);
}
}

Expand Down
75 changes: 36 additions & 39 deletions triton-vm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Error for InstructionError {}
mod tests {
use proptest::prelude::*;
use proptest_arbitrary_interop::arb;
use test_strategy::proptest;

use crate::instruction::AnInstruction::*;
use crate::instruction::LabelledInstruction;
Expand Down Expand Up @@ -129,46 +130,42 @@ mod tests {
program.run([].into(), [].into()).unwrap();
}

proptest! {
#[test]
fn assert_unequal_vec(
test_vector in arb::<[BFieldElement; DIGEST_LENGTH]>(),
disturbance_index in 0..DIGEST_LENGTH,
random_element in arb::<BFieldElement>(),
) {
let mut disturbed_vector = test_vector;
disturbed_vector[disturbance_index] = random_element;

if disturbed_vector == test_vector {
return Ok(());
}
#[proptest]
fn assert_unequal_vec(
#[strategy(arb())] test_vector: [BFieldElement; DIGEST_LENGTH],
#[strategy(0..DIGEST_LENGTH)] disturbance_index: usize,
#[strategy(arb())]
#[filter(#test_vector[#disturbance_index] != #random_element)]
random_element: BFieldElement,
) {
let mut disturbed_vector = test_vector;
disturbed_vector[disturbance_index] = random_element;

let program = triton_program!{
push {test_vector[4]}
push {test_vector[3]}
push {test_vector[2]}
push {test_vector[1]}
push {test_vector[0]}

push {disturbed_vector[4]}
push {disturbed_vector[3]}
push {disturbed_vector[2]}
push {disturbed_vector[1]}
push {disturbed_vector[0]}

assert_vector
halt
};

let err = program.run([].into(), [].into()).unwrap_err();

let err = err.downcast::<InstructionError>().unwrap();
let VectorAssertionFailed(_, _, index, _, _) = err else {
panic!("VM panicked with unexpected error {err}.")
};
let index: usize = index.into();
prop_assert_eq!(disturbance_index, index);
}
let program = triton_program! {
push {test_vector[4]}
push {test_vector[3]}
push {test_vector[2]}
push {test_vector[1]}
push {test_vector[0]}

push {disturbed_vector[4]}
push {disturbed_vector[3]}
push {disturbed_vector[2]}
push {disturbed_vector[1]}
push {disturbed_vector[0]}

assert_vector
halt
};

let err = program.run([].into(), [].into()).unwrap_err();

let err = err.downcast::<InstructionError>().unwrap();
let VectorAssertionFailed(_, _, index, _, _) = err else {
panic!("VM panicked with unexpected error {err}.")
};
let index: usize = index.into();
prop_assert_eq!(disturbance_index, index);
}

#[test]
Expand Down
Loading

0 comments on commit 01e5e22

Please sign in to comment.