2020-06-06 00:09:12 +03:00
|
|
|
use crate::{
|
2020-06-06 01:34:06 +03:00
|
|
|
boolean::{output_expected_boolean, output_false, output_true},
|
|
|
|
compile_program, get_error, get_output,
|
|
|
|
integer::IntegerTester,
|
|
|
|
EdwardsConstrainedValue, EdwardsTestCompiler,
|
2020-06-06 00:09:12 +03:00
|
|
|
};
|
2020-06-02 04:35:43 +03:00
|
|
|
use leo_compiler::{
|
|
|
|
errors::{CompilerError, FunctionError, IntegerError},
|
|
|
|
types::Integer,
|
|
|
|
ConstrainedValue, InputValue,
|
|
|
|
};
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
use snarkos_curves::edwards_bls12::Fq;
|
|
|
|
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
|
|
|
|
use snarkos_models::gadgets::utilities::{alloc::AllocGadget, uint::UInt32};
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
const DIRECTORY_NAME: &str = "tests/integer/u32/";
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
fn output_expected_allocated(program: EdwardsTestCompiler, expected: UInt32) {
|
|
|
|
let output = get_output(program);
|
|
|
|
|
|
|
|
match output {
|
|
|
|
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
|
|
|
|
[ConstrainedValue::Integer(Integer::U32(actual))] => assert_eq!(*actual, expected),
|
|
|
|
_ => panic!("program output unknown return value"),
|
|
|
|
},
|
|
|
|
_ => panic!("program output unknown return value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
pub(crate) fn output_zero(program: EdwardsTestCompiler) {
|
2020-05-20 01:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
2020-05-30 01:55:57 +03:00
|
|
|
UInt32::constant(0u32)
|
2020-05-30 03:34:31 +03:00
|
|
|
))])
|
|
|
|
.to_string(),
|
|
|
|
output.to_string()
|
2020-05-20 01:45:40 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
pub(crate) fn output_one(program: EdwardsTestCompiler) {
|
2020-05-20 01:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
2020-05-30 01:55:57 +03:00
|
|
|
UInt32::constant(1u32)
|
2020-05-30 03:34:31 +03:00
|
|
|
))])
|
|
|
|
.to_string(),
|
|
|
|
output.to_string()
|
2020-05-20 01:45:40 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_integer(program: EdwardsTestCompiler) {
|
2020-05-20 03:08:38 +03:00
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
|
|
|
IntegerError::InvalidInteger(_string),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected invalid boolean error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_synthesis(program: EdwardsTestCompiler) {
|
2020-05-20 03:08:38 +03:00
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
|
|
|
IntegerError::SynthesisError(_string),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected synthesis error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-06 00:09:12 +03:00
|
|
|
fn test_u32() {
|
|
|
|
test_uint!(TestU32, u32, UInt32, DIRECTORY_NAME);
|
2020-05-20 03:08:38 +03:00
|
|
|
|
2020-06-06 00:09:12 +03:00
|
|
|
TestU32::test_min(std::u32::MIN);
|
|
|
|
TestU32::test_max(std::u32::MAX);
|
|
|
|
|
|
|
|
TestU32::test_input();
|
|
|
|
|
|
|
|
TestU32::test_add();
|
|
|
|
// TestU32::test_sub(); //Todo: Catch subtraction overflow error in gadget
|
|
|
|
TestU32::test_mul();
|
|
|
|
TestU32::test_div();
|
|
|
|
TestU32::test_pow();
|
2020-06-06 01:34:06 +03:00
|
|
|
|
|
|
|
TestU32::test_eq();
|
|
|
|
TestU32::test_ge();
|
|
|
|
TestU32::test_gt();
|
|
|
|
TestU32::test_le();
|
|
|
|
TestU32::test_gt();
|
|
|
|
|
|
|
|
TestU32::test_assert_eq();
|
|
|
|
TestU32::test_ternary();
|
2020-05-20 03:08:38 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
|
|
|
|
output_zero(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_one() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "one.leo").unwrap();
|
|
|
|
output_one(program);
|
|
|
|
}
|