Skip to content

Commit

Permalink
add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Mar 14, 2024
1 parent 7de5a92 commit 0332a42
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/uu/df/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type MaybeInodesT = Option<InodesIntT>;
///
/// A row comprises several pieces of information, including the
/// filesystem device, the mountpoint, the number of bytes used, etc.
#[derive(Clone)]
pub(crate) struct Row {
/// The filename given on the command-line, if given.
file: Option<String>,
Expand Down Expand Up @@ -873,4 +874,116 @@ mod tests {

assert_eq!(row.inodes_used, Some(0));
}

#[test]
fn test_row_accumulation_u64_overflow() {
let total = u64::MAX as super::InodesIntT;
let used1 = 3000 as super::InodesIntT;

Check failure on line 881 in src/uu/df/src/table.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: casting integer literal to `u128` is unnecessary (file:'src/uu/df/src/table.rs', line:881)
let used2 = 50000 as super::InodesIntT;

Check failure on line 882 in src/uu/df/src/table.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: casting integer literal to `u128` is unnecessary (file:'src/uu/df/src/table.rs', line:882)

let mut row1 = Row {
inodes: Some(total),
inodes_used: Some(used1),
inodes_free: Some(total - used1),
..Default::default()
};

let row2 = Row {
inodes: Some(total),
inodes_used: Some(used2),
inodes_free: Some(total - used2),
..Default::default()
};

row1 += row2;

assert_eq!(row1.inodes, Some(total * 2));
assert_eq!(row1.inodes_used, Some(used1 + used2));
assert_eq!(row1.inodes_free, Some(total * 2 - used1 - used2));
}

#[test]
fn test_row_accumulation_close_to_u128_overflow() {
let total = u128::MAX as super::InodesIntT / 2 - 1;

Check failure on line 907 in src/uu/df/src/table.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: casting to the same type is unnecessary (`u128` -> `u128`) (file:'src/uu/df/src/table.rs', line:907)
let used1 = total - 50000;
let used2 = total - 100000;

let mut row1 = Row {
inodes: Some(total),
inodes_used: Some(used1),
inodes_free: Some(total - used1),
..Default::default()
};

let row2 = Row {
inodes: Some(total),
inodes_used: Some(used2),
inodes_free: Some(total - used2),
..Default::default()
};

row1 += row2;

assert_eq!(row1.inodes, Some(total * 2));
assert_eq!(row1.inodes_used, Some(used1 + used2));
assert_eq!(row1.inodes_free, Some(total * 2 - used1 - used2));
}

#[test]
fn test_row_accumulation_and_usage_close_over_u128_overflow() {
let total = u128::MAX as super::InodesIntT / 2 - 1;

Check failure on line 934 in src/uu/df/src/table.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: casting to the same type is unnecessary (`u128` -> `u128`) (file:'src/uu/df/src/table.rs', line:934)
let used1 = total / 2;
let free1 = total - used1;
let used2 = total / 2 - 10;
let free2 = total - used2;

let mut row1 = Row {
inodes: Some(total),
inodes_used: Some(used1),
inodes_free: Some(free1),
..Default::default()
};

let row2 = Row {
inodes: Some(total),
inodes_used: Some(used2),
inodes_free: Some(free2),
..Default::default()
};

row1 += row2.clone();

assert_eq!(row1.inodes, Some(total * 2));
assert_eq!(row1.inodes_used, Some(used1 + used2));
assert_eq!(row1.inodes_free, Some(free1 + free2));
assert_eq!(row1.inodes_usage, Some(0.5));

row1 += row2.clone();

assert_eq!(row1.inodes, None); // total * 3
assert_eq!(row1.inodes_used, Some(used1 + used2 * 2));
assert_eq!(row1.inodes_free, Some(free1 + free2 * 2));
assert_eq!(row1.inodes_usage, None);

row1 += row2.clone();

assert_eq!(row1.inodes, None); // total * 4
assert_eq!(row1.inodes_used, Some(used1 + used2 * 3)); // used * 4
assert_eq!(row1.inodes_free, None); // free * 4
assert_eq!(row1.inodes_usage, None);

row1 += row2.clone();

assert_eq!(row1.inodes, None); // total * 5
assert_eq!(row1.inodes_used, None); // used * 5
assert_eq!(row1.inodes_free, None); // free * 5
assert_eq!(row1.inodes_usage, None);

row1 += row2.clone();

assert_eq!(row1.inodes, None); // total * 6
assert_eq!(row1.inodes_used, None); // used * 6
assert_eq!(row1.inodes_free, None); // free * 6
assert_eq!(row1.inodes_usage, None);
}
}

0 comments on commit 0332a42

Please sign in to comment.