Skip to content

Commit

Permalink
slabtop: move unit tests to parse.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Apr 16, 2024
1 parent 5457361 commit 5ced295
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 67 deletions.
65 changes: 65 additions & 0 deletions src/uu/slabtop/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,68 @@ pub(crate) 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!("../../../../tests/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
);
}
}
67 changes: 0 additions & 67 deletions tests/by-util/test_slabtop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
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() {
Expand All @@ -16,63 +9,3 @@ fn test_invalid_arg() {
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
);
}

0 comments on commit 5ced295

Please sign in to comment.