Skip to content

Commit

Permalink
Add tests module sui-types/object.rs (#19316)
Browse files Browse the repository at this point in the history
## Description 

Put tests in sui-types/object.rs in their own module
The tests are just floating around in the main module, which presumably
means they are also compiled into our production builds? They should go
into their own module, guarded by a #[cfg(test] annotation.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
siomari authored Sep 12, 2024
1 parent 88c7c85 commit 495def8
Showing 1 changed file with 77 additions and 67 deletions.
144 changes: 77 additions & 67 deletions crates/sui-types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,73 +1204,83 @@ impl Display for PastObjectRead {
}
}

// Ensure that object digest computation and bcs serialized format are not inadvertently changed.
#[test]
fn test_object_digest_and_serialized_format() {
let g = GasCoin::new_for_testing_with_id(ObjectID::ZERO, 123).to_object(OBJECT_START_VERSION);
let o = Object::new_move(
g,
Owner::AddressOwner(SuiAddress::ZERO),
TransactionDigest::ZERO,
);
let bytes = bcs::to_bytes(&o).unwrap();

assert_eq!(
bytes,
[
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
);

let objref = format!("{:?}", o.compute_object_reference());
assert_eq!(objref, "(0x0000000000000000000000000000000000000000000000000000000000000000, SequenceNumber(1), o#59tZq65HVqZjUyNtD7BCGLTD87N5cpayYwEFrtwR4aMz)");
}
#[cfg(test)]
mod tests {
use crate::object::{Object, Owner, OBJECT_START_VERSION};
use crate::{
base_types::{ObjectID, SuiAddress, TransactionDigest},
gas_coin::GasCoin,
};

// Ensure that object digest computation and bcs serialized format are not inadvertently changed.
#[test]
fn test_object_digest_and_serialized_format() {
let g =
GasCoin::new_for_testing_with_id(ObjectID::ZERO, 123).to_object(OBJECT_START_VERSION);
let o = Object::new_move(
g,
Owner::AddressOwner(SuiAddress::ZERO),
TransactionDigest::ZERO,
);
let bytes = bcs::to_bytes(&o).unwrap();

assert_eq!(
bytes,
[
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
);

#[test]
fn test_get_coin_value_unsafe() {
fn test_for_value(v: u64) {
let g = GasCoin::new_for_testing(v).to_object(OBJECT_START_VERSION);
assert_eq!(g.get_coin_value_unsafe(), v);
assert_eq!(GasCoin::try_from(&g).unwrap().value(), v);
}

test_for_value(0);
test_for_value(1);
test_for_value(8);
test_for_value(9);
test_for_value(u8::MAX as u64);
test_for_value(u8::MAX as u64 + 1);
test_for_value(u16::MAX as u64);
test_for_value(u16::MAX as u64 + 1);
test_for_value(u32::MAX as u64);
test_for_value(u32::MAX as u64 + 1);
test_for_value(u64::MAX);
}
let objref = format!("{:?}", o.compute_object_reference());
assert_eq!(objref, "(0x0000000000000000000000000000000000000000000000000000000000000000, SequenceNumber(1), o#59tZq65HVqZjUyNtD7BCGLTD87N5cpayYwEFrtwR4aMz)");
}

#[test]
fn test_set_coin_value_unsafe() {
fn test_for_value(v: u64) {
let mut g = GasCoin::new_for_testing(u64::MAX).to_object(OBJECT_START_VERSION);
g.set_coin_value_unsafe(v);
assert_eq!(g.get_coin_value_unsafe(), v);
assert_eq!(GasCoin::try_from(&g).unwrap().value(), v);
assert_eq!(g.version(), OBJECT_START_VERSION);
assert_eq!(g.contents().len(), 40);
}

test_for_value(0);
test_for_value(1);
test_for_value(8);
test_for_value(9);
test_for_value(u8::MAX as u64);
test_for_value(u8::MAX as u64 + 1);
test_for_value(u16::MAX as u64);
test_for_value(u16::MAX as u64 + 1);
test_for_value(u32::MAX as u64);
test_for_value(u32::MAX as u64 + 1);
test_for_value(u64::MAX);
#[test]
fn test_get_coin_value_unsafe() {
fn test_for_value(v: u64) {
let g = GasCoin::new_for_testing(v).to_object(OBJECT_START_VERSION);
assert_eq!(g.get_coin_value_unsafe(), v);
assert_eq!(GasCoin::try_from(&g).unwrap().value(), v);
}

test_for_value(0);
test_for_value(1);
test_for_value(8);
test_for_value(9);
test_for_value(u8::MAX as u64);
test_for_value(u8::MAX as u64 + 1);
test_for_value(u16::MAX as u64);
test_for_value(u16::MAX as u64 + 1);
test_for_value(u32::MAX as u64);
test_for_value(u32::MAX as u64 + 1);
test_for_value(u64::MAX);
}

#[test]
fn test_set_coin_value_unsafe() {
fn test_for_value(v: u64) {
let mut g = GasCoin::new_for_testing(u64::MAX).to_object(OBJECT_START_VERSION);
g.set_coin_value_unsafe(v);
assert_eq!(g.get_coin_value_unsafe(), v);
assert_eq!(GasCoin::try_from(&g).unwrap().value(), v);
assert_eq!(g.version(), OBJECT_START_VERSION);
assert_eq!(g.contents().len(), 40);
}

test_for_value(0);
test_for_value(1);
test_for_value(8);
test_for_value(9);
test_for_value(u8::MAX as u64);
test_for_value(u8::MAX as u64 + 1);
test_for_value(u16::MAX as u64);
test_for_value(u16::MAX as u64 + 1);
test_for_value(u32::MAX as u64);
test_for_value(u32::MAX as u64 + 1);
test_for_value(u64::MAX);
}
}

0 comments on commit 495def8

Please sign in to comment.