]> git.lizzy.rs Git - rust.git/blob - library/core/tests/atomic.rs
More doc-comments for pointer metadata APIs
[rust.git] / library / core / tests / atomic.rs
1 use core::sync::atomic::Ordering::SeqCst;
2 use core::sync::atomic::*;
3
4 #[test]
5 fn bool_() {
6     let a = AtomicBool::new(false);
7     assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Ok(false));
8     assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Err(true));
9
10     a.store(false, SeqCst);
11     assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Ok(false));
12 }
13
14 #[test]
15 fn bool_and() {
16     let a = AtomicBool::new(true);
17     assert_eq!(a.fetch_and(false, SeqCst), true);
18     assert_eq!(a.load(SeqCst), false);
19 }
20
21 #[test]
22 fn bool_nand() {
23     let a = AtomicBool::new(false);
24     assert_eq!(a.fetch_nand(false, SeqCst), false);
25     assert_eq!(a.load(SeqCst), true);
26     assert_eq!(a.fetch_nand(false, SeqCst), true);
27     assert_eq!(a.load(SeqCst), true);
28     assert_eq!(a.fetch_nand(true, SeqCst), true);
29     assert_eq!(a.load(SeqCst), false);
30     assert_eq!(a.fetch_nand(true, SeqCst), false);
31     assert_eq!(a.load(SeqCst), true);
32 }
33
34 #[test]
35 fn uint_and() {
36     let x = AtomicUsize::new(0xf731);
37     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
38     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
39 }
40
41 #[test]
42 fn uint_nand() {
43     let x = AtomicUsize::new(0xf731);
44     assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
45     assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
46 }
47
48 #[test]
49 fn uint_or() {
50     let x = AtomicUsize::new(0xf731);
51     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
52     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
53 }
54
55 #[test]
56 fn uint_xor() {
57     let x = AtomicUsize::new(0xf731);
58     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
59     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
60 }
61
62 #[test]
63 fn int_and() {
64     let x = AtomicIsize::new(0xf731);
65     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
66     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
67 }
68
69 #[test]
70 fn int_nand() {
71     let x = AtomicIsize::new(0xf731);
72     assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
73     assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
74 }
75
76 #[test]
77 fn int_or() {
78     let x = AtomicIsize::new(0xf731);
79     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
80     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
81 }
82
83 #[test]
84 fn int_xor() {
85     let x = AtomicIsize::new(0xf731);
86     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
87     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
88 }
89
90 static S_FALSE: AtomicBool = AtomicBool::new(false);
91 static S_TRUE: AtomicBool = AtomicBool::new(true);
92 static S_INT: AtomicIsize = AtomicIsize::new(0);
93 static S_UINT: AtomicUsize = AtomicUsize::new(0);
94
95 #[test]
96 fn static_init() {
97     // Note that we're not really testing the mutability here but it's important
98     // on Android at the moment (#49775)
99     assert!(!S_FALSE.swap(true, SeqCst));
100     assert!(S_TRUE.swap(false, SeqCst));
101     assert!(S_INT.fetch_add(1, SeqCst) == 0);
102     assert!(S_UINT.fetch_add(1, SeqCst) == 0);
103 }
104
105 #[test]
106 fn atomic_access_bool() {
107     static mut ATOMIC: AtomicBool = AtomicBool::new(false);
108
109     unsafe {
110         assert_eq!(*ATOMIC.get_mut(), false);
111         ATOMIC.store(true, SeqCst);
112         assert_eq!(*ATOMIC.get_mut(), true);
113         ATOMIC.fetch_or(false, SeqCst);
114         assert_eq!(*ATOMIC.get_mut(), true);
115         ATOMIC.fetch_and(false, SeqCst);
116         assert_eq!(*ATOMIC.get_mut(), false);
117         ATOMIC.fetch_nand(true, SeqCst);
118         assert_eq!(*ATOMIC.get_mut(), true);
119         ATOMIC.fetch_xor(true, SeqCst);
120         assert_eq!(*ATOMIC.get_mut(), false);
121     }
122 }
123
124 #[test]
125 fn atomic_alignment() {
126     use std::mem::{align_of, size_of};
127
128     #[cfg(target_has_atomic = "8")]
129     assert_eq!(align_of::<AtomicBool>(), size_of::<AtomicBool>());
130     #[cfg(target_has_atomic = "ptr")]
131     assert_eq!(align_of::<AtomicPtr<u8>>(), size_of::<AtomicPtr<u8>>());
132     #[cfg(target_has_atomic = "8")]
133     assert_eq!(align_of::<AtomicU8>(), size_of::<AtomicU8>());
134     #[cfg(target_has_atomic = "8")]
135     assert_eq!(align_of::<AtomicI8>(), size_of::<AtomicI8>());
136     #[cfg(target_has_atomic = "16")]
137     assert_eq!(align_of::<AtomicU16>(), size_of::<AtomicU16>());
138     #[cfg(target_has_atomic = "16")]
139     assert_eq!(align_of::<AtomicI16>(), size_of::<AtomicI16>());
140     #[cfg(target_has_atomic = "32")]
141     assert_eq!(align_of::<AtomicU32>(), size_of::<AtomicU32>());
142     #[cfg(target_has_atomic = "32")]
143     assert_eq!(align_of::<AtomicI32>(), size_of::<AtomicI32>());
144     #[cfg(target_has_atomic = "64")]
145     assert_eq!(align_of::<AtomicU64>(), size_of::<AtomicU64>());
146     #[cfg(target_has_atomic = "64")]
147     assert_eq!(align_of::<AtomicI64>(), size_of::<AtomicI64>());
148     #[cfg(target_has_atomic = "128")]
149     assert_eq!(align_of::<AtomicU128>(), size_of::<AtomicU128>());
150     #[cfg(target_has_atomic = "128")]
151     assert_eq!(align_of::<AtomicI128>(), size_of::<AtomicI128>());
152     #[cfg(target_has_atomic = "ptr")]
153     assert_eq!(align_of::<AtomicUsize>(), size_of::<AtomicUsize>());
154     #[cfg(target_has_atomic = "ptr")]
155     assert_eq!(align_of::<AtomicIsize>(), size_of::<AtomicIsize>());
156 }
157
158 #[test]
159 fn atomic_compare_exchange() {
160     use Ordering::*;
161
162     static ATOMIC: AtomicIsize = AtomicIsize::new(0);
163
164     ATOMIC.compare_exchange(0, 1, Relaxed, Relaxed).ok();
165     ATOMIC.compare_exchange(0, 1, Acquire, Relaxed).ok();
166     ATOMIC.compare_exchange(0, 1, Release, Relaxed).ok();
167     ATOMIC.compare_exchange(0, 1, AcqRel, Relaxed).ok();
168     ATOMIC.compare_exchange(0, 1, SeqCst, Relaxed).ok();
169     ATOMIC.compare_exchange(0, 1, Acquire, Acquire).ok();
170     ATOMIC.compare_exchange(0, 1, AcqRel, Acquire).ok();
171     ATOMIC.compare_exchange(0, 1, SeqCst, Acquire).ok();
172     ATOMIC.compare_exchange(0, 1, SeqCst, SeqCst).ok();
173     ATOMIC.compare_exchange_weak(0, 1, Relaxed, Relaxed).ok();
174     ATOMIC.compare_exchange_weak(0, 1, Acquire, Relaxed).ok();
175     ATOMIC.compare_exchange_weak(0, 1, Release, Relaxed).ok();
176     ATOMIC.compare_exchange_weak(0, 1, AcqRel, Relaxed).ok();
177     ATOMIC.compare_exchange_weak(0, 1, SeqCst, Relaxed).ok();
178     ATOMIC.compare_exchange_weak(0, 1, Acquire, Acquire).ok();
179     ATOMIC.compare_exchange_weak(0, 1, AcqRel, Acquire).ok();
180     ATOMIC.compare_exchange_weak(0, 1, SeqCst, Acquire).ok();
181     ATOMIC.compare_exchange_weak(0, 1, SeqCst, SeqCst).ok();
182 }