Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add even more tests #367

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl Checker for Daemon {
if let Ok(check_bat_cond) = check_bat_cond() {
battery_condition = format!(
"Condition: {}%",
get_battery_condition(check_bat_cond).unwrap().to_string()
get_battery_condition(check_bat_cond).to_string()
);
} else {
println!("Failed to get battery condition");
Expand Down
132 changes: 130 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,15 @@ pub fn check_bat_cond() -> Result<f32, Error> {
Ok(bat_cond_calc)
}

pub fn get_battery_condition(check_bat_cond: f32) -> Result<f32, Error> {
pub fn get_battery_condition(check_bat_cond: f32) -> f32 {
let mut bat_cond = check_bat_cond * 100.0;
if bat_cond >= 100.0 {
bat_cond = 100.00;
} else if bat_cond <= 0.0 {
bat_cond = 0.0;
}
Ok(bat_cond.round())

bat_cond.round()
}

fn read_govs_file() -> Result<String, Error> {
Expand Down Expand Up @@ -357,6 +358,133 @@ mod tests {
assert!(check_cpu_freq(&list_cpus()) > 0.0);
}

#[test]
fn check_cpu_usage_unit_test() {
let usages = Vec::<CPU>::from([
CPU {
cur_freq: -1,
cur_usage: 345.0,
cur_temp: 453,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: 456.0,
cur_temp: 345,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
]);

let usage = check_cpu_usage(&usages);
assert_eq!(usage, 40050.0);
}

#[test]
fn check_cpu_temperature_unit_test() {
let temps = Vec::<CPU>::from([
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 453,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 345,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 645,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 234,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
]);

let high = check_cpu_temperature(&temps);
assert_eq!(high, 419.25);
}

#[test]
fn get_highest_temp_unit_test() {
let temps = Vec::<CPU>::from([
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 453,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 345,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 645,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
CPU {
cur_freq: -1,
cur_usage: -1.0,
cur_temp: 234,
gov: "gov".to_string(),
max_freq: -1,
min_freq: -1,
name: "dsf".to_string(),
number: 0,
},
]);

let high = get_highest_temp(&temps);
assert_eq!(high, 645);
}

#[test]
fn test_parse_proc_stat_file() {
let cpu_percent = get_cpu_percent().parse::<f32>().unwrap();
Expand Down