-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add regression test for decoding files on platforms generated with di…
…ffering SIMD support.
- Loading branch information
1 parent
b58e329
commit 91c66ce
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
/crosstest/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |