Skip to content

Commit

Permalink
slabtop: move unit testing into tests/by_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Krysztal112233 committed Apr 12, 2024
1 parent 5407aaa commit 152224f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 78 deletions.
81 changes: 3 additions & 78 deletions src/uu/slabtop/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ impl SlabInfo {
self.data.iter().map(|(k, _)| k).collect()
}

#[allow(unused)] // for slabinfo checking
pub fn meta(&self) -> Vec<&String> {
self.meta.iter().collect()
}

#[allow(unused)] // for slabinfo checking
pub fn version(&self) -> &String {
&self.version
}

pub fn sort(mut self, by: char, ascending_order: bool) -> Self {
let mut sort = |by_meta: &str| {
if let Some(offset) = self.offset(by_meta) {
Expand Down Expand Up @@ -276,22 +266,22 @@ impl SlabInfo {
}
}

fn parse_version(line: &str) -> Option<String> {
pub(crate) fn parse_version(line: &str) -> Option<String> {
line.replace(':', " ")
.split_whitespace()
.last()
.map(String::from)
}

fn parse_meta(line: &str) -> Vec<String> {
pub(crate) fn parse_meta(line: &str) -> Vec<String> {
line.replace(['#', ':'], " ")
.split_whitespace()
.filter(|it| it.starts_with('<') && it.ends_with('>'))
.map(|it| it.replace(['<', '>'], ""))
.collect()
}

fn parse_data(line: &str) -> Option<(String, Vec<u64>)> {
pub(crate) fn parse_data(line: &str) -> Option<(String, Vec<u64>)> {
let split: Vec<String> = line
.replace(':', " ")
.split_whitespace()
Expand All @@ -309,68 +299,3 @@ fn parse_data(line: &str) -> Option<(String, Vec<u64>)> {
)
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_version() {
let test = "slabinfo - version: 2.1";
assert_eq!("2.1", parse_version(test).unwrap())
}

#[test]
fn test_parse_meta() {
let test="# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>";

let result = parse_meta(test);

assert_eq!(
result,
[
"active_objs",
"num_objs",
"objsize",
"objperslab",
"pagesperslab",
"limit",
"batchcount",
"sharedfactor",
"active_slabs",
"num_slabs",
"sharedavail"
]
)
}

#[test]
fn test_parse_data() {
// Success case

let test = "nf_conntrack_expect 0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0";
let (name, value) = parse_data(test).unwrap();

assert_eq!(name, "nf_conntrack_expect");
assert_eq!(value, [0, 0, 208, 39, 2, 0, 0, 0, 0, 0, 0]);

// Fail case
let test =
"0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0";
let (name, _value) = parse_data(test).unwrap();

assert_ne!(name, "nf_conntrack_expect");
}

#[test]
fn test_parse() {
let test = include_str!("./data/test_data.txt");
let result = SlabInfo::parse(test.into()).unwrap();

assert_eq!(result.fetch("nf_conntrack_expect", "objsize").unwrap(), 208);
assert_eq!(
result.fetch("dmaengine-unmap-2", "active_slabs").unwrap(),
16389
);
}
}
72 changes: 72 additions & 0 deletions tests/by-util/test_slabtop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
use crate::common::util::TestScenario;
use crate::test_slabtop::parse::parse_data;
use crate::test_slabtop::parse::parse_meta;
use crate::test_slabtop::parse::parse_version;
use crate::test_slabtop::parse::SlabInfo;

#[path = "../../src/uu/slabtop/src/parse.rs"]
mod parse;

#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
fn test_slabtop() {
new_ucmd!().arg("--help").succeeds().code_is(0);
}

#[test]
fn test_parse_version() {
let test = "slabinfo - version: 2.1";
assert_eq!("2.1", parse_version(test).unwrap())
}

#[test]
fn test_parse_meta() {
let test="# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>";

let result = parse_meta(test);

assert_eq!(
result,
[
"active_objs",
"num_objs",
"objsize",
"objperslab",
"pagesperslab",
"limit",
"batchcount",
"sharedfactor",
"active_slabs",
"num_slabs",
"sharedavail"
]
)
}

#[test]
fn test_parse_data() {
// Success case

let test = "nf_conntrack_expect 0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0";
let (name, value) = parse_data(test).unwrap();

assert_eq!(name, "nf_conntrack_expect");
assert_eq!(value, [0, 0, 208, 39, 2, 0, 0, 0, 0, 0, 0]);

// Fail case
let test =
"0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0";
let (name, _value) = parse_data(test).unwrap();

assert_ne!(name, "nf_conntrack_expect");
}

#[test]
fn test_parse() {
let test = include_str!("../fixtures/slabtop/data.txt");
let result = SlabInfo::parse(test.into()).unwrap();

assert_eq!(result.fetch("nf_conntrack_expect", "objsize").unwrap(), 208);
assert_eq!(
result.fetch("dmaengine-unmap-2", "active_slabs").unwrap(),
16389
);
}
File renamed without changes.

0 comments on commit 152224f

Please sign in to comment.