Skip to content

Commit d596628

Browse files
committed
cpubits: test detection results in CI
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 d596628

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

.github/workflows/cpubits.yml

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,57 @@ env:
1717
RUSTFLAGS: "-Dwarnings"
1818

1919
jobs:
20-
build:
20+
test:
2121
strategy:
2222
matrix:
23-
rust:
24-
- 1.85 # MSRV
25-
- stable
26-
runs-on: ubuntu-latest
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 }}
2745
steps:
2846
- uses: actions/checkout@v6
2947
- uses: RustCrypto/actions/cargo-cache@master
3048
- uses: dtolnay/rust-toolchain@master
3149
with:
3250
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:
57+
test:
58+
strategy:
59+
matrix:
60+
rust:
61+
- 1.85 # MSRV
62+
- stable
63+
runs-on: ubuntu-latest
64+
env:
65+
CARGO_TARGET_WASM32_WASIP1_RUNNER: "wasmtime"
66+
steps:
67+
- uses: actions/checkout@v6
68+
- uses: bytecodealliance/actions/wasmtime/setup@v1
69+
- uses: dtolnay/rust-toolchain@master
70+
with:
71+
toolchain: ${{ matrix.rust }}
72+
targets: wasm32-wasip1
3373
- run: cargo test

cpubits/src/lib.rs

Lines changed: 46 additions & 1 deletion
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_family = "wasm",
179179
))]
180180
16 => { $( $tokens32 )* }
181181
32 => { $( $tokens32 )* }
@@ -268,6 +268,51 @@ macro_rules! cfg_if {
268268

269269
#[cfg(test)]
270270
mod tests {
271+
/// Return an integer that maps to the number of bits `cpubits` detected.
272+
fn detect_bits() -> u32 {
273+
cpubits! {
274+
16 => { let bits = 16; }
275+
32 => { let bits = 32; }
276+
64 => { let bits = 64; }
277+
}
278+
279+
bits
280+
}
281+
282+
/// Return an integer that maps to `target_pointer_width`.
283+
#[allow(dead_code)]
284+
fn detect_pointer_width() -> u32 {
285+
#[cfg(target_pointer_width = "16")]
286+
{
287+
16
288+
}
289+
#[cfg(target_pointer_width = "32")]
290+
{
291+
32
292+
}
293+
#[cfg(target_pointer_width = "64")]
294+
{
295+
64
296+
}
297+
}
298+
299+
/// Tests for `cpubits` on platforms where we expect 64-bit overrides.
300+
/// The cfg gating here should match the macro
301+
#[cfg(target_family = "wasm")]
302+
#[test]
303+
fn cpubits_with_64bit_override() {
304+
// TODO(tarcieri): deliberately wrong to see if CI catches it
305+
assert_eq!(42, detect_bits());
306+
}
307+
308+
/// Tests for `cpubits` on platforms where the result matches `target_pointer_width`.
309+
/// The cfg gating here should match the macro, but inverted.
310+
#[cfg(not(target_family = "wasm"))]
311+
#[test]
312+
fn cpubits_without_override() {
313+
assert_eq!(detect_pointer_width(), detect_bits());
314+
}
315+
271316
mod two_arms {
272317
cpubits! {
273318
16 | 32 => {

0 commit comments

Comments
 (0)