mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-28 01:01:53 +03:00
impl negation tests for all types
This commit is contained in:
parent
269e24595f
commit
c66efc03f0
@ -23,6 +23,33 @@ pub fn field_to_decimal_string(f: Fq) -> String {
|
||||
f_bigint.to_str_radix(10)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_negate() {
|
||||
use std::ops::Neg;
|
||||
|
||||
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
||||
|
||||
for _ in 0..10 {
|
||||
let a: Fq = rng.gen();
|
||||
let b = a.neg();
|
||||
|
||||
let a_string = field_to_decimal_string(a);
|
||||
let b_string = field_to_decimal_string(b);
|
||||
|
||||
let bytes = include_bytes!("negate.leo");
|
||||
let mut program = parse_program(bytes).unwrap();
|
||||
|
||||
let main_input = generate_main_input(vec![
|
||||
("a", Some(InputValue::Field(a_string))),
|
||||
("b", Some(InputValue::Field(b_string))),
|
||||
]);
|
||||
|
||||
program.set_main_input(main_input);
|
||||
|
||||
assert_satisfied(program)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
use std::ops::Add;
|
||||
|
3
compiler/tests/field/negate.leo
Normal file
3
compiler/tests/field/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: field, b: field) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
@ -59,6 +59,32 @@ fn test_input() {
|
||||
expect_synthesis_error(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_negate() {
|
||||
use std::ops::Neg;
|
||||
|
||||
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
|
||||
|
||||
for _ in 0..10 {
|
||||
let a: EdwardsAffine = rng.gen();
|
||||
let b = a.neg();
|
||||
|
||||
let a_string = group_to_decimal_string(a);
|
||||
let b_string = group_to_decimal_string(b);
|
||||
|
||||
let bytes = include_bytes!("negate.leo");
|
||||
let mut program = parse_program(bytes).unwrap();
|
||||
|
||||
let main_input = generate_main_input(vec![
|
||||
("a", Some(InputValue::Group(a_string))),
|
||||
("b", Some(InputValue::Group(b_string))),
|
||||
]);
|
||||
program.set_main_input(main_input);
|
||||
|
||||
assert_satisfied(program)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
use std::ops::Add;
|
||||
|
3
compiler/tests/group/negate.leo
Normal file
3
compiler/tests/group/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: group, b: group) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
@ -30,6 +30,21 @@ fn test_i128_max_fail() {
|
||||
Testi128::test_max_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i128_neg() {
|
||||
Testi128::test_negate();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i128_neg_max_fail() {
|
||||
Testi128::test_negate_min_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i128_neg_zero() {
|
||||
Testi128::test_negate_zero();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i128_add() {
|
||||
Testi128::test_add();
|
||||
|
3
compiler/tests/integers/i128/negate.leo
Normal file
3
compiler/tests/integers/i128/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: i128, b: i128) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
4
compiler/tests/integers/i128/negate_min.leo
Normal file
4
compiler/tests/integers/i128/negate_min.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let a: i128 = -170141183460469231731687303715884105728;
|
||||
let b = -a;
|
||||
}
|
5
compiler/tests/integers/i128/negate_zero.leo
Normal file
5
compiler/tests/integers/i128/negate_zero.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = 0i128;
|
||||
|
||||
assert_eq!(-a, 0i128);
|
||||
}
|
@ -30,6 +30,21 @@ fn test_i16_max_fail() {
|
||||
Testi16::test_max_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i16_neg() {
|
||||
Testi16::test_negate();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i16_neg_max_fail() {
|
||||
Testi16::test_negate_min_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i16_neg_zero() {
|
||||
Testi16::test_negate_zero();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i16_add() {
|
||||
Testi16::test_add();
|
||||
|
3
compiler/tests/integers/i16/negate.leo
Normal file
3
compiler/tests/integers/i16/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: i16, b: i16) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
4
compiler/tests/integers/i16/negate_min.leo
Normal file
4
compiler/tests/integers/i16/negate_min.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let a = -32768i16;
|
||||
let b = -a;
|
||||
}
|
5
compiler/tests/integers/i16/negate_zero.leo
Normal file
5
compiler/tests/integers/i16/negate_zero.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = 0i16;
|
||||
|
||||
assert_eq!(-a, 0i16);
|
||||
}
|
@ -30,6 +30,21 @@ fn test_i32_max_fail() {
|
||||
Testi32::test_max_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i32_neg() {
|
||||
Testi32::test_negate();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i32_neg_max_fail() {
|
||||
Testi32::test_negate_min_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i32_neg_zero() {
|
||||
Testi32::test_negate_zero();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i32_add() {
|
||||
Testi32::test_add();
|
||||
|
3
compiler/tests/integers/i32/negate.leo
Normal file
3
compiler/tests/integers/i32/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: i32, b: i32) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
4
compiler/tests/integers/i32/negate_min.leo
Normal file
4
compiler/tests/integers/i32/negate_min.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let a = -2147483648i32;
|
||||
let b = -a;
|
||||
}
|
5
compiler/tests/integers/i32/negate_zero.leo
Normal file
5
compiler/tests/integers/i32/negate_zero.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = 0i32;
|
||||
|
||||
assert_eq!(-a, 0i32);
|
||||
}
|
@ -30,6 +30,21 @@ fn test_i64_max_fail() {
|
||||
Testi64::test_max_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i64_neg() {
|
||||
Testi64::test_negate();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i64_neg_max_fail() {
|
||||
Testi64::test_negate_min_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i64_neg_zero() {
|
||||
Testi64::test_negate_zero();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i64_add() {
|
||||
Testi64::test_add();
|
||||
|
3
compiler/tests/integers/i64/negate.leo
Normal file
3
compiler/tests/integers/i64/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: i64, b: i64) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
4
compiler/tests/integers/i64/negate_min.leo
Normal file
4
compiler/tests/integers/i64/negate_min.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let a: i64 = -9223372036854775808;
|
||||
let b = -a;
|
||||
}
|
5
compiler/tests/integers/i64/negate_zero.leo
Normal file
5
compiler/tests/integers/i64/negate_zero.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = 0i64;
|
||||
|
||||
assert_eq!(-a, 0i64);
|
||||
}
|
@ -30,6 +30,21 @@ fn test_i8_max_fail() {
|
||||
Testi8::test_max_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i8_neg() {
|
||||
Testi8::test_negate();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i8_neg_max_fail() {
|
||||
Testi8::test_negate_min_fail();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i8_neg_zero() {
|
||||
Testi8::test_negate_zero();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i8_add() {
|
||||
Testi8::test_add();
|
||||
|
3
compiler/tests/integers/i8/negate.leo
Normal file
3
compiler/tests/integers/i8/negate.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: i8, b: i8) {
|
||||
assert_eq!(-a, b);
|
||||
}
|
4
compiler/tests/integers/i8/negate_min.leo
Normal file
4
compiler/tests/integers/i8/negate_min.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let a = -128i8;
|
||||
let b = -a;
|
||||
}
|
5
compiler/tests/integers/i8/negate_zero.leo
Normal file
5
compiler/tests/integers/i8/negate_zero.leo
Normal file
@ -0,0 +1,5 @@
|
||||
function main() {
|
||||
let a = 0i8;
|
||||
|
||||
assert_eq!(-a, 0i8);
|
||||
}
|
@ -2,6 +2,44 @@ macro_rules! test_int {
|
||||
($name: ident, $type_: ty, $integer_type: expr, $gadget: ty) => {
|
||||
pub struct $name {}
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
let bytes = include_bytes!("negate.leo");
|
||||
let mut program = parse_program(bytes).unwrap();
|
||||
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() {
|
||||
let bytes = include_bytes!("negate_min.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
expect_computation_error(program);
|
||||
}
|
||||
|
||||
fn test_negate_zero() {
|
||||
let bytes = include_bytes!("negate_zero.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
assert_satisfied(program);
|
||||
}
|
||||
}
|
||||
|
||||
impl IntegerTester for $name {
|
||||
fn test_min() {
|
||||
let bytes = include_bytes!("min.leo");
|
||||
|
Loading…
Reference in New Issue
Block a user