-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmod.rs
67 lines (55 loc) · 1.93 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::error::{self, ErrorKind};
use super::test_stdlib_code;
use error::Result;
use rstest::rstest;
// code template
static TPL: &str = r#"
use std::int;
fn main(pub lhs: Field, rhs: Field) -> Field {
let lhs_u = int::{inttyp}.new(lhs);
let rhs_u = int::{inttyp}.new(rhs);
let res = lhs_u.{opr}(rhs_u);
return res.inner;
}
"#;
#[rstest]
#[case("Uint8", "add", r#"{"lhs": "2"}"#, r#"{"rhs": "2"}"#, vec!["4"])]
#[case("Uint8", "sub", r#"{"lhs": "2"}"#, r#"{"rhs": "2"}"#, vec!["0"])]
#[case("Uint8", "mul", r#"{"lhs": "2"}"#, r#"{"rhs": "2"}"#, vec!["4"])]
#[case("Uint8", "div", r#"{"lhs": "5"}"#, r#"{"rhs": "3"}"#, vec!["1"])]
#[case("Uint8", "mod", r#"{"lhs": "5"}"#, r#"{"rhs": "3"}"#, vec!["2"])]
fn test_uint_ops(
#[case] int_type: &str,
#[case] operation: &str,
#[case] public_inputs: &str,
#[case] private_inputs: &str,
#[case] expected_output: Vec<&str>,
) -> Result<()> {
// Replace placeholders with the given integer type.
let code = TPL
.replace("{inttyp}", int_type)
.replace("{opr}", operation);
// Call the test function with the given inputs and expected output.
test_stdlib_code(&code, None, public_inputs, private_inputs, expected_output)?;
Ok(())
}
/// test overflow after operation
#[rstest]
#[case("Uint8", "add", r#"{"lhs": "255"}"#, r#"{"rhs": "1"}"#)]
#[case("Uint8", "sub", r#"{"lhs": "0"}"#, r#"{"rhs": "1"}"#)]
#[case("Uint8", "mul", r#"{"lhs": "255"}"#, r#"{"rhs": "2"}"#)]
fn test_uint_overflow(
#[case] int_type: &str,
#[case] operation: &str,
#[case] public_inputs: &str,
#[case] private_inputs: &str,
) -> Result<()> {
let code = TPL
.replace("{inttyp}", int_type)
.replace("{opr}", operation);
let err = test_stdlib_code(&code, None, public_inputs, private_inputs, vec![])
.err()
.expect("expected overflow error");
assert!(matches!(err.kind, ErrorKind::InvalidWitness(..)));
Ok(())
}