summaryrefslogtreecommitdiffstats
path: root/library/core/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /library/core/tests
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/core/tests')
-rw-r--r--library/core/tests/iter/range.rs18
-rw-r--r--library/core/tests/lib.rs5
-rw-r--r--library/core/tests/macros.rs154
-rw-r--r--library/core/tests/num/int_macros.rs32
-rw-r--r--library/core/tests/num/uint_macros.rs29
-rw-r--r--library/core/tests/time.rs1
6 files changed, 238 insertions, 1 deletions
diff --git a/library/core/tests/iter/range.rs b/library/core/tests/iter/range.rs
index 0a77ecddb..5b87d6c1f 100644
--- a/library/core/tests/iter/range.rs
+++ b/library/core/tests/iter/range.rs
@@ -1,5 +1,6 @@
-use core::num::NonZeroUsize;
use super::*;
+use core::ascii::Char as AsciiChar;
+use core::num::NonZeroUsize;
#[test]
fn test_range() {
@@ -40,6 +41,21 @@ fn test_char_range() {
}
#[test]
+fn test_ascii_char_range() {
+ let from = AsciiChar::Null;
+ let to = AsciiChar::Delete;
+ assert!((from..=to).eq((from as u8..=to as u8).filter_map(AsciiChar::from_u8)));
+ assert!((from..=to).rev().eq((from as u8..=to as u8).filter_map(AsciiChar::from_u8).rev()));
+
+ assert_eq!((AsciiChar::CapitalA..=AsciiChar::CapitalZ).count(), 26);
+ assert_eq!((AsciiChar::CapitalA..=AsciiChar::CapitalZ).size_hint(), (26, Some(26)));
+ assert_eq!((AsciiChar::SmallA..=AsciiChar::SmallZ).count(), 26);
+ assert_eq!((AsciiChar::SmallA..=AsciiChar::SmallZ).size_hint(), (26, Some(26)));
+ assert_eq!((AsciiChar::Digit0..=AsciiChar::Digit9).count(), 10);
+ assert_eq!((AsciiChar::Digit0..=AsciiChar::Digit9).size_hint(), (10, Some(10)));
+}
+
+#[test]
fn test_range_exhaustion() {
let mut r = 10..10;
assert!(r.is_empty());
diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs
index 7a6def37a..e4003a208 100644
--- a/library/core/tests/lib.rs
+++ b/library/core/tests/lib.rs
@@ -2,6 +2,8 @@
#![feature(array_chunks)]
#![feature(array_methods)]
#![feature(array_windows)]
+#![feature(ascii_char)]
+#![feature(ascii_char_variants)]
#![feature(bigint_helper_methods)]
#![feature(cell_update)]
#![feature(const_align_offset)]
@@ -54,6 +56,7 @@
#![feature(min_specialization)]
#![feature(numfmt)]
#![feature(num_midpoint)]
+#![feature(isqrt)]
#![feature(step_trait)]
#![feature(str_internals)]
#![feature(std_internals)]
@@ -94,6 +97,7 @@
#![feature(const_option_ext)]
#![feature(const_result)]
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
+#![cfg_attr(test, feature(cfg_match))]
#![feature(int_roundings)]
#![feature(slice_group_by)]
#![feature(split_array)]
@@ -137,6 +141,7 @@ mod hash;
mod intrinsics;
mod iter;
mod lazy;
+#[cfg(test)]
mod macros;
mod manually_drop;
mod mem;
diff --git a/library/core/tests/macros.rs b/library/core/tests/macros.rs
index ff3632e35..eb886def1 100644
--- a/library/core/tests/macros.rs
+++ b/library/core/tests/macros.rs
@@ -1,3 +1,25 @@
+trait Trait {
+ fn blah(&self);
+}
+
+#[allow(dead_code)]
+struct Struct;
+
+impl Trait for Struct {
+ cfg_match! {
+ cfg(feature = "blah") => {
+ fn blah(&self) {
+ unimplemented!();
+ }
+ }
+ _ => {
+ fn blah(&self) {
+ unimplemented!();
+ }
+ }
+ }
+}
+
#[test]
fn assert_eq_trailing_comma() {
assert_eq!(1, 1,);
@@ -18,3 +40,135 @@ fn assert_ne_trailing_comma() {
fn matches_leading_pipe() {
matches!(1, | 1 | 2 | 3);
}
+
+#[test]
+fn cfg_match_basic() {
+ cfg_match! {
+ cfg(target_pointer_width = "64") => { fn f0_() -> bool { true }}
+ }
+
+ cfg_match! {
+ cfg(unix) => { fn f1_() -> bool { true }}
+ cfg(any(target_os = "macos", target_os = "linux")) => { fn f1_() -> bool { false }}
+ }
+
+ cfg_match! {
+ cfg(target_pointer_width = "32") => { fn f2_() -> bool { false }}
+ cfg(target_pointer_width = "64") => { fn f2_() -> bool { true }}
+ }
+
+ cfg_match! {
+ cfg(target_pointer_width = "16") => { fn f3_() -> i32 { 1 }}
+ _ => { fn f3_() -> i32 { 2 }}
+ }
+
+ #[cfg(target_pointer_width = "64")]
+ assert!(f0_());
+
+ #[cfg(unix)]
+ assert!(f1_());
+
+ #[cfg(target_pointer_width = "32")]
+ assert!(!f2_());
+ #[cfg(target_pointer_width = "64")]
+ assert!(f2_());
+
+ #[cfg(not(target_pointer_width = "16"))]
+ assert_eq!(f3_(), 2);
+}
+
+#[test]
+fn cfg_match_debug_assertions() {
+ cfg_match! {
+ cfg(debug_assertions) => {
+ assert!(cfg!(debug_assertions));
+ assert_eq!(4, 2+2);
+ }
+ _ => {
+ assert!(cfg!(not(debug_assertions)));
+ assert_eq!(10, 5+5);
+ }
+ }
+}
+
+#[cfg(target_pointer_width = "64")]
+#[test]
+fn cfg_match_no_duplication_on_64() {
+ cfg_match! {
+ cfg(windows) => {
+ fn foo() {}
+ }
+ cfg(unix) => {
+ fn foo() {}
+ }
+ cfg(target_pointer_width = "64") => {
+ fn foo() {}
+ }
+ }
+ foo();
+}
+
+#[test]
+fn cfg_match_options() {
+ cfg_match! {
+ cfg(test) => {
+ use core::option::Option as Option2;
+ fn works1() -> Option2<u32> { Some(1) }
+ }
+ _ => { fn works1() -> Option<u32> { None } }
+ }
+
+ cfg_match! {
+ cfg(feature = "foo") => { fn works2() -> bool { false } }
+ cfg(test) => { fn works2() -> bool { true } }
+ _ => { fn works2() -> bool { false } }
+ }
+
+ cfg_match! {
+ cfg(feature = "foo") => { fn works3() -> bool { false } }
+ _ => { fn works3() -> bool { true } }
+ }
+
+ cfg_match! {
+ cfg(test) => {
+ use core::option::Option as Option3;
+ fn works4() -> Option3<u32> { Some(1) }
+ }
+ }
+
+ cfg_match! {
+ cfg(feature = "foo") => { fn works5() -> bool { false } }
+ cfg(test) => { fn works5() -> bool { true } }
+ }
+
+ assert!(works1().is_some());
+ assert!(works2());
+ assert!(works3());
+ assert!(works4().is_some());
+ assert!(works5());
+}
+
+#[test]
+fn cfg_match_two_functions() {
+ cfg_match! {
+ cfg(target_pointer_width = "64") => {
+ fn foo1() {}
+ fn bar1() {}
+ }
+ _ => {
+ fn foo2() {}
+ fn bar2() {}
+ }
+ }
+
+ #[cfg(target_pointer_width = "64")]
+ {
+ foo1();
+ bar1();
+ }
+ #[cfg(not(target_pointer_width = "64"))]
+ {
+ foo2();
+ bar2();
+ }
+}
diff --git a/library/core/tests/num/int_macros.rs b/library/core/tests/num/int_macros.rs
index 439bbe669..165d9a296 100644
--- a/library/core/tests/num/int_macros.rs
+++ b/library/core/tests/num/int_macros.rs
@@ -291,6 +291,38 @@ macro_rules! int_module {
}
#[test]
+ fn test_isqrt() {
+ assert_eq!($T::MIN.checked_isqrt(), None);
+ assert_eq!((-1 as $T).checked_isqrt(), None);
+ 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);
+ }
+
+ #[cfg(not(miri))] // Miri is too slow
+ #[test]
+ fn test_lots_of_isqrt() {
+ let n_max: $T = (1024 * 1024).min($T::MAX as u128) as $T;
+ for n in 0..=n_max {
+ let isqrt: $T = n.isqrt();
+
+ assert!(isqrt.pow(2) <= n);
+ let (square, overflow) = (isqrt + 1).overflowing_pow(2);
+ assert!(overflow || square > n);
+ }
+
+ for n in ($T::MAX - 127)..=$T::MAX {
+ let isqrt: $T = n.isqrt();
+
+ assert!(isqrt.pow(2) <= n);
+ let (square, overflow) = (isqrt + 1).overflowing_pow(2);
+ assert!(overflow || square > n);
+ }
+ }
+
+ #[test]
fn test_div_floor() {
let a: $T = 8;
let b = 3;
diff --git a/library/core/tests/num/uint_macros.rs b/library/core/tests/num/uint_macros.rs
index 7d6203db0..955440647 100644
--- a/library/core/tests/num/uint_macros.rs
+++ b/library/core/tests/num/uint_macros.rs
@@ -207,6 +207,35 @@ macro_rules! uint_module {
}
#[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);
+ }
+
+ #[cfg(not(miri))] // Miri is too slow
+ #[test]
+ fn test_lots_of_isqrt() {
+ let n_max: $T = (1024 * 1024).min($T::MAX as u128) as $T;
+ for n in 0..=n_max {
+ let isqrt: $T = n.isqrt();
+
+ assert!(isqrt.pow(2) <= n);
+ assert!(isqrt + 1 == (1 as $T) << ($T::BITS / 2) || (isqrt + 1).pow(2) > n);
+ }
+
+ for n in ($T::MAX - 255)..=$T::MAX {
+ let isqrt: $T = n.isqrt();
+
+ assert!(isqrt.pow(2) <= n);
+ assert!(isqrt + 1 == (1 as $T) << ($T::BITS / 2) || (isqrt + 1).pow(2) > n);
+ }
+ }
+
+ #[test]
fn test_div_floor() {
assert_eq!((8 as $T).div_floor(3), 2);
}
diff --git a/library/core/tests/time.rs b/library/core/tests/time.rs
index 872611937..bd6e63edb 100644
--- a/library/core/tests/time.rs
+++ b/library/core/tests/time.rs
@@ -170,6 +170,7 @@ fn saturating_mul() {
fn div() {
assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
+ assert_eq!(Duration::new(1, 1) / 7, Duration::new(0, 142_857_143));
assert_eq!(Duration::new(99, 999_999_000) / 100, Duration::new(0, 999_999_990));
}