Skip to content

Commit

Permalink
Move tests to tests/ directory
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jan 4, 2021
1 parent 7b8f2ba commit 28cafca
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
12 changes: 0 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,3 @@ fn internal_desc(error: Error) -> Option<&'static str> {
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::Error;
use core::mem::size_of;

#[test]
fn test_size() {
assert_eq!(size_of::<Error>(), 4);
assert_eq!(size_of::<Result<(), Error>>(), 4);
}
}
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,3 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
}
imp::getrandom_inner(dest)
}

#[cfg(test)]
mod test_common;
#[cfg(test)]
mod test_rdrand;
8 changes: 0 additions & 8 deletions src/test_rdrand.rs

This file was deleted.

22 changes: 15 additions & 7 deletions src/test_common.rs → tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
// Allow getrandom to be renamed by the parent module.
use super::getrandom;
use super::getrandom_impl;

#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[cfg(feature = "test-in-browser")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[test]
fn test_error_size() {
use core::mem::size_of;
use getrandom::Error;
// Errors are always be 4 bytes, and use Rust's null pointer optimization.
assert_eq!(size_of::<Error>(), 4);
assert_eq!(size_of::<Result<(), Error>>(), 4);
}

#[test]
fn test_zero() {
// Test that APIs are happy with zero-length requests
getrandom(&mut [0u8; 0]).unwrap();
getrandom_impl(&mut [0u8; 0]).unwrap();
}

#[test]
fn test_diff() {
let mut v1 = [0u8; 1000];
getrandom(&mut v1).unwrap();
getrandom_impl(&mut v1).unwrap();

let mut v2 = [0u8; 1000];
getrandom(&mut v2).unwrap();
getrandom_impl(&mut v2).unwrap();

let mut n_diff_bits = 0;
for i in 0..v1.len() {
Expand All @@ -33,7 +41,7 @@ fn test_diff() {
#[test]
fn test_huge() {
let mut huge = [0u8; 100_000];
getrandom(&mut huge).unwrap();
getrandom_impl(&mut huge).unwrap();
}

// On WASM, the thread API always fails/panics
Expand All @@ -54,7 +62,7 @@ fn test_multithreading() {
let mut v = [0u8; 1000];

for _ in 0..100 {
getrandom(&mut v).unwrap();
getrandom_impl(&mut v).unwrap();
thread::yield_now();
}
});
Expand Down
3 changes: 3 additions & 0 deletions tests/normal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Use the normal getrandom implementation on this architecture.
use getrandom::getrandom as getrandom_impl;
mod common;
15 changes: 15 additions & 0 deletions tests/rdrand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// We only test the RDRAND-based RNG source on supported architectures.
#![cfg(any(target_arch = "x86_64", target_arch = "x86"))]

// rdrand.rs expects to be part of the getrandom main crate, so we need these
// additional imports to get rdrand.rs to compile.
use getrandom::Error;
#[macro_use]
extern crate cfg_if;
#[path = "../src/rdrand.rs"]
mod rdrand;
#[path = "../src/util.rs"]
mod util;

use rdrand::getrandom_inner as getrandom_impl;
mod common;

0 comments on commit 28cafca

Please sign in to comment.