Skip to content

Commit

Permalink
primitive-traits: add unit tests for Account
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Oct 24, 2024
1 parent 2fba3c0 commit 1622c39
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions crates/primitives-traits/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,50 @@ mod tests {
assert_eq!(decoded, bytecode);
assert!(remainder.is_empty());
}

#[test]
fn test_account_has_bytecode() {
// Account with no bytecode (None)
let acc_no_bytecode = Account { nonce: 1, balance: U256::from(1000), bytecode_hash: None };
assert_eq!(acc_no_bytecode.has_bytecode(), false, "Account should not have bytecode");

// Account with bytecode hash set to KECCAK_EMPTY (should have bytecode)
let acc_empty_bytecode =
Account { nonce: 1, balance: U256::from(1000), bytecode_hash: Some(KECCAK_EMPTY) };
assert_eq!(acc_empty_bytecode.has_bytecode(), true, "Account should have bytecode");

// Account with a non-empty bytecode hash
let acc_with_bytecode = Account {
nonce: 1,
balance: U256::from(1000),
bytecode_hash: Some(B256::from_slice(&[0x11u8; 32])),
};
assert_eq!(acc_with_bytecode.has_bytecode(), true, "Account should have bytecode");
}

#[test]
fn test_account_get_bytecode_hash() {
// Account with no bytecode (should return KECCAK_EMPTY)
let acc_no_bytecode = Account { nonce: 0, balance: U256::ZERO, bytecode_hash: None };
assert_eq!(acc_no_bytecode.get_bytecode_hash(), KECCAK_EMPTY, "Should return KECCAK_EMPTY");

// Account with bytecode hash set to KECCAK_EMPTY
let acc_empty_bytecode =
Account { nonce: 1, balance: U256::from(1000), bytecode_hash: Some(KECCAK_EMPTY) };
assert_eq!(
acc_empty_bytecode.get_bytecode_hash(),
KECCAK_EMPTY,
"Should return KECCAK_EMPTY"
);

// Account with a valid bytecode hash
let bytecode_hash = B256::from_slice(&[0x11u8; 32]);
let acc_with_bytecode =
Account { nonce: 1, balance: U256::from(1000), bytecode_hash: Some(bytecode_hash) };
assert_eq!(
acc_with_bytecode.get_bytecode_hash(),
bytecode_hash,
"Should return the bytecode hash"
);
}
}

0 comments on commit 1622c39

Please sign in to comment.