]> git.lizzy.rs Git - rust.git/blob - library/core/tests/intrinsics.rs
Rollup merge of #93391 - notriddle:notriddle/remove-srclink-tooltip, r=jsha,Guillaume...
[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 #[cfg(not(bootstrap))]
41 const fn test_write_bytes_in_const_contexts() {
42     use core::intrinsics::write_bytes;
43
44     const TEST: [u32; 3] = {
45         let mut arr = [1u32, 2, 3];
46         unsafe {
47             write_bytes(arr.as_mut_ptr(), 0, 2);
48         }
49         arr
50     };
51
52     assert!(TEST[0] == 0);
53     assert!(TEST[1] == 0);
54     assert!(TEST[2] == 3);
55
56     const TEST2: [u32; 3] = {
57         let mut arr = [1u32, 2, 3];
58         unsafe {
59             write_bytes(arr.as_mut_ptr(), 1, 2);
60         }
61         arr
62     };
63
64     assert!(TEST2[0] == 16843009);
65     assert!(TEST2[1] == 16843009);
66     assert!(TEST2[2] == 3);
67 }
68
69 #[test]
70 fn test_hints_in_const_contexts() {
71     use core::intrinsics::{likely, unlikely};
72
73     // In const contexts, they just return their argument.
74     const {
75         assert!(true == likely(true));
76         assert!(false == likely(false));
77         assert!(true == unlikely(true));
78         assert!(false == unlikely(false));
79         assert!(42u32 == core::intrinsics::black_box(42u32));
80         assert!(42u32 == core::hint::black_box(42u32));
81     }
82 }
83
84 #[cfg(not(bootstrap))]
85 #[test]
86 fn test_const_allocate_at_runtime() {
87     use core::intrinsics::const_allocate;
88     unsafe {
89         assert!(const_allocate(4, 4).is_null());
90     }
91 }
92
93 #[cfg(not(bootstrap))]
94 #[test]
95 fn test_const_deallocate_at_runtime() {
96     use core::intrinsics::const_deallocate;
97     const X: &u32 = &42u32;
98     let x = &0u32;
99     unsafe {
100         const_deallocate(X as *const _ as *mut u8, 4, 4); // nop
101         const_deallocate(x as *const _ as *mut u8, 4, 4); // nop
102         const_deallocate(core::ptr::null_mut(), 1, 1); // nop
103     }
104 }