Skip to content

Commit

Permalink
Merge pull request ethereum#871 from g-r-a-n-t/ctx-tests
Browse files Browse the repository at this point in the history
enable use of `Context`  in tests and misc test refactoring
  • Loading branch information
g-r-a-n-t authored Apr 21, 2023
2 parents 1ecc244 + bab958e commit 7054185
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 189 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/analyzer/tests/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,9 @@ test_analysis! { erc20_token, "demos/erc20_token.fe"}
test_analysis! { guest_book, "demos/guest_book.fe"}
test_analysis! { simple_open_auction, "demos/simple_open_auction.fe"}
test_analysis! { uniswap, "demos/uniswap.fe"}
test_analysis! { address_bytes10_map, "features/address_bytes10_map.fe"}
test_analysis! { abi_decode_complex, "features/abi_decode_complex.fe"}
test_analysis! { assert, "features/assert.fe"}
test_analysis! { associated_fns, "features/associated_fns.fe"}
test_analysis! { aug_assign, "features/aug_assign.fe"}
test_analysis! { base_tuple, "features/base_tuple.fe"}
test_analysis! { call_statement_with_args, "features/call_statement_with_args.fe"}
test_analysis! { call_statement_with_args_2, "features/call_statement_with_args_2.fe"}
test_analysis! { call_statement_without_args, "features/call_statement_without_args.fe"}
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/src/yul/isel/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn lower_test(db: &dyn CodegenDb, test: FunctionId) -> yul::Object {
.into_iter()
.map(yul::Statement::FunctionDefinition)
.collect();
let dep_contracts = context.resolve_contract_dependency(db);
let runtime_funcs: Vec<_> = context
.runtime
.collect_definitions()
Expand All @@ -34,7 +35,7 @@ pub fn lower_test(db: &dyn CodegenDb, test: FunctionId) -> yul::Object {
let object = yul::Object {
name,
code,
objects: vec![],
objects: dep_contracts,
data: dep_constants,
};

Expand Down
1 change: 0 additions & 1 deletion crates/library/std/src/evm.fe
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ pub unsafe fn mload(offset p: u256) -> u256 {
pub unsafe fn mstore(offset p: u256, value v: u256) {
__mstore(p, v)
}

pub unsafe fn mstore8(offset p: u256, value v: u256) {
__mstore8(p, v)
}
Expand Down
3 changes: 0 additions & 3 deletions crates/mir/tests/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ fn mir_lower_std_lib() {
test_lowering! { mir_erc20_token, "demos/erc20_token.fe"}
test_lowering! { mir_guest_book, "demos/guest_book.fe"}
test_lowering! { mir_uniswap, "demos/uniswap.fe"}
test_lowering! { mir_address_bytes10_map, "features/address_bytes10_map.fe"}
test_lowering! { mir_assert, "features/assert.fe"}
test_lowering! { mir_associated_fns, "features/associated_fns.fe"}
test_lowering! { mir_aug_assign, "features/aug_assign.fe"}
test_lowering! { mir_base_tuple, "features/base_tuple.fe"}
test_lowering! { mir_call_statement_with_args, "features/call_statement_with_args.fe"}
test_lowering! { mir_call_statement_with_args_2, "features/call_statement_with_args_2.fe"}
test_lowering! { mir_call_statement_without_args, "features/call_statement_without_args.fe"}
Expand Down
11 changes: 0 additions & 11 deletions crates/test-files/fixtures/features/address_bytes10_map.fe

This file was deleted.

14 changes: 0 additions & 14 deletions crates/test-files/fixtures/features/array_repeat.fe

This file was deleted.

12 changes: 0 additions & 12 deletions crates/test-files/fixtures/features/arrays.fe

This file was deleted.

5 changes: 0 additions & 5 deletions crates/test-files/fixtures/features/base_tuple.fe

This file was deleted.

11 changes: 0 additions & 11 deletions crates/test-files/fixtures/features/call_fn.fe

This file was deleted.

29 changes: 14 additions & 15 deletions crates/test-runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bytes::Bytes;
use colored::Colorize;
use revm::{
interpreter::{instruction_result::SuccessOrHalt, Contract, DummyHost, Interpreter},
primitives::{Bytecode, Env, LatestSpec, B160, U256},
};
use revm::primitives::{AccountInfo, Bytecode, Env, TransactTo, B160, U256};
use std::fmt::Display;

#[derive(Debug, Default)]
Expand Down Expand Up @@ -69,24 +66,26 @@ impl Display for TestSink {
}

pub fn execute(name: &str, bytecode: &str, sink: &mut TestSink) -> bool {
let input = Bytes::new();
let bytecode = Bytecode::new_raw(Bytes::copy_from_slice(&hex::decode(bytecode).unwrap()));
let address = B160::from(26);
let caller = B160::from(42);
let value = U256::ZERO;
let contract = Contract::new::<LatestSpec>(input, bytecode, address, caller, value);

let mut host = DummyHost::new(Env::default());
let mut interpreter = Interpreter::new(contract, u64::MAX, false);
let mut database = revm::InMemoryDB::default();
let test_address = B160::from(42);
let test_info = AccountInfo::new(U256::ZERO, 0, bytecode);
database.insert_account_info(test_address, test_info);

let result = interpreter.run::<DummyHost, LatestSpec>(&mut host);
let reverted = matches!(SuccessOrHalt::from(result), SuccessOrHalt::Success(_));
let mut env = Env::default();
env.tx.transact_to = TransactTo::Call(test_address);

if reverted {
let mut evm = revm::new();
evm.env = env;
evm.database(&mut database);
let result = evm.transact_commit().expect("evm failure");

if result.is_success() {
sink.inc_success_count();
} else {
sink.insert_failure(name, &format!("{result:?}"));
};

reverted
result.is_success()
}
109 changes: 0 additions & 109 deletions crates/tests-legacy/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,37 +234,6 @@ fn test_assert() {
})
}

#[test]
fn test_arrays() {
with_executor(&|mut executor| {
let harness = deploy_contract(&mut executor, "arrays.fe", "Foo", &[]);

harness.test_function(
&mut executor,
"get_from_memory",
&[uint_token(9)],
Some(&uint_token(10)),
);

validate_revert(
harness.capture_call(&mut executor, "get_from_memory", &[uint_token(10)]),
&encoded_panic_out_of_bounds(),
);

harness.test_function(
&mut executor,
"get_from_storage",
&[uint_token(9)],
Some(&uint_token(0)),
);

validate_revert(
harness.capture_call(&mut executor, "get_from_storage", &[uint_token(10)]),
&encoded_panic_out_of_bounds(),
);
})
}

macro_rules! test_method_return {
($name:ident, $path:expr, $input:expr, $expected:expr) => {
#[test]
Expand Down Expand Up @@ -447,7 +416,6 @@ test_method_return! { radix_octal, "radix_octal.fe", &[], uint_token(0o70) }
test_method_return! { radix_binary, "radix_binary.fe", &[], uint_token(0b10) }
test_method_return! { map_tuple, "map_tuple.fe", &[uint_token(1234)], uint_token(1234) }
test_method_return! { int_literal_coercion, "int_literal_coercion.fe", &[], uint_token(300) }
test_method_return! { associated_fns, "associated_fns.fe", &[uint_token(12)], uint_token(144) }
test_method_return! { struct_fns, "struct_fns.fe", &[uint_token(10), uint_token(20)], uint_token(100) }
test_method_return! { cast_address_to_u256, "cast_address_to_u256.fe", &[address_token(SOME_ADDRESS)], address_token(SOME_ADDRESS) }
test_method_return! { for_loop_with_complex_elem_array, "for_loop_with_complex_elem_array.fe", &[], int_token(222) }
Expand Down Expand Up @@ -538,37 +506,6 @@ fn test_map(fixture_file: &str) {
})
}

#[test]
fn address_bytes10_map() {
with_executor(&|mut executor| {
let harness = deploy_contract(&mut executor, "address_bytes10_map.fe", "Foo", &[]);

let address1 = address_token("0000000000000000000000000000000000000001");
let bytes1 = bytes_token("ten bytes1");

let address2 = address_token("0000000000000000000000000000000000000002");
let bytes2 = bytes_token("ten bytes2");

harness.test_function(
&mut executor,
"write_bar",
&[address1.clone(), bytes1.clone()],
None,
);

harness.test_function(
&mut executor,
"write_bar",
&[address2.clone(), bytes2.clone()],
None,
);

harness.test_function(&mut executor, "read_bar", &[address1], Some(&bytes1));
harness.test_function(&mut executor, "read_bar", &[address2], Some(&bytes2));
assert_harness_gas_report!(harness);
})
}

#[test]
fn return_builtin_attributes() {
let gas_price = 123;
Expand Down Expand Up @@ -1757,20 +1694,6 @@ fn aug_assign(target: u64, op: &str, value: u64, expected: u64) {
});
}

#[test]
fn base_tuple() {
with_executor(&|mut executor| {
let harness = deploy_contract(&mut executor, "base_tuple.fe", "Foo", &[]);
harness.test_function(
&mut executor,
"bar",
&[uint_token(42), bool_token(true)],
Some(&tuple_token(&[uint_token(42), bool_token(true)])),
);
assert_harness_gas_report!(harness);
});
}

#[test]
fn tuple_destructuring() {
with_executor(&|mut executor| {
Expand Down Expand Up @@ -2104,23 +2027,6 @@ fn intrinsics() {
});
}

#[test]
fn call_fn() {
with_executor(&|mut executor| {
let harness = deploy_contract(&mut executor, "call_fn.fe", "Foo", &[]);

let mut calldata = vec![0; 32];

calldata[31] = 1;
harness.test_call_reverts(&mut executor, calldata.clone(), &[]);

calldata[31] = 0;
harness.test_call_returns(&mut executor, calldata, &[]);

assert_harness_gas_report!(harness);
});
}

#[rstest(
method,
params,
Expand Down Expand Up @@ -2231,18 +2137,3 @@ fn execution_tests(fixture_file: &str) {
assert_harness_gas_report!(harness, fixture_file);
})
}

#[test]
fn array_repeat() {
with_executor(&|mut executor| {
let harness = deploy_contract(&mut executor, "array_repeat.fe", "Foo", &[]);
harness.test_function(
&mut executor,
"foo",
&[],
Some(&uint_array_token(&[8, 42, 8, 8])),
);

harness.test_function(&mut executor, "bar", &[], None);
});
}
1 change: 1 addition & 0 deletions crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/ethereum/fe"
[dependencies]
fe-test-runner = {path = "../test-runner", version = "^0.22.0"}
fe-driver = {path = "../driver", version = "^0.22.0"}
fe-common = {path = "../common", version = "^0.22.0"}
dir-test = "^0.1"

[features]
Expand Down
43 changes: 43 additions & 0 deletions crates/tests/fixtures/files/address_bytes10_map.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
type Bytes10 = Array<u8, 10>

contract Foo {
bar: Map<address, Bytes10>

pub fn read_bar(self, key: address) -> Bytes10 {
return self.bar[key].to_mem()
}

pub fn write_bar(mut self, key: address, value: Bytes10) {
self.bar[key] = value
}
}

#test
unsafe fn test_foo() {
let mut ctx: Context = Context()
let mut foo: Foo = Foo.create(ctx, 0)

let address1: address = address(0x01)
let address2: address = address(0x02)
let bytes1: Bytes10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let bytes2: Bytes10 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

foo.write_bar(key: address1, value: bytes1)
foo.write_bar(key: address2, value: bytes2)

assert bytes10_eq(foo.read_bar(key: address1), bytes1)
assert bytes10_eq(foo.read_bar(key: address2), bytes2)
}

fn bytes10_eq(_ a: Bytes10, _ b: Bytes10) -> bool {
let mut i: u256 = 0

while i < 10 {
if a[i] != b[i] {
return false
}
i += 1
}

return true
}
19 changes: 19 additions & 0 deletions crates/tests/fixtures/files/array_repeat.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#test
fn foo() {
let mut my_array: Array<u256, 4> = [8; 4]
my_array[1] = 42
assert my_array[0] == 8
assert my_array[1] == 42
assert my_array[2] == 8
assert my_array[3] == 8
}

#test
fn bar() {
let mut my_array: Array<(u256, u256), 2> = [(1, 0); 2]
my_array[0].item0 = 4
assert my_array[0].item0 == 4
assert my_array[0].item1 == 0
assert my_array[1].item0 == 1
assert my_array[2].item1 == 0
}
Loading

0 comments on commit 7054185

Please sign in to comment.