Skip to content

Commit 6aa9161

Browse files
authored
cpubits: test detection results in CI (#1418)
Adds tests for the detection results that confirm the result matches `target_pointer_width` on every platform except `wasm`. Updates the CI config to test on - 32-bit Linux - 64-bit Linux - 64-bit Windows - 64-bit macOS - `wasm32-wasip1` target on `wasmtime`
1 parent 0c3e384 commit 6aa9161

File tree

2 files changed

+92
-35
lines changed

2 files changed

+92
-35
lines changed

.github/workflows/cpubits.yml

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,56 @@ env:
1717
RUSTFLAGS: "-Dwarnings"
1818

1919
jobs:
20-
build:
20+
test:
21+
strategy:
22+
matrix:
23+
include:
24+
# 32-bit Linux
25+
- target: i686-unknown-linux-gnu
26+
platform: ubuntu-latest
27+
rust: 1.85.0 # MSRV
28+
deps: sudo apt update && sudo apt install gcc-multilib
29+
30+
# 64-bit Linux
31+
- target: x86_64-unknown-linux-gnu
32+
platform: ubuntu-latest
33+
rust: 1.85.0 # MSRV
34+
35+
# 64-bit Windows
36+
- target: x86_64-pc-windows-msvc
37+
platform: windows-latest
38+
rust: 1.85.0 # MSRV
39+
40+
# 64-bit macOS
41+
- target: x86_64-apple-darwin
42+
platform: macos-latest
43+
rust: 1.85.0 # MSRV
44+
runs-on: ${{ matrix.platform }}
45+
steps:
46+
- uses: actions/checkout@v6
47+
- uses: RustCrypto/actions/cargo-cache@master
48+
- uses: dtolnay/rust-toolchain@master
49+
with:
50+
toolchain: ${{ matrix.rust }}
51+
targets: ${{ matrix.target }}
52+
- run: ${{ matrix.deps }}
53+
- run: cargo test --target ${{ matrix.target }}
54+
55+
# Test WASM using `wasm32-wasip1` target on `wasmtime`
56+
test-wasm:
2157
strategy:
2258
matrix:
2359
rust:
2460
- 1.85 # MSRV
2561
- stable
2662
runs-on: ubuntu-latest
63+
env:
64+
CARGO_TARGET_WASM32_WASIP1_RUNNER: "wasmtime"
2765
steps:
2866
- uses: actions/checkout@v6
29-
- uses: RustCrypto/actions/cargo-cache@master
67+
- uses: bytecodealliance/actions/wasmtime/setup@v1
3068
- uses: dtolnay/rust-toolchain@master
3169
with:
3270
toolchain: ${{ matrix.rust }}
33-
- run: cargo test
71+
targets: wasm32-wasip1
72+
- run: cargo test --target wasm32-wasip1

cpubits/src/lib.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ macro_rules! cpubits {
175175
$crate::cpubits! {
176176
#[cfg(enable_64bit(
177177
// `cfg` selector for 64-bit targets (implicitly `any`)
178-
target_family = "wasm",
178+
target_arch = "wasm32"
179179
))]
180180
16 => { $( $tokens32 )* }
181181
32 => { $( $tokens32 )* }
@@ -268,46 +268,64 @@ macro_rules! cfg_if {
268268

269269
#[cfg(test)]
270270
mod tests {
271-
mod two_arms {
271+
/// Return an integer that maps to the number of bits `cpubits` detected.
272+
fn detected_bits() -> u32 {
272273
cpubits! {
273-
16 | 32 => {
274-
pub type Word = u32;
275-
}
276-
64 => {
277-
pub type Word = u64;
278-
}
274+
16 => { 16 }
275+
32 => { 32 }
276+
64 => { 64 }
279277
}
278+
}
280279

281-
#[test]
282-
fn word_matches_pointer_size() {
283-
match size_of::<usize>() {
284-
2 | 4 => {
285-
assert_eq!(size_of::<Word>(), 4);
286-
}
287-
8 => {
288-
assert_eq!(size_of::<Word>(), 8);
289-
}
290-
_ => todo!("unsupported target pointer width"),
291-
}
280+
/// Return an integer that maps to `target_pointer_width`.
281+
#[allow(dead_code)]
282+
fn detect_pointer_width() -> u32 {
283+
if cfg!(target_pointer_width = "16") {
284+
16
285+
} else if cfg!(target_pointer_width = "32") {
286+
32
287+
} else if cfg!(target_pointer_width = "64") {
288+
64
289+
} else {
290+
unreachable!("rustc only support 16, 32, and 64-bit pointer widths")
292291
}
293292
}
294293

295-
mod three_arms {
296-
cpubits! {
297-
16 => {
298-
pub type Word = u16;
299-
}
300-
32 => {
301-
pub type Word = u32;
302-
}
303-
64 => {
304-
pub type Word = u64;
294+
/// Return the expected number of bits for the target.
295+
fn expected_bits() -> u32 {
296+
// Duplicated 64-bit override predicates need to go here
297+
if cfg!(target_arch = "wasm32") {
298+
64
299+
} else {
300+
detect_pointer_width()
301+
}
302+
}
303+
304+
#[test]
305+
fn cpubits_works() {
306+
assert_eq!(detected_bits(), expected_bits());
307+
}
308+
309+
/// Explicit test for WASM so we can see the predicate is working
310+
#[cfg(target_arch = "wasm32")]
311+
#[test]
312+
fn cpubits_on_wasm_is_64bit() {
313+
assert_eq!(detected_bits(), 64);
314+
}
315+
316+
#[test]
317+
fn cpubits_16_or_32_vs_64() {
318+
fn bits32or64() -> u32 {
319+
cpubits! {
320+
16 | 32 => { 32 }
321+
64 => { 64 }
305322
}
306323
}
307324

308-
#[test]
309-
fn word_matches_pointer_size() {
310-
assert_eq!(size_of::<Word>(), size_of::<usize>());
325+
match expected_bits() {
326+
16 | 32 => assert_eq!(32, bits32or64()),
327+
64 => assert_eq!(64, bits32or64()),
328+
bits => unreachable!("#{bits}-bits should be one of: 16, 32, 64"),
311329
}
312330
}
313331
}

0 commit comments

Comments
 (0)