coreutils/tests/by-util/test_nproc.rs

191 lines
5.6 KiB
Rust
Raw Normal View History

// spell-checker:ignore incorrectnumber
2020-06-02 21:20:29 +00:00
use crate::common::util::*;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
2020-06-02 21:20:29 +00:00
#[test]
fn test_nproc() {
2021-04-17 23:28:06 +00:00
let nproc: u8 = new_ucmd!().succeeds().stdout_str().trim().parse().unwrap();
2020-06-02 21:20:29 +00:00
assert!(nproc > 0);
}
#[test]
fn test_nproc_all_omp() {
2021-04-17 23:28:06 +00:00
let result = new_ucmd!().arg("--all").succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
2020-06-02 21:20:29 +00:00
assert!(nproc > 0);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "60")
2021-04-17 23:28:06 +00:00
.succeeds();
let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc_omp, 60);
2020-06-02 21:20:29 +00:00
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
2020-06-09 11:37:59 +00:00
.env("OMP_NUM_THREADS", "1") // Has no effect
.arg("--all")
2021-04-17 23:28:06 +00:00
.succeeds();
let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, nproc_omp);
// If the parsing fails, returns the number of CPU
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "incorrectnumber") // returns the number CPU
.succeeds();
let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, nproc_omp);
2020-06-02 21:20:29 +00:00
}
#[test]
fn test_nproc_ignore() {
2021-04-17 23:28:06 +00:00
let result = new_ucmd!().succeeds();
2022-03-11 23:33:18 +00:00
let nproc_total: u8 = result.stdout_str().trim().parse().unwrap();
if nproc_total > 1 {
2020-06-02 21:20:29 +00:00
// Ignore all CPU but one
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
2020-06-02 21:20:29 +00:00
.arg("--ignore")
2022-03-11 23:33:18 +00:00
.arg((nproc_total - 1).to_string())
2021-04-17 23:28:06 +00:00
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 1);
2022-03-11 23:33:18 +00:00
// Ignore all CPU but one with a string
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
2022-03-11 23:33:18 +00:00
.arg("--ignore= 1")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc_total - 1, nproc);
2020-06-02 21:20:29 +00:00
}
}
#[test]
fn test_nproc_ignore_all_omp() {
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "42")
.arg("--ignore=40")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 2);
}
#[test]
fn test_nproc_omp_limit() {
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "0")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 42);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "2")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 2);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "2bad")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 42);
let result = new_ucmd!().arg("--all").succeeds();
let nproc_system: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc_system > 0);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_THREAD_LIMIT", "1")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, 1);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "0")
.env("OMP_THREAD_LIMIT", "")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, nproc_system);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "")
.env("OMP_THREAD_LIMIT", "")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(nproc, nproc_system);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "2,2,1")
.env("OMP_THREAD_LIMIT", "")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(2, nproc);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "2,ignored")
.env("OMP_THREAD_LIMIT", "")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(2, nproc);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "2,2,1")
.env("OMP_THREAD_LIMIT", "0")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(2, nproc);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "2,2,1")
.env("OMP_THREAD_LIMIT", "1bad")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(2, nproc);
let result = TestScenario::new(util_name!())
.ucmd()
.keep_env()
.env("OMP_NUM_THREADS", "29,2,1")
.env("OMP_THREAD_LIMIT", "1bad")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert_eq!(29, nproc);
}