Skip to content

Commit 2c4e592

Browse files
committed
add tests
1 parent 5d707b0 commit 2c4e592

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(duration_constructors)]
3131
#![feature(duration_constructors_lite)]
3232
#![feature(error_generic_member_access)]
33+
#![feature(exact_div)]
3334
#![feature(exact_size_is_empty)]
3435
#![feature(extend_one)]
3536
#![feature(extern_types)]

library/coretests/tests/num/int_macros.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,5 +683,31 @@ macro_rules! int_module {
683683
assert_eq_const_safe!($T: <$T>::unbounded_shr(17, SHIFT_AMOUNT_OVERFLOW3), 0);
684684
}
685685
}
686+
687+
const EXACT_DIV_SUCCESS_DIVIDEND1: $T = 42;
688+
const EXACT_DIV_SUCCESS_DIVISOR1: $T = 6;
689+
const EXACT_DIV_SUCCESS_QUOTIENT1: $T = 7;
690+
const EXACT_DIV_SUCCESS_DIVIDEND2: $T = 18;
691+
const EXACT_DIV_SUCCESS_DIVISOR2: $T = 3;
692+
const EXACT_DIV_SUCCESS_QUOTIENT2: $T = 6;
693+
694+
test_runtime_and_compiletime! {
695+
fn test_exact_div() {
696+
// 42 / 6
697+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), Some(EXACT_DIV_SUCCESS_QUOTIENT1));
698+
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), EXACT_DIV_SUCCESS_QUOTIENT1);
699+
assert_eq_const_safe!($T: unsafe { <$T>::unchecked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1) }, EXACT_DIV_SUCCESS_QUOTIENT1);
700+
701+
// 18 / 3
702+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), Some(EXACT_DIV_SUCCESS_QUOTIENT2));
703+
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), EXACT_DIV_SUCCESS_QUOTIENT2);
704+
assert_eq_const_safe!($T: unsafe { <$T>::unchecked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2) }, EXACT_DIV_SUCCESS_QUOTIENT2);
705+
706+
// failures
707+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(1, 2), None);
708+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(<$T>::MIN, -1), None);
709+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(0, 0), None);
710+
}
711+
}
686712
};
687713
}

library/coretests/tests/num/uint_macros.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,5 +516,30 @@ macro_rules! uint_module {
516516
assert_eq_const_safe!($T: <$T>::unbounded_shr(17, SHIFT_AMOUNT_OVERFLOW3), 0);
517517
}
518518
}
519+
520+
const EXACT_DIV_SUCCESS_DIVIDEND1: $T = 42;
521+
const EXACT_DIV_SUCCESS_DIVISOR1: $T = 6;
522+
const EXACT_DIV_SUCCESS_QUOTIENT1: $T = 7;
523+
const EXACT_DIV_SUCCESS_DIVIDEND2: $T = 18;
524+
const EXACT_DIV_SUCCESS_DIVISOR2: $T = 3;
525+
const EXACT_DIV_SUCCESS_QUOTIENT2: $T = 6;
526+
527+
test_runtime_and_compiletime! {
528+
fn test_exact_div() {
529+
// 42 / 6
530+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), Some(EXACT_DIV_SUCCESS_QUOTIENT1));
531+
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1), EXACT_DIV_SUCCESS_QUOTIENT1);
532+
assert_eq_const_safe!($T: unsafe { <$T>::unchecked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND1, EXACT_DIV_SUCCESS_DIVISOR1) }, EXACT_DIV_SUCCESS_QUOTIENT1);
533+
534+
// 18 / 3
535+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), Some(EXACT_DIV_SUCCESS_QUOTIENT2));
536+
assert_eq_const_safe!($T: <$T>::exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2), EXACT_DIV_SUCCESS_QUOTIENT2);
537+
assert_eq_const_safe!($T: unsafe { <$T>::unchecked_exact_div(EXACT_DIV_SUCCESS_DIVIDEND2, EXACT_DIV_SUCCESS_DIVISOR2) }, EXACT_DIV_SUCCESS_QUOTIENT2);
538+
539+
// failures
540+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(1, 2), None);
541+
assert_eq_const_safe!(Option<$T>: <$T>::checked_exact_div(0, 0), None);
542+
}
543+
}
519544
};
520545
}

0 commit comments

Comments
 (0)