diff --git a/Cargo.toml b/Cargo.toml index 181ec13..31ec604 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,8 @@ targets = ["x86_64-unknown-linux-gnu"] [target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))'.dependencies] libc = "0.2.107" + +[[test]] +name = "tests" +path = "tests.rs" +harness = false diff --git a/src/lib.rs b/src/lib.rs index e54a652..b7361fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,56 +18,3 @@ pub fn num_threads() -> Option { pub fn is_single_threaded() -> Option { num_threads().map(|n| n.get() == 1) } - -#[cfg(test)] -mod test { - use std::num::NonZeroUsize; - - // Run each expression in its own thread. - macro_rules! threaded { - ($first:expr;) => { - $first; - }; - ($first:expr; $($rest:expr;)*) => { - $first; - ::std::thread::spawn(|| { - threaded!($($rest;)*); - }) - .join() - .unwrap(); - }; - } - - #[test] - fn require_test_threads_1() { - if super::is_single_threaded() != Some(true) { - panic!( - "Process is not single-threaded. Are you running `cargo test -- --test-threads=1`?" - ); - } - } - - #[test] - fn num_threads() { - threaded! { - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(1)); - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(2)); - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(3)); - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(4)); - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(5)); - assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(6)); - } - } - - #[test] - fn is_single_threaded() { - threaded! { - assert_eq!(super::is_single_threaded(), Some(true)); - assert_eq!(super::is_single_threaded(), Some(false)); - assert_eq!(super::is_single_threaded(), Some(false)); - assert_eq!(super::is_single_threaded(), Some(false)); - assert_eq!(super::is_single_threaded(), Some(false)); - assert_eq!(super::is_single_threaded(), Some(false)); - } - } -} diff --git a/tests.rs b/tests.rs new file mode 100644 index 0000000..3e88243 --- /dev/null +++ b/tests.rs @@ -0,0 +1,57 @@ +extern crate num_threads; + +use std::num::NonZeroUsize; + +use num_threads::*; + +// Run each expression in its own thread. +macro_rules! threaded { + ($first:expr;) => { + $first; + }; + ($first:expr; $($rest:expr;)*) => { + $first; + ::std::thread::spawn(|| { + threaded!($($rest;)*); + }) + .join() + .unwrap(); + }; +} + +fn test_single_threaded_by_default() { + if is_single_threaded() != Some(true) { + panic!("Process is not single-threaded, but it should be."); + } +} + +fn test_num_threads() { + threaded! { + assert_eq!(num_threads().map(NonZeroUsize::get), Some(1)); + assert_eq!(num_threads().map(NonZeroUsize::get), Some(2)); + assert_eq!(num_threads().map(NonZeroUsize::get), Some(3)); + assert_eq!(num_threads().map(NonZeroUsize::get), Some(4)); + assert_eq!(num_threads().map(NonZeroUsize::get), Some(5)); + assert_eq!(num_threads().map(NonZeroUsize::get), Some(6)); + } +} + +fn test_is_single_threaded() { + threaded! { + assert_eq!(is_single_threaded(), Some(true)); + assert_eq!(is_single_threaded(), Some(false)); + assert_eq!(is_single_threaded(), Some(false)); + assert_eq!(is_single_threaded(), Some(false)); + assert_eq!(is_single_threaded(), Some(false)); + assert_eq!(is_single_threaded(), Some(false)); + } +} + +fn main() { + test_single_threaded_by_default(); + std::thread::sleep(std::time::Duration::new(0, 100)); + test_num_threads(); + std::thread::sleep(std::time::Duration::new(0, 100)); + test_is_single_threaded(); + eprintln!("All tests passed!"); +}