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

rename bench to tests

This commit is contained in:
schaeff 2019-09-18 10:47:17 +02:00
parent 3b6c7bddf7
commit 92a9695974
61 changed files with 31 additions and 151 deletions

View file

@ -3,5 +3,5 @@ use zokrates_test::write_tests;
fn main() {
// generate tests
write_tests(Path::new("./tests/bench/"));
write_tests("./tests/tests/");
}

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/add.code",
"entry_point": "./tests/tests/add.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/array_if.code",
"entry_point": "./tests/tests/array_if.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/assert_one.code",
"entry_point": "./tests/tests/assert_one.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/fact_up_to_4.code",
"entry_point": "./tests/tests/fact_up_to_4.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/split.code",
"entry_point": "./tests/tests/split.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/spread_slice.code",
"entry_point": "./tests/tests/spread_slice.code",
"tests": [
{
"input": {

View file

@ -1,121 +0,0 @@
extern crate serde_json;
extern crate zokrates_field;
use std::io;
use zokrates_core::compile::{compile as generic_compile, CompileErrors};
use zokrates_core::ir;
use zokrates_field::field::{Field, FieldPrime};
#[derive(Serialize, Deserialize)]
pub struct Tests {
pub tests: Vec<Test>,
}
#[derive(Serialize, Deserialize)]
pub struct Input {
pub values: Vec<Val>,
}
#[derive(Serialize, Deserialize)]
pub struct Test {
pub input: Input,
pub output: TestResult,
}
pub type TestResult = Result<Output, ir::Error>;
#[derive(PartialEq, Debug)]
pub struct ComparableResult(Result<Vec<FieldPrime>, ir::Error>);
#[derive(Serialize, Deserialize)]
pub struct Output {
values: Vec<Val>,
}
type Val = String;
impl From<ir::ExecutionResult<FieldPrime>> for ComparableResult {
fn from(r: ir::ExecutionResult<FieldPrime>) -> ComparableResult {
ComparableResult(r.map(|v| v.return_values()))
}
}
impl From<TestResult> for ComparableResult {
fn from(r: TestResult) -> ComparableResult {
ComparableResult(r.map(|v| {
v.values
.iter()
.map(|v| FieldPrime::try_from_dec_str(v).unwrap())
.collect()
}))
}
}
pub fn compare(
result: ir::ExecutionResult<FieldPrime>,
expected: TestResult,
) -> Result<(), String> {
// extract outputs from result
let result = ComparableResult::from(result);
// deserialize expected result
let expected = ComparableResult::from(expected);
if result != expected {
return Err(format!(
"Expected {:?} but found {:?}",
expected.0, result.0
));
}
Ok(())
}
pub fn read_file(path: &str) -> String {
use std::fs::File;
use std::io::Read;
let mut file = File::open(format!("./tests/bench/{}", path)).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read the file");
contents
}
pub fn compile(code: &str) -> Result<ir::Prog<FieldPrime>, CompileErrors> {
generic_compile::<FieldPrime, &[u8], &[u8], io::Error>(&mut code.as_bytes(), None, None)
}
macro_rules! zokrates_test {
($($name:ident,)*) => {
$(
#[test]
fn $name() {
use zokrates_field::field::{Field, FieldPrime};
let code_string = $crate::utils::read_file(&format!("./{}.code", stringify!($name)));
let test_string = $crate::utils::read_file(&format!("./{}.json", stringify!($name)));
let bin = $crate::utils::compile(&code_string).unwrap();
let t: $crate::utils::Tests = serde_json::from_str(&test_string).unwrap();
for test in t.tests.into_iter() {
let input = &test.input.values;
let output = bin.execute(&input.iter().map(|v| FieldPrime::try_from_dec_str(v).unwrap()).collect());
let context = format!("
{}
Called with input ({})
", code_string, input.iter().map(|i| format!("{}", i)).collect::<Vec<_>>().join(", "));
match $crate::utils::compare(output, test.output) {
Err(e) => panic!("{}{}", context, e),
Ok(..) => {}
};
}
}
)*
};
}

View file

@ -9,7 +9,7 @@ fn main() {
export_stdlib();
// generate tests
write_tests(Path::new("./tests/bench/"));
write_tests("./tests/tests/");
}
fn export_stdlib() {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/edwardsAdd.code",
"entry_point": "./tests/tests/ecc/edwardsAdd.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/edwardsCompress.code",
"entry_point": "./tests/tests/ecc/edwardsCompress.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/edwardsOnCurve.code",
"entry_point": "./tests/tests/ecc/edwardsOnCurve.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/edwardsOrderCheck.code",
"entry_point": "./tests/tests/ecc/edwardsOrderCheck.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/edwardsScalarMult.code",
"entry_point": "./tests/tests/ecc/edwardsScalarMult.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/ecc/proofOfOwnership.code",
"entry_point": "./tests/tests/ecc/proofOfOwnership.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/pedersen/512bit.code",
"entry_point": "./tests/tests/hashes/pedersen/512bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/pedersen/6bit.code",
"entry_point": "./tests/tests/hashes/pedersen/6bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/sha256/512bit.code",
"entry_point": "./tests/tests/hashes/sha256/512bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/sha256/512bitPacked.code",
"entry_point": "./tests/tests/hashes/sha256/512bitPacked.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/sha256/512bitPadded.code",
"entry_point": "./tests/tests/hashes/sha256/512bitPadded.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/hashes/utils/256bitsDirectionHelper.code",
"entry_point": "./tests/tests/hashes/utils/256bitsDirectionHelper.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/signatures/verifyEddsa.code",
"entry_point": "./tests/tests/signatures/verifyEddsa.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/multiplexer/256bit.code",
"entry_point": "./tests/tests/utils/multiplexer/256bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/multiplexer/2bit.code",
"entry_point": "./tests/tests/utils/multiplexer/2bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/multiplexer/lookup1bit.code",
"entry_point": "./tests/tests/utils/multiplexer/lookup1bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/multiplexer/lookup2bit.code",
"entry_point": "./tests/tests/utils/multiplexer/lookup2bit.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/multiplexer/lookup3bitSigned.code",
"entry_point": "./tests/tests/utils/multiplexer/lookup3bitSigned.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/pack/nonStrictUnpack256.code",
"entry_point": "./tests/tests/utils/pack/nonStrictUnpack256.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/pack/pack128.code",
"entry_point": "./tests/tests/utils/pack/pack128.code",
"tests": [
{
"input": {

View file

@ -1,5 +1,5 @@
{
"entry_point": "./tests/bench/utils/pack/unpack128.code",
"entry_point": "./tests/tests/utils/pack/unpack128.code",
"tests": [
{
"input": {

View file

@ -126,9 +126,10 @@ use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
pub fn write_tests(base: &Path) {
pub fn write_tests(base: &str) {
use glob::glob;
let base = Path::new(&base);
let out_dir = env::var("OUT_DIR").unwrap();
let destination = Path::new(&out_dir).join("tests.rs");
let test_file = File::create(&destination).unwrap();