Skip to content

Commit 8d268a8

Browse files
committed
windows(gnu): add test to check time_t bit width
Adds a test to ensure the changes in rust-lang#5062 are correct. The test ensures the wrong definition is kept on stable. It will use the right bit width in the following cases. - Under targets other than x86 with GNU. - If the `gnu_time_bits64` `cfg` is set.
1 parent 465e736 commit 8d268a8

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

libc-test/tests/windows_time.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Ensures Windows `time`-related routines align with `libc`'s interface. By
2+
//! default, both MSVC and GNU (under `mingw`) expose 64-bit symbols, but in
3+
//! stable we need to cope with a 32-bit `time-t`, so the routines should link
4+
//! to their 32-bit variants.
5+
6+
#![cfg(windows)]
7+
8+
/// Ensures a 64-bit write is performed on values that should always be 64 bits,
9+
/// and that a corresponding 32-bit write is performed on values that should be
10+
/// 32 bits. This basically makes sure `time_t`'s bit-width aligns with those of
11+
/// functions that expect it as a parameter.
12+
#[test]
13+
fn test_bitwidth_store() {
14+
if cfg!(all(
15+
target_arch = "x86",
16+
target_env = "gnu",
17+
not(gnu_time_bits64)
18+
)) {
19+
assert_eq!(size_of::<libc::time_t>(), 4);
20+
} else {
21+
assert_eq!(size_of::<libc::time_t>(), 8);
22+
}
23+
24+
let mut time_values: [libc::time_t; 2] = [123, 456];
25+
let ptr = time_values.as_mut_ptr();
26+
27+
unsafe { libc::time(ptr) };
28+
29+
// Succeeds if the first array value is written. Fails if both elements are
30+
// overwritten. That would imply:
31+
// - We are linking a `time()` that expects a 64-bit `time_t`.
32+
// - We are exposing and using here a 32-bit `time_t`.
33+
assert_ne!(time_values[0], 123);
34+
assert_eq!(time_values[1], 456);
35+
}

0 commit comments

Comments
 (0)