Debian Codegraph @

debian/rust-vendor/portable-atomic/src/tests/mod.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 #![allow(
4 clippy::alloc_instead_of_core,
5 clippy::std_instead_of_alloc,
6 clippy::std_instead_of_core,
7 clippy::undocumented_unsafe_blocks,
8 clippy::wildcard_imports
9 )]
10
11 #[macro_use]
12 pub(crate) mod helper;
13
14 #[allow(dead_code)]
15 #[path = "../../version.rs"]
16 mod version;
17
18 use test_helper as _; // For critical-section test
19
20 use super::*;
21
22 test_atomic_bool_pub!();
23 test_atomic_ptr_pub!();
24
25 test_atomic_int_pub!(isize);
26 test_atomic_int_pub!(usize);
27 test_atomic_int_pub!(i8);
28 test_atomic_int_pub!(u8);
29 test_atomic_int_pub!(i16);
30 test_atomic_int_pub!(u16);
31 test_atomic_int_pub!(i32);
32 test_atomic_int_pub!(u32);
33 test_atomic_int_pub!(i64);
34 test_atomic_int_pub!(u64);
35 #[cfg(not(all(valgrind, target_arch = "powerpc64")))] // TODO(powerpc64): Hang (as of Valgrind 3.26)
36 test_atomic_int_pub!(i128);
37 #[cfg(not(all(valgrind, target_arch = "powerpc64")))] // TODO(powerpc64): Hang (as of Valgrind 3.26)
38 test_atomic_int_pub!(u128);
39
40 #[cfg(all(feature = "float", portable_atomic_unstable_f16))]
41 test_atomic_float_pub!(f16);
42 #[cfg(feature = "float")]
43 test_atomic_float_pub!(f32);
44 #[cfg(feature = "float")]
45 // TODO: rustc bug: https://github.com/rust-lang/rust/issues/114479
46 #[cfg(not(all(not(debug_assertions), target_arch = "x86", not(target_feature = "sse2"))))]
47 test_atomic_float_pub!(f64);
48 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
49 test_atomic_float_pub!(f128);
50
51 #[deny(improper_ctypes)]
52 extern "C" {
53 fn _atomic_bool_ffi_safety(_: AtomicBool);
54 fn _atomic_ptr_ffi_safety(_: AtomicPtr<u8>);
55 fn _atomic_isize_ffi_safety(_: AtomicIsize);
56 fn _atomic_usize_ffi_safety(_: AtomicUsize);
57 fn _atomic_i8_ffi_safety(_: AtomicI8);
58 fn _atomic_u8_ffi_safety(_: AtomicU8);
59 fn _atomic_i16_ffi_safety(_: AtomicI16);
60 fn _atomic_u16_ffi_safety(_: AtomicU16);
61 fn _atomic_i32_ffi_safety(_: AtomicI32);
62 fn _atomic_u32_ffi_safety(_: AtomicU32);
63 fn _atomic_i64_ffi_safety(_: AtomicI64);
64 fn _atomic_u64_ffi_safety(_: AtomicU64);
65 #[rustversion::since(1.89)] // https://github.com/rust-lang/rust/pull/137306
66 fn _atomic_i128_ffi_safety(_: AtomicI128);
67 #[rustversion::since(1.89)] // https://github.com/rust-lang/rust/pull/137306
68 fn _atomic_u128_ffi_safety(_: AtomicU128);
69 #[cfg(all(feature = "float", portable_atomic_unstable_f16))]
70 fn _atomic_f16_ffi_safety(_: AtomicF16);
71 #[cfg(feature = "float")]
72 fn _atomic_f32_ffi_safety(_: AtomicF32);
73 #[cfg(feature = "float")]
74 fn _atomic_f64_ffi_safety(_: AtomicF64);
75 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
76 fn _atomic_f128_ffi_safety(_: AtomicF128);
77 }
78
79 #[test]
80 fn test_is_lock_free() {
81 assert!(AtomicI8::is_always_lock_free());
82 assert!(AtomicI8::is_lock_free());
83 assert!(AtomicU8::is_always_lock_free());
84 assert!(AtomicU8::is_lock_free());
85 assert!(AtomicI16::is_always_lock_free());
86 assert!(AtomicI16::is_lock_free());
87 assert!(AtomicU16::is_always_lock_free());
88 assert!(AtomicU16::is_lock_free());
89 #[cfg(all(feature = "float", portable_atomic_unstable_f16))]
90 assert!(AtomicF16::is_always_lock_free());
91 #[cfg(all(feature = "float", portable_atomic_unstable_f16))]
92 assert!(AtomicF16::is_lock_free());
93 assert!(AtomicI32::is_always_lock_free());
94 assert!(AtomicI32::is_lock_free());
95 assert!(AtomicU32::is_always_lock_free());
96 assert!(AtomicU32::is_lock_free());
97 #[cfg(feature = "float")]
98 assert!(AtomicF32::is_always_lock_free());
99 #[cfg(feature = "float")]
100 assert!(AtomicF32::is_lock_free());
101 #[cfg(not(portable_atomic_no_cfg_target_has_atomic))]
102 {
103 if cfg!(any(
104 target_has_atomic = "64",
105 all(
106 target_arch = "riscv32",
107 not(any(miri, portable_atomic_sanitize_thread)),
108 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
109 any(target_feature = "zacas", portable_atomic_target_feature = "zacas"),
110 ),
111 )) {
112 assert!(AtomicI64::is_always_lock_free());
113 assert!(AtomicI64::is_lock_free());
114 assert!(AtomicU64::is_always_lock_free());
115 assert!(AtomicU64::is_lock_free());
116 #[cfg(feature = "float")]
117 assert!(AtomicF64::is_always_lock_free());
118 #[cfg(feature = "float")]
119 assert!(AtomicF64::is_lock_free());
120 } else if cfg!(all(
121 feature = "fallback",
122 target_arch = "arm",
123 not(any(miri, portable_atomic_sanitize_thread)),
124 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
125 any(target_os = "linux", target_os = "android"),
126 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
127 not(portable_atomic_no_outline_atomics),
128 not(target_has_atomic = "64"),
129 not(portable_atomic_test_detect_false),
130 )) {
131 assert!(!AtomicI64::is_always_lock_free());
132 assert!(!AtomicU64::is_always_lock_free());
133 #[cfg(feature = "float")]
134 assert!(!AtomicF64::is_always_lock_free());
135 assert!(AtomicI64::is_lock_free());
136 assert!(AtomicU64::is_lock_free());
137 #[cfg(feature = "float")]
138 assert!(AtomicF64::is_lock_free());
139 } else {
140 assert!(!AtomicI64::is_always_lock_free());
141 assert!(!AtomicU64::is_always_lock_free());
142 #[cfg(feature = "float")]
143 assert!(!AtomicF64::is_always_lock_free());
144 #[cfg(not(target_arch = "riscv32"))]
145 {
146 assert!(!AtomicI64::is_lock_free());
147 assert!(!AtomicU64::is_lock_free());
148 #[cfg(feature = "float")]
149 assert!(!AtomicF64::is_lock_free());
150 }
151 #[cfg(target_arch = "riscv32")]
152 {
153 // TODO(riscv): check detect.has_zacas
154 }
155 }
156 }
157 if cfg!(portable_atomic_no_asm) && cfg!(not(portable_atomic_unstable_asm)) {
158 assert!(!AtomicI128::is_always_lock_free());
159 assert!(!AtomicI128::is_lock_free());
160 assert!(!AtomicU128::is_always_lock_free());
161 assert!(!AtomicU128::is_lock_free());
162 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
163 assert!(!AtomicF128::is_always_lock_free());
164 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
165 assert!(!AtomicF128::is_lock_free());
166 } else if cfg!(any(
167 all(
168 target_arch = "aarch64",
169 not(all(
170 any(miri, portable_atomic_sanitize_thread),
171 not(portable_atomic_atomic_intrinsics),
172 )),
173 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
174 ),
175 all(
176 target_arch = "arm64ec",
177 not(all(
178 any(miri, portable_atomic_sanitize_thread),
179 not(portable_atomic_atomic_intrinsics),
180 )),
181 not(portable_atomic_no_asm),
182 ),
183 all(
184 target_arch = "x86_64",
185 any(target_feature = "cmpxchg16b", portable_atomic_target_feature = "cmpxchg16b"),
186 ),
187 all(
188 target_arch = "riscv64",
189 any(target_feature = "zacas", portable_atomic_target_feature = "zacas"),
190 ),
191 all(
192 target_arch = "powerpc64",
193 not(all(
194 any(miri, portable_atomic_sanitize_thread),
195 not(portable_atomic_atomic_intrinsics),
196 )),
197 not(portable_atomic_no_asm),
198 any(
199 target_feature = "quadword-atomics",
200 portable_atomic_target_feature = "quadword-atomics",
201 ),
202 ),
203 all(
204 target_arch = "s390x",
205 not(all(
206 any(miri, portable_atomic_sanitize_thread),
207 not(portable_atomic_atomic_intrinsics),
208 )),
209 not(portable_atomic_no_asm),
210 ),
211 )) {
212 assert!(AtomicI128::is_always_lock_free());
213 assert!(AtomicI128::is_lock_free());
214 assert!(AtomicU128::is_always_lock_free());
215 assert!(AtomicU128::is_lock_free());
216 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
217 assert!(AtomicF128::is_always_lock_free());
218 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
219 assert!(AtomicF128::is_lock_free());
220 } else {
221 assert!(!AtomicI128::is_always_lock_free());
222 assert!(!AtomicU128::is_always_lock_free());
223 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
224 assert!(!AtomicF128::is_always_lock_free());
225 #[cfg(not(any(
226 target_arch = "x86_64",
227 target_arch = "powerpc64",
228 target_arch = "riscv64",
229 )))]
230 {
231 assert!(!AtomicI128::is_lock_free());
232 assert!(!AtomicU128::is_lock_free());
233 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
234 assert!(!AtomicF128::is_lock_free());
235 }
236 #[cfg(target_arch = "x86_64")]
237 {
238 let has_cmpxchg16b = cfg!(all(
239 feature = "fallback",
240 not(portable_atomic_no_outline_atomics),
241 not(any(target_env = "sgx", miri)),
242 not(portable_atomic_test_detect_false),
243 )) && std::is_x86_feature_detected!("cmpxchg16b");
244 assert_eq!(AtomicI128::is_lock_free(), has_cmpxchg16b);
245 assert_eq!(AtomicU128::is_lock_free(), has_cmpxchg16b);
246 #[cfg(all(feature = "float", portable_atomic_unstable_f128))]
247 assert_eq!(AtomicF128::is_lock_free(), has_cmpxchg16b);
248 }
249 #[cfg(target_arch = "powerpc64")]
250 {
251 // TODO(powerpc64): is_powerpc_feature_detected is unstable
252 }
253 #[cfg(target_arch = "riscv64")]
254 {
255 // TODO(riscv): check detect.has_zacas
256 }
257 }
258 }
259
260 // test version parsing code used in the build script.
261 #[test]
262 fn test_rustc_version() {
263 use self::version::Version;
264
265 // rustc 1.34 (rustup)
266 let v = Version::parse(
267 "rustc 1.34.2 (6c2484dc3 2019-05-13)
268 binary: rustc
269 commit-hash: 6c2484dc3c532c052f159264e970278d8b77cdc9
270 commit-date: 2019-05-13
271 host: x86_64-apple-darwin
272 release: 1.34.2
273 LLVM version: 8.0",
274 )
275 .unwrap();
276 assert_eq!(v, Version::stable(34, 8));
277
278 // rustc 1.50 (rustup)
279 let v = Version::parse(
280 "rustc 1.50.0 (cb75ad5db 2021-02-10)
281 binary: rustc
282 commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
283 commit-date: 2021-02-10
284 host: aarch64-unknown-linux-gnu
285 release: 1.50.0",
286 )
287 .unwrap();
288 assert_eq!(v, Version::stable(50, 0));
289
290 // rustc 1.67 (rustup)
291 let v = Version::parse(
292 "rustc 1.67.0 (fc594f156 2023-01-24)
293 binary: rustc
294 commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
295 commit-date: 2023-01-24
296 host: aarch64-apple-darwin
297 release: 1.67.0
298 LLVM version: 15.0.6",
299 )
300 .unwrap();
301 assert_eq!(v, Version::stable(67, 15));
302
303 // rustc 1.68-beta (rustup)
304 let v = Version::parse(
305 "rustc 1.68.0-beta.2 (10b73bf73 2023-02-01)
306 binary: rustc
307 commit-hash: 10b73bf73a6b770cd92ad8ff538173bc3298411c
308 commit-date: 2023-02-01
309 host: aarch64-apple-darwin
310 release: 1.68.0-beta.2
311 LLVM version: 15.0.6",
312 )
313 .unwrap();
314 // We do not distinguish between stable and beta because we are only
315 // interested in whether unstable features are potentially available.
316 assert_eq!(v, Version::stable(68, 15));
317
318 // rustc nightly-2019-01-27 (rustup)
319 let v = Version::parse(
320 "rustc 1.33.0-nightly (20c2cba61 2019-01-26)
321 binary: rustc
322 commit-hash: 20c2cba61dc83e612d25ed496025171caa3db30f
323 commit-date: 2019-01-26
324 host: x86_64-apple-darwin
325 release: 1.33.0-nightly
326 LLVM version: 8.0",
327 )
328 .unwrap();
329 assert_eq!(v.minor, 33);
330 assert!(v.nightly);
331 assert_eq!(v.llvm, 8);
332 assert_eq!(v.commit_date().year, 2019);
333 assert_eq!(v.commit_date().month, 1);
334 assert_eq!(v.commit_date().day, 26);
335
336 // rustc 1.69-nightly (rustup)
337 let v = Version::parse(
338 "rustc 1.69.0-nightly (bd39bbb4b 2023-02-07)
339 binary: rustc
340 commit-hash: bd39bbb4bb92df439bf6d85470e296cc6a47ffbd
341 commit-date: 2023-02-07
342 host: aarch64-apple-darwin
343 release: 1.69.0-nightly
344 LLVM version: 15.0.7",
345 )
346 .unwrap();
347 assert_eq!(v.minor, 69);
348 assert!(v.nightly);
349 assert_eq!(v.llvm, 15);
350 assert_eq!(v.commit_date().year, 2023);
351 assert_eq!(v.commit_date().month, 2);
352 assert_eq!(v.commit_date().day, 7);
353
354 // clippy-driver 1.69-nightly (rustup)
355 let v = Version::parse(
356 "rustc 1.69.0-nightly (bd39bbb4b 2023-02-07)
357 binary: rustc
358 commit-hash: bd39bbb4bb92df439bf6d85470e296cc6a47ffbd
359 commit-date: 2023-02-07
360 host: aarch64-apple-darwin
361 release: 1.69.0-nightly
362 LLVM version: 15.0.7",
363 )
364 .unwrap();
365 assert_eq!(v.minor, 69);
366 assert!(v.nightly);
367 assert_eq!(v.llvm, 15);
368 assert_eq!(v.commit_date().year, 2023);
369 assert_eq!(v.commit_date().month, 2);
370 assert_eq!(v.commit_date().day, 7);
371
372 // rustc 1.69-dev (from source: ./x.py build)
373 let v = Version::parse(
374 "rustc 1.69.0-dev
375 binary: rustc
376 commit-hash: unknown
377 commit-date: unknown
378 host: aarch64-unknown-linux-gnu
379 release: 1.69.0-dev
380 LLVM version: 16.0.0",
381 )
382 .unwrap();
383 assert_eq!(v.minor, 69);
384 assert!(v.nightly);
385 assert_eq!(v.llvm, 16);
386 assert_eq!(v.commit_date().year, 0);
387 assert_eq!(v.commit_date().month, 0);
388 assert_eq!(v.commit_date().day, 0);
389
390 // rustc 1.48 (debian 11: apt-get install cargo)
391 let v = Version::parse(
392 "rustc 1.48.0
393 binary: rustc
394 commit-hash: unknown
395 commit-date: unknown
396 host: aarch64-unknown-linux-gnu
397 release: 1.48.0
398 LLVM version: 11.0",
399 )
400 .unwrap();
401 assert_eq!(v, Version::stable(48, 11));
402
403 // rustc 1.67 (fedora: dnf install cargo)
404 let v = Version::parse(
405 "rustc 1.67.0 (fc594f156 2023-01-24) (Fedora 1.67.0-2.fc37)
406 binary: rustc
407 commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
408 commit-date: 2023-01-24
409 host: aarch64-unknown-linux-gnu
410 release: 1.67.0
411 LLVM version: 15.0.7",
412 )
413 .unwrap();
414 assert_eq!(v, Version::stable(67, 15));
415
416 // rustc 1.64 (alpine: apk add cargo)
417 let v = Version::parse(
418 "rustc 1.64.0
419 binary: rustc
420 commit-hash: unknown
421 commit-date: unknown
422 host: aarch64-alpine-linux-musl
423 release: 1.64.0
424 LLVM version: 15.0.3",
425 )
426 .unwrap();
427 assert_eq!(v, Version::stable(64, 15));
428 }
429
430 #[cfg(feature = "serde")]
431 #[test]
432 fn test_serde() {
433 use std::{eprint, eprintln, fmt};
434
435 use serde::{
436 de::{Deserialize, Deserializer},
437 ser::{Serialize, Serializer},
438 };
439 use serde_test::{Token, assert_tokens};
440
441 #[derive(Debug)]
442 struct DebugPartialEq<T>(T);
443 impl<T: fmt::Debug> PartialEq for DebugPartialEq<T> {
444 fn eq(&self, other: &Self) -> bool {
445 std::format!("{:?}", self) == std::format!("{:?}", other)
446 }
447 }
448 impl<T: Serialize> Serialize for DebugPartialEq<T> {
449 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
450 where
451 S: Serializer,
452 {
453 self.0.serialize(serializer)
454 }
455 }
456 impl<'de, T: Deserialize<'de>> Deserialize<'de> for DebugPartialEq<T> {
457 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
458 where
459 D: Deserializer<'de>,
460 {
461 T::deserialize(deserializer).map(Self)
462 }
463 }
464
465 macro_rules! t {
466 ($atomic_type:ty, $value_type:ident $(as $token_value_type:ident)?, $token_type:ident) => {
467 eprint!("test_serde {} ... ", stringify!($value_type));
468 assert_tokens(&DebugPartialEq(<$atomic_type>::new($value_type::MAX)), &[
469 Token::$token_type($value_type::MAX $(as $token_value_type)?),
470 ]);
471 assert_tokens(&DebugPartialEq(<$atomic_type>::new($value_type::MIN)), &[
472 Token::$token_type($value_type::MIN $(as $token_value_type)?),
473 ]);
474 eprintln!("ok");
475 };
476 }
477
478 assert_tokens(&DebugPartialEq(AtomicBool::new(true)), &[Token::Bool(true)]);
479 assert_tokens(&DebugPartialEq(AtomicBool::new(false)), &[Token::Bool(false)]);
480 t!(AtomicIsize, isize as i64, I64);
481 t!(AtomicUsize, usize as u64, U64);
482 t!(AtomicI8, i8, I8);
483 t!(AtomicU8, u8, U8);
484 t!(AtomicI16, i16, I16);
485 t!(AtomicU16, u16, U16);
486 t!(AtomicI32, i32, I32);
487 t!(AtomicU32, u32, U32);
488 t!(AtomicI64, i64, I64);
489 t!(AtomicU64, u64, U64);
490 #[cfg(not(all(valgrind, target_arch = "powerpc64")))] // TODO(powerpc64): Hang (as of Valgrind 3.26)
491 t!(AtomicI128, i128, I128);
492 #[cfg(not(all(valgrind, target_arch = "powerpc64")))] // TODO(powerpc64): Hang (as of Valgrind 3.26)
493 t!(AtomicU128, u128, U128);
494 // TODO(f16_and_f128): Test f16 & f128 once stabilized.
495 #[cfg(feature = "float")]
496 t!(AtomicF32, f32, F32);
497 #[cfg(feature = "float")]
498 t!(AtomicF64, f64, F64);
499 }