From f4f2cee5d2bf42add907e2e6b16c1da180b6ee29 Mon Sep 17 00:00:00 2001 From: Moritz Zielke Date: Tue, 3 Mar 2026 09:46:27 +0100 Subject: [PATCH] test: `Felt` constructor edge cases --- .../src/rust_masm_tests/constructors.rs | 56 +++++++++++++++++++ tests/integration/src/rust_masm_tests/mod.rs | 1 + 2 files changed, 57 insertions(+) create mode 100644 tests/integration/src/rust_masm_tests/constructors.rs diff --git a/tests/integration/src/rust_masm_tests/constructors.rs b/tests/integration/src/rust_masm_tests/constructors.rs new file mode 100644 index 000000000..2e4429a55 --- /dev/null +++ b/tests/integration/src/rust_masm_tests/constructors.rs @@ -0,0 +1,56 @@ +use miden_core::{Felt, StarkField}; +use miden_debug::ToMidenRepr; +use midenc_frontend_wasm::WasmTranslationConfig; + +use crate::{CompilerTest, testing::eval_package}; + +#[test] +fn test_felt_construction_edge_cases() { + // Return `0` zero as sentinel value for failure of constructing the Felt. + // This avoids having to deal with panics. + let main_fn = r#"(x: u64) -> Felt { + Felt::new(x).unwrap_or(Felt::new(0u64).unwrap()) + } + "#; + let config = WasmTranslationConfig::default(); + let mut test = CompilerTest::rust_fn_body_with_stdlib_sys( + "felt_constructor_edge_case", + main_fn, + config, + None, + ); + let package = test.compile_package(); + + // (input, expected_out) with `expected_out = None` if Felt construction is expected to fail. + let cases: Vec<(u64, Option)> = Vec::from([ + (1, Some(1)), + (Felt::MODULUS - 1, Some(Felt::MODULUS - 1)), + // failure as these values do not fit into Felt + (Felt::MODULUS, None), + (Felt::MODULUS + 1, None), + (u64::MAX, None), + ]); + + for (input, expected_out) in cases { + let mut args = Vec::::default(); + input.push_to_operand_stack(&mut args); + eval_package::(&package, [], &args, &test.session, |trace| { + let expected_out = match expected_out { + Some(out) => { + assert!( + out != 0, + "don't explicitly expect 0, it is the sentinel value for error" + ); + out + } + None => 0, + }; + + let res: Felt = trace.parse_result().unwrap(); + println!("input: {input}"); + assert_eq!(res.as_int(), expected_out); + Ok(()) + }) + .unwrap(); + } +} diff --git a/tests/integration/src/rust_masm_tests/mod.rs b/tests/integration/src/rust_masm_tests/mod.rs index 649e4b58c..360fc67d2 100644 --- a/tests/integration/src/rust_masm_tests/mod.rs +++ b/tests/integration/src/rust_masm_tests/mod.rs @@ -12,6 +12,7 @@ use crate::testing::eval_package; mod abi_transform; mod apps; +mod constructors; mod debug_source_locations; mod examples; mod instructions;