Math median tests and documentation additions (#2018)

* Add math median example and unit tests

* Update output of other all math ls command examples to keep consistent with math median output

* Fix output of math max example

* Update output of other math commands using pwd examples to keep data consistent
This commit is contained in:
Joseph T. Lyons 2020-06-20 01:28:03 -04:00 committed by GitHub
parent b0c30098e4
commit 853d7e7120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 22 deletions

View File

@ -35,7 +35,8 @@ impl WholeStreamCommand for Command {
mod tests { mod tests {
use super::*; use super::*;
use crate::commands::math::{ use crate::commands::math::{
avg::average, max::maximum, min::minimum, sum::summation, utils::MathFunction, avg::average, max::maximum, median::median, min::minimum, sum::summation,
utils::MathFunction,
}; };
use nu_plugin::test_helpers::value::{decimal, int}; use nu_plugin::test_helpers::value::{decimal, int};
use nu_protocol::Value; use nu_protocol::Value;
@ -67,13 +68,25 @@ mod tests {
description: "Single value", description: "Single value",
values: vec![int(10)], values: vec![int(10)],
expected_err: None, expected_err: None,
expected_res: vec![Ok(decimal(10)), Ok(int(10)), Ok(int(10)), Ok(int(10))], expected_res: vec![
Ok(decimal(10)),
Ok(int(10)),
Ok(int(10)),
Ok(int(10)),
Ok(int(10)),
],
}, },
TestCase { TestCase {
description: "Multiple Values", description: "Multiple Values",
values: vec![int(10), int(30), int(20)], values: vec![int(10), int(20), int(30)],
expected_err: None, expected_err: None,
expected_res: vec![Ok(decimal(20)), Ok(int(10)), Ok(int(30)), Ok(int(60))], expected_res: vec![
Ok(decimal(20)),
Ok(int(10)),
Ok(int(30)),
Ok(int(20)),
Ok(int(60)),
],
}, },
TestCase { TestCase {
description: "Mixed Values", description: "Mixed Values",
@ -83,23 +96,31 @@ mod tests {
Ok(decimal(21)), Ok(decimal(21)),
Ok(int(10)), Ok(int(10)),
Ok(decimal(26.5)), Ok(decimal(26.5)),
Ok(decimal(26.5)),
Ok(decimal(63)), Ok(decimal(63)),
], ],
}, },
TestCase { TestCase {
description: "Negative Values", description: "Negative Values",
values: vec![int(10), int(-11), int(-14)], values: vec![int(-14), int(-11), int(10)],
expected_err: None, expected_err: None,
expected_res: vec![Ok(decimal(-5)), Ok(int(-14)), Ok(int(10)), Ok(int(-15))], expected_res: vec![
Ok(decimal(-5)),
Ok(int(-14)),
Ok(int(10)),
Ok(int(-11)),
Ok(int(-15)),
],
}, },
TestCase { TestCase {
description: "Mixed Negative Values", description: "Mixed Negative Values",
values: vec![int(10), decimal(-11.5), decimal(-13.5)], values: vec![decimal(-13.5), decimal(-11.5), int(10)],
expected_err: None, expected_err: None,
expected_res: vec![ expected_res: vec![
Ok(decimal(-5)), Ok(decimal(-5)),
Ok(decimal(-13.5)), Ok(decimal(-13.5)),
Ok(int(10)), Ok(int(10)),
Ok(decimal(-11.5)),
Ok(decimal(-15)), Ok(decimal(-15)),
], ],
}, },
@ -126,7 +147,8 @@ mod tests {
for tc in tt.iter() { for tc in tt.iter() {
let tc: &TestCase = tc; // Just for type annotations let tc: &TestCase = tc; // Just for type annotations
let math_functions: Vec<MathFunction> = vec![average, minimum, maximum, summation]; let math_functions: Vec<MathFunction> =
vec![average, minimum, maximum, median, summation];
let results = math_functions let results = math_functions
.iter() .iter()
.map(|mf| mf(&tc.values, &test_tag)) .map(|mf| mf(&tc.values, &test_tag))

View File

@ -6,6 +6,7 @@ Currently the following functions are implemented:
* `math avg`: Finds the average of a list of numbers or tables * `math avg`: Finds the average of a list of numbers or tables
* `math min`: Finds the minimum within a list of numbers or tables * `math min`: Finds the minimum within a list of numbers or tables
* `math max`: Finds the maximum within a list of numbers or tables * `math max`: Finds the maximum within a list of numbers or tables
* `math median`: Finds the median of a list of numbers or tables
* `math sum`: Finds the sum of a list of numbers or tables * `math sum`: Finds the sum of a list of numbers or tables
However, the mathematical functions like `min` and `max` are more permissive and also work on `Dates`. However, the mathematical functions like `min` and `max` are more permissive and also work on `Dates`.
@ -46,6 +47,8 @@ To get the average of the file sizes in a directory, simply pipe the size column
```shell ```shell
> ls | get size | math avg > ls | get size | math avg
───┬──────── ───┬────────
# │
───┼────────
0 │ 7.2 KB 0 │ 7.2 KB
───┴──────── ───┴────────
``` ```
@ -53,21 +56,35 @@ To get the average of the file sizes in a directory, simply pipe the size column
```shell ```shell
> ls | get size | math min > ls | get size | math min
───┬───── ───┬─────
# │
───┼─────
0 │ 0 B 0 │ 0 B
───┴───── ───┴─────
``` ```
```shell ```shell
> ls | get size | math max
───┬────────── ───┬──────────
0 │ 113.5 KB # │
───┼──────────
0 │ 113.6 KB
───┴────────── ───┴──────────
``` ```
```shell
> ls | get size | math median
───┬───────
# │
───┼───────
0 │ 320 B
───┴───────
```
```shell ```shell
> ls | get size | math sum > ls | get size | math sum
───┬────────── ───┬──────────
0 │ 143.4 KB # │
───┼──────────
0 │ 143.6 KB
───┴────────── ───┴──────────
``` ```
@ -91,29 +108,31 @@ To get the average of the file sizes in a directory, simply pipe the size column
# │ lines │ words │ chars │ max length # │ lines │ words │ chars │ max length
───┼───────┼───────┼───────┼──────────── ───┼───────┼───────┼───────┼────────────
0 │ 0 │ 1 │ 5 │ 5 0 │ 0 │ 1 │ 5 │ 5
1 │ 0 │ 1 │ 7 │ 7 1 │ 0 │ 1 │ 11 │ 11
2 │ 0 │ 1 │ 9 │ 9 2 │ 0 │ 1 │ 11 │ 11
3 │ 0 │ 1 │ 7 │ 7 3 │ 0 │ 1 │ 4 │ 4
4 │ 0 │ 2 │ 12 │ 12
5 │ 0 │ 1 │ 7 │ 7
───┴───────┴───────┴───────┴──────────── ───┴───────┴───────┴───────┴────────────
``` ```
```shell ```shell
> pwd | split row / | size | math max > pwd | split row / | size | math max
───────────┬─── ──────────────
lines │ 0 lines │ 0
words │ 1 words │ 2
chars │ 9 chars │ 12
max length │ 9 max length │ 12
────────────┴─── ────────────┴───
``` ```
```shell ```shell
> pwd | split row / | size | math avg > pwd | split row / | size | math avg
────────────┬──────── ────────────┬────────
lines │ 0.0000 lines │ 0.0000
words │ 1.0000 words │ 1.1666
chars │ 7.0000 chars │ 8.3333
max length │ 7.0000 max length │ 8.3333
────────────┴──────── ────────────┴────────
``` ```