| 1 |
//! Tests for the `parallel` crate feature |
| 2 |
#![cfg(feature = "parallel")] |
| 3 |
use core::array; |
| 4 |
use hybrid_array::Array; |
| 5 |
use keccak::{Backend, BackendClosure, State1600}; |
| 6 |
|
| 7 |
const N: usize = 50; |
| 8 |
|
| 9 |
/// Test that we get the same result for scalar and parallel functions. |
| 10 |
fn test_fn<B: Backend>() { |
| 11 |
let f1600 = B::get_f1600(); |
| 12 |
let par_f1600 = B::get_par_f1600(); |
| 13 |
|
| 14 |
let mut buf: [State1600; N] = array::from_fn(|i| array::from_fn(|_| i as u64)); |
| 15 |
let expected: [State1600; N] = buf.map(|mut s| { |
| 16 |
f1600(&mut s); |
| 17 |
s |
| 18 |
}); |
| 19 |
|
| 20 |
let (chunks, tail) = Array::slice_as_chunks_mut(&mut buf[..]); |
| 21 |
|
| 22 |
chunks.iter_mut().for_each(par_f1600); |
| 23 |
tail.iter_mut().for_each(f1600); |
| 24 |
|
| 25 |
assert_eq!(buf, expected); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn keccak_par_f1600() { |
| 30 |
struct Closure; |
| 31 |
|
| 32 |
impl BackendClosure for Closure { |
| 33 |
fn call_once<B: Backend>(self) { |
| 34 |
test_fn::<B>(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
keccak::Keccak::new().with_backend(Closure); |
| 39 |
} |