mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
Auto merge of #132053 - matthiaskrgr:rollup-u5ds6i3, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #131707 (Run most `core::num` tests in const context too) - #132002 (abi/compatibility: also test Option-like types) - #132026 (analyse: remove unused uncanonicalized field) - #132031 (Optimize `Rc<T>::default`) - #132040 (relnotes: fix stabilizations of `assume_init`) r? `@ghost` `@rustbot` modify labels: rollup
This commit is contained in:
commit
9abfcb4900
@ -68,15 +68,15 @@ Stabilized APIs
|
||||
- [`impl Default for std::collections::vec_deque::Iter`](https://doc.rust-lang.org/nightly/std/collections/vec_deque/struct.Iter.html#impl-Default-for-Iter%3C'_,+T%3E)
|
||||
- [`impl Default for std::collections::vec_deque::IterMut`](https://doc.rust-lang.org/nightly/std/collections/vec_deque/struct.IterMut.html#impl-Default-for-IterMut%3C'_,+T%3E)
|
||||
- [`Rc<T>::new_uninit`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.new_uninit)
|
||||
- [`Rc<T>::assume_init`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.assume_init)
|
||||
- [`Rc<MaybeUninit<T>>::assume_init`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.assume_init)
|
||||
- [`Rc<[T]>::new_uninit_slice`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.new_uninit_slice)
|
||||
- [`Rc<[MaybeUninit<T>]>::assume_init`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.assume_init-1)
|
||||
- [`Arc<T>::new_uninit`](https://doc.rust-lang.org/nightly/std/sync/struct.Arc.html#method.new_uninit)
|
||||
- [`Arc<T>::assume_init`](https://doc.rust-lang.org/nightly/std/sync/struct.Arc.html#method.assume_init)
|
||||
- [`Arc<MaybeUninit<T>>::assume_init`](https://doc.rust-lang.org/nightly/std/sync/struct.Arc.html#method.assume_init)
|
||||
- [`Arc<[T]>::new_uninit_slice`](https://doc.rust-lang.org/nightly/std/sync/struct.Arc.html#method.new_uninit_slice)
|
||||
- [`Arc<[MaybeUninit<T>]>::assume_init`](https://doc.rust-lang.org/nightly/std/sync/struct.Arc.html#method.assume_init-1)
|
||||
- [`Box<T>::new_uninit`](https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html#method.new_uninit)
|
||||
- [`Box<T>::assume_init`](https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html#method.assume_init)
|
||||
- [`Box<MaybeUninit<T>>::assume_init`](https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html#method.assume_init)
|
||||
- [`Box<[T]>::new_uninit_slice`](https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html#method.new_uninit_slice)
|
||||
- [`Box<[MaybeUninit<T>]>::assume_init`](https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html#method.assume_init-1)
|
||||
- [`core::arch::x86_64::_bextri_u64`](https://doc.rust-lang.org/stable/core/arch/x86_64/fn._bextri_u64.html)
|
||||
|
@ -170,7 +170,7 @@ where
|
||||
//
|
||||
// We don't do so for `NormalizesTo` goals as we erased the expected term and
|
||||
// bailing with overflow here would prevent us from detecting a type-mismatch,
|
||||
// causing a coherence error in diesel, see #131969. We still bail with verflow
|
||||
// causing a coherence error in diesel, see #131969. We still bail with overflow
|
||||
// when later returning from the parent AliasRelate goal.
|
||||
if !self.is_normalizes_to_goal {
|
||||
let num_non_region_vars =
|
||||
|
@ -291,7 +291,7 @@ where
|
||||
search_graph,
|
||||
nested_goals: NestedGoals::new(),
|
||||
tainted: Ok(()),
|
||||
inspect: canonical_goal_evaluation.new_goal_evaluation_step(var_values, input),
|
||||
inspect: canonical_goal_evaluation.new_goal_evaluation_step(var_values),
|
||||
};
|
||||
|
||||
for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
|
||||
|
@ -13,7 +13,7 @@ use rustc_type_ir::{self as ty, Interner};
|
||||
use crate::delegate::SolverDelegate;
|
||||
use crate::solve::eval_ctxt::canonical;
|
||||
use crate::solve::{
|
||||
CanonicalInput, Certainty, GenerateProofTree, Goal, GoalEvaluationKind, GoalSource, QueryInput,
|
||||
CanonicalInput, Certainty, GenerateProofTree, Goal, GoalEvaluationKind, GoalSource,
|
||||
QueryResult, inspect,
|
||||
};
|
||||
|
||||
@ -119,6 +119,9 @@ impl<I: Interner> WipCanonicalGoalEvaluation<I> {
|
||||
}
|
||||
}
|
||||
|
||||
/// This only exists during proof tree building and does not have
|
||||
/// a corresponding struct in `inspect`. We need this to track a
|
||||
/// bunch of metadata about the current evaluation.
|
||||
#[derive_where(PartialEq, Eq, Debug; I: Interner)]
|
||||
struct WipCanonicalGoalEvaluationStep<I: Interner> {
|
||||
/// Unlike `EvalCtxt::var_values`, we append a new
|
||||
@ -128,7 +131,6 @@ struct WipCanonicalGoalEvaluationStep<I: Interner> {
|
||||
/// This is necessary as we otherwise don't unify these
|
||||
/// vars when instantiating multiple `CanonicalState`.
|
||||
var_values: Vec<I::GenericArg>,
|
||||
instantiated_goal: QueryInput<I, I::Predicate>,
|
||||
probe_depth: usize,
|
||||
evaluation: WipProbe<I>,
|
||||
}
|
||||
@ -145,16 +147,12 @@ impl<I: Interner> WipCanonicalGoalEvaluationStep<I> {
|
||||
current
|
||||
}
|
||||
|
||||
fn finalize(self) -> inspect::CanonicalGoalEvaluationStep<I> {
|
||||
fn finalize(self) -> inspect::Probe<I> {
|
||||
let evaluation = self.evaluation.finalize();
|
||||
match evaluation.kind {
|
||||
inspect::ProbeKind::Root { .. } => (),
|
||||
inspect::ProbeKind::Root { .. } => evaluation,
|
||||
_ => unreachable!("unexpected root evaluation: {evaluation:?}"),
|
||||
}
|
||||
inspect::CanonicalGoalEvaluationStep {
|
||||
instantiated_goal: self.instantiated_goal,
|
||||
evaluation,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -328,11 +326,9 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> ProofTreeBuilder<D> {
|
||||
pub(crate) fn new_goal_evaluation_step(
|
||||
&mut self,
|
||||
var_values: ty::CanonicalVarValues<I>,
|
||||
instantiated_goal: QueryInput<I, I::Predicate>,
|
||||
) -> ProofTreeBuilder<D> {
|
||||
self.nested(|| WipCanonicalGoalEvaluationStep {
|
||||
var_values: var_values.var_values.to_vec(),
|
||||
instantiated_goal,
|
||||
evaluation: WipProbe {
|
||||
initial_num_var_values: var_values.len(),
|
||||
steps: vec![],
|
||||
|
@ -344,7 +344,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
|
||||
};
|
||||
|
||||
let mut nested_goals = vec![];
|
||||
self.candidates_recur(&mut candidates, &mut nested_goals, &last_eval_step.evaluation);
|
||||
self.candidates_recur(&mut candidates, &mut nested_goals, &last_eval_step);
|
||||
|
||||
candidates
|
||||
}
|
||||
|
@ -23,9 +23,7 @@ use std::hash::Hash;
|
||||
use derive_where::derive_where;
|
||||
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
|
||||
|
||||
use crate::solve::{
|
||||
CandidateSource, CanonicalInput, Certainty, Goal, GoalSource, QueryInput, QueryResult,
|
||||
};
|
||||
use crate::solve::{CandidateSource, CanonicalInput, Certainty, Goal, GoalSource, QueryResult};
|
||||
use crate::{Canonical, CanonicalVarValues, Interner};
|
||||
|
||||
/// Some `data` together with information about how they relate to the input
|
||||
@ -69,15 +67,10 @@ pub struct CanonicalGoalEvaluation<I: Interner> {
|
||||
#[derive_where(PartialEq, Eq, Hash, Debug; I: Interner)]
|
||||
pub enum CanonicalGoalEvaluationKind<I: Interner> {
|
||||
Overflow,
|
||||
Evaluation { final_revision: CanonicalGoalEvaluationStep<I> },
|
||||
}
|
||||
|
||||
#[derive_where(PartialEq, Eq, Hash, Debug; I: Interner)]
|
||||
pub struct CanonicalGoalEvaluationStep<I: Interner> {
|
||||
pub instantiated_goal: QueryInput<I, I::Predicate>,
|
||||
|
||||
/// The actual evaluation of the goal, always `ProbeKind::Root`.
|
||||
pub evaluation: Probe<I>,
|
||||
Evaluation {
|
||||
/// This is always `ProbeKind::Root`.
|
||||
final_revision: Probe<I>,
|
||||
},
|
||||
}
|
||||
|
||||
/// A self-contained computation during trait solving. This either
|
||||
|
@ -2312,7 +2312,16 @@ impl<T: Default> Default for Rc<T> {
|
||||
/// ```
|
||||
#[inline]
|
||||
fn default() -> Rc<T> {
|
||||
Rc::new(Default::default())
|
||||
unsafe {
|
||||
Self::from_inner(
|
||||
Box::leak(Box::write(Box::new_uninit(), RcInner {
|
||||
strong: Cell::new(1),
|
||||
weak: Cell::new(1),
|
||||
value: T::default(),
|
||||
}))
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3447,13 +3447,16 @@ impl<T: Default> Default for Arc<T> {
|
||||
/// assert_eq!(*x, 0);
|
||||
/// ```
|
||||
fn default() -> Arc<T> {
|
||||
let x = Box::into_raw(Box::write(Box::new_uninit(), ArcInner {
|
||||
strong: atomic::AtomicUsize::new(1),
|
||||
weak: atomic::AtomicUsize::new(1),
|
||||
data: T::default(),
|
||||
}));
|
||||
// SAFETY: `Box::into_raw` consumes the `Box` and never returns null
|
||||
unsafe { Self::from_inner(NonNull::new_unchecked(x)) }
|
||||
unsafe {
|
||||
Self::from_inner(
|
||||
Box::leak(Box::write(Box::new_uninit(), ArcInner {
|
||||
strong: atomic::AtomicUsize::new(1),
|
||||
weak: atomic::AtomicUsize::new(1),
|
||||
data: T::default(),
|
||||
}))
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,11 +17,14 @@
|
||||
#![feature(clone_to_uninit)]
|
||||
#![feature(const_align_of_val_raw)]
|
||||
#![feature(const_align_offset)]
|
||||
#![feature(const_bigint_helper_methods)]
|
||||
#![feature(const_black_box)]
|
||||
#![feature(const_eval_select)]
|
||||
#![feature(const_hash)]
|
||||
#![feature(const_heap)]
|
||||
#![feature(const_likely)]
|
||||
#![feature(const_nonnull_new)]
|
||||
#![feature(const_num_midpoint)]
|
||||
#![feature(const_option_ext)]
|
||||
#![feature(const_pin_2)]
|
||||
#![feature(const_pointer_is_aligned)]
|
||||
@ -46,6 +49,7 @@
|
||||
#![feature(get_many_mut)]
|
||||
#![feature(hasher_prefixfree_extras)]
|
||||
#![feature(hashmap_internals)]
|
||||
#![feature(inline_const_pat)]
|
||||
#![feature(int_roundings)]
|
||||
#![feature(ip)]
|
||||
#![feature(ip_from)]
|
||||
@ -104,6 +108,37 @@
|
||||
#![deny(fuzzy_provenance_casts)]
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
/// Version of `assert_matches` that ignores fancy runtime printing in const context and uses structural equality.
|
||||
macro_rules! assert_eq_const_safe {
|
||||
($left:expr, $right:expr$(, $($arg:tt)+)?) => {
|
||||
{
|
||||
fn runtime() {
|
||||
assert_eq!($left, $right, $($arg)*);
|
||||
}
|
||||
const fn compiletime() {
|
||||
assert!(matches!($left, const { $right }));
|
||||
}
|
||||
core::intrinsics::const_eval_select((), compiletime, runtime)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Creates a test for runtime and a test for constant-time.
|
||||
macro_rules! test_runtime_and_compiletime {
|
||||
($(
|
||||
$(#[$attr:meta])*
|
||||
fn $test:ident() $block:block
|
||||
)*) => {
|
||||
$(
|
||||
$(#[$attr])*
|
||||
#[test]
|
||||
fn $test() $block
|
||||
$(#[$attr])*
|
||||
const _: () = $block;
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
mod alloc;
|
||||
mod any;
|
||||
mod array;
|
||||
|
@ -17,42 +17,6 @@ macro_rules! int_module {
|
||||
num::test_num(10 as $T, 2 as $T);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rem_euclid() {
|
||||
assert_eq!((-1 as $T).rem_euclid(MIN), MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_abs() {
|
||||
assert_eq!((1 as $T).abs(), 1 as $T);
|
||||
assert_eq!((0 as $T).abs(), 0 as $T);
|
||||
assert_eq!((-1 as $T).abs(), 1 as $T);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signum() {
|
||||
assert_eq!((1 as $T).signum(), 1 as $T);
|
||||
assert_eq!((0 as $T).signum(), 0 as $T);
|
||||
assert_eq!((-0 as $T).signum(), 0 as $T);
|
||||
assert_eq!((-1 as $T).signum(), -1 as $T);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_positive() {
|
||||
assert!((1 as $T).is_positive());
|
||||
assert!(!(0 as $T).is_positive());
|
||||
assert!(!(-0 as $T).is_positive());
|
||||
assert!(!(-1 as $T).is_positive());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_negative() {
|
||||
assert!(!(1 as $T).is_negative());
|
||||
assert!(!(0 as $T).is_negative());
|
||||
assert!(!(-0 as $T).is_negative());
|
||||
assert!((-1 as $T).is_negative());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_operators() {
|
||||
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(0b1010 as $T));
|
||||
@ -63,6 +27,40 @@ macro_rules! int_module {
|
||||
assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
|
||||
}
|
||||
|
||||
test_runtime_and_compiletime! {
|
||||
|
||||
fn test_rem_euclid() {
|
||||
assert_eq_const_safe!((-1 as $T).rem_euclid(MIN), MAX);
|
||||
}
|
||||
|
||||
fn test_abs() {
|
||||
assert_eq_const_safe!((1 as $T).abs(), 1 as $T);
|
||||
assert_eq_const_safe!((0 as $T).abs(), 0 as $T);
|
||||
assert_eq_const_safe!((-1 as $T).abs(), 1 as $T);
|
||||
}
|
||||
|
||||
fn test_signum() {
|
||||
assert_eq_const_safe!((1 as $T).signum(), 1 as $T);
|
||||
assert_eq_const_safe!((0 as $T).signum(), 0 as $T);
|
||||
assert_eq_const_safe!((-0 as $T).signum(), 0 as $T);
|
||||
assert_eq_const_safe!((-1 as $T).signum(), -1 as $T);
|
||||
}
|
||||
|
||||
fn test_is_positive() {
|
||||
assert!((1 as $T).is_positive());
|
||||
assert!(!(0 as $T).is_positive());
|
||||
assert!(!(-0 as $T).is_positive());
|
||||
assert!(!(-1 as $T).is_positive());
|
||||
}
|
||||
|
||||
fn test_is_negative() {
|
||||
assert!(!(1 as $T).is_negative());
|
||||
assert!(!(0 as $T).is_negative());
|
||||
assert!(!(-0 as $T).is_negative());
|
||||
assert!((-1 as $T).is_negative());
|
||||
}
|
||||
}
|
||||
|
||||
const A: $T = 0b0101100;
|
||||
const B: $T = 0b0100001;
|
||||
const C: $T = 0b1111001;
|
||||
@ -70,134 +68,126 @@ macro_rules! int_module {
|
||||
const _0: $T = 0;
|
||||
const _1: $T = !0;
|
||||
|
||||
#[test]
|
||||
fn test_count_ones() {
|
||||
assert_eq!(A.count_ones(), 3);
|
||||
assert_eq!(B.count_ones(), 2);
|
||||
assert_eq!(C.count_ones(), 5);
|
||||
}
|
||||
test_runtime_and_compiletime! {
|
||||
fn test_count_ones() {
|
||||
assert_eq_const_safe!(A.count_ones(), 3);
|
||||
assert_eq_const_safe!(B.count_ones(), 2);
|
||||
assert_eq_const_safe!(C.count_ones(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_count_zeros() {
|
||||
assert_eq!(A.count_zeros(), $T::BITS - 3);
|
||||
assert_eq!(B.count_zeros(), $T::BITS - 2);
|
||||
assert_eq!(C.count_zeros(), $T::BITS - 5);
|
||||
}
|
||||
fn test_count_zeros() {
|
||||
assert_eq_const_safe!(A.count_zeros(), $T::BITS - 3);
|
||||
assert_eq_const_safe!(B.count_zeros(), $T::BITS - 2);
|
||||
assert_eq_const_safe!(C.count_zeros(), $T::BITS - 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_leading_trailing_ones() {
|
||||
let a: $T = 0b0101_1111;
|
||||
assert_eq!(a.trailing_ones(), 5);
|
||||
assert_eq!((!a).leading_ones(), $T::BITS - 7);
|
||||
fn test_leading_trailing_ones() {
|
||||
const A: $T = 0b0101_1111;
|
||||
assert_eq_const_safe!(A.trailing_ones(), 5);
|
||||
assert_eq_const_safe!((!A).leading_ones(), $T::BITS - 7);
|
||||
|
||||
assert_eq!(a.reverse_bits().leading_ones(), 5);
|
||||
assert_eq_const_safe!(A.reverse_bits().leading_ones(), 5);
|
||||
|
||||
assert_eq!(_1.leading_ones(), $T::BITS);
|
||||
assert_eq!(_1.trailing_ones(), $T::BITS);
|
||||
assert_eq_const_safe!(_1.leading_ones(), $T::BITS);
|
||||
assert_eq_const_safe!(_1.trailing_ones(), $T::BITS);
|
||||
|
||||
assert_eq!((_1 << 1).trailing_ones(), 0);
|
||||
assert_eq!(MAX.leading_ones(), 0);
|
||||
assert_eq_const_safe!((_1 << 1).trailing_ones(), 0);
|
||||
assert_eq_const_safe!(MAX.leading_ones(), 0);
|
||||
|
||||
assert_eq!((_1 << 1).leading_ones(), $T::BITS - 1);
|
||||
assert_eq!(MAX.trailing_ones(), $T::BITS - 1);
|
||||
assert_eq_const_safe!((_1 << 1).leading_ones(), $T::BITS - 1);
|
||||
assert_eq_const_safe!(MAX.trailing_ones(), $T::BITS - 1);
|
||||
|
||||
assert_eq!(_0.leading_ones(), 0);
|
||||
assert_eq!(_0.trailing_ones(), 0);
|
||||
assert_eq_const_safe!(_0.leading_ones(), 0);
|
||||
assert_eq_const_safe!(_0.trailing_ones(), 0);
|
||||
|
||||
let x: $T = 0b0010_1100;
|
||||
assert_eq!(x.leading_ones(), 0);
|
||||
assert_eq!(x.trailing_ones(), 0);
|
||||
}
|
||||
const X: $T = 0b0010_1100;
|
||||
assert_eq_const_safe!(X.leading_ones(), 0);
|
||||
assert_eq_const_safe!(X.trailing_ones(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rotate() {
|
||||
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
|
||||
assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
|
||||
assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
|
||||
fn test_rotate() {
|
||||
assert_eq_const_safe!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
|
||||
assert_eq_const_safe!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
|
||||
assert_eq_const_safe!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
|
||||
|
||||
// Rotating these should make no difference
|
||||
//
|
||||
// We test using 124 bits because to ensure that overlong bit shifts do
|
||||
// not cause undefined behaviour. See #10183.
|
||||
assert_eq!(_0.rotate_left(124), _0);
|
||||
assert_eq!(_1.rotate_left(124), _1);
|
||||
assert_eq!(_0.rotate_right(124), _0);
|
||||
assert_eq!(_1.rotate_right(124), _1);
|
||||
// Rotating these should make no difference
|
||||
//
|
||||
// We test using 124 bits because to ensure that overlong bit shifts do
|
||||
// not cause undefined behaviour. See #10183.
|
||||
assert_eq_const_safe!(_0.rotate_left(124), _0);
|
||||
assert_eq_const_safe!(_1.rotate_left(124), _1);
|
||||
assert_eq_const_safe!(_0.rotate_right(124), _0);
|
||||
assert_eq_const_safe!(_1.rotate_right(124), _1);
|
||||
|
||||
// Rotating by 0 should have no effect
|
||||
assert_eq!(A.rotate_left(0), A);
|
||||
assert_eq!(B.rotate_left(0), B);
|
||||
assert_eq!(C.rotate_left(0), C);
|
||||
// Rotating by a multiple of word size should also have no effect
|
||||
assert_eq!(A.rotate_left(128), A);
|
||||
assert_eq!(B.rotate_left(128), B);
|
||||
assert_eq!(C.rotate_left(128), C);
|
||||
}
|
||||
// Rotating by 0 should have no effect
|
||||
assert_eq_const_safe!(A.rotate_left(0), A);
|
||||
assert_eq_const_safe!(B.rotate_left(0), B);
|
||||
assert_eq_const_safe!(C.rotate_left(0), C);
|
||||
// Rotating by a multiple of word size should also have no effect
|
||||
assert_eq_const_safe!(A.rotate_left(128), A);
|
||||
assert_eq_const_safe!(B.rotate_left(128), B);
|
||||
assert_eq_const_safe!(C.rotate_left(128), C);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_bytes() {
|
||||
assert_eq!(A.swap_bytes().swap_bytes(), A);
|
||||
assert_eq!(B.swap_bytes().swap_bytes(), B);
|
||||
assert_eq!(C.swap_bytes().swap_bytes(), C);
|
||||
fn test_swap_bytes() {
|
||||
assert_eq_const_safe!(A.swap_bytes().swap_bytes(), A);
|
||||
assert_eq_const_safe!(B.swap_bytes().swap_bytes(), B);
|
||||
assert_eq_const_safe!(C.swap_bytes().swap_bytes(), C);
|
||||
|
||||
// Swapping these should make no difference
|
||||
assert_eq!(_0.swap_bytes(), _0);
|
||||
assert_eq!(_1.swap_bytes(), _1);
|
||||
}
|
||||
// Swapping these should make no difference
|
||||
assert_eq_const_safe!(_0.swap_bytes(), _0);
|
||||
assert_eq_const_safe!(_1.swap_bytes(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_le() {
|
||||
assert_eq!($T::from_le(A.to_le()), A);
|
||||
assert_eq!($T::from_le(B.to_le()), B);
|
||||
assert_eq!($T::from_le(C.to_le()), C);
|
||||
assert_eq!($T::from_le(_0), _0);
|
||||
assert_eq!($T::from_le(_1), _1);
|
||||
assert_eq!(_0.to_le(), _0);
|
||||
assert_eq!(_1.to_le(), _1);
|
||||
}
|
||||
fn test_le() {
|
||||
assert_eq_const_safe!($T::from_le(A.to_le()), A);
|
||||
assert_eq_const_safe!($T::from_le(B.to_le()), B);
|
||||
assert_eq_const_safe!($T::from_le(C.to_le()), C);
|
||||
assert_eq_const_safe!($T::from_le(_0), _0);
|
||||
assert_eq_const_safe!($T::from_le(_1), _1);
|
||||
assert_eq_const_safe!(_0.to_le(), _0);
|
||||
assert_eq_const_safe!(_1.to_le(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_be() {
|
||||
assert_eq!($T::from_be(A.to_be()), A);
|
||||
assert_eq!($T::from_be(B.to_be()), B);
|
||||
assert_eq!($T::from_be(C.to_be()), C);
|
||||
assert_eq!($T::from_be(_0), _0);
|
||||
assert_eq!($T::from_be(_1), _1);
|
||||
assert_eq!(_0.to_be(), _0);
|
||||
assert_eq!(_1.to_be(), _1);
|
||||
}
|
||||
fn test_be() {
|
||||
assert_eq_const_safe!($T::from_be(A.to_be()), A);
|
||||
assert_eq_const_safe!($T::from_be(B.to_be()), B);
|
||||
assert_eq_const_safe!($T::from_be(C.to_be()), C);
|
||||
assert_eq_const_safe!($T::from_be(_0), _0);
|
||||
assert_eq_const_safe!($T::from_be(_1), _1);
|
||||
assert_eq_const_safe!(_0.to_be(), _0);
|
||||
assert_eq_const_safe!(_1.to_be(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signed_checked_div() {
|
||||
assert_eq!((10 as $T).checked_div(2), Some(5));
|
||||
assert_eq!((5 as $T).checked_div(0), None);
|
||||
assert_eq!(isize::MIN.checked_div(-1), None);
|
||||
}
|
||||
fn test_signed_checked_div() {
|
||||
assert_eq_const_safe!((10 as $T).checked_div(2), Some(5));
|
||||
assert_eq_const_safe!((5 as $T).checked_div(0), None);
|
||||
assert_eq_const_safe!(isize::MIN.checked_div(-1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_saturating_abs() {
|
||||
assert_eq!((0 as $T).saturating_abs(), 0);
|
||||
assert_eq!((123 as $T).saturating_abs(), 123);
|
||||
assert_eq!((-123 as $T).saturating_abs(), 123);
|
||||
assert_eq!((MAX - 2).saturating_abs(), MAX - 2);
|
||||
assert_eq!((MAX - 1).saturating_abs(), MAX - 1);
|
||||
assert_eq!(MAX.saturating_abs(), MAX);
|
||||
assert_eq!((MIN + 2).saturating_abs(), MAX - 1);
|
||||
assert_eq!((MIN + 1).saturating_abs(), MAX);
|
||||
assert_eq!(MIN.saturating_abs(), MAX);
|
||||
}
|
||||
fn test_saturating_abs() {
|
||||
assert_eq_const_safe!((0 as $T).saturating_abs(), 0);
|
||||
assert_eq_const_safe!((123 as $T).saturating_abs(), 123);
|
||||
assert_eq_const_safe!((-123 as $T).saturating_abs(), 123);
|
||||
assert_eq_const_safe!((MAX - 2).saturating_abs(), MAX - 2);
|
||||
assert_eq_const_safe!((MAX - 1).saturating_abs(), MAX - 1);
|
||||
assert_eq_const_safe!(MAX.saturating_abs(), MAX);
|
||||
assert_eq_const_safe!((MIN + 2).saturating_abs(), MAX - 1);
|
||||
assert_eq_const_safe!((MIN + 1).saturating_abs(), MAX);
|
||||
assert_eq_const_safe!(MIN.saturating_abs(), MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_saturating_neg() {
|
||||
assert_eq!((0 as $T).saturating_neg(), 0);
|
||||
assert_eq!((123 as $T).saturating_neg(), -123);
|
||||
assert_eq!((-123 as $T).saturating_neg(), 123);
|
||||
assert_eq!((MAX - 2).saturating_neg(), MIN + 3);
|
||||
assert_eq!((MAX - 1).saturating_neg(), MIN + 2);
|
||||
assert_eq!(MAX.saturating_neg(), MIN + 1);
|
||||
assert_eq!((MIN + 2).saturating_neg(), MAX - 1);
|
||||
assert_eq!((MIN + 1).saturating_neg(), MAX);
|
||||
assert_eq!(MIN.saturating_neg(), MAX);
|
||||
fn test_saturating_neg() {
|
||||
assert_eq_const_safe!((0 as $T).saturating_neg(), 0);
|
||||
assert_eq_const_safe!((123 as $T).saturating_neg(), -123);
|
||||
assert_eq_const_safe!((-123 as $T).saturating_neg(), 123);
|
||||
assert_eq_const_safe!((MAX - 2).saturating_neg(), MIN + 3);
|
||||
assert_eq_const_safe!((MAX - 1).saturating_neg(), MIN + 2);
|
||||
assert_eq_const_safe!(MAX.saturating_neg(), MIN + 1);
|
||||
assert_eq_const_safe!((MIN + 2).saturating_neg(), MAX - 1);
|
||||
assert_eq_const_safe!((MIN + 1).saturating_neg(), MAX);
|
||||
assert_eq_const_safe!(MIN.saturating_neg(), MAX);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -222,173 +212,173 @@ macro_rules! int_module {
|
||||
assert_eq!(from_str::<$T>("x"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str_radix() {
|
||||
assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
|
||||
assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
|
||||
assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
|
||||
assert_eq!(i32::from_str_radix("123", 16), Ok(291 as i32));
|
||||
assert_eq!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
|
||||
assert_eq!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
|
||||
assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
|
||||
assert_eq!($T::from_str_radix("Z", 36), Ok(35 as $T));
|
||||
test_runtime_and_compiletime! {
|
||||
fn test_from_str_radix() {
|
||||
assert_eq_const_safe!($T::from_str_radix("123", 10), Ok(123 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("1001", 2), Ok(9 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("123", 8), Ok(83 as $T));
|
||||
assert_eq_const_safe!(i32::from_str_radix("123", 16), Ok(291 as i32));
|
||||
assert_eq_const_safe!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
|
||||
assert_eq_const_safe!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
|
||||
assert_eq_const_safe!($T::from_str_radix("z", 36), Ok(35 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("Z", 36), Ok(35 as $T));
|
||||
|
||||
assert_eq!($T::from_str_radix("-123", 10), Ok(-123 as $T));
|
||||
assert_eq!($T::from_str_radix("-1001", 2), Ok(-9 as $T));
|
||||
assert_eq!($T::from_str_radix("-123", 8), Ok(-83 as $T));
|
||||
assert_eq!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
|
||||
assert_eq!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
|
||||
assert_eq!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
|
||||
assert_eq!($T::from_str_radix("-z", 36), Ok(-35 as $T));
|
||||
assert_eq!($T::from_str_radix("-Z", 36), Ok(-35 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("-123", 10), Ok(-123 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("-1001", 2), Ok(-9 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("-123", 8), Ok(-83 as $T));
|
||||
assert_eq_const_safe!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
|
||||
assert_eq_const_safe!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
|
||||
assert_eq_const_safe!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
|
||||
assert_eq_const_safe!($T::from_str_radix("-z", 36), Ok(-35 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("-Z", 36), Ok(-35 as $T));
|
||||
|
||||
assert_eq!($T::from_str_radix("Z", 35).ok(), None::<$T>);
|
||||
assert_eq!($T::from_str_radix("-9", 2).ok(), None::<$T>);
|
||||
assert_eq!($T::from_str_radix("10_0", 10).ok(), None::<$T>);
|
||||
assert_eq!(u32::from_str_radix("-9", 10).ok(), None::<u32>);
|
||||
}
|
||||
assert!($T::from_str_radix("Z", 35).is_err());
|
||||
assert!($T::from_str_radix("-9", 2).is_err());
|
||||
assert!($T::from_str_radix("10_0", 10).is_err());
|
||||
assert!(u32::from_str_radix("-9", 10).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pow() {
|
||||
let mut r = 2 as $T;
|
||||
assert_eq!(r.pow(2), 4 as $T);
|
||||
assert_eq!(r.pow(0), 1 as $T);
|
||||
assert_eq!(r.wrapping_pow(2), 4 as $T);
|
||||
assert_eq!(r.wrapping_pow(0), 1 as $T);
|
||||
assert_eq!(r.checked_pow(2), Some(4 as $T));
|
||||
assert_eq!(r.checked_pow(0), Some(1 as $T));
|
||||
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq!(r.saturating_pow(2), 4 as $T);
|
||||
assert_eq!(r.saturating_pow(0), 1 as $T);
|
||||
fn test_pow() {
|
||||
{
|
||||
const R: $T = 2;
|
||||
assert_eq_const_safe!(R.pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
|
||||
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
|
||||
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
|
||||
}
|
||||
|
||||
r = MAX;
|
||||
// use `^` to represent .pow() with no overflow.
|
||||
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
|
||||
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
|
||||
// thussaturating_pow the overflowing result is exactly 1.
|
||||
assert_eq!(r.wrapping_pow(2), 1 as $T);
|
||||
assert_eq!(r.checked_pow(2), None);
|
||||
assert_eq!(r.overflowing_pow(2), (1 as $T, true));
|
||||
assert_eq!(r.saturating_pow(2), MAX);
|
||||
//test for negative exponent.
|
||||
r = -2 as $T;
|
||||
assert_eq!(r.pow(2), 4 as $T);
|
||||
assert_eq!(r.pow(3), -8 as $T);
|
||||
assert_eq!(r.pow(0), 1 as $T);
|
||||
assert_eq!(r.wrapping_pow(2), 4 as $T);
|
||||
assert_eq!(r.wrapping_pow(3), -8 as $T);
|
||||
assert_eq!(r.wrapping_pow(0), 1 as $T);
|
||||
assert_eq!(r.checked_pow(2), Some(4 as $T));
|
||||
assert_eq!(r.checked_pow(3), Some(-8 as $T));
|
||||
assert_eq!(r.checked_pow(0), Some(1 as $T));
|
||||
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq!(r.overflowing_pow(3), (-8 as $T, false));
|
||||
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq!(r.saturating_pow(2), 4 as $T);
|
||||
assert_eq!(r.saturating_pow(3), -8 as $T);
|
||||
assert_eq!(r.saturating_pow(0), 1 as $T);
|
||||
}
|
||||
{
|
||||
const R: $T = MAX;
|
||||
// use `^` to represent .pow() with no overflow.
|
||||
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
|
||||
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
|
||||
// thussaturating_pow the overflowing result is exactly 1.
|
||||
assert_eq_const_safe!(R.wrapping_pow(2), 1 as $T);
|
||||
assert_eq_const_safe!(R.checked_pow(2), None);
|
||||
assert_eq_const_safe!(R.overflowing_pow(2), (1 as $T, true));
|
||||
assert_eq_const_safe!(R.saturating_pow(2), MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_floor() {
|
||||
let a: $T = 8;
|
||||
let b = 3;
|
||||
assert_eq!(a.div_floor(b), 2);
|
||||
assert_eq!(a.div_floor(-b), -3);
|
||||
assert_eq!((-a).div_floor(b), -3);
|
||||
assert_eq!((-a).div_floor(-b), 2);
|
||||
}
|
||||
{
|
||||
// test for negative exponent.
|
||||
const R: $T = -2;
|
||||
assert_eq_const_safe!(R.pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.pow(3), -8 as $T);
|
||||
assert_eq_const_safe!(R.pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(3), -8 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
|
||||
assert_eq_const_safe!(R.checked_pow(3), Some(-8 as $T));
|
||||
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
|
||||
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq_const_safe!(R.overflowing_pow(3), (-8 as $T, false));
|
||||
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.saturating_pow(3), -8 as $T);
|
||||
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_ceil() {
|
||||
let a: $T = 8;
|
||||
let b = 3;
|
||||
assert_eq!(a.div_ceil(b), 3);
|
||||
assert_eq!(a.div_ceil(-b), -2);
|
||||
assert_eq!((-a).div_ceil(b), -2);
|
||||
assert_eq!((-a).div_ceil(-b), 3);
|
||||
}
|
||||
fn test_div_floor() {
|
||||
const A: $T = 8;
|
||||
const B: $T = 3;
|
||||
assert_eq_const_safe!(A.div_floor(B), 2);
|
||||
assert_eq_const_safe!(A.div_floor(-B), -3);
|
||||
assert_eq_const_safe!((-A).div_floor(B), -3);
|
||||
assert_eq_const_safe!((-A).div_floor(-B), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_next_multiple_of() {
|
||||
assert_eq!((16 as $T).next_multiple_of(8), 16);
|
||||
assert_eq!((23 as $T).next_multiple_of(8), 24);
|
||||
assert_eq!((16 as $T).next_multiple_of(-8), 16);
|
||||
assert_eq!((23 as $T).next_multiple_of(-8), 16);
|
||||
assert_eq!((-16 as $T).next_multiple_of(8), -16);
|
||||
assert_eq!((-23 as $T).next_multiple_of(8), -16);
|
||||
assert_eq!((-16 as $T).next_multiple_of(-8), -16);
|
||||
assert_eq!((-23 as $T).next_multiple_of(-8), -24);
|
||||
assert_eq!(MIN.next_multiple_of(-1), MIN);
|
||||
}
|
||||
fn test_div_ceil() {
|
||||
const A: $T = 8;
|
||||
const B: $T = 3;
|
||||
assert_eq_const_safe!(A.div_ceil(B), 3);
|
||||
assert_eq_const_safe!(A.div_ceil(-B), -2);
|
||||
assert_eq_const_safe!((-A).div_ceil(B), -2);
|
||||
assert_eq_const_safe!((-A).div_ceil(-B), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_checked_next_multiple_of() {
|
||||
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
|
||||
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
|
||||
assert_eq!((16 as $T).checked_next_multiple_of(-8), Some(16));
|
||||
assert_eq!((23 as $T).checked_next_multiple_of(-8), Some(16));
|
||||
assert_eq!((-16 as $T).checked_next_multiple_of(8), Some(-16));
|
||||
assert_eq!((-23 as $T).checked_next_multiple_of(8), Some(-16));
|
||||
assert_eq!((-16 as $T).checked_next_multiple_of(-8), Some(-16));
|
||||
assert_eq!((-23 as $T).checked_next_multiple_of(-8), Some(-24));
|
||||
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
|
||||
assert_eq!(MAX.checked_next_multiple_of(2), None);
|
||||
assert_eq!(MIN.checked_next_multiple_of(-3), None);
|
||||
assert_eq!(MIN.checked_next_multiple_of(-1), Some(MIN));
|
||||
}
|
||||
fn test_next_multiple_of() {
|
||||
assert_eq_const_safe!((16 as $T).next_multiple_of(8), 16);
|
||||
assert_eq_const_safe!((23 as $T).next_multiple_of(8), 24);
|
||||
assert_eq_const_safe!((16 as $T).next_multiple_of(-8), 16);
|
||||
assert_eq_const_safe!((23 as $T).next_multiple_of(-8), 16);
|
||||
assert_eq_const_safe!((-16 as $T).next_multiple_of(8), -16);
|
||||
assert_eq_const_safe!((-23 as $T).next_multiple_of(8), -16);
|
||||
assert_eq_const_safe!((-16 as $T).next_multiple_of(-8), -16);
|
||||
assert_eq_const_safe!((-23 as $T).next_multiple_of(-8), -24);
|
||||
assert_eq_const_safe!(MIN.next_multiple_of(-1), MIN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carrying_add() {
|
||||
assert_eq!($T::MAX.carrying_add(1, false), ($T::MIN, true));
|
||||
assert_eq!($T::MAX.carrying_add(0, true), ($T::MIN, true));
|
||||
assert_eq!($T::MAX.carrying_add(1, true), ($T::MIN + 1, true));
|
||||
assert_eq!($T::MAX.carrying_add(-1, false), ($T::MAX - 1, false));
|
||||
assert_eq!($T::MAX.carrying_add(-1, true), ($T::MAX, false)); // no intermediate overflow
|
||||
assert_eq!($T::MIN.carrying_add(-1, false), ($T::MAX, true));
|
||||
assert_eq!($T::MIN.carrying_add(-1, true), ($T::MIN, false)); // no intermediate overflow
|
||||
assert_eq!((0 as $T).carrying_add($T::MAX, true), ($T::MIN, true));
|
||||
assert_eq!((0 as $T).carrying_add($T::MIN, true), ($T::MIN + 1, false));
|
||||
}
|
||||
fn test_checked_next_multiple_of() {
|
||||
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(8), Some(16));
|
||||
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(8), Some(24));
|
||||
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(-8), Some(16));
|
||||
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(-8), Some(16));
|
||||
assert_eq_const_safe!((-16 as $T).checked_next_multiple_of(8), Some(-16));
|
||||
assert_eq_const_safe!((-23 as $T).checked_next_multiple_of(8), Some(-16));
|
||||
assert_eq_const_safe!((-16 as $T).checked_next_multiple_of(-8), Some(-16));
|
||||
assert_eq_const_safe!((-23 as $T).checked_next_multiple_of(-8), Some(-24));
|
||||
assert_eq_const_safe!((1 as $T).checked_next_multiple_of(0), None);
|
||||
assert_eq_const_safe!(MAX.checked_next_multiple_of(2), None);
|
||||
assert_eq_const_safe!(MIN.checked_next_multiple_of(-3), None);
|
||||
assert_eq_const_safe!(MIN.checked_next_multiple_of(-1), Some(MIN));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowing_sub() {
|
||||
assert_eq!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
|
||||
assert_eq!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
|
||||
assert_eq!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
|
||||
assert_eq!($T::MIN.borrowing_sub(-1, false), ($T::MIN + 1, false));
|
||||
assert_eq!($T::MIN.borrowing_sub(-1, true), ($T::MIN, false)); // no intermediate overflow
|
||||
assert_eq!($T::MAX.borrowing_sub(-1, false), ($T::MIN, true));
|
||||
assert_eq!($T::MAX.borrowing_sub(-1, true), ($T::MAX, false)); // no intermediate overflow
|
||||
assert_eq!((0 as $T).borrowing_sub($T::MIN, false), ($T::MIN, true));
|
||||
assert_eq!((0 as $T).borrowing_sub($T::MIN, true), ($T::MAX, false));
|
||||
}
|
||||
fn test_carrying_add() {
|
||||
assert_eq_const_safe!(MAX.carrying_add(1, false), (MIN, true));
|
||||
assert_eq_const_safe!(MAX.carrying_add(0, true), (MIN, true));
|
||||
assert_eq_const_safe!(MAX.carrying_add(1, true), (MIN + 1, true));
|
||||
assert_eq_const_safe!(MAX.carrying_add(-1, false), (MAX - 1, false));
|
||||
assert_eq_const_safe!(MAX.carrying_add(-1, true), (MAX, false)); // no intermediate overflow
|
||||
assert_eq_const_safe!(MIN.carrying_add(-1, false), (MAX, true));
|
||||
assert_eq_const_safe!(MIN.carrying_add(-1, true), (MIN, false)); // no intermediate overflow
|
||||
assert_eq_const_safe!((0 as $T).carrying_add(MAX, true), (MIN, true));
|
||||
assert_eq_const_safe!((0 as $T).carrying_add(MIN, true), (MIN + 1, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_midpoint() {
|
||||
assert_eq!(<$T>::midpoint(1, 3), 2);
|
||||
assert_eq!(<$T>::midpoint(3, 1), 2);
|
||||
fn test_borrowing_sub() {
|
||||
assert_eq_const_safe!(MIN.borrowing_sub(1, false), (MAX, true));
|
||||
assert_eq_const_safe!(MIN.borrowing_sub(0, true), (MAX, true));
|
||||
assert_eq_const_safe!(MIN.borrowing_sub(1, true), (MAX - 1, true));
|
||||
assert_eq_const_safe!(MIN.borrowing_sub(-1, false), (MIN + 1, false));
|
||||
assert_eq_const_safe!(MIN.borrowing_sub(-1, true), (MIN, false)); // no intermediate overflow
|
||||
assert_eq_const_safe!(MAX.borrowing_sub(-1, false), (MIN, true));
|
||||
assert_eq_const_safe!(MAX.borrowing_sub(-1, true), (MAX, false)); // no intermediate overflow
|
||||
assert_eq_const_safe!((0 as $T).borrowing_sub(MIN, false), (MIN, true));
|
||||
assert_eq_const_safe!((0 as $T).borrowing_sub(MIN, true), (MAX, false));
|
||||
}
|
||||
|
||||
assert_eq!(<$T>::midpoint(0, 0), 0);
|
||||
assert_eq!(<$T>::midpoint(0, 2), 1);
|
||||
assert_eq!(<$T>::midpoint(2, 0), 1);
|
||||
assert_eq!(<$T>::midpoint(2, 2), 2);
|
||||
fn test_midpoint() {
|
||||
assert_eq_const_safe!(<$T>::midpoint(1, 3), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(3, 1), 2);
|
||||
|
||||
assert_eq!(<$T>::midpoint(1, 4), 2);
|
||||
assert_eq!(<$T>::midpoint(4, 1), 2);
|
||||
assert_eq!(<$T>::midpoint(3, 4), 3);
|
||||
assert_eq!(<$T>::midpoint(4, 3), 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(0, 0), 0);
|
||||
assert_eq_const_safe!(<$T>::midpoint(0, 2), 1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(2, 0), 1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(2, 2), 2);
|
||||
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), -1);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), -1);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
|
||||
assert_eq_const_safe!(<$T>::midpoint(1, 4), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(4, 1), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(3, 4), 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(4, 3), 3);
|
||||
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, 6), <$T>::MAX / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(6, <$T>::MAX), <$T>::MAX / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), -1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), -1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
|
||||
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, 6), <$T>::MAX / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MAX), <$T>::MAX / 2 + 3);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ macro_rules! uint_module {
|
||||
($T:ident) => {
|
||||
use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
|
||||
use core::$T::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::num;
|
||||
|
||||
@ -35,122 +34,115 @@ macro_rules! uint_module {
|
||||
const _0: $T = 0;
|
||||
const _1: $T = !0;
|
||||
|
||||
#[test]
|
||||
fn test_count_ones() {
|
||||
assert!(A.count_ones() == 3);
|
||||
assert!(B.count_ones() == 2);
|
||||
assert!(C.count_ones() == 5);
|
||||
test_runtime_and_compiletime! {
|
||||
fn test_count_ones() {
|
||||
assert!(A.count_ones() == 3);
|
||||
assert!(B.count_ones() == 2);
|
||||
assert!(C.count_ones() == 5);
|
||||
}
|
||||
|
||||
fn test_count_zeros() {
|
||||
assert!(A.count_zeros() == $T::BITS - 3);
|
||||
assert!(B.count_zeros() == $T::BITS - 2);
|
||||
assert!(C.count_zeros() == $T::BITS - 5);
|
||||
}
|
||||
|
||||
fn test_leading_trailing_ones() {
|
||||
const A: $T = 0b0101_1111;
|
||||
assert_eq_const_safe!(A.trailing_ones(), 5);
|
||||
assert_eq_const_safe!((!A).leading_ones(), $T::BITS - 7);
|
||||
|
||||
assert_eq_const_safe!(A.reverse_bits().leading_ones(), 5);
|
||||
|
||||
assert_eq_const_safe!(_1.leading_ones(), $T::BITS);
|
||||
assert_eq_const_safe!(_1.trailing_ones(), $T::BITS);
|
||||
|
||||
assert_eq_const_safe!((_1 << 1).trailing_ones(), 0);
|
||||
assert_eq_const_safe!((_1 >> 1).leading_ones(), 0);
|
||||
|
||||
assert_eq_const_safe!((_1 << 1).leading_ones(), $T::BITS - 1);
|
||||
assert_eq_const_safe!((_1 >> 1).trailing_ones(), $T::BITS - 1);
|
||||
|
||||
assert_eq_const_safe!(_0.leading_ones(), 0);
|
||||
assert_eq_const_safe!(_0.trailing_ones(), 0);
|
||||
|
||||
const X: $T = 0b0010_1100;
|
||||
assert_eq_const_safe!(X.leading_ones(), 0);
|
||||
assert_eq_const_safe!(X.trailing_ones(), 0);
|
||||
}
|
||||
|
||||
fn test_rotate() {
|
||||
assert_eq_const_safe!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
|
||||
assert_eq_const_safe!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
|
||||
assert_eq_const_safe!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
|
||||
|
||||
// Rotating these should make no difference
|
||||
//
|
||||
// We test using 124 bits because to ensure that overlong bit shifts do
|
||||
// not cause undefined behaviour. See #10183.
|
||||
assert_eq_const_safe!(_0.rotate_left(124), _0);
|
||||
assert_eq_const_safe!(_1.rotate_left(124), _1);
|
||||
assert_eq_const_safe!(_0.rotate_right(124), _0);
|
||||
assert_eq_const_safe!(_1.rotate_right(124), _1);
|
||||
|
||||
// Rotating by 0 should have no effect
|
||||
assert_eq_const_safe!(A.rotate_left(0), A);
|
||||
assert_eq_const_safe!(B.rotate_left(0), B);
|
||||
assert_eq_const_safe!(C.rotate_left(0), C);
|
||||
// Rotating by a multiple of word size should also have no effect
|
||||
assert_eq_const_safe!(A.rotate_left(128), A);
|
||||
assert_eq_const_safe!(B.rotate_left(128), B);
|
||||
assert_eq_const_safe!(C.rotate_left(128), C);
|
||||
}
|
||||
|
||||
fn test_swap_bytes() {
|
||||
assert_eq_const_safe!(A.swap_bytes().swap_bytes(), A);
|
||||
assert_eq_const_safe!(B.swap_bytes().swap_bytes(), B);
|
||||
assert_eq_const_safe!(C.swap_bytes().swap_bytes(), C);
|
||||
|
||||
// Swapping these should make no difference
|
||||
assert_eq_const_safe!(_0.swap_bytes(), _0);
|
||||
assert_eq_const_safe!(_1.swap_bytes(), _1);
|
||||
}
|
||||
|
||||
fn test_reverse_bits() {
|
||||
assert_eq_const_safe!(A.reverse_bits().reverse_bits(), A);
|
||||
assert_eq_const_safe!(B.reverse_bits().reverse_bits(), B);
|
||||
assert_eq_const_safe!(C.reverse_bits().reverse_bits(), C);
|
||||
|
||||
// Swapping these should make no difference
|
||||
assert_eq_const_safe!(_0.reverse_bits(), _0);
|
||||
assert_eq_const_safe!(_1.reverse_bits(), _1);
|
||||
}
|
||||
|
||||
fn test_le() {
|
||||
assert_eq_const_safe!($T::from_le(A.to_le()), A);
|
||||
assert_eq_const_safe!($T::from_le(B.to_le()), B);
|
||||
assert_eq_const_safe!($T::from_le(C.to_le()), C);
|
||||
assert_eq_const_safe!($T::from_le(_0), _0);
|
||||
assert_eq_const_safe!($T::from_le(_1), _1);
|
||||
assert_eq_const_safe!(_0.to_le(), _0);
|
||||
assert_eq_const_safe!(_1.to_le(), _1);
|
||||
}
|
||||
|
||||
fn test_be() {
|
||||
assert_eq_const_safe!($T::from_be(A.to_be()), A);
|
||||
assert_eq_const_safe!($T::from_be(B.to_be()), B);
|
||||
assert_eq_const_safe!($T::from_be(C.to_be()), C);
|
||||
assert_eq_const_safe!($T::from_be(_0), _0);
|
||||
assert_eq_const_safe!($T::from_be(_1), _1);
|
||||
assert_eq_const_safe!(_0.to_be(), _0);
|
||||
assert_eq_const_safe!(_1.to_be(), _1);
|
||||
}
|
||||
|
||||
fn test_unsigned_checked_div() {
|
||||
assert_eq_const_safe!((10 as $T).checked_div(2), Some(5));
|
||||
assert_eq_const_safe!((5 as $T).checked_div(0), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_count_zeros() {
|
||||
assert!(A.count_zeros() == $T::BITS - 3);
|
||||
assert!(B.count_zeros() == $T::BITS - 2);
|
||||
assert!(C.count_zeros() == $T::BITS - 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_leading_trailing_ones() {
|
||||
let a: $T = 0b0101_1111;
|
||||
assert_eq!(a.trailing_ones(), 5);
|
||||
assert_eq!((!a).leading_ones(), $T::BITS - 7);
|
||||
|
||||
assert_eq!(a.reverse_bits().leading_ones(), 5);
|
||||
|
||||
assert_eq!(_1.leading_ones(), $T::BITS);
|
||||
assert_eq!(_1.trailing_ones(), $T::BITS);
|
||||
|
||||
assert_eq!((_1 << 1).trailing_ones(), 0);
|
||||
assert_eq!((_1 >> 1).leading_ones(), 0);
|
||||
|
||||
assert_eq!((_1 << 1).leading_ones(), $T::BITS - 1);
|
||||
assert_eq!((_1 >> 1).trailing_ones(), $T::BITS - 1);
|
||||
|
||||
assert_eq!(_0.leading_ones(), 0);
|
||||
assert_eq!(_0.trailing_ones(), 0);
|
||||
|
||||
let x: $T = 0b0010_1100;
|
||||
assert_eq!(x.leading_ones(), 0);
|
||||
assert_eq!(x.trailing_ones(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rotate() {
|
||||
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
|
||||
assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
|
||||
assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
|
||||
|
||||
// Rotating these should make no difference
|
||||
//
|
||||
// We test using 124 bits because to ensure that overlong bit shifts do
|
||||
// not cause undefined behaviour. See #10183.
|
||||
assert_eq!(_0.rotate_left(124), _0);
|
||||
assert_eq!(_1.rotate_left(124), _1);
|
||||
assert_eq!(_0.rotate_right(124), _0);
|
||||
assert_eq!(_1.rotate_right(124), _1);
|
||||
|
||||
// Rotating by 0 should have no effect
|
||||
assert_eq!(A.rotate_left(0), A);
|
||||
assert_eq!(B.rotate_left(0), B);
|
||||
assert_eq!(C.rotate_left(0), C);
|
||||
// Rotating by a multiple of word size should also have no effect
|
||||
assert_eq!(A.rotate_left(128), A);
|
||||
assert_eq!(B.rotate_left(128), B);
|
||||
assert_eq!(C.rotate_left(128), C);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_bytes() {
|
||||
assert_eq!(A.swap_bytes().swap_bytes(), A);
|
||||
assert_eq!(B.swap_bytes().swap_bytes(), B);
|
||||
assert_eq!(C.swap_bytes().swap_bytes(), C);
|
||||
|
||||
// Swapping these should make no difference
|
||||
assert_eq!(_0.swap_bytes(), _0);
|
||||
assert_eq!(_1.swap_bytes(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reverse_bits() {
|
||||
assert_eq!(A.reverse_bits().reverse_bits(), A);
|
||||
assert_eq!(B.reverse_bits().reverse_bits(), B);
|
||||
assert_eq!(C.reverse_bits().reverse_bits(), C);
|
||||
|
||||
// Swapping these should make no difference
|
||||
assert_eq!(_0.reverse_bits(), _0);
|
||||
assert_eq!(_1.reverse_bits(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_le() {
|
||||
assert_eq!($T::from_le(A.to_le()), A);
|
||||
assert_eq!($T::from_le(B.to_le()), B);
|
||||
assert_eq!($T::from_le(C.to_le()), C);
|
||||
assert_eq!($T::from_le(_0), _0);
|
||||
assert_eq!($T::from_le(_1), _1);
|
||||
assert_eq!(_0.to_le(), _0);
|
||||
assert_eq!(_1.to_le(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_be() {
|
||||
assert_eq!($T::from_be(A.to_be()), A);
|
||||
assert_eq!($T::from_be(B.to_be()), B);
|
||||
assert_eq!($T::from_be(C.to_be()), C);
|
||||
assert_eq!($T::from_be(_0), _0);
|
||||
assert_eq!($T::from_be(_1), _1);
|
||||
assert_eq!(_0.to_be(), _0);
|
||||
assert_eq!(_1.to_be(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unsigned_checked_div() {
|
||||
assert!((10 as $T).checked_div(2) == Some(5));
|
||||
assert!((5 as $T).checked_div(0) == None);
|
||||
}
|
||||
|
||||
fn from_str<T: FromStr>(t: &str) -> Option<T> {
|
||||
FromStr::from_str(t).ok()
|
||||
fn from_str<T: core::str::FromStr>(t: &str) -> Option<T> {
|
||||
core::str::FromStr::from_str(t).ok()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -166,52 +158,55 @@ macro_rules! uint_module {
|
||||
assert_eq!(from_str::<$T>("x"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_parse_bytes() {
|
||||
assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
|
||||
assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
|
||||
assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
|
||||
assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16));
|
||||
assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
|
||||
assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
|
||||
test_runtime_and_compiletime! {
|
||||
fn test_parse_bytes() {
|
||||
assert_eq_const_safe!($T::from_str_radix("123", 10), Ok(123 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("1001", 2), Ok(9 as $T));
|
||||
assert_eq_const_safe!($T::from_str_radix("123", 8), Ok(83 as $T));
|
||||
assert_eq_const_safe!(u16::from_str_radix("123", 16), Ok(291 as u16));
|
||||
assert_eq_const_safe!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
|
||||
assert_eq_const_safe!($T::from_str_radix("z", 36), Ok(35 as $T));
|
||||
|
||||
assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
|
||||
assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
|
||||
}
|
||||
assert!($T::from_str_radix("Z", 10).is_err());
|
||||
assert!($T::from_str_radix("_", 2).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pow() {
|
||||
let mut r = 2 as $T;
|
||||
assert_eq!(r.pow(2), 4 as $T);
|
||||
assert_eq!(r.pow(0), 1 as $T);
|
||||
assert_eq!(r.wrapping_pow(2), 4 as $T);
|
||||
assert_eq!(r.wrapping_pow(0), 1 as $T);
|
||||
assert_eq!(r.checked_pow(2), Some(4 as $T));
|
||||
assert_eq!(r.checked_pow(0), Some(1 as $T));
|
||||
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq!(r.saturating_pow(2), 4 as $T);
|
||||
assert_eq!(r.saturating_pow(0), 1 as $T);
|
||||
fn test_pow() {
|
||||
{
|
||||
const R: $T = 2;
|
||||
assert_eq_const_safe!(R.pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
|
||||
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
|
||||
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
|
||||
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
|
||||
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
|
||||
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
|
||||
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
|
||||
}
|
||||
|
||||
r = MAX;
|
||||
// use `^` to represent .pow() with no overflow.
|
||||
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
|
||||
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
|
||||
// thussaturating_pow the overflowing result is exactly 1.
|
||||
assert_eq!(r.wrapping_pow(2), 1 as $T);
|
||||
assert_eq!(r.checked_pow(2), None);
|
||||
assert_eq!(r.overflowing_pow(2), (1 as $T, true));
|
||||
assert_eq!(r.saturating_pow(2), MAX);
|
||||
}
|
||||
{
|
||||
const R: $T = $T::MAX;
|
||||
// use `^` to represent .pow() with no overflow.
|
||||
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
|
||||
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
|
||||
// thussaturating_pow the overflowing result is exactly 1.
|
||||
assert_eq_const_safe!(R.wrapping_pow(2), 1 as $T);
|
||||
assert_eq_const_safe!(R.checked_pow(2), None);
|
||||
assert_eq_const_safe!(R.overflowing_pow(2), (1 as $T, true));
|
||||
assert_eq_const_safe!(R.saturating_pow(2), MAX);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_isqrt() {
|
||||
assert_eq!((0 as $T).isqrt(), 0 as $T);
|
||||
assert_eq!((1 as $T).isqrt(), 1 as $T);
|
||||
assert_eq!((2 as $T).isqrt(), 1 as $T);
|
||||
assert_eq!((99 as $T).isqrt(), 9 as $T);
|
||||
assert_eq!((100 as $T).isqrt(), 10 as $T);
|
||||
assert_eq!($T::MAX.isqrt(), (1 << ($T::BITS / 2)) - 1);
|
||||
fn test_isqrt() {
|
||||
assert_eq_const_safe!((0 as $T).isqrt(), 0 as $T);
|
||||
assert_eq_const_safe!((1 as $T).isqrt(), 1 as $T);
|
||||
assert_eq_const_safe!((2 as $T).isqrt(), 1 as $T);
|
||||
assert_eq_const_safe!((99 as $T).isqrt(), 9 as $T);
|
||||
assert_eq_const_safe!((100 as $T).isqrt(), 10 as $T);
|
||||
assert_eq_const_safe!($T::MAX.isqrt(), (1 << ($T::BITS / 2)) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(miri))] // Miri is too slow
|
||||
@ -233,85 +228,79 @@ macro_rules! uint_module {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_floor() {
|
||||
assert_eq!((8 as $T).div_floor(3), 2);
|
||||
}
|
||||
test_runtime_and_compiletime! {
|
||||
fn test_div_floor() {
|
||||
assert_eq_const_safe!((8 as $T).div_floor(3), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_ceil() {
|
||||
assert_eq!((8 as $T).div_ceil(3), 3);
|
||||
}
|
||||
fn test_div_ceil() {
|
||||
assert_eq_const_safe!((8 as $T).div_ceil(3), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_next_multiple_of() {
|
||||
assert_eq!((16 as $T).next_multiple_of(8), 16);
|
||||
assert_eq!((23 as $T).next_multiple_of(8), 24);
|
||||
assert_eq!(MAX.next_multiple_of(1), MAX);
|
||||
}
|
||||
fn test_next_multiple_of() {
|
||||
assert_eq_const_safe!((16 as $T).next_multiple_of(8), 16);
|
||||
assert_eq_const_safe!((23 as $T).next_multiple_of(8), 24);
|
||||
assert_eq_const_safe!(MAX.next_multiple_of(1), MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_checked_next_multiple_of() {
|
||||
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
|
||||
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
|
||||
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
|
||||
assert_eq!(MAX.checked_next_multiple_of(2), None);
|
||||
}
|
||||
fn test_checked_next_multiple_of() {
|
||||
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(8), Some(16));
|
||||
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(8), Some(24));
|
||||
assert_eq_const_safe!((1 as $T).checked_next_multiple_of(0), None);
|
||||
assert_eq_const_safe!(MAX.checked_next_multiple_of(2), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_next_multiple_of() {
|
||||
assert!((12 as $T).is_multiple_of(4));
|
||||
assert!(!(12 as $T).is_multiple_of(5));
|
||||
assert!((0 as $T).is_multiple_of(0));
|
||||
assert!(!(12 as $T).is_multiple_of(0));
|
||||
}
|
||||
fn test_is_next_multiple_of() {
|
||||
assert!((12 as $T).is_multiple_of(4));
|
||||
assert!(!(12 as $T).is_multiple_of(5));
|
||||
assert!((0 as $T).is_multiple_of(0));
|
||||
assert!(!(12 as $T).is_multiple_of(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_carrying_add() {
|
||||
assert_eq!($T::MAX.carrying_add(1, false), (0, true));
|
||||
assert_eq!($T::MAX.carrying_add(0, true), (0, true));
|
||||
assert_eq!($T::MAX.carrying_add(1, true), (1, true));
|
||||
fn test_carrying_add() {
|
||||
assert_eq_const_safe!($T::MAX.carrying_add(1, false), (0, true));
|
||||
assert_eq_const_safe!($T::MAX.carrying_add(0, true), (0, true));
|
||||
assert_eq_const_safe!($T::MAX.carrying_add(1, true), (1, true));
|
||||
|
||||
assert_eq!($T::MIN.carrying_add($T::MAX, false), ($T::MAX, false));
|
||||
assert_eq!($T::MIN.carrying_add(0, true), (1, false));
|
||||
assert_eq!($T::MIN.carrying_add($T::MAX, true), (0, true));
|
||||
}
|
||||
assert_eq_const_safe!($T::MIN.carrying_add($T::MAX, false), ($T::MAX, false));
|
||||
assert_eq_const_safe!($T::MIN.carrying_add(0, true), (1, false));
|
||||
assert_eq_const_safe!($T::MIN.carrying_add($T::MAX, true), (0, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borrowing_sub() {
|
||||
assert_eq!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
|
||||
assert_eq!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
|
||||
assert_eq!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
|
||||
fn test_borrowing_sub() {
|
||||
assert_eq_const_safe!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
|
||||
assert_eq_const_safe!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
|
||||
assert_eq_const_safe!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
|
||||
|
||||
assert_eq!($T::MAX.borrowing_sub($T::MAX, false), (0, false));
|
||||
assert_eq!($T::MAX.borrowing_sub(0, true), ($T::MAX - 1, false));
|
||||
assert_eq!($T::MAX.borrowing_sub($T::MAX, true), ($T::MAX, true));
|
||||
}
|
||||
assert_eq_const_safe!($T::MAX.borrowing_sub($T::MAX, false), (0, false));
|
||||
assert_eq_const_safe!($T::MAX.borrowing_sub(0, true), ($T::MAX - 1, false));
|
||||
assert_eq_const_safe!($T::MAX.borrowing_sub($T::MAX, true), ($T::MAX, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_midpoint() {
|
||||
assert_eq!(<$T>::midpoint(1, 3), 2);
|
||||
assert_eq!(<$T>::midpoint(3, 1), 2);
|
||||
fn test_midpoint() {
|
||||
assert_eq_const_safe!(<$T>::midpoint(1, 3), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(3, 1), 2);
|
||||
|
||||
assert_eq!(<$T>::midpoint(0, 0), 0);
|
||||
assert_eq!(<$T>::midpoint(0, 2), 1);
|
||||
assert_eq!(<$T>::midpoint(2, 0), 1);
|
||||
assert_eq!(<$T>::midpoint(2, 2), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(0, 0), 0);
|
||||
assert_eq_const_safe!(<$T>::midpoint(0, 2), 1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(2, 0), 1);
|
||||
assert_eq_const_safe!(<$T>::midpoint(2, 2), 2);
|
||||
|
||||
assert_eq!(<$T>::midpoint(1, 4), 2);
|
||||
assert_eq!(<$T>::midpoint(4, 1), 2);
|
||||
assert_eq!(<$T>::midpoint(3, 4), 3);
|
||||
assert_eq!(<$T>::midpoint(4, 3), 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(1, 4), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(4, 1), 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(3, 4), 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(4, 3), 3);
|
||||
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), (<$T>::MAX - <$T>::MIN) / 2);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), (<$T>::MAX - <$T>::MIN) / 2);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
|
||||
|
||||
assert_eq!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(<$T>::MAX, 6), (<$T>::MAX - <$T>::MIN) / 2 + 3);
|
||||
assert_eq!(<$T>::midpoint(6, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, 6), (<$T>::MAX - <$T>::MIN) / 2 + 3);
|
||||
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2 + 3);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
//@ compile-flags: -O
|
||||
//@ compile-flags: -Zmerge-functions=disabled
|
||||
#![crate_type = "lib"]
|
||||
|
||||
// Test to check that types with "complex" destructors, but trivial `Default` impls
|
||||
// are constructed directly into the allocation in `Box::default` and `Arc::default`.
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
// CHECK-LABEL: @box_default_inplace
|
||||
@ -16,6 +18,16 @@ pub fn box_default_inplace() -> Box<(String, String)> {
|
||||
Box::default()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @rc_default_inplace
|
||||
#[no_mangle]
|
||||
pub fn rc_default_inplace() -> Rc<(String, String)> {
|
||||
// CHECK-NOT: alloca
|
||||
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
// CHECK: ret ptr [[RC]]
|
||||
Rc::default()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @arc_default_inplace
|
||||
#[no_mangle]
|
||||
pub fn arc_default_inplace() -> Arc<(String, String)> {
|
||||
|
@ -211,6 +211,15 @@ impl Clone for Zst {
|
||||
}
|
||||
}
|
||||
|
||||
enum Either<T, U> {
|
||||
Left(T),
|
||||
Right(U),
|
||||
}
|
||||
enum Either2<T, U> {
|
||||
Left(T),
|
||||
Right(U, ()),
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
enum ReprCEnum<T> {
|
||||
Variant1,
|
||||
@ -328,7 +337,8 @@ mod unsized_ {
|
||||
test_transparent_unsized!(dyn_trait, dyn Any);
|
||||
}
|
||||
|
||||
// RFC 3391 <https://rust-lang.github.io/rfcs/3391-result_ffi_guarantees.html>.
|
||||
// RFC 3391 <https://rust-lang.github.io/rfcs/3391-result_ffi_guarantees.html>, including the
|
||||
// extension ratified at <https://github.com/rust-lang/rust/pull/130628#issuecomment-2402761599>.
|
||||
macro_rules! test_nonnull {
|
||||
($name:ident, $t:ty) => {
|
||||
mod $name {
|
||||
@ -340,6 +350,12 @@ macro_rules! test_nonnull {
|
||||
test_abi_compatible!(result_ok_zst, Result<Zst, $t>, $t);
|
||||
test_abi_compatible!(result_err_arr, Result<$t, [i8; 0]>, $t);
|
||||
test_abi_compatible!(result_ok_arr, Result<[i8; 0], $t>, $t);
|
||||
test_abi_compatible!(result_err_void, Result<$t, Void>, $t);
|
||||
test_abi_compatible!(result_ok_void, Result<Void, $t>, $t);
|
||||
test_abi_compatible!(either_err_zst, Either<$t, Zst>, $t);
|
||||
test_abi_compatible!(either_ok_zst, Either<Zst, $t>, $t);
|
||||
test_abi_compatible!(either2_err_zst, Either2<$t, Zst>, $t);
|
||||
test_abi_compatible!(either2_err_arr, Either2<$t, [i8; 0]>, $t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user