Skip to content

Commit 91c66ce

Browse files
Add regression test for decoding files on platforms generated with differing SIMD support.
1 parent b58e329 commit 91c66ce

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ jobs:
6565
run: cargo test --verbose --target ${{ matrix.arch }}-${{ fromJSON(env.target_map)[matrix.os] }} --features=no_simd
6666
- name: Build docs
6767
run: cargo doc --verbose
68+
- name: Run SIMD/no-SIMD tests
69+
run: |
70+
cd crosstest
71+
# Create some no-simd test files
72+
cargo run --verbose --features no_simd -- write
73+
cargo clean
74+
# Create some simd test files and test the no-simd files
75+
cargo run --verbose -- write read
76+
cargo clean
77+
# Test the simd-enabled files we generated in the last step
78+
cargo run --verbose --features no_simd -- read
6879
6980
benchmarks:
7081
strategy:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2+
/crosstest/target
23
Cargo.lock

crosstest/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "crosstest"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
odht = { package = "odht", path = ".." }
10+
11+
[features]
12+
no_simd = ["odht/no_simd"]

crosstest/src/main.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// This test makes sure that a hash table generated with SIMD support
2+
// can be loaded on a platform without SIMD support.
3+
4+
struct FxConfig;
5+
6+
impl odht::Config for FxConfig {
7+
type Key = u64;
8+
type Value = u32;
9+
10+
type EncodedKey = [u8; 8];
11+
type EncodedValue = [u8; 4];
12+
13+
type H = odht::FxHashFn;
14+
15+
#[inline]
16+
fn encode_key(k: &Self::Key) -> Self::EncodedKey {
17+
k.to_le_bytes()
18+
}
19+
20+
#[inline]
21+
fn encode_value(v: &Self::Value) -> Self::EncodedValue {
22+
v.to_le_bytes()
23+
}
24+
25+
#[inline]
26+
fn decode_key(k: &Self::EncodedKey) -> Self::Key {
27+
u64::from_le_bytes(*k)
28+
}
29+
30+
#[inline]
31+
fn decode_value(v: &Self::EncodedValue) -> Self::Value {
32+
u32::from_le_bytes(*v)
33+
}
34+
}
35+
36+
const FILE_NAME_NO_SIMD: &str = "odht_hash_table_no_simd";
37+
const FILE_NAME_WITH_SIMD: &str = "odht_hash_table_with_simd";
38+
39+
#[cfg(feature = "no_simd")]
40+
const WRITE_FILE_NAME: &str = FILE_NAME_NO_SIMD;
41+
#[cfg(not(feature = "no_simd"))]
42+
const WRITE_FILE_NAME: &str = FILE_NAME_WITH_SIMD;
43+
44+
#[cfg(feature = "no_simd")]
45+
const READ_FILE_NAME: &'static str = FILE_NAME_WITH_SIMD;
46+
47+
#[cfg(not(feature = "no_simd"))]
48+
const READ_FILE_NAME: &'static str = FILE_NAME_NO_SIMD;
49+
50+
fn main() -> Result<(), Box<dyn std::error::Error>> {
51+
52+
let make_entries = || (0 .. 70_000_u64).map(|x| (x * x, x as u32)).collect::<Vec<_>>();
53+
54+
if std::env::args_os().find(|arg| arg == "write").is_some() {
55+
let hash_table = odht::HashTableOwned::<FxConfig>::from_iterator(make_entries(), 85);
56+
let mut path = std::env::temp_dir();
57+
path.push(WRITE_FILE_NAME);
58+
std::fs::write(&path, hash_table.raw_bytes())?;
59+
eprintln!("Wrote hash table with {} bytes to {}", hash_table.raw_bytes().len(), path.display());
60+
}
61+
62+
if std::env::args_os().find(|arg| arg == "read").is_some() {
63+
let mut path = std::env::temp_dir();
64+
path.push(READ_FILE_NAME);
65+
eprintln!("Trying to load hash table from {}", path.display());
66+
let data = std::fs::read(&path)?;
67+
let hash_table = odht::HashTable::<FxConfig, _>::from_raw_bytes(data)?;
68+
eprintln!("Loaded hash table with {} bytes from {}", hash_table.raw_bytes().len(), path.display());
69+
let expected_entries = make_entries();
70+
71+
eprintln!("Comparing hash table to expected values.");
72+
// Check that we can read the data
73+
assert_eq!(hash_table.len(), expected_entries.len());
74+
for (key, value) in expected_entries {
75+
assert_eq!(hash_table.get(&key), Some(value));
76+
}
77+
78+
eprintln!("Success");
79+
}
80+
81+
Ok(())
82+
}

0 commit comments

Comments
 (0)