leo/compiler/tests/integers/uint_macro.rs

477 lines
18 KiB
Rust
Raw Normal View History

2020-08-18 13:50:26 +03:00
// Copyright (C) 2019-2020 Aleo Systems Inc.
// 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/>.
macro_rules! test_uint {
2020-07-17 07:27:40 +03:00
($name: ident, $type_: ty, $integer_type: expr, $gadget: ty) => {
pub struct $name {}
2020-07-31 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
2020-07-31 01:41:03 +03:00
fn test_min_fail() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("min_fail.leo");
let program = parse_program(program_string).unwrap();
expect_parsing_error(program);
}
2020-07-31 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
2020-06-09 05:13:47 +03:00
2020-07-31 01:41:03 +03:00
fn test_max_fail() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("max_fail.leo");
let program = parse_program(program_string).unwrap();
expect_parsing_error(program);
}
fn test_add() {
for _ in 0..10 {
2020-07-31 01:41:03 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 01:41:03 +03:00
let c = match a.checked_add(b) {
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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
}
fn test_sub() {
for _ in 0..10 {
2020-07-31 01:41:03 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
}
fn test_mul() {
for _ in 0..10 {
2020-07-31 01:41:03 +03:00
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 01:41:03 +03:00
let c = match a.checked_mul(b) {
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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
}
fn test_div() {
2020-07-31 01:41:03 +03:00
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 01:41:03 +03:00
let c = match a.checked_div(b) {
Some(valid) => valid,
None => continue,
};
2020-06-09 05:13:47 +03:00
2020-12-04 23:48:43 +03:00
let program_string = include_str!("div.leo");
let mut program = parse_program(program_string).unwrap();
2020-07-31 01:41:03 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
}
fn test_pow() {
2020-07-31 01:41:03 +03:00
for _ in 0..10 {
let a: $type_ = rand::random();
let b: $type_ = rand::random();
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
}
}
2020-06-06 01:34:06 +03:00
fn test_eq() {
for _ in 0..10 {
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
// test not equal
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_gt() {
for _ in 0..10 {
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_le() {
for _ in 0..10 {
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
// test less or equal
2020-06-06 01:34:06 +03:00
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
fn test_lt() {
for _ in 0..10 {
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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-07-31 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
2020-07-31 01:41:03 +03:00
// test less or equal
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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
// test not equal
2020-07-31 01:41:03 +03:00
let b: $type_ = rand::random();
2020-06-06 01:34:06 +03:00
2020-07-31 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +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 01:41:03 +03:00
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 01:41:03 +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 01:41:03 +03:00
assert_satisfied(program);
2020-06-06 01:34:06 +03:00
}
}
};
}