@@ -101,3 +101,82 @@ fn static_init() {
101101 assert ! ( S_INT . fetch_add( 1 , SeqCst ) == 0 ) ;
102102 assert ! ( S_UINT . fetch_add( 1 , SeqCst ) == 0 ) ;
103103}
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+ }
0 commit comments