Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ jobs:

- name: Test
run: cargo test --manifest-path iKeySort/Cargo.toml ${{ matrix.feature_mode.features }}

wasm32-regression:
name: wasm32 regression (Node.js)
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown

- name: Cache cargo registry + build
uses: Swatinem/rust-cache@v2

- name: Build debug wasm module
run: cargo build --manifest-path iKeySort/tests/wasm32/Cargo.toml --target wasm32-unknown-unknown

- name: Run debug wasm module
run: node iKeySort/tests/wasm32/run.mjs iKeySort/tests/wasm32/target/wasm32-unknown-unknown/debug/wasm32_regression.wasm

- name: Build release wasm module
run: cargo build --manifest-path iKeySort/tests/wasm32/Cargo.toml --target wasm32-unknown-unknown --release

- name: Run release wasm module
run: node iKeySort/tests/wasm32/run.mjs iKeySort/tests/wasm32/target/wasm32-unknown-unknown/release/wasm32_regression.wasm
14 changes: 14 additions & 0 deletions iKeySort/tests/wasm32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "wasm32_regression"
version = "0.0.0"
edition = "2024"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
i_key_sort = { path = "../.." }

[profile.release]
panic = "abort"
11 changes: 11 additions & 0 deletions iKeySort/tests/wasm32/run.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { readFile } from "node:fs/promises";

const bytes = await readFile(process.argv[2]);
const { instance } = await WebAssembly.instantiate(bytes, {});
const result = instance.exports.run_i64_32bit_boundary_regression();

if (result !== 0) {
throw new Error(`wasm32 i64 regression failed with code ${result}`);
}

console.log("wasm32 i64 regression passed");
26 changes: 26 additions & 0 deletions iKeySort/tests/wasm32/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use i_key_sort::sort::one_key::OneKeySort;

#[unsafe(no_mangle)]
pub extern "C" fn run_i64_32bit_boundary_regression() -> i32 {
const BOUNDARY: i64 = 1i64 << 32;

let mut values = [
0,
BOUNDARY - 1,
BOUNDARY,
1,
BOUNDARY / 2,
BOUNDARY - 2,
123,
BOUNDARY - 123,
]
.repeat(128);
values.reverse();

let mut expected = values.clone();
expected.sort_unstable();

values.sort_by_one_key(false, |value| *value);

i32::from(values != expected)
}
28 changes: 28 additions & 0 deletions iKeySort/tests/wasm32_i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use i_key_sort::sort::one_key::OneKeySort;

#[test]
fn sort_i64_across_32_bit_boundary() {
const BOUNDARY: i64 = 1i64 << 32;

// On wasm32, the old implementation truncated the full distance to zero
// while the intermediate distance became `usize::MAX`, escaping the layout.
let mut values = [
0,
BOUNDARY - 1,
BOUNDARY,
1,
BOUNDARY / 2,
BOUNDARY - 2,
123,
BOUNDARY - 123,
]
.repeat(128);
values.reverse();

let mut expected = values.clone();
expected.sort_unstable();

values.sort_by_one_key(false, |value| *value);

assert_eq!(values, expected);
}
Loading