rust/library/core/tests/intrinsics.rs
2021-11-24 13:05:26 +09:00

68 lines
1.6 KiB
Rust

use core::any::TypeId;
use core::intrinsics::assume;
#[test]
fn test_typeid_sized_types() {
struct X;
struct Y(u32);
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
}
#[test]
fn test_typeid_unsized_types() {
trait Z {}
struct X(str);
struct Y(dyn Z + 'static);
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
}
// Check that `const_assume` feature allow `assume` intrinsic
// to be used in const contexts.
#[test]
fn test_assume_can_be_in_const_contexts() {
const unsafe fn foo(x: usize, y: usize) -> usize {
// SAFETY: the entire function is not safe,
// but it is just an example not used elsewhere.
unsafe { assume(y != 0) };
x / y
}
let rs = unsafe { foo(42, 97) };
assert_eq!(rs, 0);
}
#[test]
#[cfg(not(bootstrap))]
const fn test_write_bytes_in_const_contexts() {
use core::intrinsics::write_bytes;
const TEST: [u32; 3] = {
let mut arr = [1u32, 2, 3];
unsafe {
write_bytes(arr.as_mut_ptr(), 0, 2);
}
arr
};
assert!(TEST[0] == 0);
assert!(TEST[1] == 0);
assert!(TEST[2] == 3);
const TEST2: [u32; 3] = {
let mut arr = [1u32, 2, 3];
unsafe {
write_bytes(arr.as_mut_ptr(), 1, 2);
}
arr
};
assert!(TEST2[0] == 16843009);
assert!(TEST2[1] == 16843009);
assert!(TEST2[2] == 3);
}