Skip to content

Commit 1e56398

Browse files
Marmaa0vil02
andauthored
Update src/math/average.rs
Co-authored-by: Piotr Idzik <[email protected]>
1 parent fb46fed commit 1e56398

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/math/average.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,36 +174,22 @@ mod test {
174174
assert_eq!(result, Some(12.0));
175175
}
176176

177-
// Tests for geometric mean function
178-
// Empty sequence returns nothing
179-
#[test]
180-
fn test_geometric_mean_empty() {
181-
let sequence: Vec<f64> = vec![];
182-
let result = geometric_mean(&sequence);
183-
assert_eq!(result, None);
184-
}
185-
186-
// Geometric mean of a single value is the value itself.
187-
#[test]
188-
fn test_geometric_mean_single_element() {
189-
let sequence = vec![5.0];
190-
let result = geometric_mean(&sequence);
191-
assert_eq!(result, Some(5.0));
192-
}
193-
194-
// Geometric means are not defined for negative values
195-
#[test]
196-
fn test_geometric_mean_negative() {
197-
let sequence = vec![1.0, -3.0, 2.0];
198-
let result = geometric_mean(&sequence);
199-
assert_eq!(result, None);
200-
}
201-
202-
// Geometric mean generic test
203-
#[test]
204-
fn test_geometric_mean_floats() {
205-
let sequence = vec![0.5, 0.5, 0.3, 0.2];
206-
let result = geometric_mean(&sequence);
207-
assert_eq!(result, Some(0.34996355115805833));
177+
macro_rules! test_geometric_mean {
178+
($($name:ident: $inputs:expr,)*) => {
179+
$(
180+
#[test]
181+
fn $name() {
182+
let (sequence, expected) = $inputs;
183+
assert_eq!(geometric_mean(&sequence), expected);
184+
}
185+
)*
186+
}
187+
}
188+
189+
test_geometric_mean! {
190+
empty: (Vec::<f64>::new(), None),
191+
single: (vec![5.0], Some(5.0)),
192+
negative: (vec![1.0, -3.0, 2.0], None),
193+
regular: (vec![0.5, 0.5, 0.3, 0.2], Some(0.34996355115805833)),
208194
}
209195
}

0 commit comments

Comments
 (0)