]> git.lizzy.rs Git - rust.git/blob - library/core/tests/intrinsics.rs
Auto merge of #105125 - matthiaskrgr:rollup-fr0snmj, r=matthiaskrgr
[rust.git] / library / core / tests / intrinsics.rs
1 use core::any::TypeId;
2 use core::intrinsics::assume;
3
4 #[test]
5 fn test_typeid_sized_types() {
6     struct X;
7     struct Y(u32);
8
9     assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
10     assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
11     assert!(TypeId::of::<X>() != TypeId::of::<Y>());
12 }
13
14 #[test]
15 fn test_typeid_unsized_types() {
16     trait Z {}
17     struct X(str);
18     struct Y(dyn Z + 'static);
19
20     assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
21     assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
22     assert!(TypeId::of::<X>() != TypeId::of::<Y>());
23 }
24
25 // Check that `const_assume` feature allow `assume` intrinsic
26 // to be used in const contexts.
27 #[test]
28 fn test_assume_can_be_in_const_contexts() {
29     const unsafe fn foo(x: usize, y: usize) -> usize {
30         // SAFETY: the entire function is not safe,
31         // but it is just an example not used elsewhere.
32         unsafe { assume(y != 0) };
33         x / y
34     }
35     let rs = unsafe { foo(42, 97) };
36     assert_eq!(rs, 0);
37 }
38
39 #[test]
40 const fn test_write_bytes_in_const_contexts() {
41     use core::intrinsics::write_bytes;
42
43     const TEST: [u32; 3] = {
44         let mut arr = [1u32, 2, 3];
45         unsafe {
46             write_bytes(arr.as_mut_ptr(), 0, 2);
47         }
48         arr
49     };
50
51     assert!(TEST[0] == 0);
52     assert!(TEST[1] == 0);
53     assert!(TEST[2] == 3);
54
55     const TEST2: [u32; 3] = {
56         let mut arr = [1u32, 2, 3];
57         unsafe {
58             write_bytes(arr.as_mut_ptr(), 1, 2);
59         }
60         arr
61     };
62
63     assert!(TEST2[0] == 16843009);
64     assert!(TEST2[1] == 16843009);
65     assert!(TEST2[2] == 3);
66 }
67
68 #[test]
69 fn test_hints_in_const_contexts() {
70     use core::intrinsics::{likely, unlikely};
71
72     // In const contexts, they just return their argument.
73     const {
74         assert!(true == likely(true));
75         assert!(false == likely(false));
76         assert!(true == unlikely(true));
77         assert!(false == unlikely(false));
78         assert!(42u32 == core::intrinsics::black_box(42u32));
79         assert!(42u32 == core::hint::black_box(42u32));
80     }
81 }
82
83 #[test]
84 fn test_const_allocate_at_runtime() {
85     use core::intrinsics::const_allocate;
86     unsafe {
87         assert!(const_allocate(4, 4).is_null());
88     }
89 }
90
91 #[test]
92 fn test_const_deallocate_at_runtime() {
93     use core::intrinsics::const_deallocate;
94     const X: &u32 = &42u32;
95     let x = &0u32;
96     unsafe {
97         const_deallocate(X as *const _ as *mut u8, 4, 4); // nop
98         const_deallocate(x as *const _ as *mut u8, 4, 4); // nop
99         const_deallocate(core::ptr::null_mut(), 1, 1); // nop
100     }
101 }