leo/compiler/tests/integers/int_macro.rs

539 lines
20 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +03:00
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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-08-05 07:37:09 +03:00
impl $name {
fn test_negate() {
for _ in 0..10 {
let a: $type_ = rand::random();
let b = match a.checked_neg() {
Some(valid) => valid,
None => continue,
};
2020-12-04 23:48:43 +03:00
let program_string = include_str!("negate.leo");
let mut program = parse_program(program_string).unwrap();
2020-08-05 07:37:09 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
]);
program.set_main_input(main_input);
assert_satisfied(program);
}
}
fn test_negate_min_fail() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("negate_min.leo");
let program = parse_program(program_string).unwrap();
2020-08-05 07:37:09 +03:00
expect_computation_error(program);
}
fn test_negate_zero() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("negate_zero.leo");
let program = parse_program(program_string).unwrap();
2020-08-05 07:37:09 +03:00
assert_satisfied(program);
}
}
2020-07-31 02:20:31 +03:00
impl IntegerTester for $name {
fn test_min() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("min.leo");
let program = parse_program(program_string).unwrap();
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
}
2020-07-31 02:20:31 +03:00
fn test_min_fail() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("min_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
2020-06-09 05:13:47 +03:00
2020-07-31 02:20:31 +03:00
fn test_max() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("max.leo");
let program = parse_program(program_string).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() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("max_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
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-12-04 23:48:43 +03:00
let program_string = include_str!("add.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("sub.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("mul.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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();
// make sure that we can calculate the inverse of each number
// Leo signed integer division is non-wrapping. Thus attempting to calculate a
// division result that wraps should be ignored here.
2020-08-04 04:54:27 +03:00
if a.checked_neg().is_none() {
continue;
}
2020-12-04 23:48:43 +03:00
let program_string = include_str!("div.leo");
let mut program = parse_program(program_string).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 {
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-16 06:42:57 +03:00
2020-08-19 10:44:01 +03:00
expect_compiler_error(program);
2020-07-31 02:20:31 +03:00
} else {
let c = match a.checked_div(b) {
Some(valid) => valid,
None => continue,
2020-07-31 02:20:31 +03:00
};
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let program_string = include_str!("pow.leo");
let mut program = parse_program(program_string).unwrap();
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let program_string = include_str!("eq.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
2020-08-14 10:25:39 +03:00
fn test_ne() {
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
// test a != a == false
2020-12-04 23:48:43 +03:00
let program_string = include_str!("ne.leo");
let mut program = parse_program(program_string).unwrap();
2020-08-14 10:25:39 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, a.to_string()))),
("c", Some(InputValue::Boolean(false))),
]);
program.set_main_input(main_input);
assert_satisfied(program);
// test not equal
let c = a.ne(&b);
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-08-14 10:25:39 +03:00
let main_input = generate_main_input(vec![
("a", Some(InputValue::Integer($integer_type, a.to_string()))),
("b", Some(InputValue::Integer($integer_type, b.to_string()))),
("c", Some(InputValue::Boolean(c))),
]);
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let program_string = include_str!("ge.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("gt.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("le.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("lt.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
2020-08-17 05:14:26 +03:00
fn test_console_assert() {
2020-06-06 01:34:06 +03:00
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-12-04 23:48:43 +03:00
let program_string = include_str!("console_assert.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
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-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-06-09 05:13:47 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
2020-08-17 05:14:26 +03:00
expect_compiler_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-12-04 23:48:43 +03:00
let program_string = include_str!("ternary.leo");
let mut program = parse_program(program_string).unwrap();
2020-06-06 01:34:06 +03:00
2020-07-08 13:30:15 +03:00
// true -> field 1
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-08 13:30:15 +03:00
// false -> field 2
2020-12-04 23:48:43 +03:00
let mut program = parse_program(program_string).unwrap();
2020-07-31 02:20:31 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 02:20:31 +03:00
("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-08-01 06:59:50 +03:00
program.set_main_input(main_input);
2020-07-31 02:20:31 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
};
}