1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

add optional field, use it on one test

This commit is contained in:
schaeff 2020-03-02 12:00:31 +01:00
parent fd8ca86ae0
commit 91c0b6e8e5
4 changed files with 30 additions and 14 deletions

2
Cargo.lock generated
View file

@ -1832,7 +1832,7 @@ dependencies = [
[[package]] [[package]]
name = "zokrates_test" name = "zokrates_test"
version = "0.1.1" version = "0.1.0"
dependencies = [ dependencies = [
"fs_extra", "fs_extra",
"glob 0.3.0", "glob 0.3.0",

View file

@ -1,5 +1,6 @@
{ {
"entry_point": "./tests/tests/spread_slice.zok", "entry_point": "./tests/tests/spread_slice.zok",
"max_constraint_count": 9,
"tests": [ "tests": [
{ {
"input": { "input": {

View file

@ -1,6 +1,6 @@
[package] [package]
name = "zokrates_test" name = "zokrates_test"
version = "0.1.1" version = "0.1.0"
authors = ["schaeff <thibaut@schaeff.fr>"] authors = ["schaeff <thibaut@schaeff.fr>"]
edition = "2018" edition = "2018"

View file

@ -8,6 +8,7 @@ use zokrates_field::field::{Field, FieldPrime};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Tests { struct Tests {
pub entry_point: PathBuf, pub entry_point: PathBuf,
pub max_constraint_count: Option<usize>,
pub tests: Vec<Test>, pub tests: Vec<Test>,
} }
@ -34,6 +35,18 @@ struct Output {
type Val = String; type Val = String;
fn parse_val<T: Field>(s: String) -> T {
let s = if s.starts_with("0x") {
u32::from_str_radix(s.trim_start_matches("0x"), 16)
.unwrap()
.to_string()
} else {
s
};
T::try_from_dec_str(&s).unwrap()
}
impl From<ir::ExecutionResult<FieldPrime>> for ComparableResult { impl From<ir::ExecutionResult<FieldPrime>> for ComparableResult {
fn from(r: ir::ExecutionResult<FieldPrime>) -> ComparableResult { fn from(r: ir::ExecutionResult<FieldPrime>) -> ComparableResult {
ComparableResult(r.map(|v| v.return_values())) ComparableResult(r.map(|v| v.return_values()))
@ -42,12 +55,7 @@ impl From<ir::ExecutionResult<FieldPrime>> for ComparableResult {
impl From<TestResult> for ComparableResult { impl From<TestResult> for ComparableResult {
fn from(r: TestResult) -> ComparableResult { fn from(r: TestResult) -> ComparableResult {
ComparableResult(r.map(|v| { ComparableResult(r.map(|v| v.values.into_iter().map(parse_val).collect()))
v.values
.iter()
.map(|v| FieldPrime::try_from_dec_str(v).unwrap())
.collect()
}))
} }
} }
@ -81,14 +89,21 @@ pub fn test_inner(test_path: &str) {
let bin = artifacts.prog(); let bin = artifacts.prog();
match t.max_constraint_count {
Some(count) => assert!(
bin.constraint_count() <= count,
"Expected at the most {} constraints, found {}:\n{}",
count,
bin.constraint_count(),
bin
),
_ => {}
};
for test in t.tests.into_iter() { for test in t.tests.into_iter() {
let input = &test.input.values; let input = &test.input.values;
let output = bin.execute(
&input let output = bin.execute(&(input.iter().cloned().map(parse_val).collect()));
.iter()
.map(|v| FieldPrime::try_from_dec_str(&v.clone()).unwrap())
.collect(),
);
match compare(output, test.output) { match compare(output, test.output) {
Err(e) => { Err(e) => {