Skip to content

Commit

Permalink
Add regression test for decoding files on platforms generated with di…
Browse files Browse the repository at this point in the history
…ffering SIMD support.
  • Loading branch information
michaelwoerister committed Sep 20, 2021
1 parent b58e329 commit 91c66ce
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ jobs:
run: cargo test --verbose --target ${{ matrix.arch }}-${{ fromJSON(env.target_map)[matrix.os] }} --features=no_simd
- name: Build docs
run: cargo doc --verbose
- name: Run SIMD/no-SIMD tests
run: |
cd crosstest
# Create some no-simd test files
cargo run --verbose --features no_simd -- write
cargo clean
# Create some simd test files and test the no-simd files
cargo run --verbose -- write read
cargo clean
# Test the simd-enabled files we generated in the last step
cargo run --verbose --features no_simd -- read
benchmarks:
strategy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/crosstest/target
Cargo.lock
12 changes: 12 additions & 0 deletions crosstest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "crosstest"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
odht = { package = "odht", path = ".." }

[features]
no_simd = ["odht/no_simd"]
82 changes: 82 additions & 0 deletions crosstest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This test makes sure that a hash table generated with SIMD support
// can be loaded on a platform without SIMD support.

struct FxConfig;

impl odht::Config for FxConfig {
type Key = u64;
type Value = u32;

type EncodedKey = [u8; 8];
type EncodedValue = [u8; 4];

type H = odht::FxHashFn;

#[inline]
fn encode_key(k: &Self::Key) -> Self::EncodedKey {
k.to_le_bytes()
}

#[inline]
fn encode_value(v: &Self::Value) -> Self::EncodedValue {
v.to_le_bytes()
}

#[inline]
fn decode_key(k: &Self::EncodedKey) -> Self::Key {
u64::from_le_bytes(*k)
}

#[inline]
fn decode_value(v: &Self::EncodedValue) -> Self::Value {
u32::from_le_bytes(*v)
}
}

const FILE_NAME_NO_SIMD: &str = "odht_hash_table_no_simd";
const FILE_NAME_WITH_SIMD: &str = "odht_hash_table_with_simd";

#[cfg(feature = "no_simd")]
const WRITE_FILE_NAME: &str = FILE_NAME_NO_SIMD;
#[cfg(not(feature = "no_simd"))]
const WRITE_FILE_NAME: &str = FILE_NAME_WITH_SIMD;

#[cfg(feature = "no_simd")]
const READ_FILE_NAME: &'static str = FILE_NAME_WITH_SIMD;

#[cfg(not(feature = "no_simd"))]
const READ_FILE_NAME: &'static str = FILE_NAME_NO_SIMD;

fn main() -> Result<(), Box<dyn std::error::Error>> {

let make_entries = || (0 .. 70_000_u64).map(|x| (x * x, x as u32)).collect::<Vec<_>>();

if std::env::args_os().find(|arg| arg == "write").is_some() {
let hash_table = odht::HashTableOwned::<FxConfig>::from_iterator(make_entries(), 85);
let mut path = std::env::temp_dir();
path.push(WRITE_FILE_NAME);
std::fs::write(&path, hash_table.raw_bytes())?;
eprintln!("Wrote hash table with {} bytes to {}", hash_table.raw_bytes().len(), path.display());
}

if std::env::args_os().find(|arg| arg == "read").is_some() {
let mut path = std::env::temp_dir();
path.push(READ_FILE_NAME);
eprintln!("Trying to load hash table from {}", path.display());
let data = std::fs::read(&path)?;
let hash_table = odht::HashTable::<FxConfig, _>::from_raw_bytes(data)?;
eprintln!("Loaded hash table with {} bytes from {}", hash_table.raw_bytes().len(), path.display());
let expected_entries = make_entries();

eprintln!("Comparing hash table to expected values.");
// Check that we can read the data
assert_eq!(hash_table.len(), expected_entries.len());
for (key, value) in expected_entries {
assert_eq!(hash_table.get(&key), Some(value));
}

eprintln!("Success");
}

Ok(())
}

0 comments on commit 91c66ce

Please sign in to comment.