Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Dec 11, 2022
1 parent c354fa0 commit cdab978
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 245 deletions.
3 changes: 0 additions & 3 deletions crates/base32-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ mod multiversion;
#[cfg(feature = "alloc")]
mod heap;

#[cfg(test)]
mod tests;

pub use outref::{AsOut, Out};

// -----------------------------------------------------------------------------
Expand Down
38 changes: 14 additions & 24 deletions crates/base32-simd/src/tests.rs → crates/base32-simd/tests/it.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{AsOut, Base32, Error, Out, BASE32, BASE32HEX, BASE32HEX_NO_PAD, BASE32_NO_PAD};

use rand::RngCore;
use base32_simd::{AsOut, Base32};
use base32_simd::{BASE32, BASE32HEX, BASE32HEX_NO_PAD, BASE32_NO_PAD};

fn rand_bytes(n: usize) -> Vec<u8> {
use rand::RngCore;
let mut bytes = vec![0u8; n];
rand::thread_rng().fill_bytes(&mut bytes);
bytes
Expand All @@ -13,9 +13,9 @@ use std::io::Write as _;

macro_rules! dbgmsg {
($($fmt:tt)*) => {
println!($($fmt)*);
#[cfg(miri)]
std::io::stdout().flush().unwrap();
// println!($($fmt)*);
// #[cfg(miri)]
// std::io::stdout().flush().unwrap();
};
}

Expand Down Expand Up @@ -110,14 +110,10 @@ fn allocation() {
assert_eq!(decode_buf, b"123helloworld");
}

#[allow(clippy::type_complexity)]
fn safety_unit_test(
check: for<'s> fn(&'_ Base32, &'s [u8]) -> Result<(), Error>,
encode: for<'s, 'd> fn(&'_ Base32, &'s [u8], Out<'d, [u8]>) -> &'d mut [u8],
decode: for<'s, 'd> fn(&'_ Base32, &'s [u8], Out<'d, [u8]>) -> Result<&'d mut [u8], Error>,
decode_inplace: for<'b> fn(&'_ Base32, &'b mut [u8]) -> Result<&'b mut [u8], Error>,
) {
println!();
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn random() {
dbgmsg!();
for n in 0..128 {
dbgmsg!("n = {}", n);
let bytes = rand_bytes(n);
Expand All @@ -133,22 +129,16 @@ fn safety_unit_test(
dbgmsg!("base32 = {:?}", base32);

let mut buf = vec![0u8; base32.encoded_length(n)];
let encoded = encode(&base32, &bytes, buf.as_out());
assert!(check(&base32, encoded).is_ok());
let encoded = base32.encode(&bytes, buf.as_out());
assert!(base32.check(encoded).is_ok());

let mut buf = encoded.to_owned();
let ans = decode_inplace(&base32, &mut buf).unwrap();
let ans = base32.decode_inplace(&mut buf).unwrap();
assert_eq!(ans, bytes);

let mut buf = vec![0u8; base32.decoded_length(encoded).unwrap()];
let ans = decode(&base32, encoded, buf.as_out()).unwrap();
let ans = base32.decode(encoded, buf.as_out()).unwrap();
assert_eq!(ans, bytes);
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_safety() {
safety_unit_test(Base32::check, Base32::encode, Base32::decode, Base32::decode_inplace);
}
3 changes: 0 additions & 3 deletions crates/base64-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ mod heap;
mod forgiving;
pub use self::forgiving::*;

#[cfg(test)]
mod tests;

pub use outref::{AsOut, Out};

// -----------------------------------------------------------------------------
Expand Down
107 changes: 49 additions & 58 deletions crates/base64-simd/src/tests.rs → crates/base64-simd/tests/it.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use crate::{AsOut, Base64, Error, Out, STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};

use rand::RngCore;
use base64_simd::{AsOut, Base64};
use base64_simd::{STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};

fn rand_bytes(n: usize) -> Vec<u8> {
use rand::RngCore;
let mut bytes = vec![0u8; n];
rand::thread_rng().fill_bytes(&mut bytes);
bytes
}

#[cfg(miri)]
use std::io::Write as _;

macro_rules! dbgmsg {
($($fmt:tt)*) => {
// println!($($fmt)*);
// #[cfg(miri)]
// std::io::stdout().flush().unwrap();
};
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn basic() {
Expand Down Expand Up @@ -56,54 +67,10 @@ fn allocation() {
assert_eq!(decode_buf, b"123helloworld");
}

#[cfg(miri)]
use std::io::Write as _;

macro_rules! dbgmsg {
($($fmt:tt)*) => {
println!($($fmt)*);
#[cfg(miri)]
std::io::stdout().flush().unwrap();
};
}

#[allow(clippy::type_complexity)]
fn safety_unit_test(
check: for<'s> fn(&'_ Base64, &'s [u8]) -> Result<(), Error>,
encode: for<'s, 'd> fn(&'_ Base64, &'s [u8], Out<'d, [u8]>) -> &'d mut [u8],
decode: for<'s, 'd> fn(&'_ Base64, &'s [u8], Out<'d, [u8]>) -> Result<&'d mut [u8], Error>,
decode_inplace: for<'b> fn(&'_ Base64, &'b mut [u8]) -> Result<&'b mut [u8], Error>,
) {
// canonicity tests
// <https://eprint.iacr.org/2022/361>
{
let test_vectors = [
("SGVsbG8=", Some("Hello")),
("SGVsbG9=", None),
("SGVsbG9", None),
("SGVsbA==", Some("Hell")),
("SGVsbA=", None),
("SGVsbA", None),
("SGVsbA====", None),
];

let mut buf = [0u8; 64];

for (encoded, expected) in test_vectors {
let base64 = STANDARD;

let is_valid = check(&base64, encoded.as_bytes()).is_ok();
let result: _ = base64.decode(encoded.as_bytes(), buf.as_mut_slice().as_out());

assert_eq!(is_valid, result.is_ok());
match expected {
Some(expected) => assert_eq!(result.unwrap(), expected.as_bytes()),
None => assert!(result.is_err(), "expected = {expected:?}"),
}
}
}

println!();
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn random() {
dbgmsg!();
for n in 0..128 {
dbgmsg!("n = {}", n);
let bytes = rand_bytes(n);
Expand All @@ -126,35 +93,59 @@ fn safety_unit_test(

let encoded = base64::encode_config(&bytes, config);
let encoded = encoded.as_bytes();
assert!(check(&base64, encoded).is_ok());
assert!(base64.check(encoded).is_ok());

{
let mut buf = vec![0u8; base64.encoded_length(n)];
let ans = encode(&base64, &bytes, buf.as_out());
let ans = base64.encode(&bytes, buf.as_out());
assert_eq!(ans, encoded);
assert!(check(&base64, ans).is_ok());
assert!(base64.check(ans).is_ok());
dbgmsg!("encoding ... ok");
}

{
let mut buf = encoded.to_owned();
let ans = decode_inplace(&base64, &mut buf).unwrap();
let ans = base64.decode_inplace(&mut buf).unwrap();
assert_eq!(ans, bytes);
dbgmsg!("decoding inplace ... ok");
}

{
let mut buf = vec![0u8; n];
let ans = decode(&base64, encoded, buf.as_out()).unwrap();
let ans = base64.decode(encoded, buf.as_out()).unwrap();
assert_eq!(ans, bytes);
dbgmsg!("decoding ... ok");
}
}
}
}

/// <https://eprint.iacr.org/2022/361>
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_safety() {
safety_unit_test(Base64::check, Base64::encode, Base64::decode, Base64::decode_inplace);
fn canonicity() {
let test_vectors = [
("SGVsbG8=", Some("Hello")),
("SGVsbG9=", None),
("SGVsbG9", None),
("SGVsbA==", Some("Hell")),
("SGVsbA=", None),
("SGVsbA", None),
("SGVsbA====", None),
];

let mut buf = [0u8; 64];

for (encoded, expected) in test_vectors {
let base64 = STANDARD;

let is_valid = base64.check(encoded.as_bytes()).is_ok();
let result: _ = base64.decode(encoded.as_bytes(), buf.as_mut_slice().as_out());

assert_eq!(is_valid, result.is_ok());
match expected {
Some(expected) => assert_eq!(result.unwrap(), expected.as_bytes()),
None => assert!(result.is_err()),
}
}
}
3 changes: 0 additions & 3 deletions crates/hex-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ mod multiversion;
#[cfg(feature = "alloc")]
mod heap;

#[cfg(test)]
mod tests;

pub use outref::{AsOut, Out};
pub use vsimd::ascii::AsciiCase;

Expand Down
Loading

0 comments on commit cdab978

Please sign in to comment.