leo/compiler/tests/integers/int_macro.rs

440 lines
16 KiB
Rust
Raw Normal View History

2020-07-16 06:42:57 +03:00
macro_rules! test_int {
2020-07-17 07:26:39 +03:00
($name: ident, $type_: ty, $integer_type: expr, $gadget: ty) => {
pub struct $name {}
2020-07-31 02:20:31 +03:00
impl IntegerTester for $name {
fn test_min() {
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("min.leo");
let program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
}
2020-07-31 02:20:31 +03:00
fn test_min_fail() {
let bytes = include_bytes!("min_fail.leo");
2020-06-09 05:13:47 +03:00
let program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
expect_fail(program);
}
2020-06-09 05:13:47 +03:00
2020-07-31 02:20:31 +03:00
fn test_max() {
let bytes = include_bytes!("max.leo");
let program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
}
2020-06-09 05:13:47 +03:00
2020-07-31 02:20:31 +03:00
fn test_max_fail() {
let bytes = include_bytes!("max_fail.leo");
let program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
expect_fail(program);
}
fn test_add() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 02:20:31 +03:00
let c = match a.checked_add(b) {
2020-07-16 06:42:57 +03:00
Some(valid) => valid,
None => continue,
};
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("add.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_sub() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-17 07:26:39 +03:00
2020-07-31 02:20:31 +03:00
if b.checked_neg().is_none() {
2020-07-17 07:26:39 +03:00
continue;
}
2020-07-31 02:20:31 +03:00
let c = match a.checked_sub(b) {
2020-07-16 05:55:04 +03:00
Some(valid) => valid,
None => continue,
};
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("sub.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_mul() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 02:20:31 +03:00
let c = match a.checked_mul(b) {
2020-07-16 06:42:57 +03:00
Some(valid) => valid,
None => continue,
};
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("mul.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
}
fn test_div() {
2020-07-31 02:20:31 +03:00
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 02:20:31 +03:00
let bytes = include_bytes!("div.leo");
let mut program = parse_program(bytes).unwrap();
2020-06-09 05:13:47 +03:00
2020-07-31 02:20:31 +03:00
// expect an error when dividing by zero
if b == 0 {
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, b.to_string()))),
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-07-16 06:42:57 +03:00
2020-07-31 02:20:31 +03:00
expect_fail(program);
} else {
let c = match a.checked_div(b) {
Some(valid) => valid,
None => return,
};
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
program.set_main_inputs(main_inputs);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
}
}
}
fn test_pow() {
2020-07-31 02:20:31 +03:00
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 02:20:31 +03:00
// rust specific conversion see https://doc.rust-lang.org/std/primitive.u8.html#method.checked_pow
let c = match a.checked_pow(b as u32) {
Some(valid) => valid,
None => continue,
};
2020-07-31 02:20:31 +03:00
let bytes = include_bytes!("pow.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, c.to_string()))),
]);
2020-06-09 05:13:47 +03:00
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
}
}
2020-06-06 01:34:06 +03:00
fn test_eq() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-07-31 02:20:31 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("eq.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
// test not equal
2020-07-31 02:20:31 +03:00
let c = a.eq(&b);
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_ge() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-07-31 02:20:31 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("ge.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
// test greater or equal
let c = a.ge(&b);
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_gt() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-07-31 02:20:31 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("gt.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
// test greater than
let c = a.gt(&b);
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_le() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-07-31 02:20:31 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("le.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(true))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
// test less or equal
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
let c = a.le(&b);
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_lt() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-07-31 02:20:31 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("lt.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
// test less or equal
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
let c = a.lt(&b);
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_assert_eq() {
for _ in 0..10 {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
// test equal
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("assert_eq.leo");
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
// test not equal
2020-07-31 02:20:31 +03:00
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
2020-07-31 02:20:31 +03:00
if a == b {
2020-06-06 01:34:06 +03:00
continue;
}
2020-06-09 05:13:47 +03:00
let mut program = parse_program(bytes).unwrap();
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
expect_synthesis_error(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_ternary() {
2020-07-31 02:20:31 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
2020-06-09 05:13:47 +03:00
let bytes = include_bytes!("ternary.leo");
2020-07-31 02:20:31 +03:00
let mut program = parse_program(bytes).unwrap();
2020-06-06 01:34:06 +03:00
2020-07-08 13:30:15 +03:00
// true -> field 1
2020-07-31 02:20:31 +03:00
let main_inputs = generate_main_inputs(vec![
("s", Some(InputValue::Boolean(true))),
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, a.to_string()))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-08 13:30:15 +03:00
// false -> field 2
2020-07-31 02:20:31 +03:00
let mut program = parse_program(bytes).unwrap();
let main_inputs = generate_main_inputs(vec![
("s", Some(InputValue::Boolean(false))),
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Integer($integer_type, b.to_string()))),
2020-06-06 01:34:06 +03:00
]);
2020-07-31 02:20:31 +03:00
program.set_main_inputs(main_inputs);
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
};
}