Skip to content

Commit

Permalink
Merge pull request #178 from near/viktar/testing_tests
Browse files Browse the repository at this point in the history
ci: check wat correctness
  • Loading branch information
MCJOHN974 authored Jan 5, 2024
2 parents ca441cc + ac12fe7 commit c41cd01
Show file tree
Hide file tree
Showing 796 changed files with 1,668 additions and 1,618 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cranelift/filetests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ cranelift.workspace = true
env_logger = { workspace = true }
expect-test = { workspace = true }
smallvec = { workspace = true }
regex = { workspace = true }
regex = { workspace = true }
wasmtime = { workspace = true, features = ["cranelift"] }
walkdir = { workspace = true }
52 changes: 49 additions & 3 deletions cranelift/filetests/src/test_zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod tests {
use cranelift_codegen::{settings, FinalizedMachReloc, FinalizedRelocTarget};
use cranelift_wasm::{translate_module, ZkasmEnvironment};

use walkdir::WalkDir;
use wasmtime::*;

fn setup() {
let _ = env_logger::builder().is_test(true).try_init();
}
Expand Down Expand Up @@ -237,6 +240,48 @@ mod tests {
program.join("\n")
}

fn run_wat_file(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let engine = Engine::default();
let binary = wat::parse_file(path)?;
let module = Module::new(&engine, &binary)?;
let mut store = Store::new(&engine, ());
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("env", "assert_eq_i32", |a: i32, b: i32| {
assert_eq!(a, b);
})?;
linker.func_wrap("env", "assert_eq_i64", |a: i64, b: i64| {
assert_eq!(a, b);
})?;
linker.instantiate(&mut store, &module)?;
Ok(())
}

#[test]
fn run_wat() -> Result<(), Box<dyn std::error::Error>> {
for entry in WalkDir::new("../zkasm_data")
.into_iter()
.map(|e| e.unwrap())
{
if entry.path().extension().map_or(false, |ext| ext == "wat") {
let result = run_wat_file(entry.path());
if entry.path().to_str().unwrap().contains("_should_fail_") {
if result.is_ok() {
panic!(
"Should fail file {} don't actually fail",
entry.path().to_str().unwrap()
);
}
} else {
if result.is_err() {
println!("Err = {:#?}", result);
panic!("Invalid wasm in {}", entry.path().to_str().unwrap());
}
}
}
}
Ok(())
}

fn test_module(name: &str) {
let module_binary = wat::parse_file(format!("../zkasm_data/{name}.wat")).unwrap();
let program = generate_zkasm(&module_binary);
Expand Down Expand Up @@ -276,7 +321,7 @@ mod tests {
let count = test_counters.entry(function_name.to_string()).or_insert(0);
*count += 1;
let mut testcase = String::new();
testcase.push_str(&format!("(module\n (import \"env\" \"assert_eq\" (func $assert_eq (param {}) (param {})))\n (func $main\n", assert_type, assert_type));
testcase.push_str(&format!("(module\n (import \"env\" \"assert_eq_{}\" (func $assert_eq_{} (param {}) (param {})))\n (func $main\n", assert_type, assert_type, assert_type, assert_type));
testcase.push_str(&format!(
"\t{}\n",
arguments
Expand All @@ -296,8 +341,9 @@ mod tests {
testcase.push_str(&format!("\t{function_type}.{}\n", function_name));
}
testcase.push_str(&format!(
"\t{}\n\tcall $assert_eq)\n (start $main))\n",
expected_result.trim()
"\t{}\n\tcall $assert_eq_{})\n (start $main))\n",
expected_result.trim(),
assert_type
));
let file_name = format!(
"../../zkasm_data/spectest/{name}/{}_{}.wat",
Expand Down
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/_should_fail_unreachable.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
unreachable
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/add.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 999999999
i32.const 1000000000
i32.add
i32.const 1999999999
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/add_func.wat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 2
i32.const 3
call $add
i32.const 5
call $assert_eq)
call $assert_eq_i32)
(func $add (param $lhs i32) (param $rhs i32) (result i32)
(i32.add
(local.get $lhs)
Expand Down
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/and.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 1
i32.const 0
i32.and
i32.const 0
call $assert_eq)
call $assert_eq_i32)
(start $main))
2 changes: 1 addition & 1 deletion cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(module
(type (;0;) (func (param i64 i64)))
(type (;1;) (func))
(import "env" "assert_eq" (func (;0;) (type 0)))
(import "env" "assert_eq_i64" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i64 i64 i32)
i64.const 1
Expand Down
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/benchmarks/fibonacci/handwritten_wat.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
(local $counter i32)
(local $fp i64)
Expand Down Expand Up @@ -28,5 +28,5 @@
)
(local.get $fp)
(i64.const -2872092127636481573)
call $assert_eq)
call $assert_eq_i64)
(start $main))
2 changes: 1 addition & 1 deletion cranelift/zkasm_data/benchmarks/sha256/from_rust.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(type (;1;) (func))
(type (;2;) (func (param i32 i32 i32)))
(type (;3;) (func (param i32 i32 i32) (result i32)))
(import "env" "assert_eq" (func (;0;) (type 0)))
(import "env" "assert_eq_i64" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32 i32 i32 i32 i32 i32 i32 i32 i64 i64 i64 i32 i32)
global.get 0
Expand Down
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/counter.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
(local $counter i32)
(local.set $counter (i32.const 0))
Expand All @@ -20,5 +20,5 @@
)
(local.get $counter)
(i32.const 10)
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/div.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 5
i32.const 2
i32.div_s
i32.const 2
call $assert_eq)
call $assert_eq_i32)
(start $main))
6 changes: 3 additions & 3 deletions cranelift/zkasm_data/eqz.wat
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 5
i32.eqz
i32.const 0
call $assert_eq
call $assert_eq_i32
i32.const 0
i32.eqz
i32.const 1
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/global.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(global $a i32 (i32.const -2))
(global $b (mut i32) (i32.const 5))
(func $main
Expand All @@ -8,5 +8,5 @@
(global.get $b)
i32.add
i32.const 1
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i32_add_overflows.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 4294967295
i32.const 1
i32.add
i32.const 0
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i32_const.wat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 4
i32.const 4
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i32_mul_overflows.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 2147483648
i32.const 2
i32.mul
i32.const 0
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i64_const.wat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
i64.const 9223372036854775807
i64.const 9223372036854775807
call $assert_eq)
call $assert_eq_i64)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i64_div.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
i64.const 214748364
i64.const 107374183
Expand All @@ -9,5 +9,5 @@
i64.const 214748364
i64.div_s
i64.const 214748366
call $assert_eq)
call $assert_eq_i64)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i64_mul.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
i64.const 214748364
i64.const 107374183
Expand All @@ -11,5 +11,5 @@
i64.const 2
i64.mul
i64.mul
call $assert_eq)
call $assert_eq_i64)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/i64_mul_overflows.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
i64.const 2
i64.const 9223372036854775808
i64.mul
i64.const 0
call $assert_eq)
call $assert_eq_i64)
(start $main))
6 changes: 3 additions & 3 deletions cranelift/zkasm_data/i64_rem.wat
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(import "env" "assert_eq_i64" (func $assert_eq_i64 (param i64) (param i64)))
(func $main
i64.const 5
i64.const 2
i64.rem_s
i64.const 1
call $assert_eq
call $assert_eq_i64
i64.const 8
i64.const 3
i64.rem_s
i64.const 2
call $assert_eq)
call $assert_eq_i64)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/locals.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
(local $x i32)
(local $y i32)
Expand All @@ -11,5 +11,5 @@
(local.set $z (i32.add))
(local.get $z)
(i32.const 5)
call $assert_eq)
call $assert_eq_i32)
(start $main))
4 changes: 2 additions & 2 deletions cranelift/zkasm_data/locals_simple.wat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
(local $x i32)
(local.set $x (i32.const 2))
(local.get $x)
(i32.const 2)
call $assert_eq)
call $assert_eq_i32)
(start $main))
6 changes: 3 additions & 3 deletions cranelift/zkasm_data/lt_s.wat
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
;; 1 - true, 0 false
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32) (param i32)))
(func $main
i32.const 2
i32.const 3
i32.lt_s
i32.const 1
call $assert_eq
call $assert_eq_i32

i32.const 3
i32.const 2
i32.lt_s
i32.const 0
call $assert_eq)
call $assert_eq_i32)
(start $main))
Loading

0 comments on commit c41cd01

Please sign in to comment.