Skip to content

Commit

Permalink
[aptos-stdlib] fix comparator tests (#5694)
Browse files Browse the repository at this point in the history
* fix comparator tests

* fix
  • Loading branch information
pause125 authored Dec 11, 2022
1 parent 7c09e05 commit 47b8c2f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions aptos-move/framework/aptos-stdlib/sources/comparator.move
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module aptos_std::comparator {
}

// Performs a comparison of two types after BCS serialization.
// BCS uses little endian encoding for all integer types,
// so comparison between primitive integer types will not behave as expected.
// For example, 1(0x1) will be larger than 256(0x100) after BCS serialization.
public fun compare<T>(left: &T, right: &T): Result {
let left_bytes = bcs::to_bytes(left);
let right_bytes = bcs::to_bytes(right);
Expand Down Expand Up @@ -81,6 +84,20 @@ module aptos_std::comparator {
assert!(is_greater_than(&compare(&value2, &value1)), 8);
}

#[test]
#[expected_failure]
public fun test_integer() {
// 1(0x1) will be larger than 256(0x100) after BCS serialization.
let value0: u128 = 1;
let value1: u128 = 256;

assert!(is_equal(&compare(&value0, &value0)), 0);
assert!(is_equal(&compare(&value1, &value1)), 1);

assert!(is_smaller_than(&compare(&value0, &value1)), 2);
assert!(is_greater_than(&compare(&value1, &value0)), 3);
}

#[test]
public fun test_u128() {
let value0: u128 = 5;
Expand Down

0 comments on commit 47b8c2f

Please sign in to comment.