| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::build_solidity;
- use parity_scale_codec::{Decode, Encode};
- use primitive_types::U256;
- #[derive(Debug, Encode, Decode)]
- struct Val256(U256);
- #[test]
- fn assign_suffixes() {
- #[derive(Debug, Encode, Decode)]
- struct CallDataInput {
- pub input: U256,
- pub vec: Vec<u32>,
- }
- let mut runtime = build_solidity(
- r#"
- contract testing {
- struct stru_test {
- string a;
- uint b;
- }
- stru_test t_s_t;
- function test_local_vec(uint256 input) public pure returns (uint256 ret) {
- uint256[] vec;
- vec.push(uint256(4));
- assembly {
- vec := add(input, 7)
- input := 6
- ret := vec
- }
- }
- function test_struct(uint256 input) public pure returns (uint256 ret) {
- stru_test tt = stru_test({a: "tea", b: 5});
- assembly {
- tt := add(input, 7)
- input := 6
- ret := tt
- }
- }
- function test_mem_vec(uint256 input) public pure returns (uint256 ret) {
- uint256[] memory vec;
- vec.push(uint256(4));
- assembly {
- vec := add(input, 7)
- input := 9
- ret := vec
- }
- }
- function test_mem_struct(uint256 input) public pure returns (uint256 ret) {
- stru_test memory tt = stru_test({a: "tea", b: 5});
- assembly {
- tt := add(input, 7)
- input := 9
- ret := tt
- }
- }
- function calldata_vec(uint256 input, uint32[] calldata vec) public view returns (uint256 ret) {
- assembly {
- vec.offset := add(input, 7)
- input := 9
- ret := vec.offset
- }
- }
- function storage_struct(uint256 input) public returns (uint256 ret) {
- stru_test storage local_t = t_s_t;
- assembly {
- local_t.slot := add(input, 7)
- input := 90
- ret := local_t.slot
- }
- }
- }
- "#,
- );
- runtime.function("test_local_vec", Val256(U256::from(7)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(14)).encode());
- runtime.function("test_struct", Val256(U256::from(20)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(27)).encode());
- runtime.function("test_mem_vec", Val256(U256::from(30)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(37)).encode());
- runtime.function("test_mem_struct", Val256(U256::from(8)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(15)).encode());
- runtime.function(
- "calldata_vec",
- CallDataInput {
- input: U256::from(19),
- vec: vec![0, 1, 2, 3, 4],
- }
- .encode(),
- );
- assert_eq!(runtime.output(), Val256(U256::from(26)).encode());
- runtime.function("storage_struct", Val256(U256::from(17)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(24)).encode());
- }
- #[test]
- fn eth_builtins() {
- let mut runtime = build_solidity(
- r#"
- contract testing {
- function test_address() public view returns (uint256 ret) {
- assembly {
- let a := address()
- ret := a
- }
- }
- function test_balance() public view returns (uint256 ret) {
- assembly {
- let a := address()
- ret := balance(a)
- }
- }
- function test_selfbalance() public view returns (uint256 ret) {
- assembly {
- let a := selfbalance()
- ret := a
- }
- }
- function test_caller() public view returns (uint256 ret) {
- assembly {
- let a := caller()
- ret := a
- }
- }
- function test_callvalue() public payable returns (uint256 ret) {
- assembly {
- let a := callvalue()
- ret := a
- }
- }
- }"#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test_address", Vec::new());
- let mut b_vec = runtime.output().to_vec();
- b_vec.reverse();
- assert_eq!(b_vec, runtime.caller().to_vec());
- runtime.function("test_balance", Vec::new());
- assert_eq!(runtime.output()[..16].to_vec(), runtime.balance(0).encode());
- runtime.function("test_selfbalance", Vec::new());
- assert_eq!(runtime.output()[..16].to_vec(), runtime.balance(0).encode());
- runtime.function("test_caller", Vec::new());
- let mut b_vec = runtime.output().to_vec();
- b_vec.reverse();
- assert_eq!(b_vec, runtime.caller().to_vec());
- runtime.set_transferred_value(0xdeadcafe);
- runtime.raw_function(runtime.selector(0, "test_callvalue").to_vec());
- let mut expected = 0xdeadcafeu32.to_le_bytes().to_vec();
- expected.resize(32, 0);
- assert_eq!(runtime.output(), expected);
- }
- #[test]
- fn switch_statement() {
- let mut runtime = build_solidity(
- r#"
- contract Testing {
- function switch_default(uint a) public pure returns (uint b) {
- b = 4;
- assembly {
- switch a
- case 1 {
- b := 5
- }
- case 2 {
- b := 6
- }
- default {
- b := 7
- }
- }
- if (b == 7) {
- b += 2;
- }
- }
- function switch_no_default(uint a) public pure returns (uint b) {
- b = 4;
- assembly {
- switch a
- case 1 {
- b := 5
- }
- case 2 {
- b := 6
- }
- }
- if (b == 5) {
- b -= 2;
- }
- }
- function switch_no_case(uint a) public pure returns (uint b) {
- b = 7;
- assembly {
- switch a
- default {
- b := 5
- }
- }
- if (b == 5) {
- b -= 1;
- }
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("switch_default", Val256(U256::from(1)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(5)).encode());
- runtime.function("switch_default", Val256(U256::from(2)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(6)).encode());
- runtime.function("switch_default", Val256(U256::from(6)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(9)).encode());
- runtime.function("switch_no_default", Val256(U256::from(1)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(3)).encode());
- runtime.function("switch_no_default", Val256(U256::from(2)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(6)).encode());
- runtime.function("switch_no_default", Val256(U256::from(6)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(4)).encode());
- runtime.function("switch_no_case", Val256(U256::from(3)).encode());
- assert_eq!(runtime.output(), Val256(U256::from(4)).encode());
- }
|