Skip to content

Commit

Permalink
cargo clippy & cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kenarab committed Jun 5, 2024
1 parent 8cde355 commit a564e68
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
18 changes: 9 additions & 9 deletions crates/concrete_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use concrete_session::{
Session,
};
use config::{Package, Profile};
use core::panic;
use db::Database;
use git2::{IndexAddOption, Repository};
use owo_colors::OwoColorize;
Expand Down Expand Up @@ -605,15 +606,14 @@ pub fn compile(args: &CompilerArgs) -> Result<PathBuf> {

#[allow(unused_variables)]
if args.check {
let linearity_result =
match concrete_check::linearity_check::linearity_check_program(&programs, &session) {
Ok(ir) => ir,
Err(error) => {
//TODO improve reporting
println!("Linearity check failed: {:#?}", error);
std::process::exit(1);
}
};
//let linearity_result =
match concrete_check::linearity_check::linearity_check_program(&programs, &session) {
Ok(ir) => ir,
Err(error) => {
//TODO improve reporting
panic!("Linearity check failed: {:#?}", error);
}
};
}

if args.ir {
Expand Down
50 changes: 28 additions & 22 deletions crates/concrete_driver/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::{
};

use ariadne::Source;
use concrete_ast::Program;
use concrete_driver::linker::{link_binary, link_shared_lib};
use concrete_driver::CompilerArgs;
use concrete_ir::lowering::lower_programs;
use concrete_parser::{error::Diagnostics, ProgramSource};
use concrete_ast::Program;
use concrete_session::{
config::{DebugInfo, OptLevel},
Session,
Expand Down Expand Up @@ -64,7 +64,6 @@ pub fn compile_program(
compile_program_with_args(source, name, library, optlevel, &compile_args)
}


pub fn compile_program_with_args(
source: &str,
name: &str,
Expand Down Expand Up @@ -92,12 +91,12 @@ pub fn compile_program_with_args(
return Err(Box::new(TestError("error compiling".into())));
}
};

let test_dir = tempfile::tempdir()?;
let test_dir_path = test_dir.path();

let input_file = test_dir_path.join(name).with_extension("con");

//Build Vec for programs_for_check before moving program
if args.check {
programs_for_check.push((input_file.clone(), real_source, program.clone()));
Expand Down Expand Up @@ -125,20 +124,22 @@ pub fn compile_program_with_args(
output_asm: false,
file_paths: vec![input_file],
};

// By now only used check for being able to run tests with linearity checking
let mut linearity_errors = Vec::new();
//#[allow(unused_variables)]
if args.check {
let _linearity_result =
match concrete_check::linearity_check::linearity_check_program(&programs_for_check, &session) {
Ok(ir) => ir,
Err(error) => {
//TODO improve reporting
println!("Linearity check failed: {:#?}", error);
linearity_errors.push(error);
}
};
let _linearity_result = match concrete_check::linearity_check::linearity_check_program(
&programs_for_check,
&session,
) {
Ok(ir) => ir,
Err(error) => {
//TODO improve reporting
println!("Linearity check failed: {:#?}", error);
linearity_errors.push(error);
}
};
}

let program_ir = lower_programs(&[program])?;
Expand All @@ -161,20 +162,21 @@ pub fn compile_program_with_args(
if linearity_errors.len() > 0 {
let error = build_test_linearity_error(&linearity_errors[0]);
Err(error)
}
else{
} else {
Ok(CompileResult {
folder: test_dir,
object_file: object_path,
binary_file: session.output_file,
})
}
}
}

pub fn build_test_linearity_error(linearity_error: &concrete_check::linearity_check::errors::LinearityError) -> Box<dyn std::error::Error> {
pub fn build_test_linearity_error(
linearity_error: &concrete_check::linearity_check::errors::LinearityError,
) -> Box<dyn std::error::Error> {
let mut ret = "Linearity check failed<".to_string();
ret.push_str(&linearity_error.to_string());
ret.push_str(">");
ret.push_str(">");
Box::new(TestError(ret.into()))
}

Expand All @@ -193,9 +195,13 @@ pub fn compile_and_run(source: &str, name: &str, library: bool, optlevel: OptLev
}

#[track_caller]
pub fn compile_and_run_with_args(source: &str, name: &str, library: bool, optlevel: OptLevel, args: &CompilerArgs) ->
//Result<Output, std::io::Error> {
Result<Output, Box<dyn std::error::Error>>{
pub fn compile_and_run_with_args(
source: &str,
name: &str,
library: bool,
optlevel: OptLevel,
args: &CompilerArgs,
) -> Result<Output, Box<dyn std::error::Error>> {
let compile_result = compile_program_with_args(source, name, library, optlevel, args);
match compile_result {
//Err(e) => Err(std::error::Error::new(std::io::ErrorKind::Other, e.to_string())),
Expand Down
32 changes: 19 additions & 13 deletions crates/concrete_driver/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use concrete_driver::CompilerArgs;
use concrete_session::config::OptLevel;
use test_case::test_case;


mod common;

#[test_case(include_str!("../../../examples/borrow.con"), "borrow", false, 2 ; "borrow.con")]
Expand Down Expand Up @@ -45,7 +44,6 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) {
);
}


#[test_case(include_str!("../../../examples/linearExample01.con"), "linearity", false, 2; "linearExample01.con")]
#[test_case(include_str!("../../../examples/linearExample02.con"), "linearity", false, 2 ; "linearExample02.con")]
#[test_case(include_str!("../../../examples/linearExample03if.con"), "linearity", false, 0 ; "linearExample03if.con")]
Expand Down Expand Up @@ -82,39 +80,47 @@ fn example_tests_with_args(
name: &str,
is_library: bool,
expected_status_code: i32,
compile_args: CompilerArgs
compile_args: CompilerArgs,
) -> Result<(), Box<dyn std::error::Error>> {

let not_consumed_xy_error = build_test_linearity_error(&LinearityError::VariableNotConsumed {variable: "xy".to_string()});
let not_consumed_xy_error = build_test_linearity_error(&LinearityError::VariableNotConsumed {
variable: "xy".to_string(),
});

//let compile_result = crate::common::compile_program_with_args(source, name, is_library, OptLevel::None, &compile_args);
let result_1 = compile_and_run_with_args(source, name, is_library, OptLevel::None, &compile_args);
let result_1 =
compile_and_run_with_args(source, name, is_library, OptLevel::None, &compile_args);
match result_1 {
Ok(output) => assert_eq!(expected_status_code, output.status.code().unwrap()),
Err(e) => assert_eq!(not_consumed_xy_error.to_string(), e.to_string()),
}
let result_2 = compile_and_run_with_args(source, name, is_library, OptLevel::Less, &compile_args);
let result_2 =
compile_and_run_with_args(source, name, is_library, OptLevel::Less, &compile_args);
match result_2 {
Ok(output) => assert_eq!(expected_status_code, output.status.code().unwrap()),
Err(e) => assert_eq!(not_consumed_xy_error.to_string(), e.to_string()),
}

let result_3 = compile_and_run_with_args(source, name, is_library, OptLevel::Default, &compile_args);

let result_3 =
compile_and_run_with_args(source, name, is_library, OptLevel::Default, &compile_args);
match result_3 {
Ok(output) => assert_eq!(expected_status_code, output.status.code().unwrap()),
Err(e) => assert_eq!(not_consumed_xy_error.to_string(), e.to_string()),
}

let result_4 = compile_and_run_with_args(source, name, is_library, OptLevel::Aggressive, &compile_args);

let result_4 = compile_and_run_with_args(
source,
name,
is_library,
OptLevel::Aggressive,
&compile_args,
);
match result_4 {
Ok(output) => assert_eq!(expected_status_code, output.status.code().unwrap()),
Err(e) => assert_eq!(not_consumed_xy_error.to_string(), e.to_string()),
}
Ok(())
}



#[test_case(include_str!("../../../examples/hello_world_hacky.con"), "hello_world_hacky", false, "Hello World\n" ; "hello_world_hacky.con")]
#[test_case(include_str!("../../../examples/hello_world_array.con"), "hello_world_array", false, "hello world!\n" ; "hello_world_array.con")]
fn example_tests_with_output(source: &str, name: &str, is_library: bool, result: &str) {
Expand Down

0 comments on commit a564e68

Please sign in to comment.