diff --git a/compiler/tests/field/mod.rs b/compiler/tests/field/mod.rs index f95899d6c3..510209b11f 100644 --- a/compiler/tests/field/mod.rs +++ b/compiler/tests/field/mod.rs @@ -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; diff --git a/compiler/tests/field/negate.leo b/compiler/tests/field/negate.leo new file mode 100644 index 0000000000..f7314f60a1 --- /dev/null +++ b/compiler/tests/field/negate.leo @@ -0,0 +1,3 @@ +function main(a: field, b: field) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/group/mod.rs b/compiler/tests/group/mod.rs index 463f61ed59..01839ecec8 100644 --- a/compiler/tests/group/mod.rs +++ b/compiler/tests/group/mod.rs @@ -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; diff --git a/compiler/tests/group/negate.leo b/compiler/tests/group/negate.leo new file mode 100644 index 0000000000..bdb93ce93f --- /dev/null +++ b/compiler/tests/group/negate.leo @@ -0,0 +1,3 @@ +function main(a: group, b: group) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i128/mod.rs b/compiler/tests/integers/i128/mod.rs index 5faca493ef..68eb1b17c1 100644 --- a/compiler/tests/integers/i128/mod.rs +++ b/compiler/tests/integers/i128/mod.rs @@ -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(); diff --git a/compiler/tests/integers/i128/negate.leo b/compiler/tests/integers/i128/negate.leo new file mode 100644 index 0000000000..a55ad8711d --- /dev/null +++ b/compiler/tests/integers/i128/negate.leo @@ -0,0 +1,3 @@ +function main(a: i128, b: i128) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i128/negate_min.leo b/compiler/tests/integers/i128/negate_min.leo new file mode 100644 index 0000000000..cfbd308362 --- /dev/null +++ b/compiler/tests/integers/i128/negate_min.leo @@ -0,0 +1,4 @@ +function main() { + let a: i128 = -170141183460469231731687303715884105728; + let b = -a; +} \ No newline at end of file diff --git a/compiler/tests/integers/i128/negate_zero.leo b/compiler/tests/integers/i128/negate_zero.leo new file mode 100644 index 0000000000..c216f1d9d8 --- /dev/null +++ b/compiler/tests/integers/i128/negate_zero.leo @@ -0,0 +1,5 @@ +function main() { + let a = 0i128; + + assert_eq!(-a, 0i128); +} \ No newline at end of file diff --git a/compiler/tests/integers/i16/mod.rs b/compiler/tests/integers/i16/mod.rs index c7f8b6dc47..3aaab7dcb8 100644 --- a/compiler/tests/integers/i16/mod.rs +++ b/compiler/tests/integers/i16/mod.rs @@ -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(); diff --git a/compiler/tests/integers/i16/negate.leo b/compiler/tests/integers/i16/negate.leo new file mode 100644 index 0000000000..19046f9cb9 --- /dev/null +++ b/compiler/tests/integers/i16/negate.leo @@ -0,0 +1,3 @@ +function main(a: i16, b: i16) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i16/negate_min.leo b/compiler/tests/integers/i16/negate_min.leo new file mode 100644 index 0000000000..2622f8d2a3 --- /dev/null +++ b/compiler/tests/integers/i16/negate_min.leo @@ -0,0 +1,4 @@ +function main() { + let a = -32768i16; + let b = -a; +} \ No newline at end of file diff --git a/compiler/tests/integers/i16/negate_zero.leo b/compiler/tests/integers/i16/negate_zero.leo new file mode 100644 index 0000000000..3edc84307b --- /dev/null +++ b/compiler/tests/integers/i16/negate_zero.leo @@ -0,0 +1,5 @@ +function main() { + let a = 0i16; + + assert_eq!(-a, 0i16); +} \ No newline at end of file diff --git a/compiler/tests/integers/i32/mod.rs b/compiler/tests/integers/i32/mod.rs index a1f0be644b..06ddcc37eb 100644 --- a/compiler/tests/integers/i32/mod.rs +++ b/compiler/tests/integers/i32/mod.rs @@ -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(); diff --git a/compiler/tests/integers/i32/negate.leo b/compiler/tests/integers/i32/negate.leo new file mode 100644 index 0000000000..60524c24f9 --- /dev/null +++ b/compiler/tests/integers/i32/negate.leo @@ -0,0 +1,3 @@ +function main(a: i32, b: i32) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i32/negate_min.leo b/compiler/tests/integers/i32/negate_min.leo new file mode 100644 index 0000000000..b3c10064e1 --- /dev/null +++ b/compiler/tests/integers/i32/negate_min.leo @@ -0,0 +1,4 @@ +function main() { + let a = -2147483648i32; + let b = -a; +} \ No newline at end of file diff --git a/compiler/tests/integers/i32/negate_zero.leo b/compiler/tests/integers/i32/negate_zero.leo new file mode 100644 index 0000000000..e606ae5994 --- /dev/null +++ b/compiler/tests/integers/i32/negate_zero.leo @@ -0,0 +1,5 @@ +function main() { + let a = 0i32; + + assert_eq!(-a, 0i32); +} \ No newline at end of file diff --git a/compiler/tests/integers/i64/mod.rs b/compiler/tests/integers/i64/mod.rs index 27b45d31f1..6a2982eb3a 100644 --- a/compiler/tests/integers/i64/mod.rs +++ b/compiler/tests/integers/i64/mod.rs @@ -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(); diff --git a/compiler/tests/integers/i64/negate.leo b/compiler/tests/integers/i64/negate.leo new file mode 100644 index 0000000000..036967943e --- /dev/null +++ b/compiler/tests/integers/i64/negate.leo @@ -0,0 +1,3 @@ +function main(a: i64, b: i64) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i64/negate_min.leo b/compiler/tests/integers/i64/negate_min.leo new file mode 100644 index 0000000000..4dd27778d0 --- /dev/null +++ b/compiler/tests/integers/i64/negate_min.leo @@ -0,0 +1,4 @@ +function main() { + let a: i64 = -9223372036854775808; + let b = -a; +} \ No newline at end of file diff --git a/compiler/tests/integers/i64/negate_zero.leo b/compiler/tests/integers/i64/negate_zero.leo new file mode 100644 index 0000000000..947aa924d8 --- /dev/null +++ b/compiler/tests/integers/i64/negate_zero.leo @@ -0,0 +1,5 @@ +function main() { + let a = 0i64; + + assert_eq!(-a, 0i64); +} \ No newline at end of file diff --git a/compiler/tests/integers/i8/mod.rs b/compiler/tests/integers/i8/mod.rs index 1961462495..9a98b4b14e 100644 --- a/compiler/tests/integers/i8/mod.rs +++ b/compiler/tests/integers/i8/mod.rs @@ -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(); diff --git a/compiler/tests/integers/i8/negate.leo b/compiler/tests/integers/i8/negate.leo new file mode 100644 index 0000000000..039067b6e4 --- /dev/null +++ b/compiler/tests/integers/i8/negate.leo @@ -0,0 +1,3 @@ +function main(a: i8, b: i8) { + assert_eq!(-a, b); +} \ No newline at end of file diff --git a/compiler/tests/integers/i8/negate_min.leo b/compiler/tests/integers/i8/negate_min.leo new file mode 100644 index 0000000000..3959da615e --- /dev/null +++ b/compiler/tests/integers/i8/negate_min.leo @@ -0,0 +1,4 @@ +function main() { + let a = -128i8; + let b = -a; +} \ No newline at end of file diff --git a/compiler/tests/integers/i8/negate_zero.leo b/compiler/tests/integers/i8/negate_zero.leo new file mode 100644 index 0000000000..5c435bcd92 --- /dev/null +++ b/compiler/tests/integers/i8/negate_zero.leo @@ -0,0 +1,5 @@ +function main() { + let a = 0i8; + + assert_eq!(-a, 0i8); +} \ No newline at end of file diff --git a/compiler/tests/integers/int_macro.rs b/compiler/tests/integers/int_macro.rs index a493ee949c..8b3fb95830 100644 --- a/compiler/tests/integers/int_macro.rs +++ b/compiler/tests/integers/int_macro.rs @@ -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");