Skip to content

Commit 9984e03

Browse files
committed
test(instructions): add arithmetic operations test
1 parent 4b48b7c commit 9984e03

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

wrt-host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ parking_lot = { version = "0.12", optional = true }
2323
default = ["std"]
2424
std = ["log", "parking_lot", "wrt-error/std", "wrt-types/std", "wrt-intercept/std"]
2525
no_std = ["wrt-error/no_std", "wrt-types/no_std", "wrt-intercept/no_std"]
26+
alloc = ["wrt-error/alloc", "wrt-types/alloc", "wrt-intercept/alloc"]
2627
kani = ["wrt-intercept/kani"]
2728

2829
[lints.rust]

wrt-instructions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ no_std = [
1717
"wrt-error/no_std",
1818
"wrt-types/no_std",
1919
"wrt-runtime/no_std",
20+
"alloc",
2021
]
2122
optimize = [] # Optional feature to bypass some safety checks in performance-critical paths
2223
safety = [] # Additional safety features for ASIL-B compliance
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::{
2+
arithmetic_ops::{ArithmeticContext, ArithmeticOp},
3+
instruction_traits::PureInstruction,
4+
Error, Value,
5+
};
6+
7+
struct SimpleContext {
8+
stack: Vec<Value>,
9+
}
10+
11+
impl SimpleContext {
12+
fn new() -> Self {
13+
Self { stack: Vec::new() }
14+
}
15+
}
16+
17+
impl ArithmeticContext for SimpleContext {
18+
fn push_arithmetic_value(&mut self, value: Value) -> crate::Result<()> {
19+
self.stack.push(value);
20+
Ok(())
21+
}
22+
23+
fn pop_arithmetic_value(&mut self) -> crate::Result<Value> {
24+
self.stack
25+
.pop()
26+
.ok_or_else(|| Error::new(wrt_error::kinds::StackUnderflow))
27+
}
28+
}
29+
30+
#[test]
31+
fn test_i32_add() {
32+
let mut context = SimpleContext::new();
33+
34+
// Test i32.add
35+
context.push_arithmetic_value(Value::I32(2)).unwrap();
36+
context.push_arithmetic_value(Value::I32(3)).unwrap();
37+
ArithmeticOp::I32Add.execute(&mut context).unwrap();
38+
assert_eq!(context.pop_arithmetic_value().unwrap(), Value::I32(5));
39+
}

0 commit comments

Comments
 (0)