leo/compiler/tests/mod.rs

179 lines
5.1 KiB
Rust
Raw Normal View History

2020-07-30 10:15:14 +03:00
pub mod address;
2020-07-30 10:56:17 +03:00
pub mod array;
pub mod boolean;
2020-07-30 21:11:54 +03:00
pub mod circuits;
pub mod field;
2020-07-30 22:10:33 +03:00
pub mod function;
2020-07-30 22:54:34 +03:00
pub mod group;
2020-07-30 23:01:04 +03:00
pub mod import;
2020-07-31 04:17:55 +03:00
pub mod input_files;
2020-07-31 01:41:03 +03:00
pub mod integers;
2020-07-31 02:27:31 +03:00
pub mod macros;
2020-07-31 02:37:01 +03:00
pub mod mutability;
2020-07-31 03:11:58 +03:00
pub mod statements;
2020-07-31 03:19:10 +03:00
pub mod syntax;
2020-06-02 04:35:43 +03:00
use leo_compiler::{
2020-06-03 02:06:25 +03:00
compiler::Compiler,
errors::CompilerError,
2020-07-08 05:53:37 +03:00
group::targets::edwards_bls12::EdwardsGroupType,
2020-06-02 04:35:43 +03:00
ConstrainedValue,
OutputBytes,
2020-06-02 04:35:43 +03:00
};
use leo_input::types::{IntegerType, U32Type, UnsignedIntegerType};
2020-08-03 06:56:22 +03:00
use leo_typed::{InputValue, MainInput};
2020-06-02 03:52:33 +03:00
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
use std::path::PathBuf;
2020-08-01 07:15:33 +03:00
pub const TEST_OUTPUT_DIRECTORY: &str = "/output/";
const EMPTY_FILE: &str = "";
pub type EdwardsTestCompiler = Compiler<Fq, EdwardsGroupType>;
2020-06-02 03:52:33 +03:00
pub type EdwardsConstrainedValue = ConstrainedValue<Fq, EdwardsGroupType>;
2020-05-30 03:34:31 +03:00
2020-07-31 04:17:55 +03:00
fn new_compiler() -> EdwardsTestCompiler {
let program_name = "test".to_string();
let path = PathBuf::from("/test/src/main.leo");
2020-08-01 07:15:33 +03:00
let output_dir = PathBuf::from(TEST_OUTPUT_DIRECTORY);
2020-07-31 04:17:55 +03:00
2020-08-01 07:15:33 +03:00
EdwardsTestCompiler::new(program_name, path, output_dir)
2020-07-31 04:17:55 +03:00
}
pub(crate) fn parse_program(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(bytes);
compiler.parse_program_from_string(&program_string)?;
2020-07-31 04:17:55 +03:00
Ok(compiler)
}
2020-08-01 05:39:30 +03:00
pub(crate) fn parse_input(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
2020-07-31 04:17:55 +03:00
let mut compiler = new_compiler();
2020-08-01 06:59:50 +03:00
let input_string = String::from_utf8_lossy(bytes);
2020-07-31 04:17:55 +03:00
2020-08-01 06:59:50 +03:00
compiler.parse_input(&input_string, EMPTY_FILE)?;
2020-07-31 04:17:55 +03:00
Ok(compiler)
}
pub(crate) fn parse_state(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let state_string = String::from_utf8_lossy(bytes);
2020-08-01 05:39:30 +03:00
compiler.parse_input(EMPTY_FILE, &state_string)?;
2020-07-31 04:17:55 +03:00
Ok(compiler)
}
2020-08-01 05:39:30 +03:00
pub(crate) fn parse_input_and_state(
input_bytes: &[u8],
2020-07-31 04:17:55 +03:00
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
2020-08-01 05:39:30 +03:00
let input_string = String::from_utf8_lossy(input_bytes);
2020-07-31 04:17:55 +03:00
let state_string = String::from_utf8_lossy(state_bytes);
2020-08-01 05:39:30 +03:00
compiler.parse_input(&input_string, &state_string)?;
2020-07-31 04:17:55 +03:00
Ok(compiler)
}
2020-08-01 05:39:30 +03:00
pub fn parse_program_with_input(
program_bytes: &[u8],
input_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
let program_string = String::from_utf8_lossy(program_bytes);
2020-08-01 06:59:50 +03:00
let input_string = String::from_utf8_lossy(input_bytes);
2020-08-03 05:01:15 +03:00
compiler.parse_input(&input_string, EMPTY_FILE)?;
2020-08-03 03:24:31 +03:00
compiler.parse_program_from_string(&program_string)?;
Ok(compiler)
2020-06-09 04:39:10 +03:00
}
2020-07-31 04:17:55 +03:00
pub fn parse_program_with_state(
program_bytes: &[u8],
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler();
2020-07-31 04:17:55 +03:00
let program_string = String::from_utf8_lossy(program_bytes);
let state_string = String::from_utf8_lossy(state_bytes);
2020-08-03 05:01:15 +03:00
compiler.parse_input(EMPTY_FILE, &state_string)?;
2020-08-03 03:24:31 +03:00
compiler.parse_program_from_string(&program_string)?;
2020-06-09 03:28:09 +03:00
Ok(compiler)
}
2020-08-01 05:39:30 +03:00
pub fn parse_program_with_input_and_state(
2020-07-31 04:17:55 +03:00
program_bytes: &[u8],
2020-08-01 05:39:30 +03:00
input_bytes: &[u8],
2020-07-31 04:17:55 +03:00
state_bytes: &[u8],
) -> Result<EdwardsTestCompiler, CompilerError> {
2020-07-31 03:19:10 +03:00
let mut compiler = new_compiler();
2020-07-31 04:17:55 +03:00
let program_string = String::from_utf8_lossy(program_bytes);
2020-08-01 05:39:30 +03:00
let input_string = String::from_utf8_lossy(input_bytes);
2020-07-31 04:17:55 +03:00
let state_string = String::from_utf8_lossy(state_bytes);
2020-08-03 05:01:15 +03:00
compiler.parse_input(&input_string, &state_string)?;
2020-08-03 03:24:31 +03:00
compiler.parse_program_from_string(&program_string)?;
2020-07-31 03:19:10 +03:00
Ok(compiler)
}
2020-08-01 07:15:33 +03:00
pub(crate) fn get_output(program: EdwardsTestCompiler) -> OutputBytes {
// synthesize the circuit on the test constraint system
let mut cs = TestConstraintSystem::<Fq>::new();
let output = program.generate_constraints_helper(&mut cs).unwrap();
// assert the constraint system is satisfied
assert!(cs.is_satisfied());
output
}
pub(crate) fn assert_satisfied(program: EdwardsTestCompiler) {
2020-08-01 07:15:33 +03:00
let empty_output_bytes = include_bytes!("compiler_output/empty.out");
let res = get_output(program);
// assert that the output is empty
assert_eq!(empty_output_bytes, res.bytes().as_slice());
}
2020-07-30 21:11:54 +03:00
pub(crate) fn expect_compiler_error(program: EdwardsTestCompiler) -> CompilerError {
let mut cs = TestConstraintSystem::<Fq>::new();
program.generate_constraints_helper(&mut cs).unwrap_err()
}
2020-07-30 21:11:54 +03:00
pub(crate) fn expect_synthesis_error(program: EdwardsTestCompiler) {
let mut cs = TestConstraintSystem::<Fq>::new();
let _output = program.generate_constraints_helper(&mut cs).unwrap();
assert!(!cs.is_satisfied());
}
2020-08-01 05:39:30 +03:00
pub(crate) fn generate_main_input(input: Vec<(&str, Option<InputValue>)>) -> MainInput {
let mut main_input = MainInput::new();
2020-08-01 05:39:30 +03:00
for (name, value) in input {
main_input.insert(name.to_string(), value);
}
2020-08-01 05:39:30 +03:00
main_input
}
pub(crate) fn generate_test_input_u32(number: u32) -> Option<InputValue> {
Some(InputValue::Integer(
IntegerType::Unsigned(UnsignedIntegerType::U32Type(U32Type {})),
number.to_string(),
))
}