-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move independent
run_test
function into nargo core (#2468)
Co-authored-by: Tom French <[email protected]>
- Loading branch information
1 parent
feb8d0e
commit 3e2c868
Showing
3 changed files
with
101 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use acvm::{acir::native_types::WitnessMap, BlackBoxFunctionSolver}; | ||
use noirc_driver::{compile_no_check, CompileOptions}; | ||
use noirc_errors::FileDiagnostic; | ||
use noirc_frontend::hir::{def_map::TestFunction, Context}; | ||
|
||
use super::execute_circuit; | ||
|
||
pub enum TestStatus { | ||
Pass, | ||
Fail { message: String }, | ||
CompileError(FileDiagnostic), | ||
} | ||
|
||
pub fn run_test<B: BlackBoxFunctionSolver + Default>( | ||
backend: &B, | ||
context: &Context, | ||
test_function: TestFunction, | ||
show_output: bool, | ||
config: &CompileOptions, | ||
) -> TestStatus { | ||
let program = compile_no_check(context, config, test_function.get_id()); | ||
match program { | ||
Ok(program) => { | ||
// Run the backend to ensure the PWG evaluates functions like std::hash::pedersen, | ||
// otherwise constraints involving these expressions will not error. | ||
let circuit_execution = | ||
execute_circuit(backend, program.circuit, WitnessMap::new(), show_output); | ||
|
||
if test_function.should_fail() { | ||
match circuit_execution { | ||
Ok(_) => TestStatus::Fail { | ||
// TODO: Improve color variations on this message | ||
message: "error: Test passed when it should have failed".to_string(), | ||
}, | ||
Err(_) => TestStatus::Pass, | ||
} | ||
} else { | ||
match circuit_execution { | ||
Ok(_) => TestStatus::Pass, | ||
Err(error) => TestStatus::Fail { message: error.to_string() }, | ||
} | ||
} | ||
} | ||
// Test function failed to compile | ||
// | ||
// Note: This could be because the compiler was able to deduce | ||
// that a constraint was never satisfiable. | ||
// An example of this is the program `assert(false)` | ||
// In that case, we check if the test function should fail, and if so, we return `TestStatus::Pass`. | ||
Err(diag) => { | ||
// The test has failed compilation, but it should never fail. Report error. | ||
if !test_function.should_fail() { | ||
return TestStatus::CompileError(diag); | ||
} | ||
|
||
// The test has failed compilation, check if it is because the program is never satisfiable. | ||
// If it is never satisfiable, then this is the expected behavior. | ||
let program_is_never_satisfiable = | ||
diag.diagnostic.message.contains("Failed constraint"); | ||
if program_is_never_satisfiable { | ||
return TestStatus::Pass; | ||
} | ||
|
||
// The test has failed compilation, but its a compilation error. Report error | ||
TestStatus::CompileError(diag) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters