Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move] Add do and range_do macros for u* #18187

Merged
merged 10 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix range_do_eq
  • Loading branch information
tnowacki committed Jun 11, 2024
commit 32d46047c3326957274363e8480573986dd95937
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ module std::macros {
let start = $start;
let stop = $stop;
let mut i = start;
while (i <= stop) {
// we check `i >= stop` inside the loop instead of `i <= stop` as `while` consition to avoid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: consition -> condition

// incrementing `i` past the MAX integer value.
// Because of this, we need to check if `start > stop` and return early, instead
// of letting the loop bound handle it, like in the `range_do` macro.
if (start > stop) return;
loop {
$f(i);
if (i >= stop) break;
i = i + 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ module std::integer_tests {
case_succ.range_do_eq!(case, |_| assert!(false));
});

// test upper bound being max
let max_pred = max - 1;
max_pred.range_do_eq!(max, |_| ());

// test iteration numbers
let cases: vector<$T> = vector[3, 5, 8, 11, 14];
cases!(max, cases, |case_pred, case, case_succ| {
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-framework/packages/move-stdlib/tests/u8_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#[test_only]
module std::u8_tests {
use std::integer_tests;
use std::unit_test::assert_eq;

const BIT_SIZE: u8 = 8;
const MAX: u8 = 0xFF;
Expand Down Expand Up @@ -68,6 +69,9 @@ module std::u8_tests {

#[test]
fun test_dos() {
let mut sum = 0u16;
255u8.do_eq!(|i| sum = sum + (i as u16));
assert_eq!(sum, 32640);
integer_tests::test_dos!(MAX, CASES);
}
}
Loading