|
| 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