summaryrefslogtreecommitdiffstats
path: root/library/std/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /library/std/tests
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/tests')
-rw-r--r--library/std/tests/env.rs140
-rw-r--r--library/std/tests/run-time-detect.rs153
-rw-r--r--library/std/tests/thread.rs16
3 files changed, 309 insertions, 0 deletions
diff --git a/library/std/tests/env.rs b/library/std/tests/env.rs
new file mode 100644
index 000000000..b095c2dde
--- /dev/null
+++ b/library/std/tests/env.rs
@@ -0,0 +1,140 @@
+use std::env::*;
+use std::ffi::{OsStr, OsString};
+
+use rand::distributions::Alphanumeric;
+use rand::{thread_rng, Rng};
+
+fn make_rand_name() -> OsString {
+ let rng = thread_rng();
+ let n = format!("TEST{}", rng.sample_iter(&Alphanumeric).take(10).collect::<String>());
+ let n = OsString::from(n);
+ assert!(var_os(&n).is_none());
+ n
+}
+
+fn eq(a: Option<OsString>, b: Option<&str>) {
+ assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
+}
+
+#[test]
+fn test_set_var() {
+ let n = make_rand_name();
+ set_var(&n, "VALUE");
+ eq(var_os(&n), Some("VALUE"));
+}
+
+#[test]
+fn test_remove_var() {
+ let n = make_rand_name();
+ set_var(&n, "VALUE");
+ remove_var(&n);
+ eq(var_os(&n), None);
+}
+
+#[test]
+fn test_set_var_overwrite() {
+ let n = make_rand_name();
+ set_var(&n, "1");
+ set_var(&n, "2");
+ eq(var_os(&n), Some("2"));
+ set_var(&n, "");
+ eq(var_os(&n), Some(""));
+}
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn test_var_big() {
+ let mut s = "".to_string();
+ let mut i = 0;
+ while i < 100 {
+ s.push_str("aaaaaaaaaa");
+ i += 1;
+ }
+ let n = make_rand_name();
+ set_var(&n, &s);
+ eq(var_os(&n), Some(&s));
+}
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn test_env_set_get_huge() {
+ let n = make_rand_name();
+ let s = "x".repeat(10000);
+ set_var(&n, &s);
+ eq(var_os(&n), Some(&s));
+ remove_var(&n);
+ eq(var_os(&n), None);
+}
+
+#[test]
+fn test_env_set_var() {
+ let n = make_rand_name();
+
+ let mut e = vars_os();
+ set_var(&n, "VALUE");
+ assert!(!e.any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
+
+ assert!(vars_os().any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
+}
+
+#[test]
+#[cfg_attr(not(any(unix, windows)), ignore, allow(unused))]
+#[allow(deprecated)]
+fn env_home_dir() {
+ use std::path::PathBuf;
+
+ fn var_to_os_string(var: Result<String, VarError>) -> Option<OsString> {
+ match var {
+ Ok(var) => Some(OsString::from(var)),
+ Err(VarError::NotUnicode(var)) => Some(var),
+ _ => None,
+ }
+ }
+
+ cfg_if::cfg_if! {
+ if #[cfg(unix)] {
+ let oldhome = var_to_os_string(var("HOME"));
+
+ set_var("HOME", "/home/MountainView");
+ assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
+
+ remove_var("HOME");
+ if cfg!(target_os = "android") {
+ assert!(home_dir().is_none());
+ } else {
+ // When HOME is not set, some platforms return `None`,
+ // but others return `Some` with a default.
+ // Just check that it is not "/home/MountainView".
+ assert_ne!(home_dir(), Some(PathBuf::from("/home/MountainView")));
+ }
+
+ if let Some(oldhome) = oldhome { set_var("HOME", oldhome); }
+ } else if #[cfg(windows)] {
+ let oldhome = var_to_os_string(var("HOME"));
+ let olduserprofile = var_to_os_string(var("USERPROFILE"));
+
+ remove_var("HOME");
+ remove_var("USERPROFILE");
+
+ assert!(home_dir().is_some());
+
+ set_var("HOME", "/home/MountainView");
+ assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
+
+ remove_var("HOME");
+
+ set_var("USERPROFILE", "/home/MountainView");
+ assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
+
+ set_var("HOME", "/home/MountainView");
+ set_var("USERPROFILE", "/home/PaloAlto");
+ assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
+
+ remove_var("HOME");
+ remove_var("USERPROFILE");
+
+ if let Some(oldhome) = oldhome { set_var("HOME", oldhome); }
+ if let Some(olduserprofile) = olduserprofile { set_var("USERPROFILE", olduserprofile); }
+ }
+ }
+}
diff --git a/library/std/tests/run-time-detect.rs b/library/std/tests/run-time-detect.rs
new file mode 100644
index 000000000..a57a52d9b
--- /dev/null
+++ b/library/std/tests/run-time-detect.rs
@@ -0,0 +1,153 @@
+//! These tests just check that the macros are available in libstd.
+
+#![cfg_attr(
+ any(
+ all(target_arch = "arm", any(target_os = "linux", target_os = "android")),
+ all(bootstrap, target_arch = "aarch64", any(target_os = "linux", target_os = "android")),
+ all(target_arch = "powerpc", target_os = "linux"),
+ all(target_arch = "powerpc64", target_os = "linux"),
+ ),
+ feature(stdsimd)
+)]
+
+#[test]
+#[cfg(all(target_arch = "arm", any(target_os = "linux", target_os = "android")))]
+fn arm_linux() {
+ use std::arch::is_arm_feature_detected;
+ println!("neon: {}", is_arm_feature_detected!("neon"));
+ println!("pmull: {}", is_arm_feature_detected!("pmull"));
+ println!("crypto: {}", is_arm_feature_detected!("crypto"));
+ println!("crc: {}", is_arm_feature_detected!("crc"));
+ println!("aes: {}", is_arm_feature_detected!("aes"));
+ println!("sha2: {}", is_arm_feature_detected!("sha2"));
+}
+
+#[test]
+#[cfg(all(target_arch = "aarch64", any(target_os = "linux", target_os = "android")))]
+fn aarch64_linux() {
+ use std::arch::is_aarch64_feature_detected;
+ println!("neon: {}", is_aarch64_feature_detected!("neon"));
+ println!("asimd: {}", is_aarch64_feature_detected!("asimd"));
+ println!("pmull: {}", is_aarch64_feature_detected!("pmull"));
+ println!("fp16: {}", is_aarch64_feature_detected!("fp16"));
+ println!("sve: {}", is_aarch64_feature_detected!("sve"));
+ println!("crc: {}", is_aarch64_feature_detected!("crc"));
+ println!("lse: {}", is_aarch64_feature_detected!("lse"));
+ println!("lse2: {}", is_aarch64_feature_detected!("lse2"));
+ println!("rdm: {}", is_aarch64_feature_detected!("rdm"));
+ println!("rcpc: {}", is_aarch64_feature_detected!("rcpc"));
+ println!("rcpc2: {}", is_aarch64_feature_detected!("rcpc2"));
+ println!("dotprod: {}", is_aarch64_feature_detected!("dotprod"));
+ println!("tme: {}", is_aarch64_feature_detected!("tme"));
+ println!("fhm: {}", is_aarch64_feature_detected!("fhm"));
+ println!("dit: {}", is_aarch64_feature_detected!("dit"));
+ println!("flagm: {}", is_aarch64_feature_detected!("flagm"));
+ println!("ssbs: {}", is_aarch64_feature_detected!("ssbs"));
+ println!("sb: {}", is_aarch64_feature_detected!("sb"));
+ println!("paca: {}", is_aarch64_feature_detected!("paca"));
+ println!("pacg: {}", is_aarch64_feature_detected!("pacg"));
+ println!("dpb: {}", is_aarch64_feature_detected!("dpb"));
+ println!("dpb2: {}", is_aarch64_feature_detected!("dpb2"));
+ println!("sve2: {}", is_aarch64_feature_detected!("sve2"));
+ println!("sve2-aes: {}", is_aarch64_feature_detected!("sve2-aes"));
+ println!("sve2-sm4: {}", is_aarch64_feature_detected!("sve2-sm4"));
+ println!("sve2-sha3: {}", is_aarch64_feature_detected!("sve2-sha3"));
+ println!("sve2-bitperm: {}", is_aarch64_feature_detected!("sve2-bitperm"));
+ println!("frintts: {}", is_aarch64_feature_detected!("frintts"));
+ println!("i8mm: {}", is_aarch64_feature_detected!("i8mm"));
+ println!("f32mm: {}", is_aarch64_feature_detected!("f32mm"));
+ println!("f64mm: {}", is_aarch64_feature_detected!("f64mm"));
+ println!("bf16: {}", is_aarch64_feature_detected!("bf16"));
+ println!("rand: {}", is_aarch64_feature_detected!("rand"));
+ println!("bti: {}", is_aarch64_feature_detected!("bti"));
+ println!("mte: {}", is_aarch64_feature_detected!("mte"));
+ println!("jsconv: {}", is_aarch64_feature_detected!("jsconv"));
+ println!("fcma: {}", is_aarch64_feature_detected!("fcma"));
+ println!("aes: {}", is_aarch64_feature_detected!("aes"));
+ println!("sha2: {}", is_aarch64_feature_detected!("sha2"));
+ println!("sha3: {}", is_aarch64_feature_detected!("sha3"));
+ println!("sm4: {}", is_aarch64_feature_detected!("sm4"));
+}
+
+#[test]
+#[cfg(all(target_arch = "powerpc", target_os = "linux"))]
+fn powerpc_linux() {
+ use std::arch::is_powerpc_feature_detected;
+ println!("altivec: {}", is_powerpc_feature_detected!("altivec"));
+ println!("vsx: {}", is_powerpc_feature_detected!("vsx"));
+ println!("power8: {}", is_powerpc_feature_detected!("power8"));
+}
+
+#[test]
+#[cfg(all(target_arch = "powerpc64", target_os = "linux"))]
+fn powerpc64_linux() {
+ use std::arch::is_powerpc64_feature_detected;
+ println!("altivec: {}", is_powerpc64_feature_detected!("altivec"));
+ println!("vsx: {}", is_powerpc64_feature_detected!("vsx"));
+ println!("power8: {}", is_powerpc64_feature_detected!("power8"));
+}
+
+#[test]
+#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+fn x86_all() {
+ use std::arch::is_x86_feature_detected;
+
+ // the below is the set of features we can test at runtime, but don't actually
+ // use to gate anything and are thus not part of the X86_ALLOWED_FEATURES list
+
+ println!("abm: {:?}", is_x86_feature_detected!("abm")); // this is a synonym for lzcnt but we test it anyways
+ println!("mmx: {:?}", is_x86_feature_detected!("mmx"));
+ println!("tsc: {:?}", is_x86_feature_detected!("tsc"));
+
+ // the below is in alphabetical order and matches
+ // the order of X86_ALLOWED_FEATURES in rustc_codegen_ssa's target_features.rs
+
+ println!("adx: {:?}", is_x86_feature_detected!("adx"));
+ println!("aes: {:?}", is_x86_feature_detected!("aes"));
+ println!("avx: {:?}", is_x86_feature_detected!("avx"));
+ println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
+ println!("avx512bf16: {:?}", is_x86_feature_detected!("avx512bf16"));
+ println!("avx512bitalg: {:?}", is_x86_feature_detected!("avx512bitalg"));
+ println!("avx512bw: {:?}", is_x86_feature_detected!("avx512bw"));
+ println!("avx512cd: {:?}", is_x86_feature_detected!("avx512cd"));
+ println!("avx512dq: {:?}", is_x86_feature_detected!("avx512dq"));
+ println!("avx512er: {:?}", is_x86_feature_detected!("avx512er"));
+ println!("avx512f: {:?}", is_x86_feature_detected!("avx512f"));
+ println!("avx512gfni: {:?}", is_x86_feature_detected!("avx512gfni"));
+ println!("avx512ifma: {:?}", is_x86_feature_detected!("avx512ifma"));
+ println!("avx512pf: {:?}", is_x86_feature_detected!("avx512pf"));
+ println!("avx512vaes: {:?}", is_x86_feature_detected!("avx512vaes"));
+ println!("avx512vbmi: {:?}", is_x86_feature_detected!("avx512vbmi"));
+ println!("avx512vbmi2: {:?}", is_x86_feature_detected!("avx512vbmi2"));
+ println!("avx512vl: {:?}", is_x86_feature_detected!("avx512vl"));
+ println!("avx512vnni: {:?}", is_x86_feature_detected!("avx512vnni"));
+ println!("avx512vp2intersect: {:?}", is_x86_feature_detected!("avx512vp2intersect"));
+ println!("avx512vpclmulqdq: {:?}", is_x86_feature_detected!("avx512vpclmulqdq"));
+ println!("avx512vpopcntdq: {:?}", is_x86_feature_detected!("avx512vpopcntdq"));
+ println!("bmi1: {:?}", is_x86_feature_detected!("bmi1"));
+ println!("bmi2: {:?}", is_x86_feature_detected!("bmi2"));
+ println!("cmpxchg16b: {:?}", is_x86_feature_detected!("cmpxchg16b"));
+ println!("f16c: {:?}", is_x86_feature_detected!("f16c"));
+ println!("fma: {:?}", is_x86_feature_detected!("fma"));
+ println!("fxsr: {:?}", is_x86_feature_detected!("fxsr"));
+ println!("lzcnt: {:?}", is_x86_feature_detected!("lzcnt"));
+ //println!("movbe: {:?}", is_x86_feature_detected!("movbe")); // movbe is unsupported as a target feature
+ println!("pclmulqdq: {:?}", is_x86_feature_detected!("pclmulqdq"));
+ println!("popcnt: {:?}", is_x86_feature_detected!("popcnt"));
+ println!("rdrand: {:?}", is_x86_feature_detected!("rdrand"));
+ println!("rdseed: {:?}", is_x86_feature_detected!("rdseed"));
+ println!("rtm: {:?}", is_x86_feature_detected!("rtm"));
+ println!("sha: {:?}", is_x86_feature_detected!("sha"));
+ println!("sse: {:?}", is_x86_feature_detected!("sse"));
+ println!("sse2: {:?}", is_x86_feature_detected!("sse2"));
+ println!("sse3: {:?}", is_x86_feature_detected!("sse3"));
+ println!("sse4.1: {:?}", is_x86_feature_detected!("sse4.1"));
+ println!("sse4.2: {:?}", is_x86_feature_detected!("sse4.2"));
+ println!("sse4a: {:?}", is_x86_feature_detected!("sse4a"));
+ println!("ssse3: {:?}", is_x86_feature_detected!("ssse3"));
+ println!("tbm: {:?}", is_x86_feature_detected!("tbm"));
+ println!("xsave: {:?}", is_x86_feature_detected!("xsave"));
+ println!("xsavec: {:?}", is_x86_feature_detected!("xsavec"));
+ println!("xsaveopt: {:?}", is_x86_feature_detected!("xsaveopt"));
+ println!("xsaves: {:?}", is_x86_feature_detected!("xsaves"));
+}
diff --git a/library/std/tests/thread.rs b/library/std/tests/thread.rs
new file mode 100644
index 000000000..754b264c6
--- /dev/null
+++ b/library/std/tests/thread.rs
@@ -0,0 +1,16 @@
+use std::sync::{Arc, Mutex};
+use std::thread;
+use std::time::Duration;
+
+#[test]
+#[cfg_attr(target_os = "emscripten", ignore)]
+fn sleep() {
+ let finished = Arc::new(Mutex::new(false));
+ let t_finished = finished.clone();
+ thread::spawn(move || {
+ thread::sleep(Duration::new(u64::MAX, 0));
+ *t_finished.lock().unwrap() = true;
+ });
+ thread::sleep(Duration::from_millis(100));
+ assert_eq!(*finished.lock().unwrap(), false);
+}