evm-rs/tests/execute.rs
tests/execute.rsRaw
1use evm::*;
2
3#[test]
4fn stack_simple_push_pop() {
5 let mut s = Stack::new();
6 s.push(str_to_u256("1"));
7 s.push(str_to_u256("2"));
8 s.push(str_to_u256("3"));
9 assert_eq!(s.pop(), str_to_u256("3"));
10 assert_eq!(s.pop(), str_to_u256("2"));
11 assert_eq!(s.pop(), str_to_u256("1"));
12 // assert_eq!(s.pop(), error); // TODO expect error as stack is empty
13}
14
15// arithmetic
16#[test]
17fn execute_opcodes_0() {
18 let code = hex::decode("6005600c01").unwrap(); // 5+12
19 let calldata = vec![];
20
21 let mut s = Stack::new();
22 s.execute(&code, &calldata, false);
23 assert_eq!(s.pop(), str_to_u256("17"));
24 assert_eq!(s.gas, 9999999991);
25 assert_eq!(s.pc, 5);
26}
27
28#[test]
29fn execute_opcodes_1() {
30 let code = hex::decode("60056004016000526001601ff3").unwrap();
31 let calldata = vec![];
32
33 let mut s = Stack::new();
34 let out = s.execute(&code, &calldata, false);
35
36 assert_eq!(out[0], 0x09);
37 assert_eq!(s.gas, 9999999976);
38 assert_eq!(s.pc, 12);
39 // assert_eq!(s.pop(), err); // TODO expect error as stack is empty
40}
41
42#[test]
43fn execute_opcodes_2() {
44 let code = hex::decode("61010161010201").unwrap();
45 let calldata = vec![];
46
47 let mut s = Stack::new();
48 s.execute(&code, &calldata, false);
49
50 // assert_eq!(out[0], 0x09);
51 assert_eq!(s.gas, 9999999991);
52 assert_eq!(s.pc, 7);
53 assert_eq!(s.pop(), str_to_u256("515"));
54}
55
56#[test]
57fn execute_opcodes_3() {
58 // contains calldata
59 let code = hex::decode("60003560203501").unwrap();
60 let calldata = hex::decode("00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000004").unwrap();
61
62 let mut s = Stack::new();
63 s.execute(&code, &calldata, false);
64
65 assert_eq!(s.gas, 9999999985);
66 assert_eq!(s.pc, 7);
67 assert_eq!(s.pop(), str_to_u256("9"));
68}
69
70// storage and execution
71#[test]
72fn execute_opcodes_4() {
73 // contains loops
74 let code = hex::decode("6000356000525b600160005103600052600051600657").unwrap();
75 let calldata =
76 hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
77
78 let mut s = Stack::new();
79 s.execute(&code, &calldata, false);
80
81 assert_eq!(s.gas, 9999999795);
82 assert_eq!(s.pc, 22);
83 assert_eq!(s.stack.len(), 0);
84}
85#[test]
86fn execute_opcodes_5() {
87 // contains loops, without using mem
88 let code = hex::decode("6000355b6001900380600357").unwrap();
89 let calldata =
90 hex::decode("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
91
92 let mut s = Stack::new();
93 s.execute(&code, &calldata, false);
94
95 assert_eq!(s.gas, 9999999968);
96 assert_eq!(s.pc, 12);
97
98 let code = hex::decode("6000355b6001900380600357").unwrap();
99 let calldata =
100 hex::decode("0000000000000000000000000000000000000000000000000000000000000002").unwrap();
101
102 let mut s = Stack::new();
103 s.execute(&code, &calldata, false);
104
105 assert_eq!(s.gas, 9999999942);
106 assert_eq!(s.pc, 12);
107
108 let code = hex::decode("6000355b6001900380600357").unwrap();
109 let calldata =
110 hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
111
112 let mut s = Stack::new();
113 s.execute(&code, &calldata, false);
114
115 assert_eq!(s.gas, 9999999864);
116 assert_eq!(s.pc, 12);
117}
118// #[test]
119// fn execute_opcodes_6() {
120// // 0x36: calldata_size
121// let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
122// let calldata = hex::decode("05").unwrap();
123//
124// let mut s = Stack::new();
125// s.execute(&code, &calldata, false);
126//
127// assert_eq!(s.gas, 9999999788);
128// assert_eq!(s.pc, 21);
129// assert_eq!(s.stack.len(), 0);
130// }