Skip to content

exact_div: add tests #141939

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

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![feature(duration_constructors)]
#![feature(duration_constructors_lite)]
#![feature(error_generic_member_access)]
#![feature(exact_div)]
#![feature(exact_size_is_empty)]
#![feature(extend_one)]
#![feature(extern_types)]
Expand Down
38 changes: 38 additions & 0 deletions library/coretests/tests/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,5 +683,43 @@ macro_rules! int_module {
assert_eq_const_safe!($T: <$T>::unbounded_shr(17, SHIFT_AMOUNT_OVERFLOW3), 0);
}
}

const EXACT_DIV_SUCCESS_DIVIDEND1: $T = 42;
const EXACT_DIV_SUCCESS_DIVISOR1: $T = 6;
const EXACT_DIV_SUCCESS_QUOTIENT1: $T = 7;
const EXACT_DIV_SUCCESS_DIVIDEND2: $T = 18;
const EXACT_DIV_SUCCESS_DIVISOR2: $T = 3;
const EXACT_DIV_SUCCESS_QUOTIENT2: $T = 6;
const EXACT_DIV_SUCCESS_DIVIDEND3: $T = -91;
const EXACT_DIV_SUCCESS_DIVISOR3: $T = 13;
const EXACT_DIV_SUCCESS_QUOTIENT3: $T = -7;
const EXACT_DIV_SUCCESS_DIVIDEND4: $T = -57;
const EXACT_DIV_SUCCESS_DIVISOR4: $T = -3;
const EXACT_DIV_SUCCESS_QUOTIENT4: $T = 19;

test_runtime_and_compiletime! {
fn test_exact_div() {
// 42 / 6
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), Some(EXACT_DIV_SUCCESS_QUOTIENT1));
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), EXACT_DIV_SUCCESS_QUOTIENT1);

// 18 / 3
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), Some(EXACT_DIV_SUCCESS_QUOTIENT2));
Comment on lines +703 to +707
Copy link
Contributor

Choose a reason for hiding this comment

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

Not to necessarily block anything, but maybe want to hand wrap these so they don't wrap in the UI?

assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), EXACT_DIV_SUCCESS_QUOTIENT2);

// -91 / 13
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND3, EXACT_DIV_SUCCESS_DIVISOR3), Some(EXACT_DIV_SUCCESS_QUOTIENT3));
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND3, EXACT_DIV_SUCCESS_DIVISOR3), EXACT_DIV_SUCCESS_QUOTIENT3);

// -57 / -3
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND4, EXACT_DIV_SUCCESS_DIVISOR4), Some(EXACT_DIV_SUCCESS_QUOTIENT4));
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND4, EXACT_DIV_SUCCESS_DIVISOR4), EXACT_DIV_SUCCESS_QUOTIENT4);

// failures
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(1, 2), None);
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(<$T>::MIN, -1), None);
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(0, 0), None);
}
}
};
}
23 changes: 23 additions & 0 deletions library/coretests/tests/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,28 @@ macro_rules! uint_module {
assert_eq_const_safe!($T: <$T>::unbounded_shr(17, SHIFT_AMOUNT_OVERFLOW3), 0);
}
}

const EXACT_DIV_SUCCESS_DIVIDEND1: $T = 42;
const EXACT_DIV_SUCCESS_DIVISOR1: $T = 6;
const EXACT_DIV_SUCCESS_QUOTIENT1: $T = 7;
const EXACT_DIV_SUCCESS_DIVIDEND2: $T = 18;
const EXACT_DIV_SUCCESS_DIVISOR2: $T = 3;
const EXACT_DIV_SUCCESS_QUOTIENT2: $T = 6;

test_runtime_and_compiletime! {
fn test_exact_div() {
// 42 / 6
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), Some(EXACT_DIV_SUCCESS_QUOTIENT1));
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), EXACT_DIV_SUCCESS_QUOTIENT1);

// 18 / 3
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), Some(EXACT_DIV_SUCCESS_QUOTIENT2));
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), EXACT_DIV_SUCCESS_QUOTIENT2);

// failures
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(1, 2), None);
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(0, 0), None);
Copy link
Member

Choose a reason for hiding this comment

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

I suppose we could test (<$T>::MAX >> 1) + 1 divided by -1 as _ for unsigned integers, but it would just also be None, for the same reason 1, 2 is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what, if anything, you want me to change here.

Copy link
Member

Choose a reason for hiding this comment

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

Nada, just thought process effluvia.

}
}
};
}
Loading